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)."""
from __future__ import annotations
import asyncio
from typing import Any
from aiohue.v2 import HueBridgeV2
@ -146,7 +145,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
}
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))
xy_color = kwargs.get(ATTR_XY_COLOR)
color_temp = normalize_hue_colortemp(kwargs.get(ATTR_COLOR_TEMP))
@ -155,38 +154,16 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
if flash is not None:
await self.async_set_flash(flash)
# flash can not be sent with other commands at the same time
return
# NOTE: a grouped_light can only handle turn on/off
# To set other features, you'll have to control the attached lights
if (
brightness is None
and xy_color is None
and color_temp is None
and transition is None
and flash is None
):
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)
]
await self.bridge.async_request_call(
self.controller.set_state,
id=self.resource.id,
on=True,
brightness=brightness,
color_xy=xy_color,
color_temp=color_temp,
transition_time=transition,
)
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
return
# NOTE: a grouped_light can only handle turn on/off
# To set other features, you'll have to control the attached lights
if transition is None:
await self.bridge.async_request_call(
self.controller.set_state, id=self.resource.id, on=False
)
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)
]
await self.bridge.async_request_call(
self.controller.set_state,
id=self.resource.id,
on=False,
transition_time=transition,
)
async def async_set_flash(self, flash: str) -> None:
"""Send flash command to light."""
await asyncio.gather(
*[
self.bridge.async_request_call(
self.api.lights.set_flash,
id=light.id,
short=flash == FLASH_SHORT,
)
for light in self.controller.get_lights(self.resource.id)
]
await self.bridge.async_request_call(
self.controller.set_flash,
id=self.resource.id,
short=flash == FLASH_SHORT,
)
@callback

View File

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