429 files

This commit is contained in:
Home Assistant Version Control committed 2026-08-30 02:43:31 +00:00
1 parent 472c19bceb
commit 963cce81a7
566 files changed
+50311 -47616

No files matched your search

@@ -8,6 +8,7 @@ from homeassistant.components import automation
from homeassistant.helpers import area_registry as ar
from ....entity_filtering import async_filter_known_area_ids, async_get_all_area_ids
from ....reference_extraction import extract_targets_from_config
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
if TYPE_CHECKING:
@@ -38,8 +39,15 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown area IDs referenced by ``entity``."""
area_ids = set(entity.referenced_areas)
# 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):
area_ids.update(extract_targets_from_config(raw_config).area_ids)
return async_filter_known_area_ids(
self.hass,
area_ids=entity.referenced_areas,
area_ids=area_ids,
known_area_ids=self._known_area_ids,
)
@@ -8,7 +8,9 @@ from homeassistant.components import automation
from homeassistant.helpers import device_registry as dr
from ....entity_filtering import async_filter_known_device_ids, async_get_all_device_ids
from ....reference_extraction import extract_targets_from_config
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....template_extraction import extract_device_ids_from_config
def extract_event_data_device_ids_from_trigger_config(
@@ -72,6 +74,11 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
device_ids = set(entity.referenced_devices)
if hasattr(entity, "raw_config") and entity.raw_config:
# Also walk the raw configuration; the built-in extraction misses
# references nested in some step types, like repeat sequences.
# The walker never collects from event trigger payloads, so the
# subtraction below only affects built-in extraction results.
device_ids.update(extract_targets_from_config(entity.raw_config).device_ids)
device_ids.difference_update(
extract_event_data_device_ids_from_trigger_config(
entity.raw_config.get("trigger")
@@ -82,6 +89,8 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
entity.raw_config.get("triggers")
)
)
# Devices referenced via device_entities() in templates.
device_ids.update(extract_device_ids_from_config(entity.raw_config))
return async_filter_known_device_ids(
self.hass,
@@ -2,7 +2,6 @@
from __future__ import annotations
import re
from typing import TYPE_CHECKING, Any
from homeassistant.components import automation
@@ -10,16 +9,17 @@ from homeassistant.const import EVENT_COMPONENT_LOADED, EVENT_STATE_CHANGED
from homeassistant.core import Event, callback
from homeassistant.helpers import entity_registry as er
from ....const import LOGGER
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 ....action_extraction import (
async_extract_entities_from_action_config,
async_extract_entities_from_value,
)
from ....entity_filtering import async_get_all_entity_ids, async_get_all_services
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
from ....template_extraction import (
KNOWN_DOMAINS,
async_extract_entities_from_config,
async_filter_known_entity_ids_with_templates,
)
if TYPE_CHECKING:
from collections.abc import Mapping
@@ -28,7 +28,9 @@ if TYPE_CHECKING:
async def extract_template_entities_from_automation_entity(
hass: HomeAssistant, entity: Any
hass: HomeAssistant,
entity: Any,
known_services: set[str] | None = None,
) -> set[str]:
"""Extract entities from automation configuration using Template analysis.
@@ -43,44 +45,88 @@ async def extract_template_entities_from_automation_entity(
else:
return set()
return await async_extract_entities_from_config(hass, config)
return await async_extract_entities_from_config(hass, config, known_services)
async def extract_entities_from_automation_config(
hass: HomeAssistant, config: dict[str, Any]
hass: HomeAssistant,
config: dict[str, Any],
known_services: set[str] | None = None,
) -> set[str]:
"""Extract entity IDs from automation configuration."""
"""Extract entity IDs from automation configuration.
``known_services`` is built once per inspection and handed down, because
building it flattens every service Home Assistant has and the walk below
passes a lot of template strings. See
`action_extraction.async_extract_entities_from_action_config`.
"""
entities = set()
if not isinstance(config, dict):
return entities
if known_services is None:
known_services = async_get_all_services(hass)
# Extract entities from trigger config
for key in ("trigger", "triggers"):
if key in config:
entities.update(
await extract_entities_from_trigger_config(hass, config[key])
await extract_entities_from_trigger_config(
hass, config[key], known_services
)
)
# Extract entities from condition config
for key in ("condition", "conditions"):
if key in config:
entities.update(
await extract_entities_from_condition_config(hass, config[key])
await extract_entities_from_condition_config(
hass, config[key], known_services
)
)
# Extract entities from action config
for key in ("action", "actions"):
if key in config:
entities.update(
await extract_entities_from_action_config(hass, config[key])
await async_extract_entities_from_action_config(
hass, config[key], known_services=known_services
)
)
return entities
async def _entities_from_reference_fields(
hass: HomeAssistant,
config: dict[str, Any],
known_services: set[str],
) -> set[str]:
"""Extract entities from the config keys that name a reference.
``zone`` is in here because a zone trigger and a zone condition both name
one, and it is read exactly like the others.
"""
entities = set()
for key in ("entity_id", "device_id", "zone"):
if key in config:
entities.update(
await async_extract_entities_from_value(
hass, config[key], known_services=known_services
)
)
return entities
# Where an event trigger keeps what it matches on, rather than what it needs.
_EVENT_PAYLOAD_KEYS = frozenset({"event_data", "event_data_template"})
async def extract_entities_from_trigger_config(
hass: HomeAssistant, config: dict[str, Any] | list
hass: HomeAssistant,
config: dict[str, Any] | list,
known_services: set[str] | None = None,
) -> set[str]:
"""Extract entity IDs from trigger configuration."""
entities = set()
@@ -88,31 +134,66 @@ async def extract_entities_from_trigger_config(
if not config:
return entities
if known_services is None:
known_services = async_get_all_services(hass)
if isinstance(config, list):
for item in config:
entities.update(await extract_entities_from_trigger_config(hass, item))
entities.update(
await extract_entities_from_trigger_config(hass, item, known_services)
)
return entities
if not isinstance(config, dict):
return entities
# Entity ID fields in triggers
for key in ("entity_id", "device_id"):
if key in config:
entities.update(await extract_entities_from_value(hass, config[key]))
entities.update(await _entities_from_reference_fields(hass, config, known_services))
# Zone trigger has zone field
if "zone" in config:
entities.update(await extract_entities_from_value(hass, config["zone"]))
payload_keys = _payload_keys_to_leave_alone(config)
# Extract from nested configs
for value in config.values():
for key, value in config.items():
if key in payload_keys:
continue
if isinstance(value, (dict, list)):
entities.update(await extract_entities_from_trigger_config(hass, value))
entities.update(
await extract_entities_from_trigger_config(hass, value, known_services)
)
return entities
def _payload_keys_to_leave_alone(config: dict[str, Any]) -> frozenset[str]:
"""Return the event payload keys that hold data rather than references.
An `entity_id` inside `event_data` is a reference when the event comes
from an integration that means it that way, `timer.finished` being the
usual one. On somebody's own event it is whatever the sender put there,
and reporting that as a missing entity is a repair about an automation
that works perfectly well.
Told apart by the event type: one named after a domain comes from that
integration, anything else is somebody's own.
"""
event_types = config.get("event_type")
if event_types is None:
return frozenset()
if isinstance(event_types, str):
event_types = [event_types]
for event_type in event_types:
if not isinstance(event_type, str):
continue
domain, dot, _ = event_type.partition(".")
if dot and domain in KNOWN_DOMAINS:
return frozenset()
return _EVENT_PAYLOAD_KEYS
def extract_event_types_from_trigger_config(config: dict[str, Any] | list) -> set[str]:
"""Extract event types from trigger configuration."""
event_types = set()
@@ -142,7 +223,9 @@ def extract_event_types_from_trigger_config(config: dict[str, Any] | list) -> se
async def extract_entities_from_condition_config(
hass: HomeAssistant, config: dict[str, Any] | list
hass: HomeAssistant,
config: dict[str, Any] | list,
known_services: set[str] | None = None,
) -> set[str]:
"""Extract entity IDs from condition configuration."""
entities = set()
@@ -150,161 +233,29 @@ async def extract_entities_from_condition_config(
if not config:
return entities
if known_services is None:
known_services = async_get_all_services(hass)
if isinstance(config, list):
for item in config:
entities.update(await extract_entities_from_condition_config(hass, item))
entities.update(
await extract_entities_from_condition_config(hass, item, known_services)
)
return entities
if not isinstance(config, dict):
return entities
# Entity ID fields in conditions
for key in ("entity_id", "device_id", "zone"):
if key in config:
entities.update(await extract_entities_from_value(hass, config[key]))
entities.update(await _entities_from_reference_fields(hass, config, known_services))
# Extract from nested configs
for value in config.values():
if isinstance(value, (dict, list)):
entities.update(await extract_entities_from_condition_config(hass, value))
return entities
async def extract_entities_from_action_config(
hass: HomeAssistant, config: dict[str, Any] | list
) -> set[str]:
"""Extract entity IDs from action configuration."""
entities = set()
if not config:
return entities
if isinstance(config, list):
for item in config:
entities.update(await extract_entities_from_action_config(hass, item))
return entities
if not isinstance(config, dict):
return entities
# Extract entity IDs from direct fields
entities.update(await _extract_entities_from_action_fields(hass, config))
# Extract entities from target configuration
entities.update(await _extract_entities_from_target(hass, config))
# Extract entities from service data
entities.update(await _extract_entities_from_service_data(hass, config))
# Extract from nested configs (like if/then/else, repeat, etc.)
entities.update(await _extract_entities_from_nested_configs(hass, config))
return entities
async def _extract_entities_from_action_fields(
hass: HomeAssistant, config: dict[str, Any]
) -> set[str]:
"""Extract entities from direct action fields."""
entities = set()
for key in ("entity_id", "device_id"):
if key in config:
entities.update(await extract_entities_from_value(hass, config[key]))
return entities
async def _extract_entities_from_target(
hass: HomeAssistant, config: dict[str, Any]
) -> set[str]:
"""Extract entities from target configuration."""
entities = set()
if "target" in config and isinstance(config["target"], dict):
target = config["target"]
for key in ("entity_id", "device_id", "area_id", "label_id"):
if key in target:
entities.update(await extract_entities_from_value(hass, target[key]))
return entities
def _get_action_service(config: dict[str, Any]) -> str | None:
"""Return the service/action name configured for an action."""
service = config.get("service", config.get("action"))
return service if isinstance(service, str) else None
def _should_skip_service_data_value(
service: str | None,
key: str,
) -> bool:
"""Return if a service data value should not be scanned for entity IDs."""
return service is not None and service.startswith("notify.") and key == "target"
async def _extract_entities_from_service_data(
hass: HomeAssistant, config: dict[str, Any]
) -> set[str]:
"""Extract entities from service data."""
entities = set()
if "data" in config:
data_value = config["data"]
if isinstance(data_value, str):
# data field is a template string itself
entities.update(await extract_entities_from_value(hass, data_value))
elif isinstance(data_value, dict):
service = _get_action_service(config)
# data field is a dictionary, process all its values
for key, value in data_value.items():
if _should_skip_service_data_value(service, key):
continue
entities.update(await extract_entities_from_value(hass, value))
return entities
async def _extract_entities_from_nested_configs(
hass: HomeAssistant, config: dict[str, Any]
) -> set[str]:
"""Extract entities from nested configurations."""
entities = set()
for value in config.values():
if isinstance(value, (dict, list)):
entities.update(await extract_entities_from_action_config(hass, value))
return entities
async def extract_entities_from_value(hass: HomeAssistant, value: Any) -> set[str]:
"""Extract entity IDs from a configuration value."""
entities = set()
if isinstance(value, str):
# Check if it's a template string using util.is_template_string
if is_template_string(value):
# Process as template to extract entity references
try:
template_entities = await async_extract_entities_from_template_string(
hass, value
entities.update(
await extract_entities_from_condition_config(
hass, value, known_services
)
entities.update(template_entities)
# pylint: disable-next=broad-exception-caught
except Exception as exc: # noqa: BLE001 - Keep broad for unexpected template issues
LOGGER.debug(
"Failed to extract entities from template: %s, error: %s",
value,
exc,
)
elif re.match(rf"^{ENTITY_ID_PATTERN}$", value):
# Check if it matches the entity ID pattern with known domains
entities.add(value)
elif isinstance(value, list):
for item in value:
entities.update(await extract_entities_from_value(hass, item))
elif (
isinstance(value, dict)
and "entity" in value
and isinstance(value["entity"], str)
):
# Handle entity dict format like {"entity": "light.living_room"}
entities.add(value["entity"])
)
return entities
@@ -324,9 +275,11 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
unavailable_entity_class = automation.UnavailableAutomationEntity
entity_label = "automation"
reference_label = "entities"
references_are_entities = True
edit_url_pattern = "/config/automation/edit/{unique_id}"
_known_entity_ids: set[str]
_known_services: set[str]
async def async_activate(self) -> None:
"""Activate the repair."""
@@ -354,10 +307,16 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
)
async def _async_setup_inspection(self) -> None:
"""Cache known entity IDs (including ALL/NONE) for this inspection cycle."""
"""Cache what every automation in this cycle needs looked up.
The service set is in here for the same reason as the entity ids:
building it flattens every service Home Assistant has, and it is the
same answer for every automation in one pass.
"""
self._known_entity_ids = async_get_all_entity_ids(
self.hass, include_all_none=True
)
self._known_services = async_get_all_services(self.hass)
def _should_inspect_entity(self, entity: Any) -> bool:
"""Skip disabled automations."""
@@ -371,7 +330,7 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
if hasattr(entity, "raw_config") and entity.raw_config:
all_entities.update(
await extract_entities_from_automation_config(
self.hass, entity.raw_config
self.hass, entity.raw_config, self._known_services
)
)
for key in ("trigger", "triggers"):
@@ -381,7 +340,9 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
# Extract entities from Template objects within the automation entity
all_entities.update(
await extract_template_entities_from_automation_entity(self.hass, entity)
await extract_template_entities_from_automation_entity(
self.hass, entity, self._known_services
)
)
return await async_filter_known_entity_ids_with_templates(
@@ -8,6 +8,7 @@ from homeassistant.components import automation
from homeassistant.helpers import floor_registry as fr
from ....entity_filtering import async_filter_known_floor_ids, async_get_all_floor_ids
from ....reference_extraction import extract_targets_from_config
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
if TYPE_CHECKING:
@@ -37,8 +38,15 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown floor IDs referenced by ``entity``."""
floor_ids = set(entity.referenced_floors)
# 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):
floor_ids.update(extract_targets_from_config(raw_config).floor_ids)
return async_filter_known_floor_ids(
self.hass,
floor_ids=entity.referenced_floors,
floor_ids=floor_ids,
known_floor_ids=self._known_floor_ids,
)
@@ -8,6 +8,7 @@ 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 extract_targets_from_config
from ....repairs import AbstractSpookEntityComponentUnknownReferencesRepair
if TYPE_CHECKING:
@@ -37,8 +38,15 @@ class SpookRepair(AbstractSpookEntityComponentUnknownReferencesRepair):
async def _async_compute_unknown_references(self, entity: Any) -> set[str]:
"""Return unknown label IDs referenced by ``entity``."""
label_ids = set(entity.referenced_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):
label_ids.update(extract_targets_from_config(raw_config).label_ids)
return async_filter_known_label_ids(
self.hass,
label_ids=entity.referenced_labels,
label_ids=label_ids,
known_label_ids=self._known_label_ids,
)