Skip to content

Ignore entity references in Jinja comments - #1322

Merged
frenck merged 3 commits into
mainfrom
fix/jinja-comment-entities
Jun 23, 2026
Merged

frenck merged 3 commits into
mainfrom
fix/jinja-comment-entities

Conversation

@frenckatron

@frenckatron frenckatron commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Spook's regex fallback now strips Jinja comments before scanning templates for entity IDs. Entity-like strings in commented-out template code no longer count as active references.

Motivation and Context

This addresses the Jinja comment false positive reported in #1081. The commented sensor.toothbrush_change_head example was still seen by the regex fallback, even though Jinja would ignore it at render time.

How has this been tested?

  • uv run pytest tests/ectoplasms/automation/repairs/test_unknown_entity_references.py::test_value_template_ignores_entity_id_in_jinja_comment -q
  • uv run pytest tests/test_entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.py tests/ectoplasms/script/repairs/test_unknown_entity_references.py -q
  • uv run ruff check custom_components/spook/entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
  • uv run pylint custom_components/spook/entity_filtering.py tests/ectoplasms/automation/repairs/test_unknown_entity_references.py

Screenshots (if appropriate):

Not applicable.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@frenckatron frenckatron added the bugfix Inconsistencies or issues which will cause a problem for users or implementors. label Jun 23, 2026
@coderabbitai

coderabbitai Bot commented Jun 23, 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: 7b0ab542-e6e5-438d-9c3c-ffd03d0dd1b2

📥 Commits

Reviewing files that changed from the base of the PR and between e5e4b8d and f591615.

📒 Files selected for processing (1)
  • custom_components/spook/entity_filtering.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • custom_components/spook/entity_filtering.py

📝 Walkthrough

Walkthrough

A new _strip_jinja_comments() helper strips {# ... #} comment blocks from template strings. extract_entities_from_template_regex() now applies this helper first and operates on the comment-free string for both regex scanning and match-context predicate checks. A new test confirms commented-out entity-like strings are not extracted.

Changes

Jinja Comment Stripping in Entity Extraction

Layer / File(s) Summary
Comment stripping pattern and helper
custom_components/spook/entity_filtering.py
Adds JINJA_COMMENT_PATTERN regex constant (line 125) and _strip_jinja_comments() function (lines 411–417) to remove {# ... #} blocks from template strings via regex substitution, with early-exit when no comment marker exists.
Entity extraction with comment stripping
custom_components/spook/entity_filtering.py
Updates extract_entities_from_template_regex() (lines 515–524) to compute a comment-stripped template and apply both the entity-id regex patterns and the _is_* match-context predicates against the comment-free version instead of the original.
Test for Jinja comment exclusion
tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
Adds test_value_template_ignores_entity_id_in_jinja_comment (lines 159–171) to assert that extract_entities_from_value returns an empty set when entity-like strings appear only inside Jinja comment blocks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • frenck/spook#1284: Also modifies extract_entities_from_template_regex to prevent false-positive entity extraction and extends test_unknown_entity_references.py test scenarios.
  • frenck/spook#1306: Similarly prevents false-positive unknown-entity detection in entity_filtering.py by excluding entity-like literals in specific Jinja argument positions.
  • frenck/spook#1307: Also modifies template entity-extraction flow in entity_filtering.py to skip non-entity Jinja constructs and updates the same unknown-entity-references tests.

Poem

🐇 A comment said "light.bedroom" clear,
But Jinja hid it — no alarm, no fear!
I stripped the {# #} away with care,
So only real entities live there.
Hop hop hooray, no false repairs! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: ignoring entity references in Jinja comments, which directly addresses the core objective of the PR.
Description check ✅ Passed The description provides relevant context about the fix, motivation from issue #1081, and testing details that align with the changeset modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/jinja-comment-entities

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.

Copilot AI 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.

Pull request overview

This pull request updates Spook’s template entity reference extraction so that entity-like strings inside Jinja {# ... #} comments are ignored by the regex-based fallback, preventing false positives in unknown-entity repairs.

Changes:

  • Strip Jinja comment blocks from template strings before running entity-ID regex scans.
  • Add a regression test ensuring entity-like strings inside Jinja comments don’t produce entity references.

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 Jinja comment stripping and applies it to the regex entity extraction path to avoid false positives.
tests/ectoplasms/automation/repairs/test_unknown_entity_references.py Adds a test case covering entity-like strings inside Jinja comments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@frenck
frenck merged commit 23030ac into main Jun 23, 2026
19 checks passed
@frenck
frenck deleted the fix/jinja-comment-entities branch June 23, 2026 13:21
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants