mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 02:03:27 +01:00
Use OptionsFlowWithReload in pvpc_hourly_pricing (#151255)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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__(
|
||||
|
||||
@@ -121,16 +121,14 @@ 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"}
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert pvpc_aioclient_mock.call_count == 2
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("sensor.esios_pvpc")
|
||||
check_valid_state(state, tariff=TARIFFS[1])
|
||||
assert pvpc_aioclient_mock.call_count == 4
|
||||
assert pvpc_aioclient_mock.call_count == 3
|
||||
assert state.attributes["period"] == "P3"
|
||||
assert state.attributes["next_period"] == "P2"
|
||||
assert state.attributes["available_power"] == 4600
|
||||
@@ -151,7 +149,7 @@ async def test_config_flow(
|
||||
state = hass.states.get("sensor.esios_pvpc")
|
||||
check_valid_state(state, tariff=TARIFFS[0], value="unavailable")
|
||||
assert "period" not in state.attributes
|
||||
assert pvpc_aioclient_mock.call_count == 6
|
||||
assert pvpc_aioclient_mock.call_count == 5
|
||||
|
||||
# disable api token in options
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
@@ -163,9 +161,8 @@ async def test_config_flow(
|
||||
user_input={ATTR_POWER: 3.0, ATTR_POWER_P3: 4.6, CONF_USE_API_TOKEN: False},
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert pvpc_aioclient_mock.call_count == 6
|
||||
await hass.async_block_till_done()
|
||||
assert pvpc_aioclient_mock.call_count == 7
|
||||
assert pvpc_aioclient_mock.call_count == 6
|
||||
|
||||
state = hass.states.get("sensor.esios_pvpc")
|
||||
state_inyection = hass.states.get("sensor.esios_injection_price")
|
||||
|
||||
Reference in New Issue
Block a user