Tuya: Prevent loop when setting colors on case-sensitive dps (#4809)

Co-authored-by: Samuel Sieb <samuel-github@sieb.net>
This commit is contained in:
richardhopton 2023-05-12 20:16:28 -07:00 committed by Jesse Hills
parent af95e781f5
commit e0ee8ca17c
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

View file

@ -57,37 +57,43 @@ void TuyaLight::setup() {
return; return;
} }
float red, green, blue;
switch (*this->color_type_) { switch (*this->color_type_) {
case TuyaColorType::RGBHSV: case TuyaColorType::RGBHSV:
case TuyaColorType::RGB: { case TuyaColorType::RGB: {
auto red = parse_hex<uint8_t>(datapoint.value_string.substr(0, 2)); auto rgb = parse_hex<uint32_t>(datapoint.value_string.substr(0, 6));
auto green = parse_hex<uint8_t>(datapoint.value_string.substr(2, 2)); if (!rgb.has_value())
auto blue = parse_hex<uint8_t>(datapoint.value_string.substr(4, 2)); return;
if (red.has_value() && green.has_value() && blue.has_value()) {
auto rgb_call = this->state_->make_call(); red = (*rgb >> 16) / 255.0f;
rgb_call.set_rgb(float(*red) / 255, float(*green) / 255, float(*blue) / 255); green = ((*rgb >> 8) & 0xff) / 255.0f;
rgb_call.perform(); blue = (*rgb & 0xff) / 255.0f;
}
break; break;
} }
case TuyaColorType::HSV: { case TuyaColorType::HSV: {
auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4)); auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4));
auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4)); auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4));
auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4)); auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4));
if (hue.has_value() && saturation.has_value() && value.has_value()) { if (!hue.has_value() || !saturation.has_value() || !value.has_value())
float red, green, blue; return;
hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue); hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue);
auto rgb_call = this->state_->make_call();
rgb_call.set_rgb(red, green, blue);
rgb_call.perform();
}
break; break;
} }
} }
float current_red, current_green, current_blue;
this->state_->current_values_as_rgb(&current_red, &current_green, &current_blue);
if (red == current_red && green == current_green && blue == current_blue)
return;
auto rgb_call = this->state_->make_call();
rgb_call.set_rgb(red, green, blue);
rgb_call.perform();
}); });
} }
if (min_value_datapoint_id_.has_value()) { if (min_value_datapoint_id_.has_value()) {
parent_->set_integer_datapoint_value(*this->min_value_datapoint_id_, this->min_value_); this->parent_->set_integer_datapoint_value(*this->min_value_datapoint_id_, this->min_value_);
} }
} }
@ -156,7 +162,7 @@ void TuyaLight::write_state(light::LightState *state) {
} }
if (!state->current_values.is_on() && this->switch_id_.has_value()) { if (!state->current_values.is_on() && this->switch_id_.has_value()) {
parent_->set_boolean_datapoint_value(*this->switch_id_, false); this->parent_->set_boolean_datapoint_value(*this->switch_id_, false);
return; return;
} }
@ -166,14 +172,14 @@ void TuyaLight::write_state(light::LightState *state) {
if (this->color_temperature_invert_) { if (this->color_temperature_invert_) {
color_temp_int = this->color_temperature_max_value_ - color_temp_int; color_temp_int = this->color_temperature_max_value_ - color_temp_int;
} }
parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int); this->parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int);
} }
if (this->dimmer_id_.has_value()) { if (this->dimmer_id_.has_value()) {
auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_); auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
brightness_int = std::max(brightness_int, this->min_value_); brightness_int = std::max(brightness_int, this->min_value_);
parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int); this->parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int);
} }
} }
@ -210,7 +216,7 @@ void TuyaLight::write_state(light::LightState *state) {
} }
if (this->switch_id_.has_value()) { if (this->switch_id_.has_value()) {
parent_->set_boolean_datapoint_value(*this->switch_id_, true); this->parent_->set_boolean_datapoint_value(*this->switch_id_, true);
} }
} }