fix options flow

This commit is contained in:
Julian Pawlowski 2025-11-02 16:58:47 +00:00
parent 4cd93d8a80
commit e02630440a
5 changed files with 85 additions and 28 deletions

View file

@ -10,7 +10,6 @@ from homeassistant.config_entries import (
ConfigEntry, ConfigEntry,
ConfigFlow, ConfigFlow,
ConfigFlowResult, ConfigFlowResult,
ConfigSubentry,
ConfigSubentryFlow, ConfigSubentryFlow,
OptionsFlow, OptionsFlow,
SubentryFlowResult, SubentryFlowResult,
@ -72,6 +71,12 @@ class TibberPricesFlowHandler(ConfigFlow, domain=DOMAIN):
"""Return subentries supported by this integration.""" """Return subentries supported by this integration."""
return {"home": TibberPricesSubentryFlowHandler} return {"home": TibberPricesSubentryFlowHandler}
@staticmethod
@callback
def async_get_options_flow() -> OptionsFlow:
"""Create an options flow for this configentry."""
return TibberPricesOptionsFlowHandler()
@staticmethod @staticmethod
def async_get_reauth_flow(entry: ConfigEntry) -> ConfigFlow: def async_get_reauth_flow(entry: ConfigEntry) -> ConfigFlow:
"""Return the reauth flow handler for this integration.""" """Return the reauth flow handler for this integration."""
@ -164,6 +169,7 @@ class TibberPricesFlowHandler(ConfigFlow, domain=DOMAIN):
"home_id": selected_home_id, "home_id": selected_home_id,
"home_data": selected_home, "home_data": selected_home,
"homes": homes, "homes": homes,
"user_login": self._user_login or "N/A",
} }
return self.async_create_entry( return self.async_create_entry(
@ -364,32 +370,22 @@ class TibberPricesSubentryFlowHandler(ConfigSubentryFlow):
return home.get("id", "Unknown Home") return home.get("id", "Unknown Home")
async def async_step_init(self, user_input: dict | None = None) -> SubentryFlowResult:
class TibberPricesOptionsSubentryFlowHandler(OptionsFlow): """Manage the options for a subentry."""
"""Tibber Prices config flow options handler.""" subentry = self._get_reconfigure_subentry()
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."""
errors: dict[str, str] = {} errors: dict[str, str] = {}
options = { options = {
vol.Optional( vol.Optional(
CONF_EXTENDED_DESCRIPTIONS, CONF_EXTENDED_DESCRIPTIONS,
default=self.config_entry.options.get( default=subentry.data.get(CONF_EXTENDED_DESCRIPTIONS, DEFAULT_EXTENDED_DESCRIPTIONS),
CONF_EXTENDED_DESCRIPTIONS,
self.config_entry.data.get(CONF_EXTENDED_DESCRIPTIONS, DEFAULT_EXTENDED_DESCRIPTIONS),
),
): BooleanSelector(), ): BooleanSelector(),
vol.Optional( vol.Optional(
CONF_BEST_PRICE_FLEX, CONF_BEST_PRICE_FLEX,
default=int( default=int(
self.config_entry.options.get( subentry.data.get(
CONF_BEST_PRICE_FLEX, CONF_BEST_PRICE_FLEX,
self.config_entry.data.get(CONF_BEST_PRICE_FLEX, DEFAULT_BEST_PRICE_FLEX), DEFAULT_BEST_PRICE_FLEX,
) )
), ),
): NumberSelector( ): NumberSelector(
@ -403,9 +399,9 @@ class TibberPricesOptionsSubentryFlowHandler(OptionsFlow):
vol.Optional( vol.Optional(
CONF_PEAK_PRICE_FLEX, CONF_PEAK_PRICE_FLEX,
default=int( default=int(
self.config_entry.options.get( subentry.data.get(
CONF_PEAK_PRICE_FLEX, CONF_PEAK_PRICE_FLEX,
self.config_entry.data.get(CONF_PEAK_PRICE_FLEX, DEFAULT_PEAK_PRICE_FLEX), DEFAULT_PEAK_PRICE_FLEX,
) )
), ),
): NumberSelector( ): NumberSelector(
@ -419,15 +415,76 @@ class TibberPricesOptionsSubentryFlowHandler(OptionsFlow):
} }
if user_input is not None: if user_input is not None:
return self.async_create_entry(title="", data=user_input) return self.async_update_and_abort(
self._get_entry(),
description_placeholders = { subentry,
"unique_id": self.config_entry.unique_id or "", data_updates=user_input,
} )
return self.async_show_form( return self.async_show_form(
step_id="init", step_id="init",
data_schema=vol.Schema(options), data_schema=vol.Schema(options),
errors=errors, 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",
},
) )

View file

@ -455,7 +455,6 @@ def get_home_type_translation(
""" """
translated = get_translation(["home_types", home_type], language) translated = get_translation(["home_types", home_type], language)
if translated: if translated:
LOGGER.debug("Found translation for home type '%s' in language '%s': %s", home_type, language, translated)
return translated return translated
fallback = HOME_TYPES.get(home_type) fallback = HOME_TYPES.get(home_type)
LOGGER.debug( LOGGER.debug(

View file

@ -83,4 +83,5 @@ class TibberPricesEntity(CoordinatorEntity[TibberPricesDataUpdateCoordinator]):
model=translated_model, model=translated_model,
model_id=home_type if home_type else None, model_id=home_type if home_type else None,
serial_number=home_id if home_id else None, serial_number=home_id if home_id else None,
configuration_url="https://developer.tibber.com/explorer",
) )

View file

@ -66,7 +66,7 @@
"options": { "options": {
"step": { "step": {
"init": { "init": {
"description": "Home ID: {unique_id}", "description": "Benutzer: {user_login}",
"data": { "data": {
"access_token": "API-Zugriffstoken", "access_token": "API-Zugriffstoken",
"extended_descriptions": "Erweiterte Beschreibungen in Entitätsattributen anzeigen", "extended_descriptions": "Erweiterte Beschreibungen in Entitätsattributen anzeigen",

View file

@ -66,7 +66,7 @@
"options": { "options": {
"step": { "step": {
"init": { "init": {
"description": "Home ID: {unique_id}", "description": "User: {user_login}",
"data": { "data": {
"access_token": "API access token", "access_token": "API access token",
"extended_descriptions": "Show extended descriptions in entity attributes", "extended_descriptions": "Show extended descriptions in entity attributes",