Skip to content

Commit

Permalink
Add repair: Find missing source switches for switch_as_x helpers (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 14, 2023
1 parent 3326eac commit 71cebbf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions custom_components/spook/repairs/switch_as_x_unknown_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Spook - Not your homie."""
from __future__ import annotations

from homeassistant.const import EVENT_COMPONENT_LOADED
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ..const import LOGGER
from . import AbstractSpookRepair


class SpookRepair(AbstractSpookRepair):
"""Spook repair tries to find unknown source entites for switch_as_x."""

domain = "switch_as_x"
repair = "switch_as_x_unknown_source"
events = {
EVENT_COMPONENT_LOADED,
er.EVENT_ENTITY_REGISTRY_UPDATED,
"event_integration_reloaded",
}

async def async_inspect(self) -> None:
"""Trigger a inspection."""
LOGGER.debug("Spook is inspecting: %s", self.repair)

platforms: list[EntityPlatform] | None
if not (platforms := self.hass.data[DATA_ENTITY_PLATFORM].get(self.domain)):
return # Nothing to do, switch_as_x is not loaded

entity_ids = {
entity.entity_id for entity in self.entity_registry.entities.values()
}.union(self.hass.states.async_entity_ids())

for platform in platforms:
for entity in platform.entities.values():
# pylint: disable-next=protected-access
source = entity._switch_entity_id # noqa: SLF001
if source not in entity_ids:
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"entity_id": entity.entity_id,
"helper": entity.name,
"source": source,
},
)
LOGGER.debug(
"Spook found unknown source entity %s in %s "
"and created an issue for it",
source,
entity.entity_id,
)
else:
self.async_delete_issue(entity.entity_id)
4 changes: 4 additions & 0 deletions custom_components/spook/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"script_unknown_entity_references": {
"title": "Unknown entities used in: {script}",
"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 👻 Not your homie."
},
"switch_as_x_unknown_source": {
"title": "Unknown source: {helper}",
"description": "Spook has found a ghost in your Switch as X helpers 👻\n\nWhile floating around, Spook crossed path with the following helper:\n\n{helper} (`{entity_id}`)\n\nThis helper has a source switch entity unknown to Home Assistant:\n\n`{source}`\n\n\n\nTo fix this error, edit the helper and adjust the source entity (or remove the helper) and restart Home Assistant.\n\nSpook 👻 Not your homie."
}
}
}

0 comments on commit 71cebbf

Please sign in to comment.