From 4136f9f203a2629776e47fe9bbba695c5ba77be3 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 5 May 2021 17:59:26 +0200 Subject: [PATCH] Fix Tasmota color scaling and RGBW lights (#50120) --- homeassistant/components/tasmota/light.py | 24 +++++++++++++++++------ tests/components/tasmota/test_light.py | 6 +++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/tasmota/light.py b/homeassistant/components/tasmota/light.py index 440b6f4267d..53db34a9001 100644 --- a/homeassistant/components/tasmota/light.py +++ b/homeassistant/components/tasmota/light.py @@ -143,9 +143,14 @@ class TasmotaLight( rgb = attributes["color"] # Tasmota's RGB color is adjusted for brightness, compensate - red_compensated = clamp(round(rgb[0] / self._brightness * 255)) - green_compensated = clamp(round(rgb[1] / self._brightness * 255)) - blue_compensated = clamp(round(rgb[2] / self._brightness * 255)) + if self._brightness > 0: + red_compensated = clamp(round(rgb[0] / self._brightness * 255)) + green_compensated = clamp(round(rgb[1] / self._brightness * 255)) + blue_compensated = clamp(round(rgb[2] / self._brightness * 255)) + else: + red_compensated = 0 + green_compensated = 0 + blue_compensated = 0 self._rgb = [red_compensated, green_compensated, blue_compensated] if "color_temp" in attributes: self._color_temp = attributes["color_temp"] @@ -211,6 +216,13 @@ class TasmotaLight( return None return [*self._rgb, self._white_value] + @property + def force_update(self): + """Force update.""" + if self.color_mode == COLOR_MODE_RGBW: + return True + return False + @property def is_on(self): """Return true if device is on.""" @@ -239,9 +251,9 @@ class TasmotaLight( if ATTR_RGBW_COLOR in kwargs and COLOR_MODE_RGBW in supported_color_modes: rgbw = kwargs[ATTR_RGBW_COLOR] # Tasmota does not support direct RGBW control, the light must be set to - # either white mode or color mode. Set the mode according to max of rgb - # and white channels - if max(rgbw[0:3]) > rgbw[3]: + # either white mode or color mode. Set the mode to white if white channel + # is on, and to color otheruse + if rgbw[3] == 0: attributes["color"] = [rgbw[0], rgbw[1], rgbw[2]] else: white_value_normalized = rgbw[3] / DEFAULT_BRIGHTNESS_MAX diff --git a/tests/components/tasmota/test_light.py b/tests/components/tasmota/test_light.py index 6b450fa805d..3a27409e433 100644 --- a/tests/components/tasmota/test_light.py +++ b/tests/components/tasmota/test_light.py @@ -855,8 +855,8 @@ async def test_sending_mqtt_commands_rgbw(hass, mqtt_mock, setup_tasmota): ) mqtt_mock.async_publish.reset_mock() - # Set color when setting brighter color than white - await common.async_turn_on(hass, "light.test", rgbw_color=[128, 64, 32, 16]) + # Set color when setting white is off + await common.async_turn_on(hass, "light.test", rgbw_color=[128, 64, 32, 0]) mqtt_mock.async_publish.assert_called_once_with( "tasmota_49A3BC/cmnd/Backlog", "NoDelay;Power1 ON;NoDelay;Color2 128,64,32", @@ -865,7 +865,7 @@ async def test_sending_mqtt_commands_rgbw(hass, mqtt_mock, setup_tasmota): ) mqtt_mock.async_publish.reset_mock() - # Set white when setting brighter white than color + # Set white when white is on await common.async_turn_on(hass, "light.test", rgbw_color=[16, 64, 32, 128]) mqtt_mock.async_publish.assert_called_once_with( "tasmota_49A3BC/cmnd/Backlog",