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_COLOR_TEMP,
ATTR_EFFECT, ATTR_EFFECT,
ATTR_RGB_COLOR, ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_WHITE,
COLOR_MODE_BRIGHTNESS, COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP, COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF, COLOR_MODE_ONOFF,
COLOR_MODE_RGB, COLOR_MODE_RGB,
COLOR_MODE_RGBW, COLOR_MODE_WHITE,
SUPPORT_EFFECT, SUPPORT_EFFECT,
SUPPORT_TRANSITION, SUPPORT_TRANSITION,
LightEntity, LightEntity,
@ -60,6 +60,17 @@ def clamp(value):
return min(max(value, 0), 255) 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( class TasmotaLight(
TasmotaAvailability, TasmotaAvailability,
TasmotaDiscoveryUpdate, TasmotaDiscoveryUpdate,
@ -80,7 +91,6 @@ class TasmotaLight(
self._white_value = None self._white_value = None
self._flash_times = None self._flash_times = None
self._rgb = None self._rgb = None
self._rgbw = None
super().__init__( super().__init__(
**kwds, **kwds,
@ -107,8 +117,7 @@ class TasmotaLight(
self._color_mode = COLOR_MODE_RGB self._color_mode = COLOR_MODE_RGB
if light_type == LIGHT_TYPE_RGBW: if light_type == LIGHT_TYPE_RGBW:
self._supported_color_modes.add(COLOR_MODE_RGBW) self._supported_color_modes.add(COLOR_MODE_WHITE)
self._color_mode = COLOR_MODE_RGBW
if light_type in [LIGHT_TYPE_COLDWARM, LIGHT_TYPE_RGBCW]: if light_type in [LIGHT_TYPE_COLDWARM, LIGHT_TYPE_RGBCW]:
self._supported_color_modes.add(COLOR_MODE_COLOR_TEMP) self._supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
@ -150,7 +159,13 @@ class TasmotaLight(
white_value = float(attributes["white_value"]) white_value = float(attributes["white_value"])
percent_white = white_value / TASMOTA_BRIGHTNESS_MAX percent_white = white_value / TASMOTA_BRIGHTNESS_MAX
self._white_value = percent_white * 255 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 # Tasmota does not support RGBWW mode, set mode to ct or rgb
if self._white_value == 0: if self._white_value == 0:
self._color_mode = COLOR_MODE_RGB self._color_mode = COLOR_MODE_RGB
@ -211,30 +226,9 @@ class TasmotaLight(
blue_compensated = 0 blue_compensated = 0
return [red_compensated, green_compensated, blue_compensated] 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 @property
def force_update(self): def force_update(self):
"""Force update.""" """Force update."""
if self.color_mode == COLOR_MODE_RGBW:
return True
return False return False
@property @property
@ -262,25 +256,14 @@ class TasmotaLight(
rgb = kwargs[ATTR_RGB_COLOR] rgb = kwargs[ATTR_RGB_COLOR]
attributes["color"] = [rgb[0], rgb[1], rgb[2]] attributes["color"] = [rgb[0], rgb[1], rgb[2]]
if ATTR_RGBW_COLOR in kwargs and COLOR_MODE_RGBW in supported_color_modes: if ATTR_WHITE in kwargs and COLOR_MODE_WHITE in supported_color_modes:
rgbw = kwargs[ATTR_RGBW_COLOR] attributes["white_value"] = scale_brightness(kwargs[ATTR_WHITE])
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_TRANSITION in kwargs: if ATTR_TRANSITION in kwargs:
attributes["transition"] = kwargs[ATTR_TRANSITION] attributes["transition"] = kwargs[ATTR_TRANSITION]
if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes): if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes):
brightness_normalized = kwargs[ATTR_BRIGHTNESS] / DEFAULT_BRIGHTNESS_MAX attributes["brightness"] = scale_brightness(kwargs[ATTR_BRIGHTNESS])
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
if ATTR_COLOR_TEMP in kwargs and COLOR_MODE_COLOR_TEMP in supported_color_modes: if ATTR_COLOR_TEMP in kwargs and COLOR_MODE_COLOR_TEMP in supported_color_modes:
attributes["color_temp"] = int(kwargs[ATTR_COLOR_TEMP]) attributes["color_temp"] = int(kwargs[ATTR_COLOR_TEMP])

View file

@ -3,7 +3,7 @@
"name": "Tasmota", "name": "Tasmota",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tasmota", "documentation": "https://www.home-assistant.io/integrations/tasmota",
"requirements": ["hatasmota==0.2.14"], "requirements": ["hatasmota==0.2.15"],
"dependencies": ["mqtt"], "dependencies": ["mqtt"],
"mqtt": ["tasmota/discovery/#"], "mqtt": ["tasmota/discovery/#"],
"codeowners": ["@emontnemery"], "codeowners": ["@emontnemery"],

View file

@ -741,7 +741,7 @@ hass-nabucasa==0.43.0
hass_splunk==0.1.1 hass_splunk==0.1.1
# homeassistant.components.tasmota # homeassistant.components.tasmota
hatasmota==0.2.14 hatasmota==0.2.15
# homeassistant.components.jewish_calendar # homeassistant.components.jewish_calendar
hdate==0.10.2 hdate==0.10.2

View file

@ -414,7 +414,7 @@ hangups==0.4.14
hass-nabucasa==0.43.0 hass-nabucasa==0.43.0
# homeassistant.components.tasmota # homeassistant.components.tasmota
hatasmota==0.2.14 hatasmota==0.2.15
# homeassistant.components.jewish_calendar # homeassistant.components.jewish_calendar
hdate==0.10.2 hdate==0.10.2

View file

@ -17,6 +17,7 @@ from homeassistant.components.light import (
ATTR_RGBW_COLOR, ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR, ATTR_RGBWW_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_WHITE,
ATTR_WHITE_VALUE, ATTR_WHITE_VALUE,
ATTR_XY_COLOR, ATTR_XY_COLOR,
DOMAIN, DOMAIN,
@ -50,6 +51,7 @@ def turn_on(
flash=None, flash=None,
effect=None, effect=None,
color_name=None, color_name=None,
white=None,
): ):
"""Turn all or specified light on.""" """Turn all or specified light on."""
hass.add_job( hass.add_job(
@ -71,6 +73,7 @@ def turn_on(
flash, flash,
effect, effect,
color_name, color_name,
white,
) )
@ -92,6 +95,7 @@ async def async_turn_on(
flash=None, flash=None,
effect=None, effect=None,
color_name=None, color_name=None,
white=None,
): ):
"""Turn all or specified light on.""" """Turn all or specified light on."""
data = { data = {
@ -113,6 +117,7 @@ async def async_turn_on(
(ATTR_FLASH, flash), (ATTR_FLASH, flash),
(ATTR_EFFECT, effect), (ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name), (ATTR_COLOR_NAME, color_name),
(ATTR_WHITE, white),
] ]
if value is not None if value is not None
} }

View file

@ -223,8 +223,8 @@ async def test_attributes_rgbw(hass, mqtt_mock, setup_tasmota):
state.attributes.get("supported_features") state.attributes.get("supported_features")
== SUPPORT_EFFECT | SUPPORT_TRANSITION == SUPPORT_EFFECT | SUPPORT_TRANSITION
) )
assert state.attributes.get("supported_color_modes") == ["rgb", "rgbw"] assert state.attributes.get("supported_color_modes") == ["rgb", "white"]
assert state.attributes.get("color_mode") == "rgbw" assert state.attributes.get("color_mode") == "rgb"
async def test_attributes_rgbww(hass, mqtt_mock, setup_tasmota): async def test_attributes_rgbww(hass, mqtt_mock, setup_tasmota):
@ -434,7 +434,7 @@ async def test_controlling_state_via_mqtt_rgbw(hass, mqtt_mock, setup_tasmota):
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON"}') async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON"}')
state = hass.states.get("light.test") state = hass.states.get("light.test")
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get("color_mode") == "rgbw" assert state.attributes.get("color_mode") == "rgb"
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"OFF"}') async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"OFF"}')
state = hass.states.get("light.test") state = hass.states.get("light.test")
@ -442,24 +442,31 @@ async def test_controlling_state_via_mqtt_rgbw(hass, mqtt_mock, setup_tasmota):
assert "color_mode" not in state.attributes assert "color_mode" not in state.attributes
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Dimmer":50}' hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Dimmer":50,"White":0}'
) )
state = hass.states.get("light.test") state = hass.states.get("light.test")
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get("brightness") == 127.5 assert state.attributes.get("brightness") == 127.5
assert state.attributes.get("color_mode") == "rgbw" assert state.attributes.get("color_mode") == "rgb"
async_fire_mqtt_message(
hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Dimmer":75,"White":75}'
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 191.25
assert state.attributes.get("color_mode") == "white"
async_fire_mqtt_message( async_fire_mqtt_message(
hass, hass,
"tasmota_49A3BC/tele/STATE", "tasmota_49A3BC/tele/STATE",
'{"POWER":"ON","Color":"128,64,0","White":0}', '{"POWER":"ON","Dimmer":50,"Color":"128,64,0","White":0}',
) )
state = hass.states.get("light.test") state = hass.states.get("light.test")
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get("brightness") == 127.5 assert state.attributes.get("brightness") == 127.5
assert state.attributes.get("rgb_color") == (255, 128, 0) assert state.attributes.get("rgb_color") == (255, 128, 0)
assert state.attributes.get("rgbw_color") == (255, 128, 0, 0) assert state.attributes.get("color_mode") == "rgb"
assert state.attributes.get("color_mode") == "rgbw"
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","White":50}' hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","White":50}'
@ -467,9 +474,8 @@ async def test_controlling_state_via_mqtt_rgbw(hass, mqtt_mock, setup_tasmota):
state = hass.states.get("light.test") state = hass.states.get("light.test")
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get("brightness") == 127.5 assert state.attributes.get("brightness") == 127.5
assert state.attributes.get("rgb_color") == (255, 192, 128) assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") == (255, 128, 0, 255) assert state.attributes.get("color_mode") == "white"
assert state.attributes.get("color_mode") == "rgbw"
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Dimmer":0}' hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Dimmer":0}'
@ -477,9 +483,8 @@ async def test_controlling_state_via_mqtt_rgbw(hass, mqtt_mock, setup_tasmota):
state = hass.states.get("light.test") state = hass.states.get("light.test")
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get("brightness") == 0 assert state.attributes.get("brightness") == 0
assert state.attributes.get("rgb_color") == (0, 0, 0) assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") == (0, 0, 0, 0) assert state.attributes.get("color_mode") == "white"
assert state.attributes.get("color_mode") == "rgbw"
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Scheme":3}' hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON","Scheme":3}'
@ -959,21 +964,31 @@ async def test_sending_mqtt_commands_rgbw_legacy(hass, mqtt_mock, setup_tasmota)
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
# Set color when setting white is off # Set white when setting white
await common.async_turn_on(hass, "light.test", rgbw_color=[128, 64, 32, 0]) await common.async_turn_on(hass, "light.test", white=128)
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog", "tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Power1 ON;NoDelay;Color2 128,64,32", "NoDelay;Power1 ON;NoDelay;White 50",
0, 0,
False, False,
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
# Set white when white is on # rgbw_color should be ignored
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",
0,
False,
)
mqtt_mock.async_publish.reset_mock()
# rgbw_color should be ignored
await common.async_turn_on(hass, "light.test", rgbw_color=[16, 64, 32, 128]) await common.async_turn_on(hass, "light.test", rgbw_color=[16, 64, 32, 128])
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog", "tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Power1 ON;NoDelay;White 50", "NoDelay;Power1 ON",
0, 0,
False, False,
) )
@ -1041,7 +1056,7 @@ async def test_sending_mqtt_commands_rgbw(hass, mqtt_mock, setup_tasmota):
# Turn the light on and verify MQTT messages are sent # Turn the light on and verify MQTT messages are sent
await common.async_turn_on(hass, "light.test", brightness=192) await common.async_turn_on(hass, "light.test", brightness=192)
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog", "NoDelay;Dimmer4 75", 0, False "tasmota_49A3BC/cmnd/Backlog", "NoDelay;Dimmer 75", 0, False
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
@ -1055,21 +1070,31 @@ async def test_sending_mqtt_commands_rgbw(hass, mqtt_mock, setup_tasmota):
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
# Set color when setting white is off # Set white when setting white
await common.async_turn_on(hass, "light.test", rgbw_color=[128, 64, 32, 0]) await common.async_turn_on(hass, "light.test", white=128)
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog", "tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Power1 ON;NoDelay;Color2 128,64,32,0", "NoDelay;Power1 ON;NoDelay;White 50",
0, 0,
False, False,
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
# Set white when white is on # rgbw_color should be ignored
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",
0,
False,
)
mqtt_mock.async_publish.reset_mock()
# rgbw_color should be ignored
await common.async_turn_on(hass, "light.test", rgbw_color=[16, 64, 32, 128]) await common.async_turn_on(hass, "light.test", rgbw_color=[16, 64, 32, 128])
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog", "tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Power1 ON;NoDelay;Color2 16,64,32,128", "NoDelay;Power1 ON",
0, 0,
False, False,
) )