From 297f05d60015cd524efba118dfcf8b32aa617e5d Mon Sep 17 00:00:00 2001 From: lullius Date: Tue, 13 Jan 2026 00:08:33 +0100 Subject: [PATCH] [tuya] add color_type_lowercase option (#13101) Co-authored-by: lullius <> --- esphome/components/tuya/light/__init__.py | 3 +++ esphome/components/tuya/light/tuya_light.cpp | 11 +++++++---- esphome/components/tuya/light/tuya_light.h | 8 +++----- tests/components/tuya/common.yaml | 3 +++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/esphome/components/tuya/light/__init__.py b/esphome/components/tuya/light/__init__.py index 4d2ccba8b1..bf2d3daf98 100644 --- a/esphome/components/tuya/light/__init__.py +++ b/esphome/components/tuya/light/__init__.py @@ -26,6 +26,7 @@ CONF_RGB_DATAPOINT = "rgb_datapoint" CONF_HSV_DATAPOINT = "hsv_datapoint" CONF_COLOR_DATAPOINT = "color_datapoint" CONF_COLOR_TYPE = "color_type" +CONF_COLOR_TYPE_LOWERCASE = "color_type_lowercase" TuyaColorType = tuya_ns.enum("TuyaColorType") @@ -47,6 +48,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t, cv.Inclusive(CONF_COLOR_DATAPOINT, "color"): cv.uint8_t, cv.Inclusive(CONF_COLOR_TYPE, "color"): cv.enum(COLOR_TYPES, upper=True), + cv.Optional(CONF_COLOR_TYPE_LOWERCASE, default=False): cv.boolean, cv.Optional(CONF_COLOR_INTERLOCK, default=False): cv.boolean, cv.Inclusive( CONF_COLOR_TEMPERATURE_DATAPOINT, "color_temperature" @@ -91,6 +93,7 @@ async def to_code(config): if CONF_COLOR_DATAPOINT in config: cg.add(var.set_color_id(config[CONF_COLOR_DATAPOINT])) cg.add(var.set_color_type(config[CONF_COLOR_TYPE])) + cg.add(var.set_color_type_lowercase(config[CONF_COLOR_TYPE_LOWERCASE])) if CONF_COLOR_TEMPERATURE_DATAPOINT in config: cg.add(var.set_color_temperature_id(config[CONF_COLOR_TEMPERATURE_DATAPOINT])) cg.add(var.set_color_temperature_invert(config[CONF_COLOR_TEMPERATURE_INVERT])) diff --git a/esphome/components/tuya/light/tuya_light.cpp b/esphome/components/tuya/light/tuya_light.cpp index 815a089d9f..c487f9f50b 100644 --- a/esphome/components/tuya/light/tuya_light.cpp +++ b/esphome/components/tuya/light/tuya_light.cpp @@ -190,7 +190,8 @@ void TuyaLight::write_state(light::LightState *state) { switch (*this->color_type_) { case TuyaColorType::RGB: { char buffer[7]; - sprintf(buffer, "%02X%02X%02X", int(red * 255), int(green * 255), int(blue * 255)); + const char *format_str = this->color_type_lowercase_ ? "%02x%02x%02x" : "%02X%02X%02X"; + sprintf(buffer, format_str, int(red * 255), int(green * 255), int(blue * 255)); color_value = buffer; break; } @@ -199,7 +200,8 @@ void TuyaLight::write_state(light::LightState *state) { float saturation, value; rgb_to_hsv(red, green, blue, hue, saturation, value); char buffer[13]; - sprintf(buffer, "%04X%04X%04X", hue, int(saturation * 1000), int(value * 1000)); + const char *format_str = this->color_type_lowercase_ ? "%04x%04x%04x" : "%04X%04X%04X"; + sprintf(buffer, format_str, hue, int(saturation * 1000), int(value * 1000)); color_value = buffer; break; } @@ -208,8 +210,9 @@ void TuyaLight::write_state(light::LightState *state) { float saturation, value; rgb_to_hsv(red, green, blue, hue, saturation, value); char buffer[15]; - sprintf(buffer, "%02X%02X%02X%04X%02X%02X", int(red * 255), int(green * 255), int(blue * 255), hue, - int(saturation * 255), int(value * 255)); + const char *format_str = this->color_type_lowercase_ ? "%02x%02x%02x%04x%02x%02x" : "%02X%02X%02X%04X%02X%02X"; + sprintf(buffer, format_str, int(red * 255), int(green * 255), int(blue * 255), hue, int(saturation * 255), + int(value * 255)); color_value = buffer; break; } diff --git a/esphome/components/tuya/light/tuya_light.h b/esphome/components/tuya/light/tuya_light.h index bd9920f18f..ded94f390a 100644 --- a/esphome/components/tuya/light/tuya_light.h +++ b/esphome/components/tuya/light/tuya_light.h @@ -7,11 +7,7 @@ namespace esphome { namespace tuya { -enum TuyaColorType { - RGB, - HSV, - RGBHSV, -}; +enum TuyaColorType { RGB, HSV, RGBHSV }; class TuyaLight : public Component, public light::LightOutput { public: @@ -28,6 +24,7 @@ class TuyaLight : public Component, public light::LightOutput { void set_color_temperature_invert(bool color_temperature_invert) { this->color_temperature_invert_ = color_temperature_invert; } + void set_color_type_lowercase(bool color_type_lowercase) { this->color_type_lowercase_ = color_type_lowercase; } 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; } @@ -63,6 +60,7 @@ class TuyaLight : public Component, public light::LightOutput { float cold_white_temperature_; float warm_white_temperature_; bool color_temperature_invert_{false}; + bool color_type_lowercase_{false}; bool color_interlock_{false}; light::LightState *state_{nullptr}; }; diff --git a/tests/components/tuya/common.yaml b/tests/components/tuya/common.yaml index e177b7d056..9986d398f1 100644 --- a/tests/components/tuya/common.yaml +++ b/tests/components/tuya/common.yaml @@ -38,11 +38,14 @@ light: dimmer_datapoint: 2 min_value_datapoint: 3 color_temperature_datapoint: 4 + color_datapoint: 5 min_value: 1 max_value: 100 cold_white_color_temperature: 153 mireds warm_white_color_temperature: 500 mireds gamma_correct: 1 + color_type: RGB + color_type_lowercase: true number: - platform: tuya