Index entities by domain for entity services (#106759)

This commit is contained in:
J. Nick Koston 2024-01-02 04:28:58 -10:00 committed by GitHub
parent bf0d891f68
commit 09b65f14b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 60 deletions

View file

@ -55,6 +55,7 @@ SLOW_ADD_MIN_TIMEOUT = 500
PLATFORM_NOT_READY_RETRIES = 10
DATA_ENTITY_PLATFORM = "entity_platform"
DATA_DOMAIN_ENTITIES = "domain_entities"
PLATFORM_NOT_READY_BASE_WAIT_TIME = 30 # seconds
_LOGGER = getLogger(__name__)
@ -147,6 +148,10 @@ class EntityPlatform:
self.platform_name, []
).append(self)
self.domain_entities: dict[str, Entity] = hass.data.setdefault(
DATA_DOMAIN_ENTITIES, {}
).setdefault(domain, {})
def __repr__(self) -> str:
"""Represent an EntityPlatform."""
return (
@ -734,6 +739,7 @@ class EntityPlatform:
entity_id = entity.entity_id
self.entities[entity_id] = entity
self.domain_entities[entity_id] = entity
if not restored:
# Reserve the state in the state machine
@ -746,6 +752,7 @@ class EntityPlatform:
def remove_entity_cb() -> None:
"""Remove entity from entities dict."""
self.entities.pop(entity_id)
self.domain_entities.pop(entity_id)
entity.async_on_remove(remove_entity_cb)
@ -830,11 +837,7 @@ class EntityPlatform:
"""Handle the service."""
return await service.entity_service_call(
self.hass,
[
plf
for plf in self.hass.data[DATA_ENTITY_PLATFORM][self.platform_name]
if plf.domain == self.domain
],
self.domain_entities,
func,
call,
required_features,