From e02630440af2f6d8b76c6230a93b68f60d3ac207 Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Sun, 2 Nov 2025 16:58:47 +0000 Subject: [PATCH] fix options flow --- .../tibber_prices/config_flow.py | 107 ++++++++++++++---- custom_components/tibber_prices/const.py | 1 - custom_components/tibber_prices/entity.py | 1 + .../tibber_prices/translations/de.json | 2 +- .../tibber_prices/translations/en.json | 2 +- 5 files changed, 85 insertions(+), 28 deletions(-) diff --git a/custom_components/tibber_prices/config_flow.py b/custom_components/tibber_prices/config_flow.py index 9139952..f203a3d 100644 --- a/custom_components/tibber_prices/config_flow.py +++ b/custom_components/tibber_prices/config_flow.py @@ -10,7 +10,6 @@ from homeassistant.config_entries import ( ConfigEntry, ConfigFlow, ConfigFlowResult, - ConfigSubentry, ConfigSubentryFlow, OptionsFlow, SubentryFlowResult, @@ -72,6 +71,12 @@ class TibberPricesFlowHandler(ConfigFlow, domain=DOMAIN): """Return subentries supported by this integration.""" return {"home": TibberPricesSubentryFlowHandler} + @staticmethod + @callback + def async_get_options_flow() -> OptionsFlow: + """Create an options flow for this configentry.""" + return TibberPricesOptionsFlowHandler() + @staticmethod def async_get_reauth_flow(entry: ConfigEntry) -> ConfigFlow: """Return the reauth flow handler for this integration.""" @@ -164,6 +169,7 @@ class TibberPricesFlowHandler(ConfigFlow, domain=DOMAIN): "home_id": selected_home_id, "home_data": selected_home, "homes": homes, + "user_login": self._user_login or "N/A", } return self.async_create_entry( @@ -364,32 +370,22 @@ class TibberPricesSubentryFlowHandler(ConfigSubentryFlow): return home.get("id", "Unknown Home") - -class TibberPricesOptionsSubentryFlowHandler(OptionsFlow): - """Tibber Prices config flow options handler.""" - - def __init__(self, config_entry: ConfigSubentry) -> None: # noqa: ARG002 - """Initialize options flow.""" - super().__init__() - - async def async_step_init(self, user_input: dict | None = None) -> ConfigFlowResult: - """Manage the options.""" + async def async_step_init(self, user_input: dict | None = None) -> SubentryFlowResult: + """Manage the options for a subentry.""" + subentry = self._get_reconfigure_subentry() errors: dict[str, str] = {} options = { vol.Optional( CONF_EXTENDED_DESCRIPTIONS, - default=self.config_entry.options.get( - CONF_EXTENDED_DESCRIPTIONS, - self.config_entry.data.get(CONF_EXTENDED_DESCRIPTIONS, DEFAULT_EXTENDED_DESCRIPTIONS), - ), + default=subentry.data.get(CONF_EXTENDED_DESCRIPTIONS, DEFAULT_EXTENDED_DESCRIPTIONS), ): BooleanSelector(), vol.Optional( CONF_BEST_PRICE_FLEX, default=int( - self.config_entry.options.get( + subentry.data.get( CONF_BEST_PRICE_FLEX, - self.config_entry.data.get(CONF_BEST_PRICE_FLEX, DEFAULT_BEST_PRICE_FLEX), + DEFAULT_BEST_PRICE_FLEX, ) ), ): NumberSelector( @@ -403,9 +399,9 @@ class TibberPricesOptionsSubentryFlowHandler(OptionsFlow): vol.Optional( CONF_PEAK_PRICE_FLEX, default=int( - self.config_entry.options.get( + subentry.data.get( CONF_PEAK_PRICE_FLEX, - self.config_entry.data.get(CONF_PEAK_PRICE_FLEX, DEFAULT_PEAK_PRICE_FLEX), + DEFAULT_PEAK_PRICE_FLEX, ) ), ): NumberSelector( @@ -419,15 +415,76 @@ class TibberPricesOptionsSubentryFlowHandler(OptionsFlow): } if user_input is not None: - return self.async_create_entry(title="", data=user_input) - - description_placeholders = { - "unique_id": self.config_entry.unique_id or "", - } + return self.async_update_and_abort( + self._get_entry(), + subentry, + data_updates=user_input, + ) return self.async_show_form( step_id="init", data_schema=vol.Schema(options), errors=errors, - description_placeholders=description_placeholders, + ) + + +class TibberPricesOptionsFlowHandler(OptionsFlow): + """Handle options for tibber_prices entries.""" + + async def async_step_init(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult: + """Manage the options.""" + if user_input is not None: + return self.async_create_entry( + title="", + data=user_input, + ) + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Optional( + CONF_EXTENDED_DESCRIPTIONS, + default=self.config_entry.options.get( + CONF_EXTENDED_DESCRIPTIONS, DEFAULT_EXTENDED_DESCRIPTIONS + ), + ): BooleanSelector(), + vol.Optional( + CONF_BEST_PRICE_FLEX, + default=int( + self.config_entry.options.get( + CONF_BEST_PRICE_FLEX, + DEFAULT_BEST_PRICE_FLEX, + ) + ), + ): NumberSelector( + NumberSelectorConfig( + min=0, + max=100, + step=1, + mode=NumberSelectorMode.SLIDER, + ), + ), + vol.Optional( + CONF_PEAK_PRICE_FLEX, + default=int( + self.config_entry.options.get( + CONF_PEAK_PRICE_FLEX, + DEFAULT_PEAK_PRICE_FLEX, + ) + ), + ): NumberSelector( + NumberSelectorConfig( + min=0, + max=100, + step=1, + mode=NumberSelectorMode.SLIDER, + ), + ), + } + ), + description_placeholders={ + "user_login": self.config_entry.data.get("user_login", "N/A"), + "unique_id": self.config_entry.unique_id or "unknown", + }, ) diff --git a/custom_components/tibber_prices/const.py b/custom_components/tibber_prices/const.py index 164c88c..a8148c1 100644 --- a/custom_components/tibber_prices/const.py +++ b/custom_components/tibber_prices/const.py @@ -455,7 +455,6 @@ def get_home_type_translation( """ translated = get_translation(["home_types", home_type], language) if translated: - LOGGER.debug("Found translation for home type '%s' in language '%s': %s", home_type, language, translated) return translated fallback = HOME_TYPES.get(home_type) LOGGER.debug( diff --git a/custom_components/tibber_prices/entity.py b/custom_components/tibber_prices/entity.py index 207c2d1..72ebcdc 100644 --- a/custom_components/tibber_prices/entity.py +++ b/custom_components/tibber_prices/entity.py @@ -83,4 +83,5 @@ class TibberPricesEntity(CoordinatorEntity[TibberPricesDataUpdateCoordinator]): model=translated_model, model_id=home_type if home_type else None, serial_number=home_id if home_id else None, + configuration_url="https://developer.tibber.com/explorer", ) diff --git a/custom_components/tibber_prices/translations/de.json b/custom_components/tibber_prices/translations/de.json index 3301b23..dff1247 100644 --- a/custom_components/tibber_prices/translations/de.json +++ b/custom_components/tibber_prices/translations/de.json @@ -66,7 +66,7 @@ "options": { "step": { "init": { - "description": "Home ID: {unique_id}", + "description": "Benutzer: {user_login}", "data": { "access_token": "API-Zugriffstoken", "extended_descriptions": "Erweiterte Beschreibungen in Entitätsattributen anzeigen", diff --git a/custom_components/tibber_prices/translations/en.json b/custom_components/tibber_prices/translations/en.json index aa8cebe..a1b9171 100644 --- a/custom_components/tibber_prices/translations/en.json +++ b/custom_components/tibber_prices/translations/en.json @@ -66,7 +66,7 @@ "options": { "step": { "init": { - "description": "Home ID: {unique_id}", + "description": "User: {user_login}", "data": { "access_token": "API access token", "extended_descriptions": "Show extended descriptions in entity attributes",