diff --git a/custom_components/powercalc/sensors/group.py b/custom_components/powercalc/sensors/group.py index 4f24b545f..1ea1a06b1 100644 --- a/custom_components/powercalc/sensors/group.py +++ b/custom_components/powercalc/sensors/group.py @@ -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( @@ -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( @@ -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: diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 7c6e35a36..3e39eac5f 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -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"], @@ -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(