Avoid creating task per device when adding legacy device trackers (#111220)

This commit is contained in:
J. Nick Koston 2024-02-23 12:47:43 -10:00 committed by GitHub
parent 5f8ef37f2d
commit 40774101d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -714,21 +714,17 @@ class DeviceTracker:
This method is a coroutine.
"""
async def async_init_single_device(dev: Device) -> None:
"""Init a single device_tracker entity."""
await dev.async_added_to_hass()
dev.async_write_ha_state()
tasks: list[asyncio.Task] = []
for device in self.devices.values():
if device.track and not device.last_seen:
tasks.append(
self.hass.async_create_task(async_init_single_device(device))
)
if tasks:
await asyncio.wait(tasks)
# async_added_to_hass is unlikely to suspend so
# do not gather here to avoid unnecessary overhead
# of creating a task per device.
#
# We used to have the overhead of potentially loading
# restore state for each device here, but RestoreState
# is always loaded ahead of time now.
await device.async_added_to_hass()
device.async_write_ha_state()
class Device(RestoreEntity):