From 351208e02b71dd9d7e3eedf5d39ecce773cd0c95 Mon Sep 17 00:00:00 2001 From: Frenckatron Date: Tue, 23 Jun 2026 10:42:22 +0200 Subject: [PATCH 1/3] Ignore entity references in Jinja comments --- custom_components/spook/entity_filtering.py | 15 +++++++++++---- .../repairs/test_unknown_entity_references.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/custom_components/spook/entity_filtering.py b/custom_components/spook/entity_filtering.py index 8f7acbe093..3efe1beb3f 100644 --- a/custom_components/spook/entity_filtering.py +++ b/custom_components/spook/entity_filtering.py @@ -407,6 +407,11 @@ async def async_extract_entities_from_template_string( return entities +def _strip_jinja_comments(template_str: str) -> str: + """Remove Jinja comments from a template string.""" + return re.sub(r"\{#.*?#\}", "", template_str, flags=re.DOTALL) + + def _is_concatenated_template_match(template_str: str, match: re.Match[str]) -> bool: """Return if a quoted entity ID literal is part of a concatenated string.""" groups = match.groups() @@ -504,14 +509,16 @@ def extract_entities_from_template_regex( if not isinstance(template_str, str): return set() + template_without_comments = _strip_jinja_comments(template_str) + entities = set() for pattern in COMPILED_ENTITY_ID_TEMPLATE_PATTERNS: - for match in pattern.finditer(template_str): + for match in pattern.finditer(template_without_comments): if ( - _is_concatenated_template_match(template_str, match) - or _is_jinja_import_match(template_str, match) - or _is_string_method_argument_match(template_str, match) + _is_concatenated_template_match(template_without_comments, match) + or _is_jinja_import_match(template_without_comments, match) + or _is_string_method_argument_match(template_without_comments, match) ): continue diff --git a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py index 5cef68d866..20823ec518 100644 --- a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py @@ -156,6 +156,19 @@ async def test_value_template_ignores_entity_id_suffix_string_match( assert await extract_entities_from_value(hass, template) == set() +async def test_value_template_ignores_entity_id_in_jinja_comment( + hass: HomeAssistant, +) -> None: + """Entity-like strings inside Jinja comments are not active references.""" + template = ( + "{#{ state_translated('sensor.toothbrush_change_head') | string }} " + "indicates time to change, toothbrush head #}" + "{{ trigger.to_state.attributes.friendly_name | string }}" + ) + + assert await extract_entities_from_value(hass, template) == set() + + async def test_value_template_ignores_concatenated_helper_entity_id( hass: HomeAssistant, ) -> None: From e5e4b8dcecaf6a143e329a5e21e503173213e75b Mon Sep 17 00:00:00 2001 From: Frenckatron Date: Tue, 23 Jun 2026 12:37:08 +0200 Subject: [PATCH 2/3] Clarify Jinja comment regression test --- .../automation/repairs/test_unknown_entity_references.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py index 20823ec518..1056c6a26f 100644 --- a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py @@ -161,7 +161,7 @@ async def test_value_template_ignores_entity_id_in_jinja_comment( ) -> None: """Entity-like strings inside Jinja comments are not active references.""" template = ( - "{#{ state_translated('sensor.toothbrush_change_head') | string }} " + "{# {{ state_translated('sensor.toothbrush_change_head') | string }} " "indicates time to change, toothbrush head #}" "{{ trigger.to_state.attributes.friendly_name | string }}" ) From f591615bd317d0b1a5510fa734570ae0ca080039 Mon Sep 17 00:00:00 2001 From: Frenckatron Date: Tue, 23 Jun 2026 15:04:59 +0200 Subject: [PATCH 3/3] Optimize Jinja comment stripping --- custom_components/spook/entity_filtering.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/entity_filtering.py b/custom_components/spook/entity_filtering.py index 3efe1beb3f..c5f3a5de9f 100644 --- a/custom_components/spook/entity_filtering.py +++ b/custom_components/spook/entity_filtering.py @@ -122,6 +122,7 @@ COMPILED_ENTITY_ID_TEMPLATE_PATTERNS = tuple( re.compile(pattern, re.IGNORECASE) for pattern in ENTITY_ID_TEMPLATE_PATTERNS ) +JINJA_COMMENT_PATTERN = re.compile(r"\{#.*?#\}", re.DOTALL) _CACHED_ALL_ENTITY_IDS: set[str] | None = None _UNSUB_CACHE_INVALIDATION: Callable[[], None] | None = None @@ -409,7 +410,9 @@ async def async_extract_entities_from_template_string( def _strip_jinja_comments(template_str: str) -> str: """Remove Jinja comments from a template string.""" - return re.sub(r"\{#.*?#\}", "", template_str, flags=re.DOTALL) + if "{#" not in template_str: + return template_str + return JINJA_COMMENT_PATTERN.sub("", template_str) def _is_concatenated_template_match(template_str: str, match: re.Match[str]) -> bool: