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 <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot
2021-10-31 13:59:53 -07:00
parent 2a277a0c26
commit 8a1c6e0380

View File

@@ -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