From 1ed35bfcec4c60f70cb2b352c03c233bb33b0eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Mon, 3 Aug 2026 14:14:52 +0200 Subject: [PATCH 1/5] Detect unknown actions in template helpers --- .../repairs/unknown_service_references.py | 69 +++++++++++++++++++ custom_components/spook/translations/en.json | 4 ++ .../repairs/test_unknown_entity_references.py | 59 ++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py diff --git a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py new file mode 100644 index 0000000000..241d23d63a --- /dev/null +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py @@ -0,0 +1,69 @@ +"""Spook - Your homie.""" + +from __future__ import annotations + +from homeassistant.const import ( + EVENT_COMPONENT_LOADED, + EVENT_SERVICE_REGISTERED, + EVENT_SERVICE_REMOVED, +) +from homeassistant.helpers import entity_registry as er + +from ....const import LOGGER +from ....entity_filtering import ( + async_filter_known_services, + async_find_services_in_sequence, + async_get_all_services, +) +from ....repairs import AbstractSpookRepair + + +class SpookRepair(AbstractSpookRepair): + """Spook repair finds unknown actions referenced in template helpers.""" + + domain = "template" + repair = "template_unknown_service_references" + inspect_events = { + EVENT_COMPONENT_LOADED, + EVENT_SERVICE_REGISTERED, + EVENT_SERVICE_REMOVED, + er.EVENT_ENTITY_REGISTRY_UPDATED, + } + inspect_config_entry_changed = "template" + inspect_on_reload = "template" + automatically_clean_up_issues = True + + async def async_inspect(self) -> None: + """Inspect template helper actions for unavailable services.""" + LOGGER.debug("Spook is inspecting: %s", self.repair) + + known_services = async_get_all_services(self.hass) + + for entry in self.hass.config_entries.async_entries(self.domain): + self.possible_issue_ids.add(entry.entry_id) + + services = set() + for option in entry.options.values(): + if not isinstance(option, list) or not all( + isinstance(step, dict) for step in option + ): + continue + services.update(async_find_services_in_sequence(option)) + + unknown_services = async_filter_known_services( + self.hass, + services=services, + known_services=known_services, + ) + if not unknown_services: + continue + + self.async_create_issue( + issue_id=entry.entry_id, + translation_placeholders={ + "helper": entry.title, + "services": "\n".join( + f"- `{service}`" for service in sorted(unknown_services) + ), + }, + ) diff --git a/custom_components/spook/translations/en.json b/custom_components/spook/translations/en.json index ee02657438..3f07a313c0 100644 --- a/custom_components/spook/translations/en.json +++ b/custom_components/spook/translations/en.json @@ -486,6 +486,10 @@ "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n{helper}\n\nIts templates reference the following entities, which are unknown to Home Assistant:\n\n{entities}\n\n\n\nThis often happens when an entity was renamed or removed. To fix this error, reconfigure the template helper to reference existing entities.\n\nSpook 👻 Your homie.", "title": "Unknown entities used in template helper: {helper}" }, + "template_unknown_service_references": { + "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n{helper}\n\nIts actions reference the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook 👻 Your homie.", + "title": "Unknown actions used in template helper: {helper}" + }, "unknown_customized_entities": { "description": "Spook has found a ghost in your customizations 👻\n\nThe `customize:` section pins attributes on the following entities, which are unknown to Home Assistant:\n\n{entities}\n\n\n\nThis often happens when an entity was renamed or removed while its customization stayed behind. To fix this error, edit the `customize:` section in your configuration and remove these entities.\n\nSpook 👻 Your homie.", "title": "Unknown entities in your customizations" diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index 0f473096ef..8c5f026a81 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -11,6 +11,9 @@ from custom_components.spook.ectoplasms.template.repairs.unknown_entity_references import ( SpookRepair, ) +from custom_components.spook.ectoplasms.template.repairs.unknown_service_references import ( + SpookRepair as UnknownServiceReferencesRepair, +) if TYPE_CHECKING: from homeassistant.core import HomeAssistant @@ -74,3 +77,59 @@ async def test_known_entities_create_no_issue( ) is None ) + + +async def test_unknown_action_in_template_button_creates_issue( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test template button actions referencing deleted scripts are reported.""" + entry = MockConfigEntry( + domain="template", + title="Clean everything", + options={ + "name": "Clean everything", + "template_type": "button", + "press": [{"action": "script.1766687627449"}], + }, + ) + entry.add_to_hass(hass) + + await UnknownServiceReferencesRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_service_references_{entry.entry_id}", + ) + assert issue + assert issue.translation_placeholders + assert issue.translation_placeholders["services"] == "- `script.1766687627449`" + + +async def test_unknown_action_in_another_template_helper_creates_issue( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test all template helper action options are inspected.""" + entry = MockConfigEntry( + domain="template", + title="Ghostly switch", + options={ + "name": "Ghostly switch", + "template_type": "switch", + "turn_on": [{"action": "script.missing_turn_on"}], + "turn_off": [{"action": "script.working_turn_off"}], + }, + ) + entry.add_to_hass(hass) + hass.services.async_register("script", "working_turn_off", lambda _call: None) + + await UnknownServiceReferencesRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_service_references_{entry.entry_id}", + ) + assert issue + assert issue.translation_placeholders + assert issue.translation_placeholders["services"] == "- `script.missing_turn_on`" From f4343af5869fd3dff4850b40922ff477d93bb258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Mon, 3 Aug 2026 14:18:42 +0200 Subject: [PATCH 2/5] Link template helper repairs to Helpers page --- .../ectoplasms/template/repairs/unknown_entity_references.py | 1 + .../ectoplasms/template/repairs/unknown_service_references.py | 1 + custom_components/spook/translations/en.json | 4 ++-- .../template/repairs/test_unknown_entity_references.py | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) 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 a57e830ba4..cad3e8c69c 100644 --- a/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py @@ -85,5 +85,6 @@ async def async_inspect(self) -> None: self.hass, sorted(unknown_entities) ), "helper": entry.title, + "edit": "/config/helpers", }, ) diff --git a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py index 241d23d63a..4ac5bff6b8 100644 --- a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py @@ -62,6 +62,7 @@ async def async_inspect(self) -> None: issue_id=entry.entry_id, translation_placeholders={ "helper": entry.title, + "edit": "/config/helpers", "services": "\n".join( f"- `{service}`" for service in sorted(unknown_services) ), diff --git a/custom_components/spook/translations/en.json b/custom_components/spook/translations/en.json index 3f07a313c0..427ffee57c 100644 --- a/custom_components/spook/translations/en.json +++ b/custom_components/spook/translations/en.json @@ -483,11 +483,11 @@ } }, "template_unknown_entity_references": { - "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n{helper}\n\nIts templates reference the following entities, which are unknown to Home Assistant:\n\n{entities}\n\n\n\nThis often happens when an entity was renamed or removed. To fix this error, reconfigure the template helper to reference existing entities.\n\nSpook 👻 Your homie.", + "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n[{helper}]({edit})\n\nIts templates reference the following entities, which are unknown to Home Assistant:\n\n{entities}\n\n\n\nThis often happens when an entity was renamed or removed. To fix this error, reconfigure the template helper to reference existing entities.\n\nSpook 👻 Your homie.", "title": "Unknown entities used in template helper: {helper}" }, "template_unknown_service_references": { - "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n{helper}\n\nIts actions reference the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook 👻 Your homie.", + "description": "Spook has found a ghost in your template helpers 👻\n\nWhile floating around, Spook crossed path with the following template helper:\n\n[{helper}]({edit})\n\nIts actions reference the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook 👻 Your homie.", "title": "Unknown actions used in template helper: {helper}" }, "unknown_customized_entities": { diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index 8c5f026a81..ddf7c7cac7 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -48,6 +48,7 @@ async def test_unknown_entity_in_template_creates_issue( assert issue.translation_placeholders["entities"] == ( "- `binary_sensor.also_ghost`\n- `sensor.ghost`" ) + assert issue.translation_placeholders["edit"] == "/config/helpers" async def test_known_entities_create_no_issue( @@ -103,6 +104,7 @@ async def test_unknown_action_in_template_button_creates_issue( ) assert issue assert issue.translation_placeholders + assert issue.translation_placeholders["edit"] == "/config/helpers" assert issue.translation_placeholders["services"] == "- `script.1766687627449`" From 1cac1be2693fbcd9cbd747ba213d3c1152af6c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Mon, 3 Aug 2026 14:25:46 +0200 Subject: [PATCH 3/5] Skip templated template helper actions --- .../repairs/unknown_service_references.py | 7 ++++- .../repairs/test_unknown_entity_references.py | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py index 4ac5bff6b8..006554b2f3 100644 --- a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py @@ -16,6 +16,7 @@ async_get_all_services, ) from ....repairs import AbstractSpookRepair +from ....template_extraction import is_template_string class SpookRepair(AbstractSpookRepair): @@ -48,7 +49,11 @@ async def async_inspect(self) -> None: isinstance(step, dict) for step in option ): continue - services.update(async_find_services_in_sequence(option)) + services.update( + service + for service in async_find_services_in_sequence(option) + if not is_template_string(service) + ) unknown_services = async_filter_known_services( self.hass, diff --git a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py index ddf7c7cac7..e760ecc3f6 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_entity_references.py @@ -135,3 +135,30 @@ async def test_unknown_action_in_another_template_helper_creates_issue( assert issue assert issue.translation_placeholders assert issue.translation_placeholders["services"] == "- `script.missing_turn_on`" + + +async def test_templated_action_in_template_helper_creates_no_issue( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test templated actions in raw template helper options are ignored.""" + entry = MockConfigEntry( + domain="template", + title="Dynamic notification", + options={ + "name": "Dynamic notification", + "template_type": "button", + "press": [{"action": "{{ 'notify.' ~ who }}"}], + }, + ) + entry.add_to_hass(hass) + + await UnknownServiceReferencesRepair(hass).async_inspect() + + assert ( + issue_registry.async_get_issue( + DOMAIN, + f"template_unknown_service_references_{entry.entry_id}", + ) + is None + ) From e292ef3ce4a55ee2836c76bfcc9d59f3bbcce390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Mon, 3 Aug 2026 14:36:55 +0200 Subject: [PATCH 4/5] Document template helper repairs --- documentation/_toc.yml | 1 + documentation/integrations/template.md | 51 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 documentation/integrations/template.md diff --git a/documentation/_toc.yml b/documentation/_toc.yml index 3a2bff210d..64f0cce044 100644 --- a/documentation/_toc.yml +++ b/documentation/_toc.yml @@ -47,6 +47,7 @@ parts: - file: integrations/script - file: integrations/select - file: integrations/switch_as_x + - file: integrations/template - file: integrations/timer - file: integrations/trend - file: integrations/utility_meter diff --git a/documentation/integrations/template.md b/documentation/integrations/template.md new file mode 100644 index 0000000000..b1dd34afee --- /dev/null +++ b/documentation/integrations/template.md @@ -0,0 +1,51 @@ +--- +subject: Enhanced integrations +title: Template helpers +subtitle: Keep template helpers free of ghosts. 👻 +description: Spook inspects Template helpers and raises repair issues for references to unavailable entities and actions. +date: 2026-08-03T00:00:00+02:00 +--- + +The [Template integration](https://www.home-assistant.io/integrations/template/) +lets you create helpers whose values and actions are based on templates. Spook +inspects Template helpers created in the Home Assistant user interface and +raises repair issues when it finds references that are no longer available. + +## Devices & entities + +Spook does not provide any new devices or entities for this integration. + +## Actions + +Spook does not provide action enhancements for this integration. + +## Repairs + +While Spook is floating around in your Home Assistant instance, it raises a +repair issue when it finds something that is not right. + +### Unknown referenced entities + +Spook inspects templates in Template helpers for entity IDs that do not exist. +The repair issue lists the helper and the unavailable entity IDs. To resolve the +issue, open the linked helper, update the reference to an existing entity, or +remove it. Spook automatically removes the repair issue once it is fixed. + +### Unknown referenced actions + +Spook inspects action sequences configured in Template helpers, such as the +`press` action of a Template Button or the `turn_on` and `turn_off` actions of +a Template Switch. If an action is unavailable, the repair issue lists the +helper and the unavailable action. + +To resolve the issue, open the linked helper, update the action to one provided +by Home Assistant, or restore the integration or script that provided it. +Templated action names are ignored because their value is determined at runtime. +Spook automatically removes the repair issue once it is fixed. + +## Features requests, ideas, and support + +If you have an idea for a new action, entity, or repair detection, [let us know +in the discussion forums](https://github.com/frenck/spook/discussions). + +Are you stuck? See the [](../support) page for where to get help. From 35c6044a63c7c26c89452b19fe01bb2dd6e531b0 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 20 Aug 2026 09:42:29 +0000 Subject: [PATCH 5/5] Validate template helper options before walking them for actions async_find_services_in_sequence was written for validated script sequences and indexes keys that only exist after cv.SCRIPT_SCHEMA has run. This repair was the first caller handing it raw config entry options, and it raised on shapes the action editor writes every day: parallel shorthand KeyError: 'sequence' bare repeat KeyError: 'sequence' if without then KeyError: 'then' a step with an alias and nothing else ValueError The exception escaped async_inspect and repairs.py re-raises it, so one such helper meant every config entry after it went uninspected and the repair reported nothing at all, with a traceback on every trigger. Validating first fixes all of them, and turns parallel shorthand from fatal into a real detection. It also replaces two heuristics with a real answer: the isinstance() shape guard, and the is_template_string filter, since a templated action name validates into a Template and a Template is not a string, so the known-services filter drops it already. Also drop the entity registry subscription. Service availability does not track the entity registry, script entities appearing already fire EVENT_SERVICE_REGISTERED, and neither the script nor the automation service repair listens for it. And say "It references the following actions" rather than "Its actions reference the following actions", matching the script repair. --- .../repairs/unknown_service_references.py | 28 ++- custom_components/spook/translations/en.json | 2 +- .../test_unknown_service_references.py | 189 +++++++++++++++++- 3 files changed, 206 insertions(+), 13 deletions(-) diff --git a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py index 006554b2f3..7ca16fc905 100644 --- a/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py +++ b/custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py @@ -2,12 +2,14 @@ from __future__ import annotations +import voluptuous as vol + from homeassistant.const import ( EVENT_COMPONENT_LOADED, EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, ) -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import config_validation as cv from ....const import LOGGER from ....entity_filtering import ( @@ -16,7 +18,6 @@ async_get_all_services, ) from ....repairs import AbstractSpookRepair -from ....template_extraction import is_template_string class SpookRepair(AbstractSpookRepair): @@ -28,7 +29,6 @@ class SpookRepair(AbstractSpookRepair): EVENT_COMPONENT_LOADED, EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, - er.EVENT_ENTITY_REGISTRY_UPDATED, } inspect_config_entry_changed = "template" inspect_on_reload = "template" @@ -43,17 +43,23 @@ async def async_inspect(self) -> None: for entry in self.hass.config_entries.async_entries(self.domain): self.possible_issue_ids.add(entry.entry_id) + # Which options hold actions grows with every new template helper + # type, so ask Home Assistant instead of keeping a list of keys. + # + # Validating is not just a shape check. The walker reads keys that + # only exist after validation, so raw options make it raise on + # shapes the action editor writes every day, a `parallel` block + # among them. Validation normalizes those, and turns a templated + # action name into a Template, which is not a string and so falls + # out of the known-services filter on its own. services = set() for option in entry.options.values(): - if not isinstance(option, list) or not all( - isinstance(step, dict) for step in option - ): + try: + sequence = cv.SCRIPT_SCHEMA(option) + except vol.Invalid: continue - services.update( - service - for service in async_find_services_in_sequence(option) - if not is_template_string(service) - ) + + services.update(async_find_services_in_sequence(sequence)) unknown_services = async_filter_known_services( self.hass, diff --git a/custom_components/spook/translations/en.json b/custom_components/spook/translations/en.json index ef6236099d..14b13169b2 100644 --- a/custom_components/spook/translations/en.json +++ b/custom_components/spook/translations/en.json @@ -497,7 +497,7 @@ "title": "Unknown entities used in template helper: {helper}" }, "template_unknown_service_references": { - "description": "Spook has found a ghost in your template helpers \ud83d\udc7b\n\nWhile floating around, Spook crossed path with the following template helper:\n\n[{helper}]({edit})\n\nIts actions reference the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook \ud83d\udc7b Your homie.", + "description": "Spook has found a ghost in your template helpers \ud83d\udc7b\n\nWhile floating around, Spook crossed path with the following template helper:\n\n[{helper}]({edit})\n\nIt references the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook \ud83d\udc7b Your homie.", "title": "Unknown actions used in template helper: {helper}" }, "unknown_customized_entities": { diff --git a/tests/ectoplasms/template/repairs/test_unknown_service_references.py b/tests/ectoplasms/template/repairs/test_unknown_service_references.py index eaae7f8d1b..4d6eb3cbc3 100644 --- a/tests/ectoplasms/template/repairs/test_unknown_service_references.py +++ b/tests/ectoplasms/template/repairs/test_unknown_service_references.py @@ -1,6 +1,7 @@ """Tests for the template helper unknown action references repair.""" -# pylint: disable=wrong-import-order +# ruff: noqa: SLF001 +# pylint: disable=protected-access,wrong-import-order from __future__ import annotations from typing import TYPE_CHECKING @@ -92,3 +93,189 @@ async def test_templated_action_in_template_helper_creates_no_issue( await SpookRepair(hass).async_inspect() assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) is None + + +async def test_parallel_shorthand_is_inspected( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a parallel block written as shorthand is walked, not fatal. + + The action editor stores a parallel block as `parallel: [, ...]`, + and Home Assistant only wraps that into a `sequence` while validating. + Walking it raw raised `KeyError: 'sequence'`, which took down the whole + inspection and left every later config entry uninspected. + """ + entry = MockConfigEntry( + domain="template", + title="Parallel button", + options={ + "name": "Parallel button", + "template_type": "button", + "press": [{"parallel": [{"action": "script.ghost"}]}], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) + assert issue + assert issue.translation_placeholders["services"] == "- `script.ghost`" + + +async def test_nested_action_shapes_are_inspected( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test choose, if/then and repeat blocks are all walked.""" + entry = MockConfigEntry( + domain="template", + title="Nested button", + options={ + "name": "Nested button", + "template_type": "button", + "press": [ + { + "choose": [ + { + "conditions": [], + "sequence": [{"action": "script.ghost_choose"}], + }, + ], + }, + { + "if": [ + {"condition": "state", "entity_id": "light.x", "state": "on"}, + ], + "then": [{"action": "script.ghost_if"}], + }, + { + "repeat": { + "count": 2, + "sequence": [{"action": "script.ghost_repeat"}], + }, + }, + ], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + issue = issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) + assert issue + assert issue.translation_placeholders["services"] == ( + "- `script.ghost_choose`\n- `script.ghost_if`\n- `script.ghost_repeat`" + ) + + +async def test_unparsable_option_is_skipped( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test options Home Assistant itself would reject are stepped over. + + A step with nothing to identify it, and a bare `repeat` without its + sequence, both used to raise rather than being skipped. + """ + entry = MockConfigEntry( + domain="template", + title="Broken button", + options={ + "name": "Broken button", + "template_type": "button", + "press": [{"alias": "a note and nothing else"}], + "unrelated": [{"repeat": {"count": 2}}], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) is None + + +async def test_one_bad_entry_does_not_hide_the_next( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a helper Home Assistant cannot validate does not stop the scan. + + This is what made the crash expensive: the exception escaped + `async_inspect`, so every config entry after the offending one went + uninspected and the repair silently reported nothing at all. + """ + broken = MockConfigEntry( + domain="template", + title="Broken first", + options={ + "name": "Broken first", + "template_type": "button", + "press": [{"if": [{"condition": "state"}]}], + }, + ) + broken.add_to_hass(hass) + + fine = MockConfigEntry( + domain="template", + title="Reportable second", + options={ + "name": "Reportable second", + "template_type": "button", + "press": [{"action": "script.ghost"}], + }, + ) + fine.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + assert issue_registry.async_get_issue(DOMAIN, _issue_id(broken)) is None + assert issue_registry.async_get_issue(DOMAIN, _issue_id(fine)) + + +async def test_disabled_step_is_not_reported( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a step the user disabled is skipped, as the walker already does.""" + entry = MockConfigEntry( + domain="template", + title="Parked button", + options={ + "name": "Parked button", + "template_type": "button", + "press": [{"action": "script.ghost", "enabled": False}], + }, + ) + entry.add_to_hass(hass) + + await SpookRepair(hass).async_inspect() + + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) is None + + +async def test_issue_clears_once_the_action_exists( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test the issue disappears when the action is registered again.""" + entry = MockConfigEntry( + domain="template", + title="Recovering button", + options={ + "name": "Recovering button", + "template_type": "button", + "press": [{"action": "script.comes_back"}], + }, + ) + entry.add_to_hass(hass) + + repair = SpookRepair(hass) + await repair._async_inspect_with_cleanup() + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) + + hass.services.async_register("script", "comes_back", lambda _call: None) + await repair._async_inspect_with_cleanup() + + assert issue_registry.async_get_issue(DOMAIN, _issue_id(entry)) is None