Skip to content

Commit a11616a

Browse files
authored
Fix light group assume behavior (#82)
* Fix light group assume * Add regression tests
1 parent c5a23ad commit a11616a

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

tests/test_light.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,8 @@ async def test_on_with_off_color(
19051905
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
19061906
)
19071907
@patch(
1908-
"zha.application.platforms.light.const.ASSUME_UPDATE_GROUP_FROM_CHILD_DELAY",
1909-
new=0,
1908+
"zigpy.zcl.clusters.general.LevelControl.request",
1909+
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
19101910
)
19111911
@pytest.mark.looptime
19121912
async def test_group_member_assume_state(
@@ -1958,6 +1958,7 @@ async def test_group_member_assume_state(
19581958
# turn on via UI
19591959
await entity.async_turn_on()
19601960
await zha_gateway.async_block_till_done()
1961+
await asyncio.sleep(1) # wait for assume debounce
19611962

19621963
# members also instantly assume STATE_ON
19631964
assert bool(device_1_light_entity.state["on"]) is True
@@ -1967,12 +1968,39 @@ async def test_group_member_assume_state(
19671968
# turn off via UI
19681969
await entity.async_turn_off()
19691970
await zha_gateway.async_block_till_done()
1971+
await asyncio.sleep(1)
19701972

19711973
# members also instantly assume STATE_OFF
19721974
assert bool(device_1_light_entity.state["on"]) is False
19731975
assert bool(device_2_light_entity.state["on"]) is False
19741976
assert bool(entity.state["on"]) is False
19751977

1978+
# now test members with different state not being overridden
1979+
# turn on light 1 to brightness 50
1980+
await device_1_light_entity.async_turn_on(brightness=50)
1981+
await zha_gateway.async_block_till_done()
1982+
assert bool(device_1_light_entity.state["on"]) is True
1983+
assert device_1_light_entity.state["brightness"] == 50
1984+
1985+
# turn on light 2 to brightness 100
1986+
await device_2_light_entity.async_turn_on(brightness=100)
1987+
await zha_gateway.async_block_till_done()
1988+
assert bool(device_2_light_entity.state["on"]) is True
1989+
assert device_2_light_entity.state["brightness"] == 100
1990+
1991+
await asyncio.sleep(1) # wait for assume debounce
1992+
1993+
# turn on group, but do not change brightness
1994+
await entity.async_turn_on(brightness=None)
1995+
await zha_gateway.async_block_till_done()
1996+
await asyncio.sleep(1)
1997+
1998+
assert entity.state["brightness"] == 75 # average
1999+
2000+
# but members do not change unchanged state
2001+
assert device_1_light_entity.state["brightness"] == 50
2002+
assert device_2_light_entity.state["brightness"] == 100
2003+
19762004

19772005
async def test_light_state_restoration(
19782006
device_light_3, # pylint: disable=redefined-outer-name

zha/application/platforms/light/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,27 +1361,27 @@ def _make_members_assume_group_state(
13611361

13621362
# check if the parameters were actually updated
13631363
# in the service call before updating members
1364-
if ATTR_BRIGHTNESS in service_kwargs: # or off brightness
1364+
if service_kwargs.get(ATTR_BRIGHTNESS) is not None: # or off brightness
13651365
update_params[ATTR_BRIGHTNESS] = self._brightness
13661366
elif off_brightness is not None:
13671367
# if we turn on the group light with "off brightness",
13681368
# pass that to the members
13691369
update_params[ATTR_BRIGHTNESS] = off_brightness
13701370

1371-
if ATTR_COLOR_TEMP in service_kwargs:
1371+
if service_kwargs.get(ATTR_COLOR_TEMP) is not None:
13721372
update_params[ATTR_COLOR_MODE] = self._color_mode
13731373
update_params[ATTR_COLOR_TEMP] = self._color_temp
13741374

1375-
if ATTR_XY_COLOR in service_kwargs:
1375+
if service_kwargs.get(ATTR_XY_COLOR) is not None:
13761376
update_params[ATTR_COLOR_MODE] = self._color_mode
13771377
update_params[ATTR_XY_COLOR] = self._xy_color
13781378

1379-
if ATTR_HS_COLOR in service_kwargs:
1379+
if service_kwargs.get(ATTR_HS_COLOR) is not None:
13801380
update_params[ATTR_COLOR_MODE] = self._color_mode
13811381
update_params[ATTR_HS_COLOR] = self._hs_color
13821382

1383-
if ATTR_EFFECT in service_kwargs:
1384-
update_params[ATTR_EFFECT] = self._effect
1383+
# we always update effect for now, as we don't know if it was set or not
1384+
update_params[ATTR_EFFECT] = self._effect
13851385

13861386
for platform_entity in self.group.get_platform_entities(Light.PLATFORM):
13871387
platform_entity._assume_group_state(update_params)

0 commit comments

Comments
 (0)