Files
HomeAssistantVS/custom_components/spook/ectoplasms/automation/repairs/unknown_label_references.py
T

63 lines
2.2 KiB
Python

"""Spook - Your homie."""
from __future__ import annotations
from functools import partial
from typing import TYPE_CHECKING
from homeassistant.components import automation
from homeassistant.helpers import label_registry as lr
from ....entity_filtering import async_filter_known_label_ids, async_get_all_label_ids
from ....reference_extraction import (
async_raw_config_references,
core_references,
only_in_disabled_steps,
)
from . import AbstractSpookAutomationReferencesRepair
if TYPE_CHECKING:
from typing import Any
class SpookRepair(AbstractSpookAutomationReferencesRepair):
"""Spook repair tries to find unknown referenced labels in automations."""
domain = automation.DOMAIN
repair = "automation_unknown_label_references"
inspect_events = {
lr.EVENT_LABEL_REGISTRY_UPDATED,
}
inspect_on_reload = True
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "labels"
edit_url_pattern = "/config/automation/edit/{unique_id}"
_known_label_ids: set[str]
async def _async_setup_inspection(self) -> None:
"""Cache known label IDs for this inspection cycle."""
self._known_label_ids = async_get_all_label_ids(self.hass)
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown label IDs referenced by ``entity``."""
label_ids = core_references(entity, "labels")
# Also walk the raw configuration; the built-in extraction misses
# references nested in some step types, like repeat sequences.
if raw_config := getattr(entity, "raw_config", None):
# Templates that look up a label that is not there are read too.
references = partial(async_raw_config_references, self.hass, kind="labels")
label_ids.update(references(raw_config))
# A disabled step does nothing, so what only it names is left out.
# Read the same way, or a template there still counts.
label_ids -= only_in_disabled_steps(raw_config, references)
return async_filter_known_label_ids(
self.hass,
label_ids=label_ids,
known_label_ids=self._known_label_ids,
)