Use HassKey in core components (a-c) (#126258)

* Use HassKey in conversation

* Use HassKey in assist_satellite

* automation

* More

* Unrelated

* Improve
This commit is contained in:
epenet 2024-09-20 12:07:15 +02:00 committed by GitHub
parent ef94fcf873
commit 8b44c16b57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 125 additions and 108 deletions

View file

@ -94,6 +94,7 @@ from homeassistant.helpers.trigger import (
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass
from homeassistant.util.dt import parse_datetime
from homeassistant.util.hass_dict import HassKey
from .config import AutomationConfig, ValidationStatus
from .const import (
@ -109,6 +110,7 @@ from .const import (
from .helpers import async_get_blueprints
from .trace import trace_automation
DOMAIN_DATA: HassKey[EntityComponent[BaseAutomationEntity]] = HassKey(DOMAIN)
ENTITY_ID_FORMAT = DOMAIN + ".{}"
@ -161,14 +163,12 @@ def _automations_with_x(
hass: HomeAssistant, referenced_id: str, property_name: str
) -> list[str]:
"""Return all automations that reference the x."""
if DOMAIN not in hass.data:
if DOMAIN_DATA not in hass.data:
return []
component: EntityComponent[BaseAutomationEntity] = hass.data[DOMAIN]
return [
automation_entity.entity_id
for automation_entity in component.entities
for automation_entity in hass.data[DOMAIN_DATA].entities
if referenced_id in getattr(automation_entity, property_name)
]
@ -177,12 +177,10 @@ def _x_in_automation(
hass: HomeAssistant, entity_id: str, property_name: str
) -> list[str]:
"""Return all x in an automation."""
if DOMAIN not in hass.data:
if DOMAIN_DATA not in hass.data:
return []
component: EntityComponent[BaseAutomationEntity] = hass.data[DOMAIN]
if (automation_entity := component.get_entity(entity_id)) is None:
if (automation_entity := hass.data[DOMAIN_DATA].get_entity(entity_id)) is None:
return []
return list(getattr(automation_entity, property_name))
@ -254,11 +252,9 @@ def automations_with_blueprint(hass: HomeAssistant, blueprint_path: str) -> list
if DOMAIN not in hass.data:
return []
component: EntityComponent[BaseAutomationEntity] = hass.data[DOMAIN]
return [
automation_entity.entity_id
for automation_entity in component.entities
for automation_entity in hass.data[DOMAIN_DATA].entities
if automation_entity.referenced_blueprint == blueprint_path
]
@ -266,12 +262,10 @@ def automations_with_blueprint(hass: HomeAssistant, blueprint_path: str) -> list
@callback
def blueprint_in_automation(hass: HomeAssistant, entity_id: str) -> str | None:
"""Return the blueprint the automation is based on or None."""
if DOMAIN not in hass.data:
if DOMAIN_DATA not in hass.data:
return None
component: EntityComponent[BaseAutomationEntity] = hass.data[DOMAIN]
if (automation_entity := component.get_entity(entity_id)) is None:
if (automation_entity := hass.data[DOMAIN_DATA].get_entity(entity_id)) is None:
return None
return automation_entity.referenced_blueprint
@ -279,7 +273,7 @@ def blueprint_in_automation(hass: HomeAssistant, entity_id: str) -> str | None:
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up all automations."""
hass.data[DOMAIN] = component = EntityComponent[BaseAutomationEntity](
hass.data[DOMAIN_DATA] = component = EntityComponent[BaseAutomationEntity](
LOGGER, DOMAIN, hass
)
@ -1210,9 +1204,7 @@ def websocket_config(
msg: dict[str, Any],
) -> None:
"""Get automation config."""
component: EntityComponent[BaseAutomationEntity] = hass.data[DOMAIN]
automation = component.get_entity(msg["entity_id"])
automation = hass.data[DOMAIN_DATA].get_entity(msg["entity_id"])
if automation is None:
connection.send_error(