Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ async def async_inspect(self) -> None:
self.hass, sorted(unknown_entities)
),
"helper": entry.title,
"edit": "/config/helpers",
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""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,
)
Comment thread
cnotin marked this conversation as resolved.
Resolved
if not unknown_services:
continue

self.async_create_issue(
issue_id=entry.entry_id,
translation_placeholders={
"helper": entry.title,
"edit": "/config/helpers",
"services": "\n".join(
f"- `{service}`" for service in sorted(unknown_services)
),
},
)
6 changes: 5 additions & 1 deletion custom_components/spook/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,13 @@
}
},
"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}]({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": {
"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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,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(
Expand Down Expand Up @@ -74,3 +78,60 @@ 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["edit"] == "/config/helpers"
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`"