Use OptionsFlowWithReload in pvpc_hourly_pricing

This commit is contained in:
G Johansson
2025-08-27 17:50:25 +00:00
parent 87f0703be1
commit c68fee754d
4 changed files with 9 additions and 22 deletions

View File

@@ -4,7 +4,6 @@ from homeassistant.const import CONF_API_TOKEN, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .const import ATTR_POWER, ATTR_POWER_P3
from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry
from .helpers import get_enabled_sensor_keys from .helpers import get_enabled_sensor_keys
@@ -23,23 +22,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool
entry.runtime_data = coordinator entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_update_options))
return True return True
async def async_update_options(hass: HomeAssistant, entry: PVPCConfigEntry) -> None:
"""Handle options update."""
if any(
entry.data.get(attrib) != entry.options.get(attrib)
for attrib in (ATTR_POWER, ATTR_POWER_P3, CONF_API_TOKEN)
):
# update entry replacing data with new options
hass.config_entries.async_update_entry(
entry, data={**entry.data, **entry.options}
)
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -13,7 +13,7 @@ from homeassistant.config_entries import (
ConfigEntry, ConfigEntry,
ConfigFlow, ConfigFlow,
ConfigFlowResult, ConfigFlowResult,
OptionsFlow, OptionsFlowWithReload,
) )
from homeassistant.const import CONF_API_TOKEN, CONF_NAME from homeassistant.const import CONF_API_TOKEN, CONF_NAME
from homeassistant.core import callback from homeassistant.core import callback
@@ -178,7 +178,7 @@ class TariffSelectorConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_show_form(step_id="reauth_confirm", data_schema=data_schema) return self.async_show_form(step_id="reauth_confirm", data_schema=data_schema)
class PVPCOptionsFlowHandler(OptionsFlow): class PVPCOptionsFlowHandler(OptionsFlowWithReload):
"""Handle PVPC options.""" """Handle PVPC options."""
_power: float | None = None _power: float | None = None

View File

@@ -29,13 +29,16 @@ class ElecPricesDataUpdateCoordinator(DataUpdateCoordinator[EsiosApiData]):
self, hass: HomeAssistant, entry: PVPCConfigEntry, sensor_keys: set[str] self, hass: HomeAssistant, entry: PVPCConfigEntry, sensor_keys: set[str]
) -> None: ) -> None:
"""Initialize.""" """Initialize."""
config = entry.data.copy()
config.update({attr: value for attr, value in entry.options.items() if value})
self.api = PVPCData( self.api = PVPCData(
session=async_get_clientsession(hass), session=async_get_clientsession(hass),
tariff=entry.data[ATTR_TARIFF], tariff=config[ATTR_TARIFF],
local_timezone=hass.config.time_zone, local_timezone=hass.config.time_zone,
power=entry.data[ATTR_POWER], power=config[ATTR_POWER],
power_valley=entry.data[ATTR_POWER_P3], power_valley=config[ATTR_POWER_P3],
api_token=entry.data.get(CONF_API_TOKEN), api_token=config.get(CONF_API_TOKEN),
sensor_keys=tuple(sensor_keys), sensor_keys=tuple(sensor_keys),
) )
super().__init__( super().__init__(

View File

@@ -121,7 +121,6 @@ async def test_config_flow(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "api_token" assert result["step_id"] == "api_token"
assert pvpc_aioclient_mock.call_count == 2 assert pvpc_aioclient_mock.call_count == 2
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_API_TOKEN: "test-token"} result["flow_id"], user_input={CONF_API_TOKEN: "test-token"}
) )