From f0c54565258780ed3ee674838b13309e38e05cff Mon Sep 17 00:00:00 2001 From: mzspicoli Date: Thu, 2 Jul 2026 18:40:19 +0200 Subject: [PATCH 1/2] Fix false positive: notify.* service names in third-party data payloads The automation unknown_entity_references repair scans every value inside a service call's `data` payload and flags any `domain.object_id`-shaped string as a referenced entity. The only existing guard skips this for the notify domain's own `data.target` field. This misses a common case: a third-party "fan-out" action (e.g. notifier_hub.send) that accepts a list of legacy notify.* service names under an arbitrary data key (e.g. data.notify). Those strings are service identifiers, not entity references, so they get wrongly reported as unknown entities. Genuine references to notify domain entities are always expressed via target.entity_id, which is handled separately and unaffected by this change. Filter out notify.* matches found via the generic data-payload scan, regardless of the calling service or key name. --- .../repairs/unknown_entity_references.py | 16 +++++++- .../repairs/test_unknown_entity_references.py | 39 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py index 9c4e61c71c..cd90c3a5d9 100644 --- a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py @@ -241,6 +241,19 @@ def _should_skip_service_data_value( return service is not None and service.startswith("notify.") and key == "target" +def _filter_out_notify_service_references(entities: set[str]) -> set[str]: + """Drop ``notify.*`` matches found in a ``data`` payload. + + A genuine reference to a ``notify`` domain entity is always expressed via + ``target.entity_id`` (handled separately by ``_extract_entities_from_target``). + A ``notify.*``-shaped string found as a free-form value inside ``data`` is, + in practice, a legacy notify *service* identifier forwarded by a + third-party integration (e.g. a "send to multiple notifiers" action), not + an entity reference - so it should never be flagged as unknown. + """ + return {entity for entity in entities if not entity.startswith("notify.")} + + async def _extract_entities_from_service_data( hass: HomeAssistant, config: dict[str, Any] ) -> set[str]: @@ -257,7 +270,8 @@ async def _extract_entities_from_service_data( for key, value in data_value.items(): if _should_skip_service_data_value(service, key): continue - entities.update(await extract_entities_from_value(hass, value)) + found = await extract_entities_from_value(hass, value) + entities.update(_filter_out_notify_service_references(found)) return entities diff --git a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py index 6544554ccc..a79289c485 100644 --- a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py @@ -378,6 +378,45 @@ async def test_notify_service_data_target_is_not_an_entity_reference( assert await extract_entities_from_action_config(hass, config) == set() +async def test_third_party_service_data_notify_list_is_not_an_entity_reference( + hass: HomeAssistant, +) -> None: + """Legacy notify service names forwarded by a third-party action are not entities. + + Reproduces a real-world false positive: a custom "fan-out notifier" action + (e.g. ``notifier_hub.send``) accepts a list of legacy ``notify.*`` service + names under an arbitrary ``data`` key. These are service identifiers, not + entity references, and must not be flagged as unknown entities even though + the calling service is not itself in the ``notify`` domain. + """ + config = { + "action": "notifier_hub.send", + "data": { + "title": "Hello", + "message": "World", + "notify": ["notify.mobile_app_phone", "notify.old_tablet"], + }, + } + assert await extract_entities_from_action_config(hass, config) == set() + + +async def test_notify_target_entity_id_is_still_detected(hass: HomeAssistant) -> None: + """A genuine ``target.entity_id`` reference to a notify entity is still found. + + Guards against overreaching the ``data``-payload notify filter: entities in + the ``notify`` domain referenced through ``target.entity_id`` (the only + legitimate way to target a notify *entity*, as opposed to a legacy notify + *service*) must still be reported when unknown. + """ + config = { + "action": "some_domain.some_service", + "target": {"entity_id": "notify.mobile_app_phone"}, + } + assert await extract_entities_from_action_config(hass, config) == { + "notify.mobile_app_phone" + } + + async def test_non_notify_action_data_target_remains_entity_reference( hass: HomeAssistant, ) -> None: From 02e90d17e2787c31ab7406d47e2e74c5c2a8b29d Mon Sep 17 00:00:00 2001 From: mzspicoli Date: Thu, 2 Jul 2026 18:52:25 +0200 Subject: [PATCH 2/2] Apply notify.* filter to the bare-string data branch too The notify-service filter introduced to fix the dict-shaped data branch was missing from the string-shaped data branch, so a literal notify.* service name assigned directly to data (as opposed to nested under a dict key) was still incorrectly flagged as an unknown entity. Found by CodeRabbit's automated review on the PR. --- .../repairs/unknown_entity_references.py | 3 ++- .../repairs/test_unknown_entity_references.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py index cd90c3a5d9..5661eb4e59 100644 --- a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py @@ -263,7 +263,8 @@ async def _extract_entities_from_service_data( data_value = config["data"] if isinstance(data_value, str): # data field is a template string itself - entities.update(await extract_entities_from_value(hass, data_value)) + found = await extract_entities_from_value(hass, data_value) + entities.update(_filter_out_notify_service_references(found)) elif isinstance(data_value, dict): service = _get_action_service(config) # data field is a dictionary, process all its values diff --git a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py index a79289c485..302b3e98b3 100644 --- a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py @@ -439,6 +439,22 @@ async def test_action_data_as_template_string(hass: HomeAssistant) -> None: } +async def test_action_data_bare_notify_string_is_not_an_entity_reference( + hass: HomeAssistant, +) -> None: + """A literal ``notify.*`` string assigned directly to ``data`` is not an entity. + + Mirrors ``test_third_party_service_data_notify_list_is_not_an_entity_reference`` + but for the bare-string ``data`` branch, which forwards a legacy notify + *service* identifier rather than an entity reference. + """ + config = { + "action": "some_custom_domain.some_service", + "data": "notify.old_tablet", + } + assert await extract_entities_from_action_config(hass, config) == set() + + async def test_action_if_then_else_nested(hass: HomeAssistant) -> None: """``if``/``then``/``else`` nested actions are walked.""" config = {