From 4d39a85553d2fd913c9d343680a6bfd970435da0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 Feb 2024 06:04:29 -0600 Subject: [PATCH] Avoid creating many tasks when adding hue v2 entities (#110386) Each entity creation would call async_add_entities which spawned a seperate task fixes ``` 2024-02-12 18:06:09.819 WARNING (MainThread) [asyncio] Executing wait_for=._outer_done_callback() at /usr/local/lib/python3.12/asyncio/tasks.py:922, Task.task_wakeup()] created at /usr/local/lib/python3.12/asyncio/base_events.py:447> cb=[gather.._done_callback() at /usr/local/lib/python3.12/asyncio/tasks.py:767] created at /usr/local/lib/python3.12/asyncio/tasks.py:420> took 1.260 seconds ``` --- homeassistant/components/hue/v2/binary_sensor.py | 8 +++++--- homeassistant/components/hue/v2/light.py | 9 ++++----- homeassistant/components/hue/v2/sensor.py | 8 +++++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/hue/v2/binary_sensor.py b/homeassistant/components/hue/v2/binary_sensor.py index f1bcd0bbbe3..4707302d288 100644 --- a/homeassistant/components/hue/v2/binary_sensor.py +++ b/homeassistant/components/hue/v2/binary_sensor.py @@ -1,6 +1,7 @@ """Support for Hue binary sensors.""" from __future__ import annotations +from functools import partial from typing import TypeAlias from aiohue.v2 import HueBridgeV2 @@ -58,14 +59,15 @@ async def async_setup_entry( @callback def register_items(controller: ControllerType, sensor_class: SensorType): + make_binary_sensor_entity = partial(sensor_class, bridge, controller) + @callback def async_add_sensor(event_type: EventType, resource: SensorType) -> None: """Add Hue Binary Sensor.""" - async_add_entities([sensor_class(bridge, controller, resource)]) + async_add_entities([make_binary_sensor_entity(resource)]) # add all current items in controller - for sensor in controller: - async_add_sensor(EventType.RESOURCE_ADDED, sensor) + async_add_entities(make_binary_sensor_entity(sensor) for sensor in controller) # register listener for new sensors config_entry.async_on_unload( diff --git a/homeassistant/components/hue/v2/light.py b/homeassistant/components/hue/v2/light.py index 81993c7627f..bbf5dc9c19f 100644 --- a/homeassistant/components/hue/v2/light.py +++ b/homeassistant/components/hue/v2/light.py @@ -1,6 +1,7 @@ """Support for Hue lights.""" from __future__ import annotations +from functools import partial from typing import Any from aiohue import HueBridgeV2 @@ -51,17 +52,15 @@ async def async_setup_entry( bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id] api: HueBridgeV2 = bridge.api controller: LightsController = api.lights + make_light_entity = partial(HueLight, bridge, controller) @callback def async_add_light(event_type: EventType, resource: Light) -> None: """Add Hue Light.""" - light = HueLight(bridge, controller, resource) - async_add_entities([light]) + async_add_entities([make_light_entity(resource)]) # add all current items in controller - for light in controller: - async_add_light(EventType.RESOURCE_ADDED, resource=light) - + async_add_entities(make_light_entity(light) for light in controller) # register listener for new lights config_entry.async_on_unload( controller.subscribe(async_add_light, event_filter=EventType.RESOURCE_ADDED) diff --git a/homeassistant/components/hue/v2/sensor.py b/homeassistant/components/hue/v2/sensor.py index 56f708e2dfd..59dc8de2975 100644 --- a/homeassistant/components/hue/v2/sensor.py +++ b/homeassistant/components/hue/v2/sensor.py @@ -1,6 +1,7 @@ """Support for Hue sensors.""" from __future__ import annotations +from functools import partial from typing import Any, TypeAlias from aiohue.v2 import HueBridgeV2 @@ -53,14 +54,15 @@ async def async_setup_entry( @callback def register_items(controller: ControllerType, sensor_class: SensorType): + make_sensor_entity = partial(sensor_class, bridge, controller) + @callback def async_add_sensor(event_type: EventType, resource: SensorType) -> None: """Add Hue Sensor.""" - async_add_entities([sensor_class(bridge, controller, resource)]) + async_add_entities([make_sensor_entity(resource)]) # add all current items in controller - for sensor in controller: - async_add_sensor(EventType.RESOURCE_ADDED, sensor) + async_add_entities(make_sensor_entity(sensor) for sensor in controller) # register listener for new sensors config_entry.async_on_unload(