From 40774101d39e1f693f92a8bbed5ed1d52e6d19bc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 23 Feb 2024 12:47:43 -1000 Subject: [PATCH] Avoid creating task per device when adding legacy device trackers (#111220) --- .../components/device_tracker/legacy.py | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index a17972526cf..e1a8058d819 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -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):