diff --git a/homeassistant/components/homekit/type_lights.py b/homeassistant/components/homekit/type_lights.py index 1720c2c58c8e90ae347381f61c32d4ba1c324e1a..e38af1a04eb8130720548b2b65890970fa71c545 100644 --- a/homeassistant/components/homekit/type_lights.py +++ b/homeassistant/components/homekit/type_lights.py @@ -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) diff --git a/tests/components/homekit/test_type_lights.py b/tests/components/homekit/test_type_lights.py index 888ad87a848afa49879427081e920be68aac58e9..3ee2e61cc722dd1a21603290b3d16d09bb2aeed1 100644 --- a/tests/components/homekit/test_type_lights.py +++ b/tests/components/homekit/test_type_lights.py @@ -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")