From c3938d04f3b1c01afc8d69c498793072687a9aa6 Mon Sep 17 00:00:00 2001 From: Ryan Mounce Date: Wed, 2 Jun 2021 07:02:16 +0930 Subject: [PATCH] Support Tuya light color temperature control (#1412) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/tuya/light/__init__.py | 28 ++++++++++++++++++++ esphome/components/tuya/light/tuya_light.cpp | 25 +++++++++++++++++ esphome/components/tuya/light/tuya_light.h | 14 ++++++++++ tests/test4.yaml | 11 ++++++++ 4 files changed, 78 insertions(+) diff --git a/esphome/components/tuya/light/__init__.py b/esphome/components/tuya/light/__init__.py index 6dd8b6b775..979082d636 100644 --- a/esphome/components/tuya/light/__init__.py +++ b/esphome/components/tuya/light/__init__.py @@ -8,6 +8,8 @@ from esphome.const import ( CONF_GAMMA_CORRECT, CONF_DEFAULT_TRANSITION_LENGTH, CONF_SWITCH_DATAPOINT, + CONF_COLD_WHITE_COLOR_TEMPERATURE, + CONF_WARM_WHITE_COLOR_TEMPERATURE, ) from .. import tuya_ns, CONF_TUYA_ID, Tuya @@ -15,6 +17,8 @@ DEPENDENCIES = ["tuya"] CONF_DIMMER_DATAPOINT = "dimmer_datapoint" CONF_MIN_VALUE_DATAPOINT = "min_value_datapoint" +CONF_COLOR_TEMPERATURE_DATAPOINT = "color_temperature_datapoint" +CONF_COLOR_TEMPERATURE_MAX_VALUE = "color_temperature_max_value" TuyaLight = tuya_ns.class_("TuyaLight", light.LightOutput, cg.Component) @@ -26,8 +30,18 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_DIMMER_DATAPOINT): cv.uint8_t, cv.Optional(CONF_MIN_VALUE_DATAPOINT): cv.uint8_t, cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t, + cv.Inclusive( + CONF_COLOR_TEMPERATURE_DATAPOINT, "color_temperature" + ): cv.uint8_t, cv.Optional(CONF_MIN_VALUE): cv.int_, cv.Optional(CONF_MAX_VALUE): cv.int_, + cv.Optional(CONF_COLOR_TEMPERATURE_MAX_VALUE): cv.int_, + cv.Inclusive( + CONF_COLD_WHITE_COLOR_TEMPERATURE, "color_temperature" + ): cv.color_temperature, + cv.Inclusive( + CONF_WARM_WHITE_COLOR_TEMPERATURE, "color_temperature" + ): cv.color_temperature, # Change the default gamma_correct and default transition length settings. # The Tuya MCU handles transitions and gamma correction on its own. cv.Optional(CONF_GAMMA_CORRECT, default=1.0): cv.positive_float, @@ -51,9 +65,23 @@ async def to_code(config): cg.add(var.set_min_value_datapoint_id(config[CONF_MIN_VALUE_DATAPOINT])) if CONF_SWITCH_DATAPOINT in config: cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT])) + if CONF_COLOR_TEMPERATURE_DATAPOINT in config: + cg.add(var.set_color_temperature_id(config[CONF_COLOR_TEMPERATURE_DATAPOINT])) + cg.add( + var.set_cold_white_temperature(config[CONF_COLD_WHITE_COLOR_TEMPERATURE]) + ) + cg.add( + var.set_warm_white_temperature(config[CONF_WARM_WHITE_COLOR_TEMPERATURE]) + ) if CONF_MIN_VALUE in config: cg.add(var.set_min_value(config[CONF_MIN_VALUE])) if CONF_MAX_VALUE in config: cg.add(var.set_max_value(config[CONF_MAX_VALUE])) + if CONF_COLOR_TEMPERATURE_MAX_VALUE in config: + cg.add( + var.set_color_temperature_max_value( + config[CONF_COLOR_TEMPERATURE_MAX_VALUE] + ) + ) paren = await cg.get_variable(config[CONF_TUYA_ID]) cg.add(var.set_tuya_parent(paren)) diff --git a/esphome/components/tuya/light/tuya_light.cpp b/esphome/components/tuya/light/tuya_light.cpp index e7b44882a1..f03e66e150 100644 --- a/esphome/components/tuya/light/tuya_light.cpp +++ b/esphome/components/tuya/light/tuya_light.cpp @@ -7,6 +7,15 @@ namespace tuya { static const char *TAG = "tuya.light"; void TuyaLight::setup() { + if (this->color_temperature_id_.has_value()) { + this->parent_->register_listener(*this->color_temperature_id_, [this](TuyaDatapoint datapoint) { + auto call = this->state_->make_call(); + call.set_color_temperature(this->cold_white_temperature_ + + (this->warm_white_temperature_ - this->cold_white_temperature_) * + (float(datapoint.value_uint) / float(this->color_temperature_max_value_))); + call.perform(); + }); + } if (this->dimmer_id_.has_value()) { this->parent_->register_listener(*this->dimmer_id_, [this](TuyaDatapoint datapoint) { auto call = this->state_->make_call(); @@ -41,6 +50,11 @@ void TuyaLight::dump_config() { light::LightTraits TuyaLight::get_traits() { auto traits = light::LightTraits(); traits.set_supports_brightness(this->dimmer_id_.has_value()); + traits.set_supports_color_temperature(this->color_temperature_id_.has_value()); + if (this->color_temperature_id_.has_value()) { + traits.set_min_mireds(this->cold_white_temperature_); + traits.set_max_mireds(this->warm_white_temperature_); + } return traits; } @@ -69,6 +83,17 @@ void TuyaLight::write_state(light::LightState *state) { return; } + if (this->color_temperature_id_.has_value()) { + TuyaDatapoint datapoint{}; + datapoint.id = *this->color_temperature_id_; + datapoint.type = TuyaDatapointType::INTEGER; + datapoint.value_int = + static_cast(this->color_temperature_max_value_ * + (state->current_values.get_color_temperature() - this->cold_white_temperature_) / + (this->warm_white_temperature_ - this->cold_white_temperature_)); + parent_->set_datapoint_value(datapoint); + } + auto brightness_int = static_cast(brightness * this->max_value_); brightness_int = std::max(brightness_int, this->min_value_); diff --git a/esphome/components/tuya/light/tuya_light.h b/esphome/components/tuya/light/tuya_light.h index 896c0cc7ef..72422bc9e7 100644 --- a/esphome/components/tuya/light/tuya_light.h +++ b/esphome/components/tuya/light/tuya_light.h @@ -16,9 +16,19 @@ class TuyaLight : public Component, public light::LightOutput { this->min_value_datapoint_id_ = min_value_datapoint_id; } void set_switch_id(uint8_t switch_id) { this->switch_id_ = switch_id; } + void set_color_temperature_id(uint8_t color_temperature_id) { this->color_temperature_id_ = color_temperature_id; } void set_tuya_parent(Tuya *parent) { this->parent_ = parent; } void set_min_value(uint32_t min_value) { min_value_ = min_value; } void set_max_value(uint32_t max_value) { max_value_ = max_value; } + void set_color_temperature_max_value(uint32_t color_temperature_max_value) { + this->color_temperature_max_value_ = color_temperature_max_value; + } + void set_cold_white_temperature(float cold_white_temperature) { + this->cold_white_temperature_ = cold_white_temperature; + } + void set_warm_white_temperature(float warm_white_temperature) { + this->warm_white_temperature_ = warm_white_temperature; + } light::LightTraits get_traits() override; void setup_state(light::LightState *state) override; void write_state(light::LightState *state) override; @@ -31,8 +41,12 @@ class TuyaLight : public Component, public light::LightOutput { optional dimmer_id_{}; optional min_value_datapoint_id_{}; optional switch_id_{}; + optional color_temperature_id_{}; uint32_t min_value_ = 0; uint32_t max_value_ = 255; + uint32_t color_temperature_max_value_ = 255; + float cold_white_temperature_; + float warm_white_temperature_; light::LightState *state_{nullptr}; }; diff --git a/tests/test4.yaml b/tests/test4.yaml index 08213fab87..9e09f20449 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -133,6 +133,17 @@ light: rgb_order: GRB default_transition_length: 0s color_correct: [50%, 50%, 50%] + - platform: tuya + id: tuya_light + switch_datapoint: 1 + dimmer_datapoint: 2 + min_value_datapoint: 3 + color_temperature_datapoint: 4 + min_value: 1 + max_value: 100 + cold_white_color_temperature: 153 mireds + warm_white_color_temperature: 500 mireds + gamma_correct: 1 display: - platform: addressable_light