From 6b49bea6c45c4d8a0d7c47794936f7f2f853e879 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 14 Jan 2020 16:09:35 +0100 Subject: [PATCH] Fix HomeKit behavior with lights supporting color and temperature (#30756) --- .../components/homekit/type_lights.py | 12 ++++++++++-- tests/components/homekit/test_type_lights.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homekit/type_lights.py b/homeassistant/components/homekit/type_lights.py index 7f195b276d6..3fc6a0628ff 100644 --- a/homeassistant/components/homekit/type_lights.py +++ b/homeassistant/components/homekit/type_lights.py @@ -66,15 +66,20 @@ class Light(HomeAccessory): self._features = self.hass.states.get(self.entity_id).attributes.get( ATTR_SUPPORTED_FEATURES ) + if self._features & SUPPORT_BRIGHTNESS: self.chars.append(CHAR_BRIGHTNESS) - if self._features & SUPPORT_COLOR_TEMP: - self.chars.append(CHAR_COLOR_TEMPERATURE) + if self._features & SUPPORT_COLOR: self.chars.append(CHAR_HUE) self.chars.append(CHAR_SATURATION) self._hue = None self._saturation = None + elif self._features & SUPPORT_COLOR_TEMP: + # ColorTemperature and Hue characteristic should not be + # exposed both. Both states are tracked separately in HomeKit, + # causing "source of truth" problems. + self.chars.append(CHAR_COLOR_TEMPERATURE) serv_light = self.add_preload_service(SERV_LIGHTBULB, self.chars) self.char_on = serv_light.configure_char( @@ -88,6 +93,7 @@ class Light(HomeAccessory): self.char_brightness = serv_light.configure_char( CHAR_BRIGHTNESS, value=100, setter_callback=self.set_brightness ) + if CHAR_COLOR_TEMPERATURE in self.chars: min_mireds = self.hass.states.get(self.entity_id).attributes.get( ATTR_MIN_MIREDS, 153 @@ -101,10 +107,12 @@ class Light(HomeAccessory): properties={PROP_MIN_VALUE: min_mireds, PROP_MAX_VALUE: max_mireds}, setter_callback=self.set_color_temperature, ) + if CHAR_HUE in self.chars: self.char_hue = serv_light.configure_char( CHAR_HUE, value=0, setter_callback=self.set_hue ) + if CHAR_SATURATION in self.chars: self.char_saturation = serv_light.configure_char( CHAR_SATURATION, value=75, setter_callback=self.set_saturation diff --git a/tests/components/homekit/test_type_lights.py b/tests/components/homekit/test_type_lights.py index c1811a2e2fc..f357702040b 100644 --- a/tests/components/homekit/test_type_lights.py +++ b/tests/components/homekit/test_type_lights.py @@ -177,6 +177,25 @@ async def test_light_color_temperature(hass, hk_driver, cls, events): assert events[-1].data[ATTR_VALUE] == "color temperature at 250" +async def test_light_color_temperature_and_rgb_color(hass, hk_driver, cls, events): + """Test light with color temperature and rgb color not exposing temperature.""" + entity_id = "light.demo" + + hass.states.async_set( + entity_id, + STATE_ON, + { + ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR_TEMP | SUPPORT_COLOR, + ATTR_COLOR_TEMP: 190, + ATTR_HS_COLOR: (260, 90), + }, + ) + await hass.async_block_till_done() + acc = cls.light(hass, hk_driver, "Light", entity_id, 2, None) + + assert not hasattr(acc, "char_color_temperature") + + async def test_light_rgb_color(hass, hk_driver, cls, events): """Test light with rgb_color.""" entity_id = "light.demo"