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(