Skip to content

Commit

Permalink
Fix broken subgroup selection (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 30, 2023
1 parent 90caf17 commit b7301d7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
10 changes: 5 additions & 5 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ async def async_step_init(
)

async def save_options(
self, user_input: dict[str, Any], schema: vol.Schema
self, user_input: dict[str, Any], schema: vol.Schema,
) -> dict:
"""Save options, and return errors when validation fails."""
if self.sensor_type == SensorType.DAILY_ENERGY:
Expand Down Expand Up @@ -753,7 +753,7 @@ async def save_options(
return {}

def _process_user_input(
self, user_input: dict[str, Any], schema: vol.Schema
self, user_input: dict[str, Any], schema: vol.Schema,
) -> None:
"""
Process the provided user input against the schema.
Expand Down Expand Up @@ -870,7 +870,7 @@ def _create_virtual_power_schema(


def _create_group_options_schema(
hass: HomeAssistant, config_entry: ConfigEntry | None = None
hass: HomeAssistant, config_entry: ConfigEntry | None = None,
) -> vol.Schema:
"""Create config schema for groups."""
member_sensors = [
Expand Down Expand Up @@ -906,7 +906,7 @@ def _create_group_options_schema(
),
),
vol.Optional(CONF_SUB_GROUPS): _create_group_selector(
hass, current_entry=config_entry, multiple=True
hass, current_entry=config_entry, multiple=True,
),
vol.Optional(CONF_AREA): selector.AreaSelector(),
vol.Optional(
Expand All @@ -930,7 +930,7 @@ def _create_group_selector(
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.GROUP
and (current_entry and config_entry.entry_id != current_entry.entry_id)
and (current_entry is None or config_entry.entry_id != current_entry.entry_id)
]

return selector.SelectSelector(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ async def create_sensors( # noqa: C901
if CONF_ENTITIES in entity_config or context.group:
try:
child_entities = await create_sensors(
hass, entity_config, context=context
hass, entity_config, context=context,
)
entities_to_add.extend_items(child_entities)
except SensorConfigurationError as exception:
Expand Down
57 changes: 57 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import sensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_ENTITIES,
CONF_ENTITY_ID,
Expand Down Expand Up @@ -53,6 +54,7 @@
CONF_POWER_TEMPLATE,
CONF_SENSOR_TYPE,
CONF_STATES_POWER,
CONF_SUB_GROUPS,
CONF_SUB_PROFILE,
CONF_UPDATE_FREQUENCY,
CONF_VALUE,
Expand Down Expand Up @@ -1118,6 +1120,61 @@ async def test_autodiscovered_option_flow(hass: HomeAssistant) -> None:
assert not entry.data[CONF_CREATE_ENERGY_SENSOR]


async def test_subgroup_selector(hass: HomeAssistant) -> None:
# Create two existing group config entries
group1_entry = _create_mock_entry(
hass,
{
CONF_NAME: "Group1",
CONF_SENSOR_TYPE: SensorType.GROUP,
},
)
group2_entry = _create_mock_entry(
hass,
{
CONF_NAME: "Group2",
CONF_SENSOR_TYPE: SensorType.GROUP,
},
)

# Initialize a new config flow
result = await _select_sensor_type(hass, SensorType.GROUP)

# Assert the two existing groups can be selected as subgroup
data_schema: vol.Schema = result["data_schema"]
sub_group_selector: SelectSelector = data_schema.schema[CONF_SUB_GROUPS]
options = sub_group_selector.config["options"]
assert options == [
{"label": "Group1", "value": group1_entry.entry_id},
{"label": "Group2", "value": group2_entry.entry_id},
]

# Create the new group
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Group3",
CONF_SUB_GROUPS: [group1_entry.entry_id, group2_entry.entry_id],
},
)

# Initialize the options flow for the newly created group
new_entry: ConfigEntry = result["result"]
result = await hass.config_entries.options.async_init(
new_entry.entry_id,
data=None,
)

# Assert that the group itself is not selectable as subgroup
data_schema: vol.Schema = result["data_schema"]
sub_group_selector: SelectSelector = data_schema.schema[CONF_SUB_GROUPS]
options = sub_group_selector.config["options"]
assert options == [
{"label": "Group1", "value": group1_entry.entry_id},
{"label": "Group2", "value": group2_entry.entry_id},
]


def _create_mock_entry(
hass: HomeAssistant,
entry_data: ConfigType,
Expand Down

0 comments on commit b7301d7

Please sign in to comment.