Handle float values for homekit lightning (#33683)

* Handle float values for homekit lightning

* Empty commit to rerun CI
pull/33814/head
J. Nick Koston 2020-04-05 01:21:44 -05:00 committed by Paulus Schoutsen
parent 30a391b88b
commit dd0fd36049
2 changed files with 32 additions and 16 deletions

View File

@ -149,7 +149,7 @@ class Light(HomeAccessory):
# Handle Brightness
if CHAR_BRIGHTNESS in self.chars:
brightness = new_state.attributes.get(ATTR_BRIGHTNESS)
if isinstance(brightness, int):
if isinstance(brightness, (int, float)):
brightness = round(brightness / 255 * 100, 0)
# The homeassistant component might report its brightness as 0 but is
# not off. But 0 is a special value in homekit. When you turn on a
@ -169,22 +169,18 @@ class Light(HomeAccessory):
# Handle color temperature
if CHAR_COLOR_TEMPERATURE in self.chars:
color_temperature = new_state.attributes.get(ATTR_COLOR_TEMP)
if (
isinstance(color_temperature, int)
and self.char_color_temperature.value != color_temperature
):
self.char_color_temperature.set_value(color_temperature)
if isinstance(color_temperature, (int, float)):
color_temperature = round(color_temperature, 0)
if self.char_color_temperature.value != color_temperature:
self.char_color_temperature.set_value(color_temperature)
# Handle Color
if CHAR_SATURATION in self.chars and CHAR_HUE in self.chars:
hue, saturation = new_state.attributes.get(ATTR_HS_COLOR, (None, None))
if (
isinstance(hue, (int, float))
and isinstance(saturation, (int, float))
and (
hue != self.char_hue.value
or saturation != self.char_saturation.value
)
):
self.char_hue.set_value(hue)
self.char_saturation.set_value(saturation)
if isinstance(hue, (int, float)) and isinstance(saturation, (int, float)):
hue = round(hue, 0)
saturation = round(saturation, 0)
if hue != self.char_hue.value:
self.char_hue.set_value(hue)
if saturation != self.char_saturation.value:
self.char_saturation.set_value(saturation)

View File

@ -235,6 +235,17 @@ async def test_light_brightness(hass, hk_driver, cls, events, driver):
await hass.async_block_till_done()
assert acc.char_brightness.value == 1
# Ensure floats are handled
hass.states.async_set(entity_id, STATE_ON, {ATTR_BRIGHTNESS: 55.66})
await hass.async_block_till_done()
assert acc.char_brightness.value == 22
hass.states.async_set(entity_id, STATE_ON, {ATTR_BRIGHTNESS: 108.4})
await hass.async_block_till_done()
assert acc.char_brightness.value == 43
hass.states.async_set(entity_id, STATE_ON, {ATTR_BRIGHTNESS: 0.0})
await hass.async_block_till_done()
assert acc.char_brightness.value == 1
async def test_light_color_temperature(hass, hk_driver, cls, events, driver):
"""Test light with color temperature."""
@ -417,6 +428,11 @@ async def test_light_set_brightness_and_color(hass, hk_driver, cls, events, driv
await hass.async_block_till_done()
assert acc.char_brightness.value == 40
hass.states.async_set(entity_id, STATE_ON, {ATTR_HS_COLOR: (4.5, 9.2)})
await hass.async_block_till_done()
assert acc.char_hue.value == 4
assert acc.char_saturation.value == 9
# Set from HomeKit
call_turn_on = async_mock_service(hass, DOMAIN, "turn_on")
@ -489,6 +505,10 @@ async def test_light_set_brightness_and_color_temp(
await hass.async_block_till_done()
assert acc.char_brightness.value == 40
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP: (224.14)})
await hass.async_block_till_done()
assert acc.char_color_temperature.value == 224
# Set from HomeKit
call_turn_on = async_mock_service(hass, DOMAIN, "turn_on")