Enforce strict target format in exclude blocks (#189275)

This commit is contained in:
Artur Pragacz
2026-10-28 16:15:48 +01:00
committed by GitHub
parent 9ba6491dcd
commit 437b58b1e8
5 changed files with 52 additions and 56 deletions
@@ -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,
)
+5 -22
View File
@@ -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),
),
+3 -17
View File
@@ -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 = {}
+14 -1
View File
@@ -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)
+29 -15
View File
@@ -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,
)