Add support for color_mode white to tasmota light (#51608)

This commit is contained in:
Erik Montnemery 2021-06-08 17:43:04 +02:00 committed by GitHub
parent b3a67a2dd7
commit 67f3e717a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 69 deletions

View file

@ -13,13 +13,13 @@ from homeassistant.components.light import (
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
ATTR_TRANSITION,
ATTR_WHITE,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF,
COLOR_MODE_RGB,
COLOR_MODE_RGBW,
COLOR_MODE_WHITE,
SUPPORT_EFFECT,
SUPPORT_TRANSITION,
LightEntity,
@ -60,6 +60,17 @@ def clamp(value):
return min(max(value, 0), 255)
def scale_brightness(brightness):
"""Scale brightness from 0..255 to 1..100."""
brightness_normalized = brightness / DEFAULT_BRIGHTNESS_MAX
device_brightness = min(
round(brightness_normalized * TASMOTA_BRIGHTNESS_MAX),
TASMOTA_BRIGHTNESS_MAX,
)
# Make sure the brightness is not rounded down to 0
return max(device_brightness, 1)
class TasmotaLight(
TasmotaAvailability,
TasmotaDiscoveryUpdate,
@ -80,7 +91,6 @@ class TasmotaLight(
self._white_value = None
self._flash_times = None
self._rgb = None
self._rgbw = None
super().__init__(
**kwds,
@ -107,8 +117,7 @@ class TasmotaLight(
self._color_mode = COLOR_MODE_RGB
if light_type == LIGHT_TYPE_RGBW:
self._supported_color_modes.add(COLOR_MODE_RGBW)
self._color_mode = COLOR_MODE_RGBW
self._supported_color_modes.add(COLOR_MODE_WHITE)
if light_type in [LIGHT_TYPE_COLDWARM, LIGHT_TYPE_RGBCW]:
self._supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
@ -150,7 +159,13 @@ class TasmotaLight(
white_value = float(attributes["white_value"])
percent_white = white_value / TASMOTA_BRIGHTNESS_MAX
self._white_value = percent_white * 255
if self._tasmota_entity.light_type == LIGHT_TYPE_RGBCW:
if self._tasmota_entity.light_type == LIGHT_TYPE_RGBW:
# Tasmota does not support RGBW mode, set mode to white or rgb
if self._white_value == 0:
self._color_mode = COLOR_MODE_RGB
else:
self._color_mode = COLOR_MODE_WHITE
elif self._tasmota_entity.light_type == LIGHT_TYPE_RGBCW:
# Tasmota does not support RGBWW mode, set mode to ct or rgb
if self._white_value == 0:
self._color_mode = COLOR_MODE_RGB
@ -211,30 +226,9 @@ class TasmotaLight(
blue_compensated = 0
return [red_compensated, green_compensated, blue_compensated]
@property
def rgbw_color(self):
"""Return the rgbw color value."""
if self._rgb is None or self._white_value is None:
return None
rgb = self._rgb
# Tasmota's color is adjusted for brightness, compensate
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))
white_compensated = clamp(round(self._white_value / self._brightness * 255))
else:
red_compensated = 0
green_compensated = 0
blue_compensated = 0
white_compensated = 0
return [red_compensated, green_compensated, blue_compensated, white_compensated]
@property
def force_update(self):
"""Force update."""
if self.color_mode == COLOR_MODE_RGBW:
return True
return False
@property
@ -262,25 +256,14 @@ class TasmotaLight(
rgb = kwargs[ATTR_RGB_COLOR]
attributes["color"] = [rgb[0], rgb[1], rgb[2]]
if ATTR_RGBW_COLOR in kwargs and COLOR_MODE_RGBW in supported_color_modes:
rgbw = kwargs[ATTR_RGBW_COLOR]
attributes["color"] = [rgbw[0], rgbw[1], rgbw[2], rgbw[3]]
# Tasmota does not support direct RGBW control, the light must be set to
# either white mode or color mode. Set the mode to white if white channel
# is on, and to color otherwise
if ATTR_WHITE in kwargs and COLOR_MODE_WHITE in supported_color_modes:
attributes["white_value"] = scale_brightness(kwargs[ATTR_WHITE])
if ATTR_TRANSITION in kwargs:
attributes["transition"] = kwargs[ATTR_TRANSITION]
if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes):
brightness_normalized = kwargs[ATTR_BRIGHTNESS] / DEFAULT_BRIGHTNESS_MAX
device_brightness = min(
round(brightness_normalized * TASMOTA_BRIGHTNESS_MAX),
TASMOTA_BRIGHTNESS_MAX,
)
# Make sure the brightness is not rounded down to 0
device_brightness = max(device_brightness, 1)
attributes["brightness"] = device_brightness
attributes["brightness"] = scale_brightness(kwargs[ATTR_BRIGHTNESS])
if ATTR_COLOR_TEMP in kwargs and COLOR_MODE_COLOR_TEMP in supported_color_modes:
attributes["color_temp"] = int(kwargs[ATTR_COLOR_TEMP])