From d748894b882eed7bba9fd6afcdd9c189a094ffc7 Mon Sep 17 00:00:00 2001 From: Eugenio Panadero Date: Fri, 13 Jan 2023 13:27:11 +0100 Subject: [PATCH] Remove 'tariff' edition from options-flow (#85703) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🎨 Add missing typing for config-flow * 🐛 Remove 'tariff' edition from options-flow The `entry.data["tariff"]` is what makes the `entry.unique_id`, so it's an incoherence to be able to change it in the Options flow * 🌐 Update generated EN translation * 🎨 Link translations of option-flow to those of config-flow --- .../components/pvpc_hourly_pricing/__init__.py | 2 +- .../pvpc_hourly_pricing/config_flow.py | 17 ++++++++++------- .../components/pvpc_hourly_pricing/strings.json | 5 ++--- .../pvpc_hourly_pricing/translations/en.json | 3 +-- .../pvpc_hourly_pricing/test_config_flow.py | 4 ++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/pvpc_hourly_pricing/__init__.py b/homeassistant/components/pvpc_hourly_pricing/__init__.py index a482b175ab0..808ff1b4cc4 100644 --- a/homeassistant/components/pvpc_hourly_pricing/__init__.py +++ b/homeassistant/components/pvpc_hourly_pricing/__init__.py @@ -52,7 +52,7 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None: """Handle options update.""" if any( entry.data.get(attrib) != entry.options.get(attrib) - for attrib in (ATTR_TARIFF, ATTR_POWER, ATTR_POWER_P3) + for attrib in (ATTR_POWER, ATTR_POWER_P3) ): # update entry replacing data with new options hass.config_entries.async_update_entry( diff --git a/homeassistant/components/pvpc_hourly_pricing/config_flow.py b/homeassistant/components/pvpc_hourly_pricing/config_flow.py index 694a3db98fe..9412aa2e97d 100644 --- a/homeassistant/components/pvpc_hourly_pricing/config_flow.py +++ b/homeassistant/components/pvpc_hourly_pricing/config_flow.py @@ -1,12 +1,15 @@ """Config flow for pvpc_hourly_pricing.""" from __future__ import annotations +from typing import Any + import voluptuous as vol from homeassistant import config_entries from homeassistant.core import callback +from homeassistant.data_entry_flow import FlowResult -from . import CONF_NAME, UI_CONFIG_SCHEMA, VALID_POWER, VALID_TARIFF +from . import CONF_NAME, UI_CONFIG_SCHEMA, VALID_POWER from .const import ATTR_POWER, ATTR_POWER_P3, ATTR_TARIFF, DOMAIN @@ -23,7 +26,9 @@ class TariffSelectorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return PVPCOptionsFlowHandler(config_entry) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle the initial step.""" if user_input is not None: await self.async_set_unique_id(user_input[ATTR_TARIFF]) @@ -40,15 +45,14 @@ class PVPCOptionsFlowHandler(config_entries.OptionsFlow): """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) # Fill options with entry data - tariff = self.config_entry.options.get( - ATTR_TARIFF, self.config_entry.data[ATTR_TARIFF] - ) power = self.config_entry.options.get( ATTR_POWER, self.config_entry.data[ATTR_POWER] ) @@ -57,7 +61,6 @@ class PVPCOptionsFlowHandler(config_entries.OptionsFlow): ) schema = vol.Schema( { - vol.Required(ATTR_TARIFF, default=tariff): VALID_TARIFF, vol.Required(ATTR_POWER, default=power): VALID_POWER, vol.Required(ATTR_POWER_P3, default=power_valley): VALID_POWER, } diff --git a/homeassistant/components/pvpc_hourly_pricing/strings.json b/homeassistant/components/pvpc_hourly_pricing/strings.json index a008ef9f4da..1a0055ddbac 100644 --- a/homeassistant/components/pvpc_hourly_pricing/strings.json +++ b/homeassistant/components/pvpc_hourly_pricing/strings.json @@ -18,9 +18,8 @@ "step": { "init": { "data": { - "tariff": "Applicable tariff by geographic zone", - "power": "Contracted power (kW)", - "power_p3": "Contracted power for valley period P3 (kW)" + "power": "[%key:component::pvpc_hourly_pricing::config::step::user::data::power%]", + "power_p3": "[%key:component::pvpc_hourly_pricing::config::step::user::data::power_p3%]" } } } diff --git a/homeassistant/components/pvpc_hourly_pricing/translations/en.json b/homeassistant/components/pvpc_hourly_pricing/translations/en.json index 9667d14fd05..73a5a2ca306 100644 --- a/homeassistant/components/pvpc_hourly_pricing/translations/en.json +++ b/homeassistant/components/pvpc_hourly_pricing/translations/en.json @@ -19,8 +19,7 @@ "init": { "data": { "power": "Contracted power (kW)", - "power_p3": "Contracted power for valley period P3 (kW)", - "tariff": "Applicable tariff by geographic zone" + "power_p3": "Contracted power for valley period P3 (kW)" } } } diff --git a/tests/components/pvpc_hourly_pricing/test_config_flow.py b/tests/components/pvpc_hourly_pricing/test_config_flow.py index 94d9898aa58..125214066a5 100644 --- a/tests/components/pvpc_hourly_pricing/test_config_flow.py +++ b/tests/components/pvpc_hourly_pricing/test_config_flow.py @@ -102,11 +102,11 @@ async def test_config_flow(hass, pvpc_aioclient_mock: AiohttpClientMocker): result = await hass.config_entries.options.async_configure( result["flow_id"], - user_input={ATTR_TARIFF: TARIFFS[0], ATTR_POWER: 3.0, ATTR_POWER_P3: 4.6}, + user_input={ATTR_POWER: 3.0, ATTR_POWER_P3: 4.6}, ) await hass.async_block_till_done() state = hass.states.get("sensor.test") - check_valid_state(state, tariff=TARIFFS[0]) + check_valid_state(state, tariff=TARIFFS[1]) assert pvpc_aioclient_mock.call_count == 3 assert state.attributes["period"] == "P3" assert state.attributes["next_period"] == "P2"