Fix HomeKit behavior with lights supporting color and temperature (#30756)

This commit is contained in:
Franck Nijhof 2020-01-14 16:09:35 +01:00 committed by Paulus Schoutsen
parent aeb789ddcc
commit 6b49bea6c4
2 changed files with 29 additions and 2 deletions

View File

@ -66,15 +66,20 @@ class Light(HomeAccessory):
self._features = self.hass.states.get(self.entity_id).attributes.get( self._features = self.hass.states.get(self.entity_id).attributes.get(
ATTR_SUPPORTED_FEATURES ATTR_SUPPORTED_FEATURES
) )
if self._features & SUPPORT_BRIGHTNESS: if self._features & SUPPORT_BRIGHTNESS:
self.chars.append(CHAR_BRIGHTNESS) self.chars.append(CHAR_BRIGHTNESS)
if self._features & SUPPORT_COLOR_TEMP:
self.chars.append(CHAR_COLOR_TEMPERATURE)
if self._features & SUPPORT_COLOR: if self._features & SUPPORT_COLOR:
self.chars.append(CHAR_HUE) self.chars.append(CHAR_HUE)
self.chars.append(CHAR_SATURATION) self.chars.append(CHAR_SATURATION)
self._hue = None self._hue = None
self._saturation = 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) serv_light = self.add_preload_service(SERV_LIGHTBULB, self.chars)
self.char_on = serv_light.configure_char( self.char_on = serv_light.configure_char(
@ -88,6 +93,7 @@ class Light(HomeAccessory):
self.char_brightness = serv_light.configure_char( self.char_brightness = serv_light.configure_char(
CHAR_BRIGHTNESS, value=100, setter_callback=self.set_brightness CHAR_BRIGHTNESS, value=100, setter_callback=self.set_brightness
) )
if CHAR_COLOR_TEMPERATURE in self.chars: if CHAR_COLOR_TEMPERATURE in self.chars:
min_mireds = self.hass.states.get(self.entity_id).attributes.get( min_mireds = self.hass.states.get(self.entity_id).attributes.get(
ATTR_MIN_MIREDS, 153 ATTR_MIN_MIREDS, 153
@ -101,10 +107,12 @@ class Light(HomeAccessory):
properties={PROP_MIN_VALUE: min_mireds, PROP_MAX_VALUE: max_mireds}, properties={PROP_MIN_VALUE: min_mireds, PROP_MAX_VALUE: max_mireds},
setter_callback=self.set_color_temperature, setter_callback=self.set_color_temperature,
) )
if CHAR_HUE in self.chars: if CHAR_HUE in self.chars:
self.char_hue = serv_light.configure_char( self.char_hue = serv_light.configure_char(
CHAR_HUE, value=0, setter_callback=self.set_hue CHAR_HUE, value=0, setter_callback=self.set_hue
) )
if CHAR_SATURATION in self.chars: if CHAR_SATURATION in self.chars:
self.char_saturation = serv_light.configure_char( self.char_saturation = serv_light.configure_char(
CHAR_SATURATION, value=75, setter_callback=self.set_saturation CHAR_SATURATION, value=75, setter_callback=self.set_saturation

View File

@ -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" 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): async def test_light_rgb_color(hass, hk_driver, cls, events):
"""Test light with rgb_color.""" """Test light with rgb_color."""
entity_id = "light.demo" entity_id = "light.demo"