diff --git a/homeassistant/components/ozw/light.py b/homeassistant/components/ozw/light.py index c985a5b7f41..d9e3ed2a51d 100644 --- a/homeassistant/components/ozw/light.py +++ b/homeassistant/components/ozw/light.py @@ -30,8 +30,8 @@ COLOR_CHANNEL_COLD_WHITE = 0x02 COLOR_CHANNEL_RED = 0x04 COLOR_CHANNEL_GREEN = 0x08 COLOR_CHANNEL_BLUE = 0x10 -TEMP_COLOR_MAX = 500 # mireds (inverted) -TEMP_COLOR_MIN = 154 +TEMP_COLOR_MAX = 500 # mired equivalent to 2000K +TEMP_COLOR_MIN = 154 # mired equivalent to 6500K TEMP_COLOR_DIFF = TEMP_COLOR_MAX - TEMP_COLOR_MIN @@ -193,10 +193,15 @@ class ZwaveLight(ZWaveDeviceEntity, LightEntity): rgbw = f"#00000000{white:02x}" elif color_temp is not None: - cold = round((TEMP_COLOR_MAX - round(color_temp)) / TEMP_COLOR_DIFF * 255) + # Limit color temp to min/max values + cold = max( + 0, + min( + 255, + round((TEMP_COLOR_MAX - round(color_temp)) / TEMP_COLOR_DIFF * 255), + ), + ) warm = 255 - cold - if warm < 0: - warm = 0 rgbw = f"#000000{warm:02x}{cold:02x}" if rgbw and self.values.color: diff --git a/tests/components/ozw/test_light.py b/tests/components/ozw/test_light.py index 67eebdfdea7..c1d92688825 100644 --- a/tests/components/ozw/test_light.py +++ b/tests/components/ozw/test_light.py @@ -316,6 +316,39 @@ async def test_light(hass, light_data, light_msg, light_rgb_msg, sent_messages): assert state.state == "on" assert state.attributes["color_temp"] == 465 + # Test setting invalid color temp + new_color = 120 + await hass.services.async_call( + "light", + "turn_on", + {"entity_id": "light.led_bulb_6_multi_colour_level", "color_temp": new_color}, + blocking=True, + ) + assert len(sent_messages) == 19 + msg = sent_messages[-1] + assert msg["topic"] == "OpenZWave/1/command/setvalue/" + assert msg["payload"] == {"Value": 255, "ValueIDKey": 659128337} + + msg = sent_messages[-2] + assert msg["topic"] == "OpenZWave/1/command/setvalue/" + assert msg["payload"] == {"Value": "#00000000ff", "ValueIDKey": 659341335} + + # Feedback on state + light_msg.decode() + light_msg.payload["Value"] = byte_to_zwave_brightness(255) + light_msg.encode() + light_rgb_msg.decode() + light_rgb_msg.payload["Value"] = "#00000000ff" + light_rgb_msg.encode() + receive_message(light_msg) + receive_message(light_rgb_msg) + await hass.async_block_till_done() + + state = hass.states.get("light.led_bulb_6_multi_colour_level") + assert state is not None + assert state.state == "on" + assert state.attributes["color_temp"] == 154 + async def test_no_rgb_light(hass, light_no_rgb_data, light_no_rgb_msg, sent_messages): """Test setting up config entry."""