Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -2,16 +2,19 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from homeassistant.components import automation
from homeassistant.helpers import area_registry as ar
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....util import async_filter_known_area_ids, async_get_all_area_ids

if TYPE_CHECKING:
from typing import Any


class SpookRepair(AbstractSpookRepair):
class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
"""Spook repair tries to find unknown referenced areas in automations."""

domain = automation.DOMAIN
Expand All @@ -22,44 +25,21 @@ class SpookRepair(AbstractSpookRepair):
}
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[automation.AutomationEntity] = self.hass.data[
DATA_INSTANCES
][self.domain]
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "areas"
edit_url_pattern = "/config/automation/edit/{unique_id}"

LOGGER.debug("Spook is inspecting: %s", self.repair)
_known_area_ids: set[str]

known_area_ids = async_get_all_area_ids(self.hass)
async def _async_setup_inspection(self) -> None:
"""Cache known area IDs for this inspection cycle."""
self._known_area_ids = async_get_all_area_ids(self.hass)

for entity in entity_component.entities:
self.possible_issue_ids.add(entity.entity_id)
if not isinstance(entity, automation.UnavailableAutomationEntity) and (
unknown_areas := async_filter_known_area_ids(
self.hass,
area_ids=entity.referenced_areas,
known_area_ids=known_area_ids,
)
):
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"areas": "\n".join(f"- `{area}`" for area in unknown_areas),
"automation": entity.name,
"edit": f"/config/automation/edit/{entity.unique_id}",
"entity_id": entity.entity_id,
},
)
LOGGER.debug(
(
"Spook found unknown areas in %s "
"and created an issue for it; Areas: %s",
),
entity.entity_id,
", ".join(unknown_areas),
)
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown area IDs referenced by ``entity``."""
return async_filter_known_area_ids(
self.hass,
area_ids=entity.referenced_areas,
known_area_ids=self._known_area_ids,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from homeassistant.components import automation
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....util import async_filter_known_device_ids, async_get_all_device_ids

if TYPE_CHECKING:
from typing import Any


class SpookRepair(AbstractSpookRepair):
class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
"""Spook repair tries to find unknown referenced devices in automations."""

domain = automation.DOMAIN
Expand All @@ -23,46 +26,21 @@ class SpookRepair(AbstractSpookRepair):
inspect_config_entry_changed = True
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[automation.AutomationEntity] = self.hass.data[
DATA_INSTANCES
][self.domain]
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "devices"
edit_url_pattern = "/config/automation/edit/{unique_id}"

LOGGER.debug("Spook is inspecting: %s", self.repair)
_known_device_ids: set[str]

known_device_ids = async_get_all_device_ids(self.hass)
async def _async_setup_inspection(self) -> None:
"""Cache known device IDs for this inspection cycle."""
self._known_device_ids = async_get_all_device_ids(self.hass)

for entity in entity_component.entities:
self.possible_issue_ids.add(entity.entity_id)
if not isinstance(entity, automation.UnavailableAutomationEntity) and (
unknown_devices := async_filter_known_device_ids(
self.hass,
device_ids=entity.referenced_devices,
known_device_ids=known_device_ids,
)
):
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"devices": "\n".join(
f"- `{device}`" for device in unknown_devices
),
"automation": entity.name,
"edit": f"/config/automation/edit/{entity.unique_id}",
"entity_id": entity.entity_id,
},
)
LOGGER.debug(
(
"Spook found unknown devices in %s "
"and created an issue for it; Areas: %s",
),
entity.entity_id,
", ".join(unknown_devices),
)
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown device IDs referenced by ``entity``."""
return async_filter_known_device_ids(
self.hass,
device_ids=entity.referenced_devices,
known_device_ids=self._known_device_ids,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
from homeassistant.components import automation
from homeassistant.const import EVENT_COMPONENT_LOADED
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....util import (
ENTITY_ID_PATTERN,
async_extract_entities_from_config,
Expand Down Expand Up @@ -259,7 +258,7 @@ async def extract_entities_from_value(hass: HomeAssistant, value: Any) -> set[st
return entities


class SpookRepair(AbstractSpookRepair):
class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
"""Spook repair tries to find unknown referenced entity in automations."""

domain = automation.DOMAIN
Expand All @@ -271,67 +270,42 @@ class SpookRepair(AbstractSpookRepair):
inspect_config_entry_changed = True
inspect_on_reload = True

automatically_clean_up_issues = True
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "entities"
edit_url_pattern = "/config/automation/edit/{unique_id}"

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

entity_component: EntityComponent[automation.AutomationEntity] = self.hass.data[
DATA_INSTANCES
][self.domain]

LOGGER.debug("Spook is inspecting: %s", self.repair)

known_entity_ids = async_get_all_entity_ids(self.hass, include_all_none=True)

for entity in entity_component.entities:
self.possible_issue_ids.add(entity.entity_id)
async def _async_setup_inspection(self) -> None:
"""Cache known entity IDs (including ALL/NONE) for this inspection cycle."""
self._known_entity_ids = async_get_all_entity_ids(
self.hass, include_all_none=True
)

# Skip disabled automations
if not entity.enabled:
continue
def _should_inspect_entity(self, entity: Any) -> bool:
"""Skip disabled automations."""
return entity.enabled

# Collect entities from multiple sources
all_entities = set(entity.referenced_entities)
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown entity IDs referenced by ``entity`` (incl. templates)."""
all_entities = set(entity.referenced_entities)

# Also extract entities directly from raw configuration if available
if hasattr(entity, "raw_config") and entity.raw_config:
config_entities = await extract_entities_from_automation_config(
# Also extract entities directly from raw configuration if available
if hasattr(entity, "raw_config") and entity.raw_config:
all_entities.update(
await extract_entities_from_automation_config(
self.hass, entity.raw_config
)
all_entities.update(config_entities)

# Extract entities from Template objects within the automation entity
template_entities = await extract_template_entities_from_automation_entity(
self.hass, entity
)
all_entities.update(template_entities)

if not isinstance(entity, automation.UnavailableAutomationEntity) and (
unknown_entities := await async_filter_known_entity_ids_with_templates(
self.hass,
entity_ids=all_entities,
known_entity_ids=known_entity_ids,
)
):
self.async_create_issue(
issue_id=entity.entity_id,
translation_placeholders={
"entities": "\n".join(
f"- `{entity_id}`" for entity_id in unknown_entities
),
"automation": entity.name,
"edit": f"/config/automation/edit/{entity.unique_id}",
"entity_id": entity.entity_id,
},
)
LOGGER.debug(
(
"Spook found unknown entities in %s "
"and created an issue for it; Entities: %s",
),
entity.entity_id,
", ".join(unknown_entities),
)
# Extract entities from Template objects within the automation entity
all_entities.update(
await extract_template_entities_from_automation_entity(self.hass, entity)
)

return await async_filter_known_entity_ids_with_templates(
self.hass,
entity_ids=all_entities,
known_entity_ids=self._known_entity_ids,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from homeassistant.components import automation
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 ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....util import async_filter_known_floor_ids, async_get_all_floor_ids

if TYPE_CHECKING:
from typing import Any


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

domain = automation.DOMAIN
Expand All @@ -21,44 +24,21 @@ class SpookRepair(AbstractSpookRepair):
}
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[automation.AutomationEntity] = self.hass.data[
DATA_INSTANCES
][self.domain]
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "floors"
edit_url_pattern = "/config/automation/edit/{unique_id}"

LOGGER.debug("Spook is inspecting: %s", self.repair)
_known_floor_ids: set[str]

known_floor_ids = async_get_all_floor_ids(self.hass)
async def _async_setup_inspection(self) -> None:
"""Cache known floor IDs for this inspection cycle."""
self._known_floor_ids = async_get_all_floor_ids(self.hass)

for entity in entity_component.entities:
self.possible_issue_ids.add(entity.entity_id)
if not isinstance(entity, automation.UnavailableAutomationEntity) and (
unknown_floors := async_filter_known_floor_ids(
self.hass,
floor_ids=entity.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),
"automation": entity.name,
"edit": f"/config/automation/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),
)
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown floor IDs referenced by ``entity``."""
return async_filter_known_floor_ids(
self.hass,
floor_ids=entity.referenced_floors,
known_floor_ids=self._known_floor_ids,
)
Loading
Loading