Skip to content

Commit

Permalink
fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 8, 2023
1 parent c31e7b1 commit 22cc190
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 8 additions & 6 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ 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),
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.POWER) +
[entity.entity_id for entity in area_entities if isinstance(entity, PowerSensor)],
)
if power_sensor_ids:
power_sensor = create_grouped_power_sensor(
Expand All @@ -183,7 +188,8 @@ 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),
resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.ENERGY) +
[entity.entity_id for entity in area_entities if isinstance(entity, EnergySensor)],
)
if energy_sensor_ids:
energy_sensor = create_grouped_energy_sensor(
Expand Down Expand Up @@ -341,10 +347,6 @@ def resolve_entity_ids_recursively(
)
resolved_ids.extend(entry.data.get(conf_key) or [])

if CONF_AREA in entry.data:
include_entities = resolve_include_entities(hass, {CONF_AREA: entry.data[CONF_AREA]})
resolved_ids.extend([entity.entity_id for entity in include_entities])

# Include the entities from sub groups
subgroups = entry.data.get(CONF_SUB_GROUPS)
if not subgroups:
Expand Down
17 changes: 12 additions & 5 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ async def test_group_include_area(hass: HomeAssistant, entity_reg: EntityRegistr
user_input = {
CONF_NAME: "My group sensor",
CONF_AREA: area.id,
CONF_CREATE_UTILITY_METERS: True,
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
Expand All @@ -744,13 +745,19 @@ async def test_group_include_area(hass: HomeAssistant, entity_reg: EntityRegistr
CONF_HIDE_MEMBERS: False,
CONF_AREA: area.id,
CONF_UNIQUE_ID: "My group sensor",
CONF_CREATE_UTILITY_METERS: False,
CONF_CREATE_UTILITY_METERS: True,
}

await hass.async_block_till_done()
group_state = hass.states.get("sensor.my_group_sensor_power")
assert group_state
assert group_state.attributes.get(CONF_ENTITIES) == {"sensor.test_energy", "sensor.test_power"}

power_state = hass.states.get("sensor.my_group_sensor_power")
assert power_state
assert power_state.attributes.get(CONF_ENTITIES) == {"sensor.test_power"}

energy_state = hass.states.get("sensor.my_group_sensor_energy")
assert energy_state
assert energy_state.attributes.get(CONF_ENTITIES) == {"sensor.test_energy"}

assert hass.states.get("sensor.my_group_sensor_energy_daily")


async def test_can_select_existing_powercalc_entry_as_group_member(
Expand Down

0 comments on commit 22cc190

Please sign in to comment.