Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize OptionsFlow (#2827) #2898

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, cast

import voluptuous as vol
from awesomeversion import AwesomeVersion
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.utility_meter import CONF_METER_TYPE, METER_TYPES
from homeassistant.config_entries import ConfigEntry, ConfigEntryBaseFlow, ConfigFlow, ConfigFlowResult, OptionsFlow
Expand All @@ -30,6 +31,9 @@
UnitOfPower,
UnitOfTime,
)
from homeassistant.const import (
__version__ as HAVERSION, # noqa
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import entity_registry as er
Expand Down Expand Up @@ -1675,7 +1679,8 @@ class PowercalcOptionsFlow(PowercalcCommonFlow, OptionsFlow):
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
super().__init__()
self._config_entry = config_entry
if AwesomeVersion(HAVERSION) < "2024.12":
self.config_entry = config_entry
self.sensor_config = dict(config_entry.data)
self.sensor_type: SensorType = self.sensor_config.get(CONF_SENSOR_TYPE) or SensorType.VIRTUAL_POWER
self.source_entity_id: str = self.sensor_config.get(CONF_ENTITY_ID) # type: ignore
Expand All @@ -1686,11 +1691,11 @@ async def async_step_init(
user_input: dict[str, Any] | None = None,
) -> FlowResult:
"""Handle options flow."""
if self._config_entry.unique_id == ENTRY_GLOBAL_CONFIG_UNIQUE_ID:
if self.config_entry.unique_id == ENTRY_GLOBAL_CONFIG_UNIQUE_ID:
self.global_config = self.get_global_powercalc_config()
return self.async_show_menu(step_id=Step.INIT, menu_options=self.build_global_config_menu())

self.sensor_config = dict(self._config_entry.data)
self.sensor_config = dict(self.config_entry.data)
if self.source_entity_id:
self.source_entity = await create_source_entity(
self.source_entity_id,
Expand Down Expand Up @@ -1830,7 +1835,7 @@ async def async_step_real_power(self, user_input: dict[str, Any] | None = None)
async def async_step_group_custom(self, user_input: dict[str, Any] | None = None) -> FlowResult:
"""Handle the group options flow."""
schema = self.fill_schema_defaults(
self.create_schema_group_custom(self._config_entry, True),
self.create_schema_group_custom(self.config_entry, True),
self.sensor_config,
)
return await self.async_handle_options_step(user_input, schema, Step.GROUP_CUSTOM)
Expand Down Expand Up @@ -1928,10 +1933,10 @@ async def async_handle_options_step(self, user_input: dict[str, Any] | None, sch

def persist_config_entry(self) -> FlowResult:
"""Persist changed options on the config entry."""
data = (self._config_entry.unique_id == ENTRY_GLOBAL_CONFIG_UNIQUE_ID and self.global_config) or self.sensor_config
data = (self.config_entry.unique_id == ENTRY_GLOBAL_CONFIG_UNIQUE_ID and self.global_config) or self.sensor_config

self.hass.config_entries.async_update_entry(
self._config_entry,
self.config_entry,
data=data,
)
return self.async_create_entry(title="", data={})
Expand Down
Loading