From 938f27330cc62a2491c6c198b3f32c575045a759 Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 8 Jul 2023 08:27:36 +0200 Subject: [PATCH] When using include also include individual YAML sensors --- custom_components/powercalc/__init__.py | 4 +- tests/group_include/test_include.py | 50 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/custom_components/powercalc/__init__.py b/custom_components/powercalc/__init__.py index f5788091a..9771d3469 100644 --- a/custom_components/powercalc/__init__.py +++ b/custom_components/powercalc/__init__.py @@ -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, @@ -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( diff --git a/tests/group_include/test_include.py b/tests/group_include/test_include.py index 0a8bb4be5..1ddcea4ad 100644 --- a/tests/group_include/test_include.py +++ b/tests/group_include/test_include.py @@ -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(