From d0b11568cc056c667f70ee6ca44cdf63e15f6de0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 Aug 2021 20:28:01 -0500 Subject: [PATCH] Ensure HomeKit passes min/max mireds as ints (#54372) --- .../components/homekit/type_lights.py | 5 +++-- tests/components/homekit/test_type_lights.py | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homekit/type_lights.py b/homeassistant/components/homekit/type_lights.py index aea760534fd..90c55d52153 100644 --- a/homeassistant/components/homekit/type_lights.py +++ b/homeassistant/components/homekit/type_lights.py @@ -1,5 +1,6 @@ """Class to hold all light accessories.""" import logging +import math from pyhap.const import CATEGORY_LIGHTBULB @@ -89,8 +90,8 @@ class Light(HomeAccessory): self.char_brightness = serv_light.configure_char(CHAR_BRIGHTNESS, value=100) if self.color_temp_supported: - min_mireds = attributes.get(ATTR_MIN_MIREDS, 153) - max_mireds = attributes.get(ATTR_MAX_MIREDS, 500) + min_mireds = math.floor(attributes.get(ATTR_MIN_MIREDS, 153)) + max_mireds = math.ceil(attributes.get(ATTR_MAX_MIREDS, 500)) self.char_color_temp = serv_light.configure_char( CHAR_COLOR_TEMPERATURE, value=min_mireds, diff --git a/tests/components/homekit/test_type_lights.py b/tests/components/homekit/test_type_lights.py index 90e3aa0cabe..de0fd532ec9 100644 --- a/tests/components/homekit/test_type_lights.py +++ b/tests/components/homekit/test_type_lights.py @@ -15,6 +15,8 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP, ATTR_HS_COLOR, + ATTR_MAX_MIREDS, + ATTR_MIN_MIREDS, ATTR_SUPPORTED_COLOR_MODES, DOMAIN, ) @@ -639,6 +641,26 @@ async def test_light_set_brightness_and_color(hass, hk_driver, events): ) +async def test_light_min_max_mireds(hass, hk_driver, events): + """Test mireds are forced to ints.""" + entity_id = "light.demo" + + hass.states.async_set( + entity_id, + STATE_ON, + { + ATTR_SUPPORTED_COLOR_MODES: ["color_temp"], + ATTR_BRIGHTNESS: 255, + ATTR_MAX_MIREDS: 500.5, + ATTR_MIN_MIREDS: 100.5, + }, + ) + await hass.async_block_till_done() + acc = Light(hass, hk_driver, "Light", entity_id, 1, None) + acc.char_color_temp.properties["maxValue"] == 500 + acc.char_color_temp.properties["minValue"] == 100 + + async def test_light_set_brightness_and_color_temp(hass, hk_driver, events): """Test light with all chars in one go.""" entity_id = "light.demo"