From 8a1c6e03806fbcf410e896b323e76436f581b439 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Sun, 31 Oct 2021 13:59:53 -0700 Subject: [PATCH] Fix out-of-range color temperatures If light.turn_on is invoked with `brightness: 128, color_temp: 500` for a bulb that supports mired range 154..370 in value range 1..255 the calculated color temperature will be negative. When the bulb receives the command it may turn on, but will fail processing the color_temp operation and will not send a response. At this point `light.toggle` will have no effect because Home Assistant is unaware that the bulb is on. Fix the state by clamping the sent color temperature to the allowed range. Signed-off-by: Peter A. Bigot --- custom_components/localtuya/light.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 5a33192..7c74e49 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -377,16 +377,17 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP): if brightness is None: brightness = self._brightness - color_temp_value = ( - (self._max_mired - self._min_mired) - - (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) - if self._color_temp_reverse - else (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) - ) + mired = int(kwargs[ATTR_COLOR_TEMP]) + if self._color_temp_reverse: + mired = self._max_mired - (mired - self._min_mired) + if mired < self._min_mired: + mired = self._min_mired + elif mired > self._max_mired: + mired = self._max_mired color_temp = int( self._upper_color_temp - (self._upper_color_temp / (self._max_mired - self._min_mired)) - * color_temp_value + * (mired - self._min_mired) ) states[self._config.get(CONF_COLOR_MODE)] = MODE_WHITE states[self._config.get(CONF_BRIGHTNESS)] = brightness