From 6a85259e4d6d1b0a0f819688b8e555efcb99ecb0 Mon Sep 17 00:00:00 2001 From: VitaliyKurokhtin Date: Tue, 7 Jun 2022 04:07:08 -0700 Subject: [PATCH] Block Tuya light from reacting to dp changes if transitioning (#3076) --- esphome/components/tuya/light/tuya_light.cpp | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/esphome/components/tuya/light/tuya_light.cpp b/esphome/components/tuya/light/tuya_light.cpp index 7facc52946..4d8bc9e37b 100644 --- a/esphome/components/tuya/light/tuya_light.cpp +++ b/esphome/components/tuya/light/tuya_light.cpp @@ -10,6 +10,11 @@ static const char *const TAG = "tuya.light"; void TuyaLight::setup() { if (this->color_temperature_id_.has_value()) { this->parent_->register_listener(*this->color_temperature_id_, [this](const TuyaDatapoint &datapoint) { + if (this->state_->current_values != this->state_->remote_values) { + ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored"); + return; + } + auto datapoint_value = datapoint.value_uint; if (this->color_temperature_invert_) { datapoint_value = this->color_temperature_max_value_ - datapoint_value; @@ -23,6 +28,11 @@ void TuyaLight::setup() { } if (this->dimmer_id_.has_value()) { this->parent_->register_listener(*this->dimmer_id_, [this](const TuyaDatapoint &datapoint) { + if (this->state_->current_values != this->state_->remote_values) { + ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored"); + return; + } + auto call = this->state_->make_call(); call.set_brightness(float(datapoint.value_uint) / this->max_value_); call.perform(); @@ -30,6 +40,11 @@ void TuyaLight::setup() { } if (switch_id_.has_value()) { this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) { + if (this->state_->current_values != this->state_->remote_values) { + ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored"); + return; + } + auto call = this->state_->make_call(); call.set_state(datapoint.value_bool); call.perform(); @@ -41,6 +56,11 @@ void TuyaLight::setup() { auto green = parse_hex(datapoint.value_string.substr(2, 2)); auto blue = parse_hex(datapoint.value_string.substr(4, 2)); if (red.has_value() && green.has_value() && blue.has_value()) { + if (this->state_->current_values != this->state_->remote_values) { + ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored"); + return; + } + auto call = this->state_->make_call(); call.set_rgb(float(*red) / 255, float(*green) / 255, float(*blue) / 255); call.perform(); @@ -52,6 +72,11 @@ void TuyaLight::setup() { auto saturation = parse_hex(datapoint.value_string.substr(4, 4)); auto value = parse_hex(datapoint.value_string.substr(8, 4)); if (hue.has_value() && saturation.has_value() && value.has_value()) { + if (this->state_->current_values != this->state_->remote_values) { + ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored"); + return; + } + float red, green, blue; hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue); auto call = this->state_->make_call();