From 673ebe291192c0d6ad2de82ab30897b8b535694a Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Tue, 16 Mar 2021 10:02:26 -0400 Subject: [PATCH] Guard extra call in ZHA lights (#47832) * add flag to prevent sending an on command * fix condition * add constant for default transition * make groups work with new force on flag * reorder light entity creation * rearrange logic * update test * remove failed attempt at group light flag * fix flag --- homeassistant/components/zha/light.py | 19 +++++++++++++++++-- tests/components/zha/test_light.py | 8 +++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/zha/light.py b/homeassistant/components/zha/light.py index e7d9be62374..f81b931a49d 100644 --- a/homeassistant/components/zha/light.py +++ b/homeassistant/components/zha/light.py @@ -65,6 +65,8 @@ CAPABILITIES_COLOR_LOOP = 0x4 CAPABILITIES_COLOR_XY = 0x08 CAPABILITIES_COLOR_TEMP = 0x10 +DEFAULT_TRANSITION = 1 + UPDATE_COLORLOOP_ACTION = 0x1 UPDATE_COLORLOOP_DIRECTION = 0x2 UPDATE_COLORLOOP_TIME = 0x4 @@ -114,6 +116,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BaseLight(LogMixin, light.LightEntity): """Operations common to all light entities.""" + _FORCE_ON = False + def __init__(self, *args, **kwargs): """Initialize the light.""" super().__init__(*args, **kwargs) @@ -201,7 +205,7 @@ class BaseLight(LogMixin, light.LightEntity): async def async_turn_on(self, **kwargs): """Turn the entity on.""" transition = kwargs.get(light.ATTR_TRANSITION) - duration = transition * 10 if transition else 1 + duration = transition * 10 if transition else DEFAULT_TRANSITION brightness = kwargs.get(light.ATTR_BRIGHTNESS) effect = kwargs.get(light.ATTR_EFFECT) flash = kwargs.get(light.ATTR_FLASH) @@ -228,7 +232,7 @@ class BaseLight(LogMixin, light.LightEntity): if level: self._brightness = level - if brightness is None or brightness: + if brightness is None or (self._FORCE_ON and brightness): # since some lights don't always turn on with move_to_level_with_on_off, # we should call the on command on the on_off cluster if brightness is not 0. result = await self._on_off_channel.on() @@ -512,6 +516,17 @@ class HueLight(Light): _REFRESH_INTERVAL = (3, 5) +@STRICT_MATCH( + channel_names=CHANNEL_ON_OFF, + aux_channels={CHANNEL_COLOR, CHANNEL_LEVEL}, + manufacturers="Jasco", +) +class ForceOnLight(Light): + """Representation of a light which does not respect move_to_level_with_on_off.""" + + _FORCE_ON = True + + @GROUP_MATCH() class LightGroup(BaseLight, ZhaGroupEntity): """Representation of a light group.""" diff --git a/tests/components/zha/test_light.py b/tests/components/zha/test_light.py index 0a9a492a148..021f4f09dd9 100644 --- a/tests/components/zha/test_light.py +++ b/tests/components/zha/test_light.py @@ -392,13 +392,11 @@ async def async_test_level_on_off_from_hass( await hass.services.async_call( DOMAIN, "turn_on", {"entity_id": entity_id, "brightness": 10}, blocking=True ) - assert on_off_cluster.request.call_count == 1 - assert on_off_cluster.request.await_count == 1 + # the onoff cluster is now not used when brightness is present by default + assert on_off_cluster.request.call_count == 0 + assert on_off_cluster.request.await_count == 0 assert level_cluster.request.call_count == 1 assert level_cluster.request.await_count == 1 - assert on_off_cluster.request.call_args == call( - False, ON, (), expect_reply=True, manufacturer=None, tries=1, tsn=None - ) assert level_cluster.request.call_args == call( False, 4,