Hue aggregated control for grouped lights (#68566)

This commit is contained in:
Marcel van der Veldt 2022-03-23 21:06:10 +01:00 committed by GitHub
parent d2dc9e6cbe
commit ccd8c7d5f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 93 deletions

View File

@ -1,7 +1,6 @@
"""Support for Hue groups (room/zone).""" """Support for Hue groups (room/zone)."""
from __future__ import annotations from __future__ import annotations
import asyncio
from typing import Any from typing import Any
from aiohue.v2 import HueBridgeV2 from aiohue.v2 import HueBridgeV2
@ -146,7 +145,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
} }
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on.""" """Turn the grouped_light on."""
transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION))
xy_color = kwargs.get(ATTR_XY_COLOR) xy_color = kwargs.get(ATTR_XY_COLOR)
color_temp = normalize_hue_colortemp(kwargs.get(ATTR_COLOR_TEMP)) color_temp = normalize_hue_colortemp(kwargs.get(ATTR_COLOR_TEMP))
@ -155,38 +154,16 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
if flash is not None: if flash is not None:
await self.async_set_flash(flash) await self.async_set_flash(flash)
# flash can not be sent with other commands at the same time
return return
# NOTE: a grouped_light can only handle turn on/off await self.bridge.async_request_call(
# To set other features, you'll have to control the attached lights self.controller.set_state,
if ( id=self.resource.id,
brightness is None on=True,
and xy_color is None brightness=brightness,
and color_temp is None color_xy=xy_color,
and transition is None color_temp=color_temp,
and flash is None transition_time=transition,
):
await self.bridge.async_request_call(
self.controller.set_state, id=self.resource.id, on=True
)
return
# redirect all other feature commands to underlying lights
# note that this silently ignores params sent to light that are not supported
await asyncio.gather(
*[
self.bridge.async_request_call(
self.api.lights.set_state,
light.id,
on=True,
brightness=brightness if light.supports_dimming else None,
color_xy=xy_color if light.supports_color else None,
color_temp=color_temp if light.supports_color_temperature else None,
transition_time=transition,
)
for light in self.controller.get_lights(self.resource.id)
]
) )
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -199,38 +176,19 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
# flash can not be sent with other commands at the same time # flash can not be sent with other commands at the same time
return return
# NOTE: a grouped_light can only handle turn on/off await self.bridge.async_request_call(
# To set other features, you'll have to control the attached lights self.controller.set_state,
if transition is None: id=self.resource.id,
await self.bridge.async_request_call( on=False,
self.controller.set_state, id=self.resource.id, on=False transition_time=transition,
)
return
# redirect all other feature commands to underlying lights
await asyncio.gather(
*[
self.bridge.async_request_call(
self.api.lights.set_state,
light.id,
on=False,
transition_time=transition,
)
for light in self.controller.get_lights(self.resource.id)
]
) )
async def async_set_flash(self, flash: str) -> None: async def async_set_flash(self, flash: str) -> None:
"""Send flash command to light.""" """Send flash command to light."""
await asyncio.gather( await self.bridge.async_request_call(
*[ self.controller.set_flash,
self.bridge.async_request_call( id=self.resource.id,
self.api.lights.set_flash, short=flash == FLASH_SHORT,
id=light.id,
short=flash == FLASH_SHORT,
)
for light in self.controller.get_lights(self.resource.id)
]
) )
@callback @callback

View File

@ -372,31 +372,24 @@ async def test_grouped_lights(hass, mock_bridge_v2, v2_resources_test_data):
blocking=True, blocking=True,
) )
# PUT request should have been sent to ALL group lights with correct params # PUT request should have been sent to group_light with correct params
assert len(mock_bridge_v2.mock_requests) == 3 assert len(mock_bridge_v2.mock_requests) == 1
for index in range(0, 3): assert mock_bridge_v2.mock_requests[0]["json"]["on"]["on"] is True
assert mock_bridge_v2.mock_requests[index]["json"]["on"]["on"] is True assert mock_bridge_v2.mock_requests[0]["json"]["dimming"]["brightness"] == 100
assert ( assert mock_bridge_v2.mock_requests[0]["json"]["color"]["xy"]["x"] == 0.123
mock_bridge_v2.mock_requests[index]["json"]["dimming"]["brightness"] == 100 assert mock_bridge_v2.mock_requests[0]["json"]["color"]["xy"]["y"] == 0.123
) assert mock_bridge_v2.mock_requests[0]["json"]["dynamics"]["duration"] == 200
assert mock_bridge_v2.mock_requests[index]["json"]["color"]["xy"]["x"] == 0.123
assert mock_bridge_v2.mock_requests[index]["json"]["color"]["xy"]["y"] == 0.123
assert (
mock_bridge_v2.mock_requests[index]["json"]["dynamics"]["duration"] == 200
)
# Now generate update events by emitting the json we've sent as incoming events # Now generate update events by emitting the json we've sent as incoming events
for index, light_id in enumerate( for light_id in [
[ "02cba059-9c2c-4d45-97e4-4f79b1bfbaa1",
"02cba059-9c2c-4d45-97e4-4f79b1bfbaa1", "b3fe71ef-d0ef-48de-9355-d9e604377df0",
"b3fe71ef-d0ef-48de-9355-d9e604377df0", "8015b17f-8336-415b-966a-b364bd082397",
"8015b17f-8336-415b-966a-b364bd082397", ]:
]
):
event = { event = {
"id": light_id, "id": light_id,
"type": "light", "type": "light",
**mock_bridge_v2.mock_requests[index]["json"], **mock_bridge_v2.mock_requests[0]["json"],
} }
mock_bridge_v2.api.emit_event("update", event) mock_bridge_v2.api.emit_event("update", event)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -452,13 +445,10 @@ async def test_grouped_lights(hass, mock_bridge_v2, v2_resources_test_data):
blocking=True, blocking=True,
) )
# PUT request should have been sent to ALL group lights with correct params # PUT request should have been sent to group_light with correct params
assert len(mock_bridge_v2.mock_requests) == 3 assert len(mock_bridge_v2.mock_requests) == 1
for index in range(0, 3): assert mock_bridge_v2.mock_requests[0]["json"]["on"]["on"] is False
assert mock_bridge_v2.mock_requests[index]["json"]["on"]["on"] is False assert mock_bridge_v2.mock_requests[0]["json"]["dynamics"]["duration"] == 200
assert (
mock_bridge_v2.mock_requests[index]["json"]["dynamics"]["duration"] == 200
)
# Test sending short flash effect to a grouped light # Test sending short flash effect to a grouped light
mock_bridge_v2.mock_requests.clear() mock_bridge_v2.mock_requests.clear()
@ -494,12 +484,9 @@ async def test_grouped_lights(hass, mock_bridge_v2, v2_resources_test_data):
blocking=True, blocking=True,
) )
# PUT request should have been sent to ALL group lights with correct params # PUT request should have been sent to grouped_light with correct params
assert len(mock_bridge_v2.mock_requests) == 3 assert len(mock_bridge_v2.mock_requests) == 1
for index in range(0, 3): assert mock_bridge_v2.mock_requests[0]["json"]["alert"]["action"] == "breathe"
assert (
mock_bridge_v2.mock_requests[index]["json"]["alert"]["action"] == "breathe"
)
# Test sending flash effect in turn_off call # Test sending flash effect in turn_off call
mock_bridge_v2.mock_requests.clear() mock_bridge_v2.mock_requests.clear()