ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
tuya_light.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tuya_light.h"
4
5namespace esphome::tuya {
6
7static const char *const TAG = "tuya.light";
8
10 if (this->color_temperature_id_.has_value()) {
11 this->parent_->register_listener(*this->color_temperature_id_, [this](const TuyaDatapoint &datapoint) {
12 if (this->state_->current_values != this->state_->remote_values) {
13 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
14 return;
15 }
16
17 auto datapoint_value = datapoint.value_uint;
18 if (this->color_temperature_invert_) {
19 datapoint_value = this->color_temperature_max_value_ - datapoint_value;
20 }
21 auto call = this->state_->make_call();
24 (float(datapoint_value) / this->color_temperature_max_value_));
25 call.perform();
26 });
27 }
28 if (this->dimmer_id_.has_value()) {
29 this->parent_->register_listener(*this->dimmer_id_, [this](const TuyaDatapoint &datapoint) {
30 if (this->state_->current_values != this->state_->remote_values) {
31 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
32 return;
33 }
34
35 auto call = this->state_->make_call();
36 call.set_brightness(float(datapoint.value_uint) / this->max_value_);
37 call.perform();
38 });
39 }
40 if (switch_id_.has_value()) {
41 this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
42 if (this->state_->current_values != this->state_->remote_values) {
43 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
44 return;
45 }
46
47 auto call = this->state_->make_call();
48 call.set_state(datapoint.value_bool);
49 call.perform();
50 });
51 }
52 if (color_id_.has_value()) {
53 this->parent_->register_listener(*this->color_id_, [this](const TuyaDatapoint &datapoint) {
54 if (this->state_->current_values != this->state_->remote_values) {
55 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
56 return;
57 }
58
59 if (!this->color_type_.has_value())
60 return;
61
62 float red, green, blue;
63 switch (*this->color_type_) {
65 case TuyaColorType::RGB: {
66 auto rgb = parse_hex<uint32_t>(datapoint.value_string.substr(0, 6));
67 if (!rgb.has_value())
68 return;
69
70 red = (*rgb >> 16) / 255.0f;
71 green = ((*rgb >> 8) & 0xff) / 255.0f;
72 blue = (*rgb & 0xff) / 255.0f;
73 break;
74 }
75 case TuyaColorType::HSV: {
76 auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4));
77 auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4));
78 auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4));
79 if (!hue.has_value() || !saturation.has_value() || !value.has_value())
80 return;
81
82 hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue);
83 break;
84 }
85 }
86
87 float current_red, current_green, current_blue;
88 this->state_->current_values_as_rgb(&current_red, &current_green, &current_blue);
89 if (red == current_red && green == current_green && blue == current_blue)
90 return;
91 auto rgb_call = this->state_->make_call();
92 rgb_call.set_rgb(red, green, blue);
93 rgb_call.perform();
94 });
95 }
96
97 if (min_value_datapoint_id_.has_value()) {
99 }
100}
101
103 ESP_LOGCONFIG(TAG, "Tuya Dimmer:");
104 if (this->dimmer_id_.has_value()) {
105 ESP_LOGCONFIG(TAG, " Dimmer has datapoint ID %u", *this->dimmer_id_);
106 }
107 if (this->switch_id_.has_value()) {
108 ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
109 }
110 if (this->color_id_.has_value()) {
111 ESP_LOGCONFIG(TAG, " Color has datapoint ID %u", *this->color_id_);
112 }
113}
114
116 auto traits = light::LightTraits();
117 if (this->color_temperature_id_.has_value() && this->dimmer_id_.has_value()) {
118 if (this->color_id_.has_value()) {
119 if (this->color_interlock_) {
120 traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::COLOR_TEMPERATURE});
121 } else {
122 traits.set_supported_color_modes(
124 }
125 } else {
126 traits.set_supported_color_modes({light::ColorMode::COLOR_TEMPERATURE});
127 }
128 traits.set_min_mireds(this->cold_white_temperature_);
129 traits.set_max_mireds(this->warm_white_temperature_);
130 } else if (this->color_id_.has_value()) {
131 if (this->dimmer_id_.has_value()) {
132 if (this->color_interlock_) {
133 traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::WHITE});
134 } else {
135 traits.set_supported_color_modes({light::ColorMode::RGB_WHITE});
136 }
137 } else {
138 traits.set_supported_color_modes({light::ColorMode::RGB});
139 }
140 } else if (this->dimmer_id_.has_value()) {
141 traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
142 } else {
143 traits.set_supported_color_modes({light::ColorMode::ON_OFF});
144 }
145 return traits;
146}
147
149
151 float red = 0.0f, green = 0.0f, blue = 0.0f;
152 float color_temperature = 0.0f, brightness = 0.0f;
153
154 if (this->color_id_.has_value()) {
155 if (this->color_temperature_id_.has_value()) {
156 state->current_values_as_rgbct(&red, &green, &blue, &color_temperature, &brightness);
157 } else if (this->dimmer_id_.has_value()) {
158 state->current_values_as_rgbw(&red, &green, &blue, &brightness);
159 } else {
160 state->current_values_as_rgb(&red, &green, &blue);
161 }
162 } else if (this->color_temperature_id_.has_value()) {
163 state->current_values_as_ct(&color_temperature, &brightness);
164 } else {
165 state->current_values_as_brightness(&brightness);
166 }
167
168 if (!state->current_values.is_on() && this->switch_id_.has_value()) {
169 this->parent_->set_boolean_datapoint_value(*this->switch_id_, false);
170 return;
171 }
172
173 if (brightness > 0.0f || !color_interlock_) {
174 if (this->color_temperature_id_.has_value()) {
175 uint32_t color_temp_int = static_cast<uint32_t>(roundf(color_temperature * this->color_temperature_max_value_));
176 if (this->color_temperature_invert_) {
177 color_temp_int = this->color_temperature_max_value_ - color_temp_int;
178 }
179 this->parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int);
180 }
181
182 if (this->dimmer_id_.has_value()) {
183 auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
184 brightness_int = std::max(brightness_int, this->min_value_);
185
186 this->parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int);
187 }
188 }
189
190 if (this->color_id_.has_value() && this->color_type_.has_value() && (brightness == 0.0f || !color_interlock_)) {
191 std::string color_value;
192 switch (*this->color_type_) {
193 case TuyaColorType::RGB: {
194 char buffer[7];
195 const char *format_str = this->color_type_lowercase_ ? "%02x%02x%02x" : "%02X%02X%02X";
196 snprintf(buffer, sizeof(buffer), format_str, int(red * 255), int(green * 255), int(blue * 255));
197 color_value = buffer;
198 break;
199 }
200 case TuyaColorType::HSV: {
201 int hue;
202 float saturation, value;
203 rgb_to_hsv(red, green, blue, hue, saturation, value);
204 char buffer[13];
205 const char *format_str = this->color_type_lowercase_ ? "%04x%04x%04x" : "%04X%04X%04X";
206 snprintf(buffer, sizeof(buffer), format_str, hue, int(saturation * 1000), int(value * 1000));
207 color_value = buffer;
208 break;
209 }
211 int hue;
212 float saturation, value;
213 rgb_to_hsv(red, green, blue, hue, saturation, value);
214 char buffer[15];
215 const char *format_str = this->color_type_lowercase_ ? "%02x%02x%02x%04x%02x%02x" : "%02X%02X%02X%04X%02X%02X";
216 snprintf(buffer, sizeof(buffer), format_str, int(red * 255), int(green * 255), int(blue * 255), hue,
217 int(saturation * 255), int(value * 255));
218 color_value = buffer;
219 break;
220 }
221 }
222 this->parent_->set_string_datapoint_value(*this->color_id_, color_value);
223 }
224
225 if (this->switch_id_.has_value()) {
227 }
228}
229
230} // namespace esphome::tuya
LightCall & set_color_temperature(optional< float > color_temperature)
Set the color temperature of the light in mireds for CWWW or RGBWW lights.
LightCall & set_rgb(float red, float green, float blue)
Set the RGB color of the light by RGB values.
LightCall & set_brightness(optional< float > brightness)
Set the target brightness of the light from 0.0 (fully off) to 1.0 (fully on)
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
float state_
ON / OFF, float for transition.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
void current_values_as_rgb(float *red, float *green, float *blue)
void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature, float *white_brightness)
void current_values_as_ct(float *color_temperature, float *white_brightness)
void current_values_as_rgbw(float *red, float *green, float *blue, float *white)
LightColorValues current_values
The current values of the light as outputted to the light.
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
void set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
Definition tuya.cpp:626
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:618
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:749
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:622
void setup() override
Definition tuya_light.cpp:9
optional< uint8_t > min_value_datapoint_id_
Definition tuya_light.h:51
void setup_state(light::LightState *state) override
void dump_config() override
light::LightTraits get_traits() override
optional< TuyaColorType > color_type_
Definition tuya_light.h:54
optional< uint8_t > switch_id_
Definition tuya_light.h:52
optional< uint8_t > color_temperature_id_
Definition tuya_light.h:55
void write_state(light::LightState *state) override
optional< uint8_t > dimmer_id_
Definition tuya_light.h:50
optional< uint8_t > color_id_
Definition tuya_light.h:53
uint32_t color_temperature_max_value_
Definition tuya_light.h:58
light::LightState * state_
Definition tuya_light.h:64
bool state
Definition fan.h:2
@ ON_OFF
Only on/off control.
@ BRIGHTNESS
Dimmable light.
@ RGB_WHITE
RGB color output and a separate white output.
@ RGB_COLOR_TEMPERATURE
RGB color output and a separate white output with controllable color temperature.
@ RGB
RGB color output.
@ COLOR_TEMPERATURE
Controllable color temperature output.
@ WHITE
White output only (use only if the light also has another color mode such as RGB).
const char *const TAG
Definition spi.cpp:7
void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value)
Convert red, green and blue (all 0-1) values to hue (0-360), saturation (0-1) and value (0-1).
Definition helpers.cpp:664
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:274
void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue)
Convert hue (0-360), saturation (0-1) and value (0-1) to red, green and blue (all 0-1).
Definition helpers.cpp:687
static void uint32_t
std::string value_string
Definition tuya.h:38