Repository navigation
Ignore concatenated template entity fragments - #1284
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughRefactors template entity extraction to ignore regex matches that are part of concatenated template string literals, normalizes regex capture groups into canonical ChangesTemplated Entity Reference Detection
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1284 +/- ##
==========================================
+ Coverage 62.72% 62.91% +0.18%
==========================================
Files 124 124
Lines 3147 3163 +16
Branches 403 406 +3
==========================================
+ Hits 1974 1990 +16
Misses 1126 1126
Partials 47 47 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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/entity_filtering.py (1)
436-446:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winFix concatenation heuristic dropping unquoted
states.<domain>.<entity>references.
extract_entities_from_template_regexskips everyENTITY_ID_TEMPLATE_PATTERNSmatch whenever_is_concatenated_template_matchdetects the match is adjacent to~. This suppresses the only matcher for unquotedstates.<domain>.<entity>.state|attributes(pattern#2), e.g.{{ 'Label: ' ~ states.sensor.temp.state }}results insensor.tempnot being added.🐛 Proposed fix
for pattern in ENTITY_ID_TEMPLATE_PATTERNS: for match in re.finditer(pattern, template_str, re.IGNORECASE): - if _is_concatenated_template_match(template_str, match): + # The unquoted states.<domain>.<entity> access is always a + # complete reference; only quoted literals can be fragments. + if ( + len(match.groups()) != _STATES_DOMAIN_ENTITY_GROUPS + and _is_concatenated_template_match(template_str, match) + ): continue entity_id = _entity_id_from_template_match(match)🤖 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/entity_filtering.py` around lines 436 - 446, The loop in extract_entities_from_template_regex currently skips any match when _is_concatenated_template_match returns True, which incorrectly suppresses unquoted states.<domain>.<entity> references; change the logic so that before skipping a match you special-case the "states" accessor: if match.group(0) (the raw matched text from template_str) contains "states." or ends with ".state" / ".attributes" (i.e. the states.<domain>.<entity> pattern), do not continue — proceed to call _entity_id_from_template_match and the subsequent split_comma_separated_entity_ids/valid_entity_id flow; keep the existing _is_concatenated_template_match behavior for other patterns.
🤖 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/entity_filtering.py`:
- Around line 436-446: The loop in extract_entities_from_template_regex
currently skips any match when _is_concatenated_template_match returns True,
which incorrectly suppresses unquoted states.<domain>.<entity> references;
change the logic so that before skipping a match you special-case the "states"
accessor: if match.group(0) (the raw matched text from template_str) contains
"states." or ends with ".state" / ".attributes" (i.e. the
states.<domain>.<entity> pattern), do not continue — proceed to call
_entity_id_from_template_match and the subsequent
split_comma_separated_entity_ids/valid_entity_id flow; keep the existing
_is_concatenated_template_match behavior for other patterns.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: bd2d498d-cdfe-449d-afd5-34b070c242f5
📒 Files selected for processing (2)
custom_components/spook/entity_filtering.pytests/ectoplasms/automation/repairs/test_unknown_entity_references.py
There was a problem hiding this comment.
Pull request overview
This PR updates Spook’s template entity-reference extraction to avoid false-positive “unknown entity” reports when an entity-looking literal is only a fragment of a Jinja string concatenation (e.g., building entity_id dynamically with ~), and adds regression tests for those scenarios.
Changes:
- Add test coverage ensuring concatenated entity-id fragments (including helper calls like
is_state(...)) do not produce static entity references. - Refactor template regex extraction to use
re.finditer()and helper functions to reconstruct captured entity IDs. - Introduce concatenation-adjacency filtering to skip entity-looking literals that are part of
~concatenations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
custom_components/spook/entity_filtering.py |
Adds helper logic to skip template regex matches that appear to be concatenated entity-id fragments. |
tests/ectoplasms/automation/repairs/test_unknown_entity_references.py |
Adds regression tests covering concatenated entity-id fragments in templates and action targets. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|



Description
Adjusts the template entity extraction regex to ignore entity-looking literals when they are part of a Jinja string concatenation.
This prevents partial literals like
switch.camerain{{ 'switch.camera' ~ cam_id ~ '_movies' }}from being reported as an unknown entity, while keeping static template references such as{{ states('light.kitchen') }}covered.Motivation and Context
Fixes #1250.
Spook currently treats the static part of a templated entity ID as a complete entity reference. That creates a false positive repair for valid automations using templated
entity_idvalues.How has this been tested?
uv run ruff check custom_components/spook/entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.pyuv run ruff format --check custom_components/spook/entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.pyuv run pylint custom_components/spook/entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.pyuv run pytest -qScreenshots (if appropriate):
Not applicable.
Types of changes
Checklist