Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 3 additions & 6 deletions custom_components/spook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
from homeassistant.helpers import issue_registry as ir

from .const import DOMAIN, LOGGER, PLATFORMS
from .entity_filtering import async_setup_all_entity_ids_cache_invalidation
from .integration_linking import link_sub_integrations, unlink_sub_integrations
from .repairs import SpookRepairManager
from .services import SpookServiceManager
from .util import (
async_forward_setup_entry,
async_setup_all_entity_ids_cache_invalidation,
link_sub_integrations,
unlink_sub_integrations,
)
from .setup_helpers import async_forward_setup_entry

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.const import Platform

from .util import async_forward_platform_entry_setups_to_ectoplasm
from .setup_helpers import async_forward_platform_entry_setups_to_ectoplasm

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.const import Platform

from .util import async_forward_platform_entry_setups_to_ectoplasm
from .setup_helpers import async_forward_platform_entry_setups_to_ectoplasm

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
Expand Down
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 ....util import async_filter_known_area_ids, async_get_all_area_ids
from ....entity_filtering import async_filter_known_area_ids, async_get_all_area_ids
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair

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 ....util import async_filter_known_device_ids, async_get_all_device_ids
from ....entity_filtering import async_filter_known_device_ids, async_get_all_device_ids
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair

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,18 +8,17 @@
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 ....util import (
from ....entity_filtering import (
ENTITY_ID_PATTERN,
async_extract_entities_from_config,
async_extract_entities_from_template_string,
async_filter_known_entity_ids_with_templates,
async_get_all_entity_ids,
is_template_string,
)
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
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,
)
Loading
Loading