Skip to content

Fix false positive: notify.* service names in third-party data payloads - #1339

Closed
mzspicoli wants to merge 2 commits into
frenck:mainfrom
mzspicoli:fix/automation-unknown-entity-notify-data-false-positive
Closed

mzspicoli wants to merge 2 commits into
frenck:mainfrom
mzspicoli:fix/automation-unknown-entity-notify-data-false-positive

Conversation

@mzspicoli

Copy link
Copy Markdown

Summary

Fixes #1338.

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 (_should_skip_service_data_value) only skips this for the notify domain's own data.target field, when the calling service itself starts with notify..

This misses a common pattern: a third-party "fan-out notifier" 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 legacy notify service identifiers, not entity references, but get wrongly flagged as unknown entities since neither the calling service nor the key matches the existing guard.

A genuine reference to a notify domain entity is always expressed via target.entity_id, which is handled separately by _extract_entities_from_target and is completely unaffected by this change.

Change

  • Added _filter_out_notify_service_references(): strips any notify.* match found via the generic data-payload scan, regardless of key name or calling service.
  • Applied it in _extract_entities_from_service_data() right after the existing extract_entities_from_value() call.

This is intentionally narrow/surgical - it only changes behavior for notify.*-shaped values discovered inside data, and leaves target.entity_id/target.device_id extraction (and all other entity detection) untouched.

Tests

Added two tests to tests/ectoplasms/automation/repairs/test_unknown_entity_references.py:

  • test_third_party_service_data_notify_list_is_not_an_entity_reference - reproduces the real-world false positive (a notifier_hub.send-style action with data.notify: [...]).
  • test_notify_target_entity_id_is_still_detected - regression guard confirming a genuine target.entity_id: "notify.xyz" reference is still correctly detected/flagged when unknown, so the fix doesn't overreach.

All existing tests in the file (48 total before, 50 after) continue to pass.

Verification

uv run pytest tests/ectoplasms/automation/repairs/test_unknown_entity_references.py -q
# 50 passed

uv run pytest tests/ectoplasms/automation -q --no-cov
# 52 passed

uv run ruff check custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
# All checks passed!

uv run pylint custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py
# Your code has been rated at 10.00/10

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.
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a6b6640c-a073-4995-accf-b4e321f69111

📥 Commits

Reviewing files that changed from the base of the PR and between f0c5456 and 02e90d1.

📒 Files selected for processing (2)
  • custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py
  • tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py

📝 Walkthrough

Walkthrough

Adds filtering so notify.* service identifiers found in action data payloads are not treated as entity references, while notify.* entity references in target.entity_id remain detectable. Adds regression tests for both the false positive and the preserved detection path.

Changes

Notify Service Reference Filtering

Layer / File(s) Summary
Filter notify.* identifiers from data payload extraction
custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py
Adds _filter_out_notify_service_references() and applies it while extracting entities from action.data string and dictionary values.
Tests for notify filtering and continued detection
tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
Adds tests covering data.notify lists, bare data notify strings, and target.entity_id notify entities.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • frenck/spook#977: Both PRs modify the same _extract_entities_from_service_data extraction flow in unknown_entity_references.py.
  • frenck/spook#1275: Both PRs extend the same test_unknown_entity_references.py test suite with notify-related coverage.
  • frenck/spook#1283: Both PRs prevent notify.*-shaped values in action data from being misidentified as entity references.

Suggested labels: bugfix

Suggested reviewers: frenck

Poem

A rabbit hopped through automation's maze,
Chasing false alarms in notify's haze,
"That's a service, not a friend!" it cried,
So notify.* was gently pushed aside,
Now entities true still get their due,
Twitching whiskers, tests passing too. 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: filtering false-positive notify.* service names in third-party data payloads.
Description check ✅ Passed The description clearly matches the change and explains the notify.* false-positive fix and regression tests.
Linked Issues check ✅ Passed The changes address #1338 by filtering notify.* strings from generic data scans while preserving genuine target.entity_id detection.
Out of Scope Changes check ✅ Passed The PR stays focused on the notify.* false-positive fix and adds only targeted regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py (1)

262-274: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Notify filter not applied when data itself is a bare string.

The new filter is only wired into the dict-item loop (lines 270-274). When config["data"] is itself a plain string matching ENTITY_ID_PATTERN (line 264-266, via extract_entities_from_value), a value like data: "notify.old_tablet" would still be flagged as an entity, since _filter_out_notify_service_references is never applied to that branch's result. This is the same false-positive class the PR is fixing, just for the "data value as a raw string" shape rather than "data value as a dict of key→value(s)".

🐛 Proposed fix to also filter the string-branch result
         if isinstance(data_value, str):
             # data field is a template string itself
-            entities.update(await extract_entities_from_value(hass, data_value))
+            entities.update(
+                _filter_out_notify_service_references(
+                    await extract_entities_from_value(hass, data_value)
+                )
+            )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py`
around lines 262 - 274, The notify-service filter is only applied in the
dict-handling branch of the data processing logic, so bare string values in
config["data"] can still produce false positives. Update the data handling in
unknown_entity_references.py so the string path that calls
extract_entities_from_value also passes its result through
_filter_out_notify_service_references, matching the behavior already used for
the dict-item loop. Keep the fix localized to the existing data branch in the
same helper that processes config["data"].
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py`:
- Around line 262-274: The notify-service filter is only applied in the
dict-handling branch of the data processing logic, so bare string values in
config["data"] can still produce false positives. Update the data handling in
unknown_entity_references.py so the string path that calls
extract_entities_from_value also passes its result through
_filter_out_notify_service_references, matching the behavior already used for
the dict-item loop. Keep the fix localized to the existing data branch in the
same helper that processes config["data"].

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9a2d958e-c24d-49b0-b1c1-f8c3f930a934

📥 Commits

Reviewing files that changed from the base of the PR and between dd5f7f5 and f0c5456.

📒 Files selected for processing (2)
  • custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py
  • tests/ectoplasms/automation/repairs/test_unknown_entity_references.py

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 2, 2026
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.
@sonarqubecloud

sonarqubecloud Bot commented Jul 2, 2026

Copy link
Copy Markdown

@erkr

erkr commented Aug 10, 2026

Copy link
Copy Markdown

Any idea when this fix enters a release 🙏

@frenck

frenck commented Aug 21, 2026

Copy link
Copy Markdown
Owner

superseed by #1440

@frenck frenck closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automation unknown_entity_references false positive: notify.* service names inside third-party data payloads

3 participants