Use HassKey for zone data (#143323)

This commit is contained in:
Marc Mueller 2025-04-22 12:18:21 +02:00 committed by GitHub
parent a3605921c9
commit fa9af6a021
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from collections.abc import Callable, Iterable
from collections.abc import Callable
import logging
from operator import attrgetter
import sys
@ -47,6 +47,7 @@ from homeassistant.helpers import (
)
from homeassistant.helpers.typing import ConfigType, VolDictType
from homeassistant.loader import bind_hass
from homeassistant.util.hass_dict import HassKey
from homeassistant.util.location import distance
from .const import ATTR_PASSIVE, ATTR_RADIUS, CONF_PASSIVE, DOMAIN, HOME_ZONE
@ -108,6 +109,9 @@ ENTITY_ID_SORTER = attrgetter("entity_id")
ZONE_ENTITY_IDS = "zone_entity_ids"
DATA_ZONE_STORAGE_COLLECTION: HassKey[ZoneStorageCollection] = HassKey(DOMAIN)
DATA_ZONE_ENTITY_IDS: HassKey[list[str]] = HassKey(ZONE_ENTITY_IDS)
@bind_hass
def async_active_zone(
@ -122,7 +126,7 @@ def async_active_zone(
closest: State | None = None
# This can be called before async_setup by device tracker
zone_entity_ids: Iterable[str] = hass.data.get(ZONE_ENTITY_IDS, ())
zone_entity_ids = hass.data.get(DATA_ZONE_ENTITY_IDS, ())
for entity_id in zone_entity_ids:
if (
@ -168,8 +172,8 @@ def async_active_zone(
@callback
def async_setup_track_zone_entity_ids(hass: HomeAssistant) -> None:
"""Set up track of entity IDs for zones."""
zone_entity_ids: list[str] = hass.states.async_entity_ids(DOMAIN)
hass.data[ZONE_ENTITY_IDS] = zone_entity_ids
zone_entity_ids = hass.states.async_entity_ids(DOMAIN)
hass.data[DATA_ZONE_ENTITY_IDS] = zone_entity_ids
@callback
def _async_add_zone_entity_id(
@ -290,7 +294,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, core_config_updated)
hass.data[DOMAIN] = storage_collection
hass.data[DATA_ZONE_STORAGE_COLLECTION] = storage_collection
return True
@ -312,13 +316,11 @@ async def async_setup_entry(
hass: HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
"""Set up zone as config entry."""
storage_collection = cast(ZoneStorageCollection, hass.data[DOMAIN])
data = dict(config_entry.data)
data.setdefault(CONF_PASSIVE, DEFAULT_PASSIVE)
data.setdefault(CONF_RADIUS, DEFAULT_RADIUS)
await storage_collection.async_create_item(data)
await hass.data[DATA_ZONE_STORAGE_COLLECTION].async_create_item(data)
hass.async_create_task(
hass.config_entries.async_remove(config_entry.entry_id), eager_start=True