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,
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",
},
)

View file

@ -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(

View file

@ -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",
)

View file

@ -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",

View file

@ -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",