Skip to content

Commit

Permalink
Commit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 13, 2022
1 parent 670f255 commit fcfcdf0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
49 changes: 28 additions & 21 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Config flow for Adaptive Lighting integration."""

from __future__ import annotations
from audioop import mul

import copy
import logging
Expand Down Expand Up @@ -117,7 +118,7 @@
}
)

SCHEMA_POWER = vol.Schema(
SCHEMA_POWER_BASE = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(),
vol.Optional(CONF_NAME): selector.TextSelector(),
Expand All @@ -135,9 +136,8 @@
mode=selector.SelectSelectorMode.DROPDOWN,
)
),
vol.Optional(CONF_GROUP): selector.TextSelector(),
}
).extend(SCHEMA_POWER_OPTIONS.schema)
)

SCHEMA_POWER_FIXED = vol.Schema(
{
Expand Down Expand Up @@ -227,7 +227,7 @@ async def async_step_virtual_power(

return self.async_show_form(
step_id="virtual_power",
data_schema=SCHEMA_POWER,
data_schema=_create_virtual_power_schema(self.hass),
errors={},
)

Expand Down Expand Up @@ -526,10 +526,9 @@ def build_options_schema(self) -> vol.Schema:

strategy_options = {}
if self.sensor_type == SensorType.VIRTUAL_POWER:
base_power_schema = SCHEMA_POWER_OPTIONS
strategy: str = self.current_config.get(CONF_MODE)
strategy_schema = _get_strategy_schema(strategy, self.source_entity_id)
data_schema = base_power_schema.extend(strategy_schema.schema)
data_schema = SCHEMA_POWER_OPTIONS.extend(strategy_schema.schema)
strategy_options = self.current_config.get(strategy) or {}

if self.sensor_type == SensorType.DAILY_ENERGY:
Expand Down Expand Up @@ -569,23 +568,16 @@ def _get_strategy_schema(strategy: str, source_entity_id: str) -> vol.Schema:
if strategy == CalculationStrategy.LUT:
return vol.Schema({})

def _create_virtual_power_schema(hass: HomeAssistant) -> vol.Schema:
base_schema: vol.Schema = SCHEMA_POWER_BASE.extend(
{
vol.Optional(CONF_GROUP): _create_group_selector(hass)
}
)
return base_schema.extend(SCHEMA_POWER_OPTIONS.schema)

def _create_group_options_schema(hass: HomeAssistant) -> vol.Schema:
"""Create config schema for groups"""
sub_groups = [
selector.SelectOptionDict(
value=config_entry.entry_id, label=config_entry.data.get(CONF_NAME)
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.GROUP
]

sub_group_selector = selector.SelectSelector(
selector.SelectSelectorConfig(
options=sub_groups, multiple=True, mode=selector.SelectSelectorMode.DROPDOWN
)
)

member_sensors = [
selector.SelectOptionDict(
value=config_entry.entry_id, label=config_entry.data.get(CONF_NAME)
Expand Down Expand Up @@ -619,14 +611,29 @@ def _create_group_options_schema(hass: HomeAssistant) -> vol.Schema:
multiple=True,
)
),
vol.Optional(CONF_SUB_GROUPS): sub_group_selector,
vol.Optional(CONF_SUB_GROUPS): _create_group_selector(hass, multiple=True),
vol.Optional(
CONF_CREATE_UTILITY_METERS, default=False
): selector.BooleanSelector(),
vol.Optional(CONF_HIDE_MEMBERS, default=False): selector.BooleanSelector(),
}
)

def _create_group_selector(hass: HomeAssistant, multiple: bool = False) -> selector.SelectSelector:
options = [
selector.SelectOptionDict(
value=config_entry.entry_id, label=config_entry.data.get(CONF_NAME)
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.GROUP
]

return selector.SelectSelector(
selector.SelectSelectorConfig(
options=options, multiple=multiple, mode=selector.SelectSelectorMode.DROPDOWN
)
)


def _validate_group_input(user_input: dict[str, str] = None) -> dict:
"""Validate the group form"""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def resolve_entity_ids_recursively(

# Include the power/energy sensors for an existing Virtual Power config entry
entity_reg = er.async_get(hass)
member_entry_ids = entry.data.get(CONF_GROUP_MEMBER_SENSORS)
member_entry_ids = entry.data.get(CONF_GROUP_MEMBER_SENSORS) or []
# Unfortunately device_class is not correctly set at this time in the entity_registry
# So we need to match on state_class.
state_class = (
Expand Down

0 comments on commit fcfcdf0

Please sign in to comment.