Skip to content

Commit

Permalink
fix: allow real power sensor to be selectable as group member
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Apr 5, 2024
1 parent f2ad643 commit 3d43aa1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion custom_components/powercalc/config_flow.py
Expand Up @@ -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
]
Expand Down
10 changes: 5 additions & 5 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -21,6 +21,7 @@
CONF_DEVICE,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_ENTITY_ID,
CONF_NAME,
CONF_UNIQUE_ID,
EVENT_HOMEASSISTANT_STOP,
Expand Down Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions tests/config_flow/test_group.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 3d43aa1

Please sign in to comment.