Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions custom_components/spook/entity_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Comment thread
frenckatron marked this conversation as resolved.
Resolved

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()
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }} "
Comment thread
Copilot marked this conversation as resolved.
Resolved
"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:
Expand Down
Loading