Skip to content

Commit

Permalink
When using include also include individual YAML sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 8, 2023
1 parent 52dbf5d commit 938f273
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion custom_components/powercalc/__init__.py
Expand Up @@ -39,6 +39,7 @@
CONF_FIXED,
CONF_FORCE_UPDATE_FREQUENCY,
CONF_IGNORE_UNAVAILABLE_STATE,
CONF_INCLUDE,
CONF_POWER,
CONF_POWER_SENSOR_CATEGORY,
CONF_POWER_SENSOR_FRIENDLY_NAMING,
Expand Down Expand Up @@ -221,7 +222,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await discovery_manager.start_discovery()

sensors: list = domain_config.get(CONF_SENSORS, [])
for sensor_config in sensors:
sorted_sensors = sorted(sensors, key=lambda x: (CONF_INCLUDE in x, x.get(CONF_INCLUDE, False)))
for sensor_config in sorted_sensors:
sensor_config.update({DISCOVERY_TYPE: PowercalcDiscoveryType.USER_YAML})
hass.async_create_task(
async_load_platform(
Expand Down
50 changes: 50 additions & 0 deletions tests/group_include/test_include.py
Expand Up @@ -343,6 +343,56 @@ async def test_include_filter_domain(
assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.test_light_power"}


async def test_include_yaml_configured_entity(hass: HomeAssistant, entity_reg: EntityRegistry,
area_reg: AreaRegistry) -> None:
"""Test that include also includes entities that the user configured with YAML"""

light_a = MockLight("light_a")
light_b = MockLight("light_b")
light_c = create_discoverable_light("light_c")
light_d = MockLight("light_d")
await create_mock_light_entity(
hass,
[light_a, light_b, light_c, light_d],
)

area = area_reg.async_get_or_create("My area")
entity_reg.async_update_entity(light_a.entity_id, area_id=area.id)
entity_reg.async_update_entity(light_b.entity_id, area_id=area.id)
entity_reg.async_update_entity(light_c.entity_id, area_id=area.id)

_create_powercalc_config_entry(hass, light_a.entity_id)

await run_powercalc_setup(
hass,
[
{
CONF_CREATE_GROUP: "Test include",
CONF_INCLUDE: {
CONF_AREA: "my_area",
},
},
{
CONF_ENTITY_ID: light_b.entity_id,
CONF_FIXED: {
CONF_POWER: 50,
},
},
{
CONF_ENTITY_ID: light_c.entity_id,
},
],
)

group_state = hass.states.get("sensor.test_include_power")
assert group_state
assert group_state.attributes.get(ATTR_ENTITIES) == {
"sensor.light_a_power",
"sensor.light_b_power",
"sensor.light_c_power",
}


def _create_powercalc_config_entry(hass: HomeAssistant, source_entity_id: str) -> MockConfigEntry:
unique_id = str(uuid.uuid4())
entry = MockConfigEntry(
Expand Down

0 comments on commit 938f273

Please sign in to comment.