Skip to content

Commit

Permalink
Fix include area based groups as subgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 30, 2023
1 parent b74df1e commit befeabc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
30 changes: 11 additions & 19 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -174,20 +174,8 @@ async def create_group_sensors_from_config_entry(
if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.entry_id

area_entities: list[Entity] = []
if CONF_AREA in entry.data:
area_entities = resolve_include_entities(
hass,
{CONF_AREA: entry.data[CONF_AREA]},
)

power_sensor_ids: set[str] = set(
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.POWER)
+ [
entity.entity_id
for entity in area_entities
if isinstance(entity, PowerSensor)
],
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.POWER),
)
if power_sensor_ids:
power_sensor = create_grouped_power_sensor(
Expand All @@ -199,12 +187,7 @@ async def create_group_sensors_from_config_entry(
group_sensors.append(power_sensor)

energy_sensor_ids: set[str] = set(
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.ENERGY)
+ [
entity.entity_id
for entity in area_entities
if isinstance(entity, EnergySensor)
],
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.ENERGY),
)
if energy_sensor_ids:
energy_sensor = create_grouped_energy_sensor(
Expand Down Expand Up @@ -362,6 +345,15 @@ def resolve_entity_ids_recursively(
)
resolved_ids.extend(entry.data.get(conf_key) or [])

# Include entities from defined areas
if CONF_AREA in entry.data:
area_entities = [
entity.entity_id
for entity in resolve_include_entities(hass, {CONF_AREA: entry.data[CONF_AREA]})
if isinstance(entity, PowerSensor if device_class == SensorDeviceClass.POWER else EnergySensor)
]
resolved_ids.extend(area_entities)

# Include the entities from sub groups
subgroups = entry.data.get(CONF_SUB_GROUPS)
if not subgroups:
Expand Down
48 changes: 48 additions & 0 deletions tests/group_include/test_include.py
Expand Up @@ -8,6 +8,7 @@
CONF_DOMAIN,
CONF_ENTITIES,
CONF_ENTITY_ID,
CONF_NAME,
CONF_UNIQUE_ID,
STATE_OFF,
)
Expand All @@ -32,6 +33,7 @@
CONF_INCLUDE,
CONF_POWER,
CONF_SENSOR_TYPE,
CONF_SUB_GROUPS,
CONF_TEMPLATE,
DOMAIN,
SensorType,
Expand Down Expand Up @@ -502,6 +504,52 @@ async def test_group_setup_continues_when_subgroup_has_no_include_entities(
assert hass.states.get("sensor.groupc_power")


async def test_area_groups_as_subgroups(
hass: HomeAssistant,
entity_reg: EntityRegistry,
area_reg: AreaRegistry,
) -> None:
await create_mock_light_entity(hass, create_discoverable_light("bathroom_mirror"))

area_bathroom = area_reg.async_get_or_create("Bathroom")
area_reg.async_get_or_create("Bedroom")
entity_reg.async_update_entity("light.bathroom_mirror", area_id=area_bathroom.id)

_create_powercalc_config_entry(hass, "light.bathroom_mirror")

group_a_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_NAME: "GroupA",
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_AREA: area_bathroom.name,
},
unique_id="groupA",
)
group_a_entry.add_to_hass(hass)

group_b_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_NAME: "GroupB",
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_SUB_GROUPS: [group_a_entry.entry_id],
},
unique_id="groupB",
)
group_b_entry.add_to_hass(hass)

await run_powercalc_setup(hass, {})

group_a_power = hass.states.get("sensor.groupa_power")
assert group_a_power
assert group_a_power.attributes.get(CONF_ENTITIES) == {"sensor.bathroom_mirror_power"}

group_b_power = hass.states.get("sensor.groupb_power")
assert group_b_power
assert group_b_power.attributes.get(CONF_ENTITIES) == {"sensor.bathroom_mirror_power"}


def _create_powercalc_config_entry(
hass: HomeAssistant,
source_entity_id: str,
Expand Down

0 comments on commit befeabc

Please sign in to comment.