From c68fee754db29017588cb5497ffca273d94862bd Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 27 Aug 2025 17:50:25 +0000 Subject: [PATCH] Use OptionsFlowWithReload in pvpc_hourly_pricing --- .../components/pvpc_hourly_pricing/__init__.py | 15 --------------- .../components/pvpc_hourly_pricing/config_flow.py | 4 ++-- .../components/pvpc_hourly_pricing/coordinator.py | 11 +++++++---- .../pvpc_hourly_pricing/test_config_flow.py | 1 - 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/pvpc_hourly_pricing/__init__.py b/homeassistant/components/pvpc_hourly_pricing/__init__.py index ad35e409627..5ea4d65596d 100644 --- a/homeassistant/components/pvpc_hourly_pricing/__init__.py +++ b/homeassistant/components/pvpc_hourly_pricing/__init__.py @@ -4,7 +4,6 @@ from homeassistant.const import CONF_API_TOKEN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .const import ATTR_POWER, ATTR_POWER_P3 from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry 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 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - entry.async_on_unload(entry.add_update_listener(async_update_options)) 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: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/pvpc_hourly_pricing/config_flow.py b/homeassistant/components/pvpc_hourly_pricing/config_flow.py index 3c6b510004a..2efb9cad939 100644 --- a/homeassistant/components/pvpc_hourly_pricing/config_flow.py +++ b/homeassistant/components/pvpc_hourly_pricing/config_flow.py @@ -13,7 +13,7 @@ from homeassistant.config_entries import ( ConfigEntry, ConfigFlow, ConfigFlowResult, - OptionsFlow, + OptionsFlowWithReload, ) from homeassistant.const import CONF_API_TOKEN, CONF_NAME 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) -class PVPCOptionsFlowHandler(OptionsFlow): +class PVPCOptionsFlowHandler(OptionsFlowWithReload): """Handle PVPC options.""" _power: float | None = None diff --git a/homeassistant/components/pvpc_hourly_pricing/coordinator.py b/homeassistant/components/pvpc_hourly_pricing/coordinator.py index bc9d6a21557..c357551be8f 100644 --- a/homeassistant/components/pvpc_hourly_pricing/coordinator.py +++ b/homeassistant/components/pvpc_hourly_pricing/coordinator.py @@ -29,13 +29,16 @@ class ElecPricesDataUpdateCoordinator(DataUpdateCoordinator[EsiosApiData]): self, hass: HomeAssistant, entry: PVPCConfigEntry, sensor_keys: set[str] ) -> None: """Initialize.""" + config = entry.data.copy() + config.update({attr: value for attr, value in entry.options.items() if value}) + self.api = PVPCData( session=async_get_clientsession(hass), - tariff=entry.data[ATTR_TARIFF], + tariff=config[ATTR_TARIFF], local_timezone=hass.config.time_zone, - power=entry.data[ATTR_POWER], - power_valley=entry.data[ATTR_POWER_P3], - api_token=entry.data.get(CONF_API_TOKEN), + power=config[ATTR_POWER], + power_valley=config[ATTR_POWER_P3], + api_token=config.get(CONF_API_TOKEN), sensor_keys=tuple(sensor_keys), ) super().__init__( diff --git a/tests/components/pvpc_hourly_pricing/test_config_flow.py b/tests/components/pvpc_hourly_pricing/test_config_flow.py index fbaeb8aa5a3..7417759cd6a 100644 --- a/tests/components/pvpc_hourly_pricing/test_config_flow.py +++ b/tests/components/pvpc_hourly_pricing/test_config_flow.py @@ -121,7 +121,6 @@ async def test_config_flow( assert result["type"] is FlowResultType.FORM assert result["step_id"] == "api_token" assert pvpc_aioclient_mock.call_count == 2 - result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={CONF_API_TOKEN: "test-token"} )