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..5661eb4e59 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]: @@ -250,14 +263,16 @@ 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 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..302b3e98b3 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: @@ -400,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 = {