diff --git a/custom_components/powercalc/config_flow.py b/custom_components/powercalc/config_flow.py index 4ebc4ecac..273684d93 100644 --- a/custom_components/powercalc/config_flow.py +++ b/custom_components/powercalc/config_flow.py @@ -1065,7 +1065,7 @@ def _create_group_options_schema( member_sensors = [ selector.SelectOptionDict(value=config_entry.entry_id, label=config_entry.title) for config_entry in hass.config_entries.async_entries(DOMAIN) - if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.VIRTUAL_POWER + if config_entry.data.get(CONF_SENSOR_TYPE) in [SensorType.VIRTUAL_POWER, SensorType.REAL_POWER] and config_entry.unique_id is not None and config_entry.title is not None ] diff --git a/custom_components/powercalc/sensors/group.py b/custom_components/powercalc/sensors/group.py index 190908350..3e95f70fb 100644 --- a/custom_components/powercalc/sensors/group.py +++ b/custom_components/powercalc/sensors/group.py @@ -21,6 +21,7 @@ CONF_DEVICE, CONF_DOMAIN, CONF_ENTITIES, + CONF_ENTITY_ID, CONF_NAME, CONF_UNIQUE_ID, EVENT_HOMEASSISTANT_STOP, @@ -325,11 +326,10 @@ async def resolve_entity_ids_recursively( member_entry = hass.config_entries.async_get_entry(member_entry_id) if member_entry is None: continue - key = ( - ENTRY_DATA_POWER_ENTITY - if device_class == SensorDeviceClass.POWER - else ENTRY_DATA_ENERGY_ENTITY - ) + if member_entry.data.get(CONF_SENSOR_TYPE) == SensorType.REAL_POWER: + key = CONF_ENTITY_ID if device_class == SensorDeviceClass.POWER else ENTRY_DATA_ENERGY_ENTITY + else: + key = ENTRY_DATA_POWER_ENTITY if device_class == SensorDeviceClass.POWER else ENTRY_DATA_ENERGY_ENTITY if key not in member_entry.data: # pragma: no cover continue diff --git a/tests/config_flow/test_group.py b/tests/config_flow/test_group.py index 0d45869a4..8022407ce 100644 --- a/tests/config_flow/test_group.py +++ b/tests/config_flow/test_group.py @@ -17,6 +17,7 @@ from custom_components.powercalc import SensorType, async_migrate_entry from custom_components.powercalc.const import ( + ATTR_ENTITIES, CONF_AREA, CONF_CREATE_ENERGY_SENSOR, CONF_CREATE_ENERGY_SENSORS, @@ -345,6 +346,61 @@ async def test_can_select_existing_powercalc_entry_as_group_member( } +async def test_real_power_entry_selectable_as_group_member( + hass: HomeAssistant, +) -> None: + """ + Test if we can select both virtual power sensor and real power sensor into the group, + and the total power is calculated correctly + """ + + config_entry_1 = await create_mocked_virtual_power_sensor_entry( + hass, + "VirtualPower1", + "abcdef", + ) + config_entry_2 = MockConfigEntry( + domain=DOMAIN, + unique_id="abcdefg", + data={ + CONF_SENSOR_TYPE: SensorType.REAL_POWER, + CONF_UNIQUE_ID: "abcdefg", + CONF_ENTITY_ID: "sensor.real_power", + }, + title="RealPower1", + ) + config_entry_2.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry_2.entry_id) + await hass.async_block_till_done() + + result = await select_sensor_type(hass, SensorType.GROUP) + assert result["type"] == data_entry_flow.FlowResultType.FORM + data_schema: vol.Schema = result["data_schema"] + select: SelectSelector = data_schema.schema[CONF_GROUP_MEMBER_SENSORS] + options = select.config["options"] + assert len(options) == 2 + assert {"value": config_entry_1.entry_id, "label": "VirtualPower1"} in options + assert {"value": config_entry_2.entry_id, "label": "RealPower1"} in options + + user_input = { + CONF_NAME: "My group sensor", + CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID, + CONF_GROUP_MEMBER_SENSORS: [config_entry_1.entry_id, config_entry_2.entry_id], + } + await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input, + ) + + hass.states.async_set("sensor.real_power", "25.00") + await hass.async_block_till_done() + + group_state = hass.states.get("sensor.my_group_sensor_power") + assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.virtualpower1_power", "sensor.real_power"} + assert group_state + assert group_state.state == "75.00" + + async def test_group_error_mandatory(hass: HomeAssistant) -> None: result = await select_sensor_type(hass, SensorType.GROUP) user_input = {CONF_NAME: "My group sensor", CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID}