Avoid creating task per device when adding legacy device trackers (#111220)
This commit is contained in:
parent
5f8ef37f2d
commit
40774101d3
1 changed files with 9 additions and 13 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Reference in a new issue