From 437b58b1e879ec893e65da6faa8f14582ea5db70 Mon Sep 17 00:00:00 2001 From: Artur Pragacz <49985303+arturpragacz@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:15:48 +0100 Subject: [PATCH] Enforce strict target format in exclude blocks (#189275) --- .../components/homeassistant/services.py | 2 +- homeassistant/helpers/config_validation.py | 27 +++--------- homeassistant/helpers/service.py | 20 ++------- tests/helpers/test_config_validation.py | 15 ++++++- tests/helpers/test_service.py | 44 ++++++++++++------- 5 files changed, 52 insertions(+), 56 deletions(-) diff --git a/homeassistant/components/homeassistant/services.py b/homeassistant/components/homeassistant/services.py index 0823ac944afb..c43d5af22f74 100644 --- a/homeassistant/components/homeassistant/services.py +++ b/homeassistant/components/homeassistant/services.py @@ -317,7 +317,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service_schema = probatio.Schema( { ATTR_ENTITY_ID: cv.entity_ids, - probatio.Optional(CONF_EXCLUDE): cv.ENTITY_SERVICE_EXCLUDE_SCHEMA, + probatio.Optional(CONF_EXCLUDE): cv.TARGET_EXCLUDE_SCHEMA, }, extra=probatio.ALLOW_EXTRA, ) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 8d5cc48992de..a65318bd7d13 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1303,9 +1303,11 @@ TARGET_ID_FIELDS: VolDictType = { probatio.Optional(ATTR_LABEL_ID): probatio.All(ensure_list, [str]), } +TARGET_EXCLUDE_SCHEMA = probatio.Schema(TARGET_ID_FIELDS) + TARGET_FIELDS: VolDictType = { **TARGET_ID_FIELDS, - probatio.Optional(CONF_EXCLUDE): probatio.Schema(TARGET_ID_FIELDS), + probatio.Optional(CONF_EXCLUDE): TARGET_EXCLUDE_SCHEMA, } @@ -1319,16 +1321,6 @@ def exclude_requires_target(value: dict[str, Any]) -> dict[str, Any]: return value -ENTITY_SERVICE_EXCLUDE_SCHEMA = probatio.Schema( - { - probatio.Optional(ATTR_ENTITY_ID): entity_ids, - probatio.Optional(ATTR_DEVICE_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_AREA_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_FLOOR_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_LABEL_ID): probatio.All(ensure_list, [str]), - } -) - ENTITY_SERVICE_FIELDS: VolDictType = { probatio.Optional(ATTR_ENTITY_ID): comp_entity_ids, probatio.Optional(ATTR_DEVICE_ID): probatio.Any( @@ -1347,7 +1339,7 @@ ENTITY_SERVICE_FIELDS: VolDictType = { ENTITY_MATCH_NONE, probatio.All(ensure_list, [str]), ), - probatio.Optional(CONF_EXCLUDE): ENTITY_SERVICE_EXCLUDE_SCHEMA, + probatio.Optional(CONF_EXCLUDE): TARGET_EXCLUDE_SCHEMA, } TARGET_SERVICE_FIELDS: VolDictType = { @@ -1355,15 +1347,6 @@ TARGET_SERVICE_FIELDS: VolDictType = { # by entity registry ID. **ENTITY_SERVICE_FIELDS, probatio.Optional(ATTR_ENTITY_ID): comp_entity_ids_or_uuids, - probatio.Optional(CONF_EXCLUDE): probatio.Schema( - { - probatio.Optional(ATTR_ENTITY_ID): entity_ids_or_uuids, - probatio.Optional(ATTR_DEVICE_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_AREA_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_FLOOR_ID): probatio.All(ensure_list, [str]), - probatio.Optional(ATTR_LABEL_ID): probatio.All(ensure_list, [str]), - } - ), } _TARGET_SERVICE_FIELDS_TEMPLATED: VolDictType = { @@ -1402,7 +1385,7 @@ _TARGET_SERVICE_FIELDS_TEMPLATED: VolDictType = { probatio.Optional(CONF_EXCLUDE): probatio.Schema( { probatio.Optional(ATTR_ENTITY_ID): probatio.Any( - entity_ids_or_uuids, + TARGET_ID_FIELDS[ATTR_ENTITY_ID], dynamic_template, probatio.All(list, template_complex), ), diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 0117690279e0..aefde3eb70a7 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -319,27 +319,13 @@ def async_prepare_call_from_config( f"Template rendered invalid entity IDs: {target[CONF_ENTITY_ID]}" ) from ex - if (exclude := target.get(CONF_EXCLUDE)) and CONF_ENTITY_ID in exclude: + if exclude := target.get(CONF_EXCLUDE): try: - registry = entity_registry.async_get(hass) - excluded_entity_ids = [ - entity_id - for entity_id_or_uuid in cv.entity_ids_or_uuids( - exclude[CONF_ENTITY_ID] - ) - if ( - entity_id := entity_registry.async_resolve_entity_id( - registry, entity_id_or_uuid - ) - ) - is not None - ] + target[CONF_EXCLUDE] = cv.TARGET_EXCLUDE_SCHEMA(exclude) except probatio.Invalid as ex: raise HomeAssistantError( - "Template rendered invalid excluded entity IDs: " - f"{exclude[CONF_ENTITY_ID]}" + f"Template rendered invalid exclude block: {exclude}" ) from ex - target[CONF_EXCLUDE] = {**exclude, CONF_ENTITY_ID: excluded_entity_ids} service_data = {} diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index 5fa500812636..ca5a49ab47f4 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -698,6 +698,13 @@ def test_entity_service_schema_exclude() -> None: {"area_id": "kitchen", "exclude": {"entity_id": "none"}}, {"area_id": "kitchen", "exclude": {"unknown": "pantry"}}, {"area_id": "kitchen", "exclude": "light.kitchen"}, + # Unlike the target, the exclude block only accepts strict entity IDs + {"area_id": "kitchen", "exclude": {"entity_id": "light.kitchen, light.pantry"}}, + {"area_id": "kitchen", "exclude": {"entity_id": "Light.Kitchen"}}, + { + "area_id": "kitchen", + "exclude": {"entity_id": "0123456789abcdef0123456789abcdef"}, + }, ) for value in invalid: with pytest.raises(probatio.MultipleInvalid): @@ -707,7 +714,7 @@ def test_entity_service_schema_exclude() -> None: { "entity_id": "all", "exclude": { - "entity_id": "light.kitchen, light.pantry", + "entity_id": ["light.kitchen", "light.pantry"], "device_id": "a_device", "area_id": "kitchen", "floor_id": "ground_floor", @@ -744,6 +751,12 @@ def test_target_fields_exclude(schema: probatio.Schema) -> None: for value in ( {"area_id": "kitchen", "exclude": {"exclude": {"area_id": "pantry"}}}, {"area_id": "kitchen", "exclude": {"entity_id": "all"}}, + {"area_id": "kitchen", "exclude": {"entity_id": "light.kitchen, light.pantry"}}, + {"area_id": "kitchen", "exclude": {"entity_id": "Light.Kitchen"}}, + { + "area_id": "kitchen", + "exclude": {"entity_id": "0123456789abcdef0123456789abcdef"}, + }, ): with pytest.raises(probatio.MultipleInvalid): schema(value) diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index f8f08a3e178d..1270e0d3fb71 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -1963,12 +1963,9 @@ async def test_entity_service_call_exclude_group_entity( } == expected_entity_ids -async def test_service_call_exclude( - hass: HomeAssistant, entity_registry: er.EntityRegistry -) -> None: +async def test_service_call_exclude(hass: HomeAssistant) -> None: """Test the exclude block of a target is passed on to the service call.""" calls = async_mock_service(hass, "test_domain", "test_service") - entry = entity_registry.async_get_or_create("light", "test", "unique") await service.async_call_from_config( hass, @@ -1978,7 +1975,7 @@ async def test_service_call_exclude( "area_id": "kitchen", "exclude": { "area_id": "{{ 'pantry' }}", - "entity_id": [entry.id, "light.static"], + "entity_id": ["{{ 'light.rendered' }}", "light.static"], }, }, }, @@ -1988,24 +1985,41 @@ async def test_service_call_exclude( assert dict(calls[0].data) == { "area_id": ["kitchen"], "exclude": { - "area_id": "pantry", - "entity_id": [entry.entity_id, "light.static"], + "area_id": ["pantry"], + "entity_id": ["light.rendered", "light.static"], }, } + +@pytest.mark.parametrize( + "target", + [ + pytest.param( + {"area_id": "kitchen", "exclude": {"entity_id": "{{ 'invalid' }}"}}, + id="invalid_entity_id", + ), + pytest.param( + {"area_id": "kitchen", "exclude": {"entity_id": "{{ 'Light.Kitchen' }}"}}, + id="uppercase_entity_id", + ), + pytest.param( + "{{ {'area_id': 'kitchen', 'exclude': {'exclude': {}}} }}", + id="nested", + ), + ], +) +async def test_service_call_exclude_invalid( + hass: HomeAssistant, target: dict[str, Any] | str +) -> None: + """Test a rendered exclude block is validated like a static one.""" with pytest.raises( exceptions.HomeAssistantError, - match="Template rendered invalid excluded entity IDs", + match="Template rendered invalid exclude block", ): service.async_prepare_call_from_config( hass, - { - "action": "test_domain.test_service", - "target": { - "area_id": "kitchen", - "exclude": {"entity_id": "{{ 'invalid' }}"}, - }, - }, + {"action": "test_domain.test_service", "target": target}, + validate_config=True, )