Skip to content

Commit

Permalink
Fix HomeKit behavior with lights supporting color and temperature (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored and balloob committed Jan 14, 2020
1 parent aeb789d commit 6b49bea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions homeassistant/components/homekit/type_lights.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ def __init__(self, *args):
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(
Expand All @@ -88,6 +93,7 @@ def __init__(self, *args):
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
Expand All @@ -101,10 +107,12 @@ def __init__(self, *args):
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
Expand Down
19 changes: 19 additions & 0 deletions tests/components/homekit/test_type_lights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6b49bea

Please sign in to comment.