Avoid creating tasks to add entities when no entities are passed (#143647)

async_add_entities would return early if no entities
were passed but its a bit cleaner to not create the
task in the first place. I noticed in py-spy that
tplink was passing empty lists frequently
which made a task and than did nothing.
This commit is contained in:
J. Nick Koston
2025-04-24 22:16:20 -10:00
committed by GitHub
parent 5b503f21d7
commit fa0bb35e6c

View File

@@ -522,8 +522,14 @@ class EntityPlatform:
self, new_entities: Iterable[Entity], update_before_add: bool = False self, new_entities: Iterable[Entity], update_before_add: bool = False
) -> None: ) -> None:
"""Schedule adding entities for a single platform async.""" """Schedule adding entities for a single platform async."""
entities: list[Entity] = (
new_entities if type(new_entities) is list else list(new_entities)
)
# handle empty list from component/platform
if not entities:
return
task = self.hass.async_create_task_internal( task = self.hass.async_create_task_internal(
self.async_add_entities(new_entities, update_before_add=update_before_add), self.async_add_entities(entities, update_before_add=update_before_add),
f"EntityPlatform async_add_entities {self.domain}.{self.platform_name}", f"EntityPlatform async_add_entities {self.domain}.{self.platform_name}",
eager_start=True, eager_start=True,
) )
@@ -541,10 +547,16 @@ class EntityPlatform:
) -> None: ) -> None:
"""Schedule adding entities for a single platform async and track the task.""" """Schedule adding entities for a single platform async and track the task."""
assert self.config_entry assert self.config_entry
entities: list[Entity] = (
new_entities if type(new_entities) is list else list(new_entities)
)
# handle empty list from component/platform
if not entities:
return
task = self.config_entry.async_create_task( task = self.config_entry.async_create_task(
self.hass, self.hass,
self.async_add_entities( self.async_add_entities(
new_entities, entities,
update_before_add=update_before_add, update_before_add=update_before_add,
config_subentry_id=config_subentry_id, config_subentry_id=config_subentry_id,
), ),
@@ -686,10 +698,6 @@ class EntityPlatform:
entities: list[Entity] = ( entities: list[Entity] = (
new_entities if type(new_entities) is list else list(new_entities) new_entities if type(new_entities) is list else list(new_entities)
) )
# handle empty list from component/platform
if not entities:
return
timeout = max(SLOW_ADD_ENTITY_MAX_WAIT * len(entities), SLOW_ADD_MIN_TIMEOUT) timeout = max(SLOW_ADD_ENTITY_MAX_WAIT * len(entities), SLOW_ADD_MIN_TIMEOUT)
if update_before_add: if update_before_add:
await self._async_add_and_update_entities( await self._async_add_and_update_entities(