mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Small cleanups to zone to reduce startup time (#114587)
This commit is contained in:
parent
886f03dd71
commit
ae640b6e1a
@ -315,7 +315,9 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
await storage_collection.async_create_item(data)
|
await storage_collection.async_create_item(data)
|
||||||
|
|
||||||
hass.async_create_task(hass.config_entries.async_remove(config_entry.entry_id))
|
hass.async_create_task(
|
||||||
|
hass.config_entries.async_remove(config_entry.entry_id), eager_start=True
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -331,6 +333,7 @@ class Zone(collection.CollectionEntity):
|
|||||||
"""Representation of a Zone."""
|
"""Representation of a Zone."""
|
||||||
|
|
||||||
editable: bool
|
editable: bool
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, config: ConfigType) -> None:
|
def __init__(self, config: ConfigType) -> None:
|
||||||
"""Initialize the zone."""
|
"""Initialize the zone."""
|
||||||
@ -339,6 +342,16 @@ class Zone(collection.CollectionEntity):
|
|||||||
self._attrs: dict | None = None
|
self._attrs: dict | None = None
|
||||||
self._remove_listener: Callable[[], None] | None = None
|
self._remove_listener: Callable[[], None] | None = None
|
||||||
self._persons_in_zone: set[str] = set()
|
self._persons_in_zone: set[str] = set()
|
||||||
|
self._set_attrs_from_config()
|
||||||
|
|
||||||
|
def _set_attrs_from_config(self) -> None:
|
||||||
|
"""Set the attributes from the config."""
|
||||||
|
config = self._config
|
||||||
|
name: str = config[CONF_NAME]
|
||||||
|
self._attr_name = name
|
||||||
|
self._case_folded_name = name.casefold()
|
||||||
|
self._attr_unique_id = config.get(CONF_ID)
|
||||||
|
self._attr_icon = config.get(CONF_ICON)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_storage(cls, config: ConfigType) -> Self:
|
def from_storage(cls, config: ConfigType) -> Self:
|
||||||
@ -361,31 +374,12 @@ class Zone(collection.CollectionEntity):
|
|||||||
"""Return the state property really does nothing for a zone."""
|
"""Return the state property really does nothing for a zone."""
|
||||||
return len(self._persons_in_zone)
|
return len(self._persons_in_zone)
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""Return name."""
|
|
||||||
return cast(str, self._config[CONF_NAME])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str | None:
|
|
||||||
"""Return unique ID."""
|
|
||||||
return self._config.get(CONF_ID)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str | None:
|
|
||||||
"""Return the icon if any."""
|
|
||||||
return self._config.get(CONF_ICON)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""Zone does not poll."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_update_config(self, config: ConfigType) -> None:
|
async def async_update_config(self, config: ConfigType) -> None:
|
||||||
"""Handle when the config is updated."""
|
"""Handle when the config is updated."""
|
||||||
if self._config == config:
|
if self._config == config:
|
||||||
return
|
return
|
||||||
self._config = config
|
self._config = config
|
||||||
|
self._set_attrs_from_config()
|
||||||
self._generate_attrs()
|
self._generate_attrs()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@ -394,13 +388,14 @@ class Zone(collection.CollectionEntity):
|
|||||||
self, evt: Event[event.EventStateChangedData]
|
self, evt: Event[event.EventStateChangedData]
|
||||||
) -> None:
|
) -> None:
|
||||||
person_entity_id = evt.data["entity_id"]
|
person_entity_id = evt.data["entity_id"]
|
||||||
cur_count = len(self._persons_in_zone)
|
persons_in_zone = self._persons_in_zone
|
||||||
|
cur_count = len(persons_in_zone)
|
||||||
if self._state_is_in_zone(evt.data["new_state"]):
|
if self._state_is_in_zone(evt.data["new_state"]):
|
||||||
self._persons_in_zone.add(person_entity_id)
|
persons_in_zone.add(person_entity_id)
|
||||||
elif person_entity_id in self._persons_in_zone:
|
elif person_entity_id in persons_in_zone:
|
||||||
self._persons_in_zone.remove(person_entity_id)
|
persons_in_zone.remove(person_entity_id)
|
||||||
|
|
||||||
if len(self._persons_in_zone) != cur_count:
|
if len(persons_in_zone) != cur_count:
|
||||||
self._generate_attrs()
|
self._generate_attrs()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@ -408,10 +403,11 @@ class Zone(collection.CollectionEntity):
|
|||||||
"""Run when entity about to be added to hass."""
|
"""Run when entity about to be added to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
person_domain = "person" # avoid circular import
|
person_domain = "person" # avoid circular import
|
||||||
persons = self.hass.states.async_entity_ids(person_domain)
|
self._persons_in_zone = {
|
||||||
for person in persons:
|
state.entity_id
|
||||||
if self._state_is_in_zone(self.hass.states.get(person)):
|
for state in self.hass.states.async_all(person_domain)
|
||||||
self._persons_in_zone.add(person)
|
if self._state_is_in_zone(state)
|
||||||
|
}
|
||||||
self._generate_attrs()
|
self._generate_attrs()
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
@ -446,7 +442,7 @@ class Zone(collection.CollectionEntity):
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
and (
|
and (
|
||||||
state.state.casefold() == self.name.casefold()
|
state.state.casefold() == self._case_folded_name
|
||||||
or (state.state == STATE_HOME and self.entity_id == ENTITY_ID_HOME)
|
or (state.state == STATE_HOME and self.entity_id == ENTITY_ID_HOME)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user