Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect unknown floors used in scripts #697

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,61 @@
"""Spook - Your homie."""

from __future__ import annotations

from homeassistant.components import script
from homeassistant.helpers import floor_registry as fr
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_floor_ids, async_get_all_floor_ids


class SpookRepair(AbstractSpookRepair):
"""Spook repair tries to find unknown referenced floors in scripts."""

domain = script.DOMAIN
repair = "script_unknown_floor_references"
inspect_events = {fr.EVENT_FLOOR_REGISTRY_UPDATED}
inspect_on_reload = True

automatically_clean_up_issues = True

async def async_inspect(self) -> None:
"""Trigger a inspection."""
if self.domain not in self.hass.data[DATA_INSTANCES]:
return

entity_component: EntityComponent[script.ScriptEntity] = self.hass.data[
DATA_INSTANCES
][self.domain]

known_floor_ids = async_get_all_floor_ids(self.hass)

LOGGER.debug("Spook is inspecting: %s", self.repair)
for entity in entity_component.entities:
self.possible_issue_ids.add(entity.entity_id)
if not isinstance(entity, script.UnavailableScriptEntity) and (
unknown_floors := async_filter_known_floor_ids(
self.hass,
floor_ids=entity.script.referenced_floors,
known_floor_ids=known_floor_ids,
)
):
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"floors": "\n".join(f"- `{floor}`" for floor in unknown_floors),
"script": entity.name,
"edit": f"/config/script/edit/{entity.unique_id}",
"entity_id": entity.entity_id,
},
)
LOGGER.debug(
(
"Spook found unknown floors in %s "
"and created an issue for it; Floors: %s",
),
entity.entity_id,
", ".join(unknown_floors),
)
4 changes: 4 additions & 0 deletions custom_components/spook/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@
"description": "Spook has found a ghost in your scripts 👻\n\nWhile floating around, Spook crossed path with the following script:\n\n[{script}]({edit}) (`{entity_id}`)\n\nThis script references the following entities, which are unknown to Home Assistant:\n\n{entities}\n\n\n\nTo fix this error, [edit the script]({edit}) and remove the use of these non-existing entities.\n\nSpook 👻 Your homie.",
"title": "Unknown entities used in: {script}"
},
"script_unknown_floor_references": {
"description": "Spook has found a ghost in your scripts 👻\n\nWhile floating around, Spook crossed path with the following script:\n\n[{script}]({edit}) (`{entity_id}`)\n\nThis script references the following floors, which are unknown to Home Assistant:\n\n{floors}\n\n\n\nTo fix this error, [edit the script]({edit}) and remove the use of these non-existing floors.\n\nSpook 👻 Your homie.",
"title": "Unknown floors used in: {script}"
},
"script_unknown_label_references": {
"description": "Spook has found a ghost in your scripts 👻\n\nWhile floating around, Spook crossed path with the following script:\n\n[{script}]({edit}) (`{entity_id}`)\n\nThis script references the following labels, which are unknown to Home Assistant:\n\n{labels}\n\n\n\nTo fix this error, [edit the script]({edit}) and remove the use of these non-existing labels.\n\nSpook 👻 Your homie.",
"title": "Unknown labels used in: {script}"
Expand Down