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 <Task pending name='config entry forward setup Philips Hue 2 hue fcb64edfc5ac2edbb656607d5193b583 light' coro=<ConfigEntries.async_forward_entry_setup() running at /usr/src/homeassistant/homeassistant/config_entries.py:1597> wait_for=<Future pending cb=[shield.<locals>._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.<locals>._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
```
This commit is contained in:
J. Nick Koston 2024-02-13 06:04:29 -06:00 committed by GitHub
parent f140c1a46d
commit 4d39a85553
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 11 deletions

View File

@ -1,6 +1,7 @@
"""Support for Hue binary sensors.""" """Support for Hue binary sensors."""
from __future__ import annotations from __future__ import annotations
from functools import partial
from typing import TypeAlias from typing import TypeAlias
from aiohue.v2 import HueBridgeV2 from aiohue.v2 import HueBridgeV2
@ -58,14 +59,15 @@ async def async_setup_entry(
@callback @callback
def register_items(controller: ControllerType, sensor_class: SensorType): def register_items(controller: ControllerType, sensor_class: SensorType):
make_binary_sensor_entity = partial(sensor_class, bridge, controller)
@callback @callback
def async_add_sensor(event_type: EventType, resource: SensorType) -> None: def async_add_sensor(event_type: EventType, resource: SensorType) -> None:
"""Add Hue Binary Sensor.""" """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 # add all current items in controller
for sensor in controller: async_add_entities(make_binary_sensor_entity(sensor) for sensor in controller)
async_add_sensor(EventType.RESOURCE_ADDED, sensor)
# register listener for new sensors # register listener for new sensors
config_entry.async_on_unload( config_entry.async_on_unload(

View File

@ -1,6 +1,7 @@
"""Support for Hue lights.""" """Support for Hue lights."""
from __future__ import annotations from __future__ import annotations
from functools import partial
from typing import Any from typing import Any
from aiohue import HueBridgeV2 from aiohue import HueBridgeV2
@ -51,17 +52,15 @@ async def async_setup_entry(
bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id] bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
api: HueBridgeV2 = bridge.api api: HueBridgeV2 = bridge.api
controller: LightsController = api.lights controller: LightsController = api.lights
make_light_entity = partial(HueLight, bridge, controller)
@callback @callback
def async_add_light(event_type: EventType, resource: Light) -> None: def async_add_light(event_type: EventType, resource: Light) -> None:
"""Add Hue Light.""" """Add Hue Light."""
light = HueLight(bridge, controller, resource) async_add_entities([make_light_entity(resource)])
async_add_entities([light])
# add all current items in controller # add all current items in controller
for light in controller: async_add_entities(make_light_entity(light) for light in controller)
async_add_light(EventType.RESOURCE_ADDED, resource=light)
# register listener for new lights # register listener for new lights
config_entry.async_on_unload( config_entry.async_on_unload(
controller.subscribe(async_add_light, event_filter=EventType.RESOURCE_ADDED) controller.subscribe(async_add_light, event_filter=EventType.RESOURCE_ADDED)

View File

@ -1,6 +1,7 @@
"""Support for Hue sensors.""" """Support for Hue sensors."""
from __future__ import annotations from __future__ import annotations
from functools import partial
from typing import Any, TypeAlias from typing import Any, TypeAlias
from aiohue.v2 import HueBridgeV2 from aiohue.v2 import HueBridgeV2
@ -53,14 +54,15 @@ async def async_setup_entry(
@callback @callback
def register_items(controller: ControllerType, sensor_class: SensorType): def register_items(controller: ControllerType, sensor_class: SensorType):
make_sensor_entity = partial(sensor_class, bridge, controller)
@callback @callback
def async_add_sensor(event_type: EventType, resource: SensorType) -> None: def async_add_sensor(event_type: EventType, resource: SensorType) -> None:
"""Add Hue Sensor.""" """Add Hue Sensor."""
async_add_entities([sensor_class(bridge, controller, resource)]) async_add_entities([make_sensor_entity(resource)])
# add all current items in controller # add all current items in controller
for sensor in controller: async_add_entities(make_sensor_entity(sensor) for sensor in controller)
async_add_sensor(EventType.RESOURCE_ADDED, sensor)
# register listener for new sensors # register listener for new sensors
config_entry.async_on_unload( config_entry.async_on_unload(