ESPHome 2025.5.0
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 {
6namespace tuya {
7
8static const char *const TAG = "tuya.light";
9
11 if (this->color_temperature_id_.has_value()) {
12 this->parent_->register_listener(*this->color_temperature_id_, [this](const TuyaDatapoint &datapoint) {
13 if (this->state_->current_values != this->state_->remote_values) {
14 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
15 return;
16 }
17
18 auto datapoint_value = datapoint.value_uint;
19 if (this->color_temperature_invert_) {
20 datapoint_value = this->color_temperature_max_value_ - datapoint_value;
21 }
22 auto call = this->state_->make_call();
25 (float(datapoint_value) / this->color_temperature_max_value_));
26 call.perform();
27 });
28 }
29 if (this->dimmer_id_.has_value()) {
30 this->parent_->register_listener(*this->dimmer_id_, [this](const TuyaDatapoint &datapoint) {
31 if (this->state_->current_values != this->state_->remote_values) {
32 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
33 return;
34 }
35
36 auto call = this->state_->make_call();
37 call.set_brightness(float(datapoint.value_uint) / this->max_value_);
38 call.perform();
39 });
40 }
41 if (switch_id_.has_value()) {
42 this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
43 if (this->state_->current_values != this->state_->remote_values) {
44 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
45 return;
46 }
47
48 auto call = this->state_->make_call();
49 call.set_state(datapoint.value_bool);
50 call.perform();
51 });
52 }
53 if (color_id_.has_value()) {
54 this->parent_->register_listener(*this->color_id_, [this](const TuyaDatapoint &datapoint) {
55 if (this->state_->current_values != this->state_->remote_values) {
56 ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
57 return;
58 }
59
60 float red, green, blue;
61 switch (*this->color_type_) {
63 case TuyaColorType::RGB: {
64 auto rgb = parse_hex<uint32_t>(datapoint.value_string.substr(0, 6));
65 if (!rgb.has_value())
66 return;
67
68 red = (*rgb >> 16) / 255.0f;
69 green = ((*rgb >> 8) & 0xff) / 255.0f;
70 blue = (*rgb & 0xff) / 255.0f;
71 break;
72 }
73 case TuyaColorType::HSV: {
74 auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4));
75 auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4));
76 auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4));
77 if (!hue.has_value() || !saturation.has_value() || !value.has_value())
78 return;
79
80 hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue);
81 break;
82 }
83 }
84
85 float current_red, current_green, current_blue;
86 this->state_->current_values_as_rgb(&current_red, &current_green, &current_blue);
87 if (red == current_red && green == current_green && blue == current_blue)
88 return;
89 auto rgb_call = this->state_->make_call();
90 rgb_call.set_rgb(red, green, blue);
91 rgb_call.perform();
92 });
93 }
94
97 }
98}
99
101 ESP_LOGCONFIG(TAG, "Tuya Dimmer:");
102 if (this->dimmer_id_.has_value()) {
103 ESP_LOGCONFIG(TAG, " Dimmer has datapoint ID %u", *this->dimmer_id_);
104 }
105 if (this->switch_id_.has_value()) {
106 ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
107 }
108 if (this->color_id_.has_value()) {
109 ESP_LOGCONFIG(TAG, " Color has datapoint ID %u", *this->color_id_);
110 }
111}
112
114 auto traits = light::LightTraits();
115 if (this->color_temperature_id_.has_value() && this->dimmer_id_.has_value()) {
116 if (this->color_id_.has_value()) {
117 if (this->color_interlock_) {
118 traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::COLOR_TEMPERATURE});
119 } else {
120 traits.set_supported_color_modes(
122 }
123 } else {
124 traits.set_supported_color_modes({light::ColorMode::COLOR_TEMPERATURE});
125 }
126 traits.set_min_mireds(this->cold_white_temperature_);
127 traits.set_max_mireds(this->warm_white_temperature_);
128 } else if (this->color_id_.has_value()) {
129 if (this->dimmer_id_.has_value()) {
130 if (this->color_interlock_) {
131 traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::WHITE});
132 } else {
133 traits.set_supported_color_modes({light::ColorMode::RGB_WHITE});
134 }
135 } else {
136 traits.set_supported_color_modes({light::ColorMode::RGB});
137 }
138 } else if (this->dimmer_id_.has_value()) {
139 traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
140 } else {
141 traits.set_supported_color_modes({light::ColorMode::ON_OFF});
142 }
143 return traits;
144}
145
147
149 float red = 0.0f, green = 0.0f, blue = 0.0f;
150 float color_temperature = 0.0f, brightness = 0.0f;
151
152 if (this->color_id_.has_value()) {
153 if (this->color_temperature_id_.has_value()) {
154 state->current_values_as_rgbct(&red, &green, &blue, &color_temperature, &brightness);
155 } else if (this->dimmer_id_.has_value()) {
156 state->current_values_as_rgbw(&red, &green, &blue, &brightness);
157 } else {
158 state->current_values_as_rgb(&red, &green, &blue);
159 }
160 } else if (this->color_temperature_id_.has_value()) {
161 state->current_values_as_ct(&color_temperature, &brightness);
162 } else {
163 state->current_values_as_brightness(&brightness);
164 }
165
166 if (!state->current_values.is_on() && this->switch_id_.has_value()) {
167 this->parent_->set_boolean_datapoint_value(*this->switch_id_, false);
168 return;
169 }
170
171 if (brightness > 0.0f || !color_interlock_) {
172 if (this->color_temperature_id_.has_value()) {
173 uint32_t color_temp_int = static_cast<uint32_t>(roundf(color_temperature * this->color_temperature_max_value_));
174 if (this->color_temperature_invert_) {
175 color_temp_int = this->color_temperature_max_value_ - color_temp_int;
176 }
177 this->parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int);
178 }
179
180 if (this->dimmer_id_.has_value()) {
181 auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
182 brightness_int = std::max(brightness_int, this->min_value_);
183
184 this->parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int);
185 }
186 }
187
188 if (this->color_id_.has_value() && (brightness == 0.0f || !color_interlock_)) {
189 std::string color_value;
190 switch (*this->color_type_) {
191 case TuyaColorType::RGB: {
192 char buffer[7];
193 sprintf(buffer, "%02X%02X%02X", int(red * 255), int(green * 255), int(blue * 255));
194 color_value = buffer;
195 break;
196 }
197 case TuyaColorType::HSV: {
198 int hue;
199 float saturation, value;
200 rgb_to_hsv(red, green, blue, hue, saturation, value);
201 char buffer[13];
202 sprintf(buffer, "%04X%04X%04X", hue, int(saturation * 1000), int(value * 1000));
203 color_value = buffer;
204 break;
205 }
207 int hue;
208 float saturation, value;
209 rgb_to_hsv(red, green, blue, hue, saturation, value);
210 char buffer[15];
211 sprintf(buffer, "%02X%02X%02X%04X%02X%02X", int(red * 255), int(green * 255), int(blue * 255), hue,
212 int(saturation * 255), int(value * 255));
213 color_value = buffer;
214 break;
215 }
216 }
217 this->parent_->set_string_datapoint_value(*this->color_id_, color_value);
218 }
219
220 if (this->switch_id_.has_value()) {
222 }
223}
224
225} // namespace tuya
226} // namespace esphome
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:63
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_rgb(float *red, float *green, float *blue, bool color_interlock=false)
void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock=false)
LightColorValues current_values
The current values of the light as outputted to the light.
Definition light_state.h:94
This class is used to represent the capabilities of a light.
bool has_value() const
Definition optional.h:87
void set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
Definition tuya.cpp:577
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:569
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:697
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:573
void setup() override
optional< uint8_t > min_value_datapoint_id_
Definition tuya_light.h:55
void setup_state(light::LightState *state) override
void dump_config() override
light::LightTraits get_traits() override
optional< TuyaColorType > color_type_
Definition tuya_light.h:58
optional< uint8_t > switch_id_
Definition tuya_light.h:56
optional< uint8_t > color_temperature_id_
Definition tuya_light.h:59
void write_state(light::LightState *state) override
optional< uint8_t > dimmer_id_
Definition tuya_light.h:54
optional< uint8_t > color_id_
Definition tuya_light.h:57
uint32_t color_temperature_max_value_
Definition tuya_light.h:62
light::LightState * state_
Definition tuya_light.h:67
bool state
Definition fan.h:0
@ 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:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.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:579
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:341
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:602
std::string value_string
Definition tuya.h:39