Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions hatasmota/light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tasmota light."""
import logging
from distutils.version import LooseVersion

import attr

Expand All @@ -18,6 +19,7 @@
CONF_MAC,
CONF_OPTIONS,
CONF_RELAY,
CONF_SW_VERSION,
CONF_TUYA,
LST_COLDWARM,
LST_NONE,
Expand Down Expand Up @@ -92,6 +94,7 @@ class TasmotaLightConfig(TasmotaAvailabilityConfig, TasmotaEntityConfig):
not_power_linked: bool = attr.ib()
poll_topic: str = attr.ib()
result_topic: str = attr.ib()
rgbw: bool = attr.ib()
state_power_off: str = attr.ib()
state_power_on: str = attr.ib()
state_topic: str = attr.ib()
Expand Down Expand Up @@ -134,6 +137,13 @@ def from_discovery_message(cls, config, idx, platform):
if config[CONF_OPTIONS][OPTION_REDUCED_CT_RANGE]:
min_mireds = REDUCED_MIN_MIREDS
max_mireds = REDUCED_MAX_MIREDS
support_rgbw = LooseVersion(config[CONF_SW_VERSION]) >= LooseVersion("9.4.0.4")
if support_rgbw and light_type == LIGHT_TYPE_RGBW:
rgbw = True
dimmer_idx = 4 # Brightness controlled by DIMMER4
dimmer_cmd = f"{COMMAND_DIMMER}{dimmer_idx}"
else:
rgbw = False

return cls(
endpoint="light",
Expand All @@ -156,6 +166,7 @@ def from_discovery_message(cls, config, idx, platform):
min_mireds=min_mireds,
not_power_linked=config[CONF_OPTIONS][OPTION_NOT_POWER_LINKED],
result_topic=get_topic_stat_result(config),
rgbw=rgbw,
state_power_off=config_get_state_power_off(config),
state_power_on=config_get_state_power_on(config),
state_topic=get_topic_tele_state(config),
Expand Down Expand Up @@ -334,7 +345,22 @@ def _set_state_light(self, state, attributes):

commands.append((command, argument))

if "color" in attributes:
if "color" in attributes and len(attributes["color"]) == 4:
color = attributes["color"]
if self._cfg.rgbw:
argument = f"{color[0]},{color[1]},{color[2]},{color[3]}"
command = f"{COMMAND_COLOR}2"
commands.append((command, argument))
elif color[3] == 0:
argument = f"{color[0]},{color[1]},{color[2]}"
command = f"{COMMAND_COLOR}2"
commands.append((command, argument))
else:
white_value_normalized = color[3] / 255
argument = min(round(white_value_normalized * 100), 100)
command = COMMAND_WHITE
commands.append((command, argument))
elif "color" in attributes:
color = attributes["color"]
argument = f"{color[0]},{color[1]},{color[2]}"
command = f"{COMMAND_COLOR}2"
Expand All @@ -351,10 +377,6 @@ def _set_state_light(self, state, attributes):
commands.append((command, argument))
except ValueError:
_LOGGER.debug("Unknown effect %s", effect)
if "white_value" in attributes:
argument = attributes["white_value"]
command = COMMAND_WHITE
commands.append((command, argument))

if self._cfg.not_power_linked and "brightness" in attributes:
# Always send power
Expand Down