From 10c195ed490096316b7efc85ea2d9d1039ed52ae Mon Sep 17 00:00:00 2001 From: pos-ei-don <1822533+pos-ei-don@users.noreply.github.com> Date: Tue, 4 Aug 2026 06:12:31 +0200 Subject: [PATCH 1/4] Qualify entity references only disabled steps make Action sequences can carry steps with `enabled: false`. Those do not run, so an unknown entity that only a disabled step references cannot break anything -- it is a reference parked for later, not a fault waiting to happen. Spook currently treats the two the same on the entity axis, and differently on the service axis: async_find_services_in_sequence skips disabled steps outright, extract_entities_from_action_config does not look at `enabled` at all. Same sequence, two answers. extract_entities_from_action_config gains an include_disabled keyword. Extracting a config twice and taking the difference gives the references that only disabled steps make, and the template repair says so per entity instead of listing them next to the ones that can actually fail. `enabled` only counts on list members, which is where steps, triggers and conditions live. The walker is fed raw config-entry options and recurses into arbitrary nesting, so gating every dict would let a service data payload that carries an `enabled` key of its own hide the entities around it. They stay in the report rather than being dropped. A step is parked for a reason -- a seasonal device, an alternative that may come back -- and the reference breaks the moment it is re-enabled. Dropping them would hide that until the day it matters. async_describe_unknown_entities gains an optional note for this, appended per line next to the existing "did you mean" and "deleted on" details. --- custom_components/spook/action_extraction.py | 44 ++++- .../repairs/unknown_entity_references.py | 42 ++++- custom_components/spook/entity_suggestions.py | 13 +- .../repairs/test_unknown_entity_references.py | 156 ++++++++++++++++++ 4 files changed, 244 insertions(+), 11 deletions(-) diff --git a/custom_components/spook/action_extraction.py b/custom_components/spook/action_extraction.py index a412f7e695..e6f9c47622 100644 --- a/custom_components/spook/action_extraction.py +++ b/custom_components/spook/action_extraction.py @@ -5,6 +5,8 @@ import re from typing import TYPE_CHECKING, Any +from homeassistant.const import CONF_ENABLED + from .const import LOGGER from .template_extraction import ( ENTITY_ID_PATTERN, @@ -21,9 +23,23 @@ async def async_extract_entities_from_action_config( - hass: HomeAssistant, config: dict[str, Any] | list + hass: HomeAssistant, + config: dict[str, Any] | list, + *, + include_disabled: bool = True, + _in_sequence: bool = False, ) -> set[str]: - """Extract entity IDs from action configuration.""" + """Extract entity IDs from action configuration. + + Steps carrying ``enabled: false`` are skipped when ``include_disabled`` is + False. Extracting twice and taking the difference tells which entities a + configuration only references from steps that do not run. + + ``enabled`` only counts on list members, which is where steps, triggers and + conditions live. Service data is walked as plain nesting, so a payload that + happens to carry an ``enabled`` key of its own does not silently hide the + entities around it. + """ entities = set() if not config: @@ -31,12 +47,22 @@ async def async_extract_entities_from_action_config( if isinstance(config, list): for item in config: - entities.update(await async_extract_entities_from_action_config(hass, item)) + entities.update( + await async_extract_entities_from_action_config( + hass, + item, + include_disabled=include_disabled, + _in_sequence=True, + ) + ) return entities if not isinstance(config, dict): return entities + if not include_disabled and _in_sequence and config.get(CONF_ENABLED) is False: + return entities + # Extract entity IDs from direct fields entities.update(await _extract_entities_from_action_fields(hass, config)) @@ -47,7 +73,11 @@ async def async_extract_entities_from_action_config( entities.update(await _extract_entities_from_service_data(hass, config)) # Extract from nested configs (like if/then/else, repeat, etc.) - entities.update(await _extract_entities_from_nested_configs(hass, config)) + entities.update( + await _extract_entities_from_nested_configs( + hass, config, include_disabled=include_disabled + ) + ) return entities @@ -113,14 +143,16 @@ async def _extract_entities_from_service_data( async def _extract_entities_from_nested_configs( - hass: HomeAssistant, config: dict[str, Any] + hass: HomeAssistant, config: dict[str, Any], *, include_disabled: bool = True ) -> set[str]: """Extract entities from nested configurations.""" entities = set() for value in config.values(): if isinstance(value, (dict, list)): entities.update( - await async_extract_entities_from_action_config(hass, value) + await async_extract_entities_from_action_config( + hass, value, include_disabled=include_disabled + ) ) return entities diff --git a/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py index a2bf9fba2d..b7c7fc6451 100644 --- a/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py @@ -79,6 +79,12 @@ async def async_inspect(self) -> None: # arm/disarm and so on. Those reference entities through structured # config (target, entity_id, service data) rather than through Jinja, # so the template extraction above does not see them. + # + # Extracting the actions twice separates references that a step + # carrying `enabled: false` is the only source of: those cannot + # break a run, so the report says so instead of listing them + # alongside the ones that can. + active = set(referenced) for option in options.values(): # Only structured options can hold an action; a plain string # option walks straight back out of the extractor. @@ -88,18 +94,48 @@ async def async_inspect(self) -> None: referenced |= await async_extract_entities_from_action_config( self.hass, option ) + active |= await async_extract_entities_from_action_config( + self.hass, option, include_disabled=False + ) if unknown_entities := async_filter_known_entity_ids( self.hass, referenced, known_entity_ids=known_entity_ids, ): + unknown_active = async_filter_known_entity_ids( + self.hass, + active, + known_entity_ids=known_entity_ids, + ) self.async_create_issue( issue_id=entry.entry_id, translation_placeholders={ - "entities": async_describe_unknown_entities( - self.hass, sorted(unknown_entities) - ), + "entities": self._describe(unknown_entities, unknown_active), "helper": entry.title, }, ) + + def _describe(self, unknown: set[str], unknown_active: set[str]) -> str: + """Describe the unknown entities, qualifying the disabled-only ones. + + An entity is only qualified when nothing that runs references it. A + Jinja template inside a disabled step is still seen by the template + extraction, so such a reference stays unqualified -- the report errs + towards saying too much rather than calling a live problem harmless. + """ + described = async_describe_unknown_entities(self.hass, sorted(unknown_active)) + if disabled_only := unknown - unknown_active: + described = "\n".join( + part + for part in ( + described, + async_describe_unknown_entities( + self.hass, + sorted(disabled_only), + note="only referenced from disabled steps", + ), + ) + if part + ) + return described diff --git a/custom_components/spook/entity_suggestions.py b/custom_components/spook/entity_suggestions.py index b081bed340..514db5a077 100644 --- a/custom_components/spook/entity_suggestions.py +++ b/custom_components/spook/entity_suggestions.py @@ -29,17 +29,26 @@ def async_describe_unknown_entities( hass: HomeAssistant, entity_ids: Iterable[str], + *, + note: str | None = None, ) -> str: - """Return a bulleted, enriched description of unknown entity IDs.""" + """Return a bulleted, enriched description of unknown entity IDs. + + A ``note`` is appended to every line, to qualify a group of entity IDs + that share something worth saying once per entry. + """ entity_registry = er.async_get(hass) deleted_by_entity_id = { deleted.entity_id: deleted for deleted in entity_registry.deleted_entities.values() } known_entity_ids = async_get_all_entity_ids(hass) + suffix = f" — {note}" if note else "" return "\n".join( - f"- `{entity_id}`" + _detail(entity_id, deleted_by_entity_id, known_entity_ids) + f"- `{entity_id}`" + + _detail(entity_id, deleted_by_entity_id, known_entity_ids) + + suffix for entity_id in entity_ids ) diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index 8a3476e9f5..48e3c23461 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -174,3 +174,159 @@ async def test_non_action_options_create_no_issue( DOMAIN, f"template_unknown_entity_references_{entry.entry_id}", ) + + +async def test_disabled_step_reference_is_qualified( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a reference only a disabled step makes is marked as such.""" + entry = MockConfigEntry( + domain="template", + title="Half-retired button", + options={ + "name": "Half-retired button", + "template_type": "button", + "press": [ + { + "action": "light.turn_on", + "target": {"entity_id": "light.gone_live"}, + }, + { + "action": "light.turn_on", + "enabled": False, + "target": {"entity_id": "light.gone_retired"}, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_entity_references_{entry.entry_id}", + ) + assert issue + entities = issue.translation_placeholders["entities"] + assert "light.gone_live" in entities + assert "light.gone_retired" in entities + # Only the disabled one carries the qualifier. + live, retired = ( + line + for line in entities.splitlines() + if "gone_live" in line or "gone_retired" in line + ) + assert "only referenced from disabled steps" not in live + assert "only referenced from disabled steps" in retired + + +async def test_disabled_step_only_still_reported( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a helper whose only bad reference is disabled is still reported. + + Disabled steps come back: a seasonal device parked for half a year is + still a broken reference waiting to happen, so it is qualified, not + dropped. + """ + entry = MockConfigEntry( + domain="template", + title="Parked button", + options={ + "name": "Parked button", + "template_type": "button", + "press": [ + { + "action": "light.turn_on", + "enabled": False, + "target": {"entity_id": "light.seasonal"}, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_entity_references_{entry.entry_id}", + ) + assert issue + entities = issue.translation_placeholders["entities"] + assert "light.seasonal" in entities + assert "only referenced from disabled steps" in entities + + +async def test_enabled_key_in_service_data_is_not_a_disabled_step( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test an `enabled` key inside a payload does not disable its surroundings. + + Only list members are steps. A service data payload carrying its own + `enabled` field must not hide the entities nested around it. + """ + entry = MockConfigEntry( + domain="template", + title="Payload button", + options={ + "name": "Payload button", + "template_type": "button", + "press": [ + { + "action": "test.service", + "data": {"payload": {"enabled": False, "entity_id": "light.gone"}}, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_entity_references_{entry.entry_id}", + ) + assert issue + entities = issue.translation_placeholders["entities"] + assert "light.gone" in entities + assert "only referenced from disabled steps" not in entities + + +async def test_templated_enabled_counts_as_active( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a step whose `enabled` is a template is treated as running.""" + entry = MockConfigEntry( + domain="template", + title="Maybe button", + options={ + "name": "Maybe button", + "template_type": "button", + "press": [ + { + "action": "light.turn_on", + "enabled": "{{ 1 == 2 }}", + "target": {"entity_id": "light.gone"}, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_entity_references_{entry.entry_id}", + ) + assert issue + entities = issue.translation_placeholders["entities"] + assert "light.gone" in entities + assert "only referenced from disabled steps" not in entities From b759c17df1918f4d9715835ad31fa00140506a20 Mon Sep 17 00:00:00 2001 From: pos-ei-don <1822533+pos-ei-don@users.noreply.github.com> Date: Tue, 4 Aug 2026 06:35:32 +0200 Subject: [PATCH 2/4] Select test lines by entity ID, not block order --- .../repairs/test_unknown_entity_references.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index 48e3c23461..a00be41068 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -212,12 +212,11 @@ async def test_disabled_step_reference_is_qualified( entities = issue.translation_placeholders["entities"] assert "light.gone_live" in entities assert "light.gone_retired" in entities - # Only the disabled one carries the qualifier. - live, retired = ( - line - for line in entities.splitlines() - if "gone_live" in line or "gone_retired" in line - ) + # Only the disabled one carries the qualifier. Select each line by its + # entity ID, so a failure names the entity rather than the block order. + lines = entities.splitlines() + live = next(line for line in lines if "gone_live" in line) + retired = next(line for line in lines if "gone_retired" in line) assert "only referenced from disabled steps" not in live assert "only referenced from disabled steps" in retired From fe1a4f42728005d160cf4d6bbd0dd7c21bcbae95 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 20 Aug 2026 08:19:14 +0000 Subject: [PATCH 3/4] Do not treat a payload list as a sequence of steps Service data is arbitrary payload, and a list in there is not a sequence of steps. The dict case was already pinned down, so a payload dict carrying its own `enabled` key was safe. A list one level deeper was not: its members were read as steps. The effect was a false qualifier, which is the one direction this must never go. An entity referenced only from a payload list was reported as "only referenced from disabled steps" while nothing was disabled. The flag rides down the walk instead of pruning it, so no detection is lost. --- custom_components/spook/action_extraction.py | 30 +++++++++---- .../repairs/test_unknown_entity_references.py | 42 +++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/custom_components/spook/action_extraction.py b/custom_components/spook/action_extraction.py index e6f9c47622..f1b69632a7 100644 --- a/custom_components/spook/action_extraction.py +++ b/custom_components/spook/action_extraction.py @@ -28,6 +28,7 @@ async def async_extract_entities_from_action_config( *, include_disabled: bool = True, _in_sequence: bool = False, + _in_payload: bool = False, ) -> set[str]: """Extract entity IDs from action configuration. @@ -36,9 +37,9 @@ async def async_extract_entities_from_action_config( configuration only references from steps that do not run. ``enabled`` only counts on list members, which is where steps, triggers and - conditions live. Service data is walked as plain nesting, so a payload that - happens to carry an ``enabled`` key of its own does not silently hide the - entities around it. + conditions live, and never below a ``data`` key. Service data is arbitrary + payload: a dict or a list in there that happens to carry an ``enabled`` key + of its own must not hide the entities around it. """ entities = set() @@ -53,6 +54,7 @@ async def async_extract_entities_from_action_config( item, include_disabled=include_disabled, _in_sequence=True, + _in_payload=_in_payload, ) ) return entities @@ -60,7 +62,12 @@ async def async_extract_entities_from_action_config( if not isinstance(config, dict): return entities - if not include_disabled and _in_sequence and config.get(CONF_ENABLED) is False: + if ( + not include_disabled + and _in_sequence + and not _in_payload + and config.get(CONF_ENABLED) is False + ): return entities # Extract entity IDs from direct fields @@ -75,7 +82,7 @@ async def async_extract_entities_from_action_config( # Extract from nested configs (like if/then/else, repeat, etc.) entities.update( await _extract_entities_from_nested_configs( - hass, config, include_disabled=include_disabled + hass, config, include_disabled=include_disabled, in_payload=_in_payload ) ) @@ -143,15 +150,22 @@ async def _extract_entities_from_service_data( async def _extract_entities_from_nested_configs( - hass: HomeAssistant, config: dict[str, Any], *, include_disabled: bool = True + hass: HomeAssistant, + config: dict[str, Any], + *, + include_disabled: bool = True, + in_payload: bool = False, ) -> set[str]: """Extract entities from nested configurations.""" entities = set() - for value in config.values(): + for key, value in config.items(): if isinstance(value, (dict, list)): entities.update( await async_extract_entities_from_action_config( - hass, value, include_disabled=include_disabled + hass, + value, + include_disabled=include_disabled, + _in_payload=in_payload or key == "data", ) ) return entities diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index a00be41068..9e145f0044 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -297,6 +297,48 @@ async def test_enabled_key_in_service_data_is_not_a_disabled_step( assert "only referenced from disabled steps" not in entities +async def test_enabled_key_in_payload_list_is_not_a_disabled_step( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test an `enabled` key in a payload list does not disable its surroundings. + + Service data is arbitrary payload, and a list in there is not a sequence + of steps. Only the dict case was pinned down before, which left a list + one level deeper reported as disabled while nothing was disabled. + """ + entry = MockConfigEntry( + domain="template", + title="Payload list button", + options={ + "name": "Payload list button", + "template_type": "button", + "press": [ + { + "action": "mqtt.publish", + "data": { + "payload": { + "items": [{"enabled": False, "entity_id": "light.gone"}], + }, + }, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_entity_references_{entry.entry_id}", + ) + assert issue + entities = issue.translation_placeholders["entities"] + assert "light.gone" in entities + assert "only referenced from disabled steps" not in entities + + async def test_templated_enabled_counts_as_active( hass: HomeAssistant, issue_registry: ir.IssueRegistry, From 846eb1fdfa36677d471303815e5b91439c37a349 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 20 Aug 2026 08:19:20 +0000 Subject: [PATCH 4/4] Parenthesize the qualifier, like the details next to it `(deleted on ...)` and `(did you mean ...?)` already set the shape for trailing detail on these lines. No reason for this one to introduce a dash. --- custom_components/spook/entity_suggestions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/entity_suggestions.py b/custom_components/spook/entity_suggestions.py index 514db5a077..9d8694d0e6 100644 --- a/custom_components/spook/entity_suggestions.py +++ b/custom_components/spook/entity_suggestions.py @@ -43,7 +43,8 @@ def async_describe_unknown_entities( for deleted in entity_registry.deleted_entities.values() } known_entity_ids = async_get_all_entity_ids(hass) - suffix = f" — {note}" if note else "" + # Parenthesized, like the deleted-on and did-you-mean details below. + suffix = f" ({note})" if note else "" return "\n".join( f"- `{entity_id}`"