mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
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:
parent
f140c1a46d
commit
4d39a85553
@ -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(
|
||||
|
@ -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)
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user