diff --git a/homeassistant/components/hue/v2/group.py b/homeassistant/components/hue/v2/group.py index eb57d99956a..c9d7bf6408b 100644 --- a/homeassistant/components/hue/v2/group.py +++ b/homeassistant/components/hue/v2/group.py @@ -162,7 +162,11 @@ class GroupedHueLight(HueBaseEntity, LightEntity): """Turn the grouped_light on.""" transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) xy_color = kwargs.get(ATTR_XY_COLOR) - color_temp = normalize_hue_colortemp(kwargs.get(ATTR_COLOR_TEMP_KELVIN)) + color_temp = normalize_hue_colortemp( + kwargs.get(ATTR_COLOR_TEMP_KELVIN), + color_util.color_temperature_kelvin_to_mired(self.max_color_temp_kelvin), + color_util.color_temperature_kelvin_to_mired(self.min_color_temp_kelvin), + ) brightness = normalize_hue_brightness(kwargs.get(ATTR_BRIGHTNESS)) flash = kwargs.get(ATTR_FLASH) diff --git a/homeassistant/components/hue/v2/helpers.py b/homeassistant/components/hue/v2/helpers.py index 384d2a30596..12c0d6d10e8 100644 --- a/homeassistant/components/hue/v2/helpers.py +++ b/homeassistant/components/hue/v2/helpers.py @@ -23,11 +23,12 @@ def normalize_hue_transition(transition: float | None) -> float | None: return transition -def normalize_hue_colortemp(colortemp_k: int | None) -> int | None: +def normalize_hue_colortemp( + colortemp_k: int | None, min_mireds: int, max_mireds: int +) -> int | None: """Return color temperature within Hue's ranges.""" if colortemp_k is None: return None - colortemp = color_util.color_temperature_kelvin_to_mired(colortemp_k) - # Hue only accepts a range between 153..500 - colortemp = min(colortemp, 500) - return max(colortemp, 153) + colortemp_mireds = color_util.color_temperature_kelvin_to_mired(colortemp_k) + # Hue only accepts a range between min_mireds..max_mireds + return min(max(colortemp_mireds, min_mireds), max_mireds) diff --git a/homeassistant/components/hue/v2/light.py b/homeassistant/components/hue/v2/light.py index d83cdaa8009..e22d2c09f43 100644 --- a/homeassistant/components/hue/v2/light.py +++ b/homeassistant/components/hue/v2/light.py @@ -40,8 +40,8 @@ from .helpers import ( normalize_hue_transition, ) -FALLBACK_MIN_KELVIN = 6500 -FALLBACK_MAX_KELVIN = 2000 +FALLBACK_MIN_MIREDS = 153 # hue default for most lights +FALLBACK_MAX_MIREDS = 500 # hue default for most lights FALLBACK_KELVIN = 5800 # halfway # HA 2025.4 replaced the deprecated effect "None" with HA default "off" @@ -177,25 +177,31 @@ class HueLight(HueBaseEntity, LightEntity): # return a fallback value to prevent issues with mired->kelvin conversions return FALLBACK_KELVIN + @property + def max_color_temp_mireds(self) -> int: + """Return the warmest color_temp in mireds (so highest number) that this light supports.""" + if color_temp := self.resource.color_temperature: + return color_temp.mirek_schema.mirek_maximum + # return a fallback value if the light doesn't provide limits + return FALLBACK_MAX_MIREDS + + @property + def min_color_temp_mireds(self) -> int: + """Return the coldest color_temp in mireds (so lowest number) that this light supports.""" + if color_temp := self.resource.color_temperature: + return color_temp.mirek_schema.mirek_minimum + # return a fallback value if the light doesn't provide limits + return FALLBACK_MIN_MIREDS + @property def max_color_temp_kelvin(self) -> int: """Return the coldest color_temp_kelvin that this light supports.""" - if color_temp := self.resource.color_temperature: - return color_util.color_temperature_mired_to_kelvin( - color_temp.mirek_schema.mirek_minimum - ) - # return a fallback value to prevent issues with mired->kelvin conversions - return FALLBACK_MAX_KELVIN + return color_util.color_temperature_mired_to_kelvin(self.min_color_temp_mireds) @property def min_color_temp_kelvin(self) -> int: """Return the warmest color_temp_kelvin that this light supports.""" - if color_temp := self.resource.color_temperature: - return color_util.color_temperature_mired_to_kelvin( - color_temp.mirek_schema.mirek_maximum - ) - # return a fallback value to prevent issues with mired->kelvin conversions - return FALLBACK_MIN_KELVIN + return color_util.color_temperature_mired_to_kelvin(self.max_color_temp_mireds) @property def extra_state_attributes(self) -> dict[str, str] | None: @@ -220,7 +226,11 @@ class HueLight(HueBaseEntity, LightEntity): """Turn the device on.""" transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) xy_color = kwargs.get(ATTR_XY_COLOR) - color_temp = normalize_hue_colortemp(kwargs.get(ATTR_COLOR_TEMP_KELVIN)) + color_temp = normalize_hue_colortemp( + kwargs.get(ATTR_COLOR_TEMP_KELVIN), + self.min_color_temp_mireds, + self.max_color_temp_mireds, + ) brightness = normalize_hue_brightness(kwargs.get(ATTR_BRIGHTNESS)) if self._last_brightness and brightness is None: # The Hue bridge sets the brightness to 1% when turning on a bulb diff --git a/tests/components/hue/test_light_v2.py b/tests/components/hue/test_light_v2.py index 13cfe3995de..a5e7d24c86e 100644 --- a/tests/components/hue/test_light_v2.py +++ b/tests/components/hue/test_light_v2.py @@ -178,7 +178,7 @@ async def test_light_turn_on_service( blocking=True, ) assert len(mock_bridge_v2.mock_requests) == 6 - assert mock_bridge_v2.mock_requests[5]["json"]["color_temperature"]["mirek"] == 500 + assert mock_bridge_v2.mock_requests[5]["json"]["color_temperature"]["mirek"] == 454 # test enable an effect await hass.services.async_call(