329 files
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
"""Config flow for Powercalc integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Awaitable, Callable
|
||||
from inspect import isawaitable
|
||||
@@ -33,6 +31,7 @@ from .common import SourceEntity, create_source_entity
|
||||
from .const import (
|
||||
CONF_CREATE_COST_SENSOR,
|
||||
CONF_CREATE_UTILITY_METERS,
|
||||
CONF_ENERGY_PRICE_SENSOR,
|
||||
CONF_MANUFACTURER,
|
||||
CONF_MODE,
|
||||
CONF_MODEL,
|
||||
@@ -47,6 +46,7 @@ from .const import (
|
||||
CalculationStrategy,
|
||||
SensorType,
|
||||
)
|
||||
from .device_binding import attach_configured_device_entry
|
||||
from .errors import ModelNotSupportedError, StrategyConfigurationError
|
||||
from .flow_helper.common import FlowType, PowercalcFormStep, Step, fill_schema_defaults, flatten_sections
|
||||
from .flow_helper.flows.cost import CostConfigFlow, CostOptionsFlow
|
||||
@@ -76,12 +76,13 @@ from .flow_helper.flows.virtual_power import (
|
||||
from .flow_helper.profile_preview import async_setup_preview as async_setup_powercalc_preview
|
||||
from .flow_helper.schema import (
|
||||
COST_DOCS_URI,
|
||||
SCHEMA_COST_PRICING,
|
||||
SCHEMA_COST_SENSOR_TOGGLE,
|
||||
SCHEMA_ENERGY_SENSOR_TOGGLE,
|
||||
SCHEMA_SENSOR_ENERGY_OPTIONS,
|
||||
SCHEMA_STANDBY_ENERGY_SENSOR_TOGGLE,
|
||||
SCHEMA_UTILITY_METER_OPTIONS,
|
||||
SCHEMA_UTILITY_METER_TOGGLE,
|
||||
build_cost_pricing_schema,
|
||||
)
|
||||
from .power_profile.factory import get_power_profile
|
||||
from .power_profile.power_profile import SUPPORTED_DOMAINS, DeviceType, DiscoveryBy, PowerProfile
|
||||
@@ -455,21 +456,21 @@ class PowercalcConfigFlow(PowercalcCommonFlow, ConfigFlow, domain=DOMAIN):
|
||||
@callback
|
||||
def persist_config_entry(self) -> ConfigFlowResult:
|
||||
"""Create the config entry."""
|
||||
self.sensor_config.update({CONF_SENSOR_TYPE: self.selected_sensor_type})
|
||||
self.sensor_config.update({CONF_NAME: self.name})
|
||||
entry_data: ConfigType = {
|
||||
**self.sensor_config,
|
||||
CONF_SENSOR_TYPE: self.selected_sensor_type,
|
||||
CONF_NAME: self.name,
|
||||
}
|
||||
|
||||
if self.source_entity_id:
|
||||
self.sensor_config.update({CONF_ENTITY_ID: self.source_entity_id})
|
||||
entry_data[CONF_ENTITY_ID] = self.source_entity_id
|
||||
|
||||
if (
|
||||
self.selected_profile
|
||||
and self.source_entity
|
||||
and self.source_entity.device_entry
|
||||
and self.selected_profile.discovery_by == DiscoveryBy.DEVICE
|
||||
):
|
||||
self.sensor_config.update({CONF_DEVICE: self.source_entity.device_entry.id})
|
||||
profile = self.selected_profile
|
||||
source_entity = self.source_entity
|
||||
if profile and source_entity and profile.discovery_by == DiscoveryBy.DEVICE and source_entity.device_entry:
|
||||
entry_data[CONF_DEVICE] = source_entity.device_entry.id
|
||||
|
||||
return self.async_create_entry(title=str(self.name), data=self.sensor_config)
|
||||
return self.async_create_entry(title=str(self.name), data=entry_data)
|
||||
|
||||
|
||||
class PowercalcOptionsFlow(PowercalcCommonFlow, OptionsFlow):
|
||||
@@ -507,9 +508,13 @@ class PowercalcOptionsFlow(PowercalcCommonFlow, OptionsFlow):
|
||||
|
||||
self.sensor_config = dict(self.config_entry.data)
|
||||
if self.source_entity_id:
|
||||
self.source_entity = create_source_entity(
|
||||
self.source_entity_id,
|
||||
self.source_entity = attach_configured_device_entry(
|
||||
self.hass,
|
||||
self.sensor_config,
|
||||
create_source_entity(
|
||||
self.source_entity_id,
|
||||
self.hass,
|
||||
),
|
||||
)
|
||||
result = await self.initialize_library_profile()
|
||||
if result:
|
||||
@@ -599,7 +604,7 @@ class PowercalcOptionsFlow(PowercalcCommonFlow, OptionsFlow):
|
||||
"""Handle the per sensor energy price override."""
|
||||
return await self.async_handle_options_step(
|
||||
user_input,
|
||||
SCHEMA_COST_PRICING,
|
||||
build_cost_pricing_schema(self.hass, self.sensor_config.get(CONF_ENERGY_PRICE_SENSOR)),
|
||||
Step.COST_OPTIONS,
|
||||
form_kwarg={"description_placeholders": {"docs_uri": COST_DOCS_URI}},
|
||||
)
|
||||
@@ -731,6 +736,7 @@ class PowercalcOptionsFlow(PowercalcCommonFlow, OptionsFlow):
|
||||
return schema.extend( # type: ignore[no-any-return]
|
||||
{
|
||||
**SCHEMA_ENERGY_SENSOR_TOGGLE.schema,
|
||||
**SCHEMA_STANDBY_ENERGY_SENSOR_TOGGLE.schema,
|
||||
**SCHEMA_COST_SENSOR_TOGGLE.schema,
|
||||
**SCHEMA_UTILITY_METER_TOGGLE.schema,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user