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. 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(): for device in self.devices.values():
if device.track and not device.last_seen: if device.track and not device.last_seen:
tasks.append( # async_added_to_hass is unlikely to suspend so
self.hass.async_create_task(async_init_single_device(device)) # do not gather here to avoid unnecessary overhead
) # of creating a task per device.
#
if tasks: # We used to have the overhead of potentially loading
await asyncio.wait(tasks) # 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): class Device(RestoreEntity):