-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add repair: Find missing source switches for switch_as_x helpers (#124)
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
custom_components/spook/repairs/switch_as_x_unknown_source.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters