ESPHome 2026.2.1
Loading...
Searching...
No Matches
tuya.cpp
Go to the documentation of this file.
1#include "tuya.h"
3#include "esphome/core/gpio.h"
5#include "esphome/core/log.h"
6#include "esphome/core/util.h"
7
8#ifdef USE_WIFI
10#endif
11
12#ifdef USE_CAPTIVE_PORTAL
14#endif
15
16namespace esphome {
17namespace tuya {
18
19static const char *const TAG = "tuya";
20static const int COMMAND_DELAY = 10;
21static const int RECEIVE_TIMEOUT = 300;
22static const int MAX_RETRIES = 5;
23// Max bytes to log for datapoint values (larger values are truncated)
24static constexpr size_t MAX_DATAPOINT_LOG_BYTES = 16;
25
27 this->set_interval("heartbeat", 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); });
28 if (this->status_pin_ != nullptr) {
29 this->status_pin_->digital_write(false);
30 }
31}
32
33void Tuya::loop() {
34 // Read all available bytes in batches to reduce UART call overhead.
35 size_t avail = this->available();
36 uint8_t buf[64];
37 while (avail > 0) {
38 size_t to_read = std::min(avail, sizeof(buf));
39 if (!this->read_array(buf, to_read)) {
40 break;
41 }
42 avail -= to_read;
43
44 for (size_t i = 0; i < to_read; i++) {
45 this->handle_char_(buf[i]);
46 }
47 }
49}
50
52 ESP_LOGCONFIG(TAG, "Tuya:");
54 if (this->init_failed_) {
55 ESP_LOGCONFIG(TAG, " Initialization failed. Current init_state: %u", static_cast<uint8_t>(this->init_state_));
56 } else {
57 ESP_LOGCONFIG(TAG, " Configuration will be reported when setup is complete. Current init_state: %u",
58 static_cast<uint8_t>(this->init_state_));
59 }
60 ESP_LOGCONFIG(TAG, " If no further output is received, confirm that this is a supported Tuya device.");
61 return;
62 }
63 for (auto &info : this->datapoints_) {
64 if (info.type == TuyaDatapointType::RAW) {
65 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
66 ESP_LOGCONFIG(TAG, " Datapoint %u: raw (value: %s)", info.id,
67 format_hex_pretty_to(hex_buf, info.value_raw.data(), info.value_raw.size()));
68 } else if (info.type == TuyaDatapointType::BOOLEAN) {
69 ESP_LOGCONFIG(TAG, " Datapoint %u: switch (value: %s)", info.id, ONOFF(info.value_bool));
70 } else if (info.type == TuyaDatapointType::INTEGER) {
71 ESP_LOGCONFIG(TAG, " Datapoint %u: int value (value: %d)", info.id, info.value_int);
72 } else if (info.type == TuyaDatapointType::STRING) {
73 ESP_LOGCONFIG(TAG, " Datapoint %u: string value (value: %s)", info.id, info.value_string.c_str());
74 } else if (info.type == TuyaDatapointType::ENUM) {
75 ESP_LOGCONFIG(TAG, " Datapoint %u: enum (value: %d)", info.id, info.value_enum);
76 } else if (info.type == TuyaDatapointType::BITMASK) {
77 ESP_LOGCONFIG(TAG, " Datapoint %u: bitmask (value: %" PRIx32 ")", info.id, info.value_bitmask);
78 } else {
79 ESP_LOGCONFIG(TAG, " Datapoint %u: unknown", info.id);
80 }
81 }
82 if ((this->status_pin_reported_ != -1) || (this->reset_pin_reported_ != -1)) {
83 ESP_LOGCONFIG(TAG, " GPIO Configuration: status: pin %d, reset: pin %d", this->status_pin_reported_,
85 }
86 LOG_PIN(" Status Pin: ", this->status_pin_);
87 ESP_LOGCONFIG(TAG, " Product: '%s'", this->product_.c_str());
88}
89
91 uint32_t at = this->rx_message_.size() - 1;
92 auto *data = &this->rx_message_[0];
93 uint8_t new_byte = data[at];
94
95 // Byte 0: HEADER1 (always 0x55)
96 if (at == 0)
97 return new_byte == 0x55;
98 // Byte 1: HEADER2 (always 0xAA)
99 if (at == 1)
100 return new_byte == 0xAA;
101
102 // Byte 2: VERSION
103 // no validation for the following fields:
104 uint8_t version = data[2];
105 if (at == 2)
106 return true;
107 // Byte 3: COMMAND
108 uint8_t command = data[3];
109 if (at == 3)
110 return true;
111
112 // Byte 4: LENGTH1
113 // Byte 5: LENGTH2
114 if (at <= 5) {
115 // no validation for these fields
116 return true;
117 }
118
119 uint16_t length = (uint16_t(data[4]) << 8) | (uint16_t(data[5]));
120
121 // wait until all data is read
122 if (at - 6 < length)
123 return true;
124
125 // Byte 6+LEN: CHECKSUM - sum of all bytes (including header) modulo 256
126 uint8_t rx_checksum = new_byte;
127 uint8_t calc_checksum = 0;
128 for (uint32_t i = 0; i < 6 + length; i++)
129 calc_checksum += data[i];
130
131 if (rx_checksum != calc_checksum) {
132 ESP_LOGW(TAG, "Tuya Received invalid message checksum %02X!=%02X", rx_checksum, calc_checksum);
133 return false;
134 }
135
136 // valid message
137 const uint8_t *message_data = data + 6;
138#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
139 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
140 ESP_LOGV(TAG, "Received Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", command, version,
141 format_hex_pretty_to(hex_buf, message_data, length), static_cast<uint8_t>(this->init_state_));
142#endif
143 this->handle_command_(command, version, message_data, length);
144
145 // return false to reset rx buffer
146 return false;
147}
148
149void Tuya::handle_char_(uint8_t c) {
150 this->rx_message_.push_back(c);
151 if (!this->validate_message_()) {
152 this->rx_message_.clear();
153 } else {
155 }
156}
157
158void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len) {
159 TuyaCommandType command_type = (TuyaCommandType) command;
160
161 if (this->expected_response_.has_value() && this->expected_response_ == command_type) {
163 this->command_queue_.erase(command_queue_.begin());
164 this->init_retries_ = 0;
165 }
166
167 switch (command_type) {
169 ESP_LOGV(TAG, "MCU Heartbeat (0x%02X)", buffer[0]);
170 this->protocol_version_ = version;
171 if (buffer[0] == 0) {
172 ESP_LOGI(TAG, "MCU restarted");
174 }
178 }
179 break;
181 // check it is a valid string made up of printable characters
182 bool valid = true;
183 for (size_t i = 0; i < len; i++) {
184 if (!std::isprint(buffer[i])) {
185 valid = false;
186 break;
187 }
188 }
189 if (valid) {
190 this->product_ = std::string(reinterpret_cast<const char *>(buffer), len);
191 } else {
192 this->product_ = R"({"p":"INVALID"})";
193 }
197 }
198 break;
199 }
201 if (len >= 2) {
202 this->status_pin_reported_ = buffer[0];
203 this->reset_pin_reported_ = buffer[1];
204 }
206 // If mcu returned status gpio, then we can omit sending wifi state
207 if (this->status_pin_reported_ != -1) {
210 bool is_pin_equals =
211 this->status_pin_ != nullptr && this->status_pin_->get_pin() == this->status_pin_reported_;
212 // Configure status pin toggling (if reported and configured) or WIFI_STATE periodic send
213 if (is_pin_equals) {
214 ESP_LOGV(TAG, "Configured status pin %i", this->status_pin_reported_);
215 this->set_interval("wifi", 1000, [this] { this->set_status_pin_(); });
216 } else {
217 ESP_LOGW(TAG, "Supplied status_pin does not equals the reported pin %i. TuyaMcu will work in limited mode.",
219 }
220 } else {
222 ESP_LOGV(TAG, "Configured WIFI_STATE periodic send");
223 this->set_interval("wifi", 1000, [this] { this->send_wifi_status_(); });
224 }
225 }
226 break;
227 }
232 }
233 break;
236 const bool is_select = (len >= 1);
237 // Send WIFI_SELECT ACK
240 ack.payload.clear();
241 this->send_command_(ack);
242 // Establish pairing mode for correct first WIFI_STATE byte, EZ (0x00) default
243 uint8_t first = 0x00;
244 const char *mode_str = "EZ";
245 if (is_select && buffer[0] == 0x01) {
246 first = 0x01;
247 mode_str = "AP";
248 }
249 // Send WIFI_STATE response, MCU exits pairing mode
250 TuyaCommand st;
252 st.payload.resize(1);
253 st.payload[0] = first;
254 this->send_command_(st);
255 st.payload[0] = 0x02;
256 this->send_command_(st);
257 st.payload[0] = 0x03;
258 this->send_command_(st);
259 st.payload[0] = 0x04;
260 this->send_command_(st);
261 ESP_LOGI(TAG, "%s received (%s), replied with WIFI_STATE confirming connection established",
262 is_select ? "WIFI_SELECT" : "WIFI_RESET", mode_str);
263 break;
264 }
266 break;
271 this->set_timeout("datapoint_dump", 1000, [this] { this->dump_config(); });
272 this->initialized_callback_.call();
273 }
274 this->handle_datapoints_(buffer, len);
275
276 if (command_type == TuyaCommandType::DATAPOINT_REPORT_SYNC) {
277 this->send_command_(
278 TuyaCommand{.cmd = TuyaCommandType::DATAPOINT_REPORT_ACK, .payload = std::vector<uint8_t>{0x01}});
279 }
280 break;
282 break;
284 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::WIFI_TEST, .payload = std::vector<uint8_t>{0x00, 0x00}});
285 break;
287 this->send_command_(
288 TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector<uint8_t>{get_wifi_rssi_()}});
289 break;
291#ifdef USE_TIME
292 if (this->time_id_ != nullptr) {
293 this->send_local_time_();
294
296 // tuya mcu supports time, so we let them know when our time changed
297 this->time_id_->add_on_time_sync_callback([this] { this->send_local_time_(); });
299 }
300 } else
301#endif
302 {
303 ESP_LOGW(TAG, "LOCAL_TIME_QUERY is not handled because time is not configured");
304 }
305 break;
307 this->send_command_(
308 TuyaCommand{.cmd = TuyaCommandType::VACUUM_MAP_UPLOAD, .payload = std::vector<uint8_t>{0x01}});
309 ESP_LOGW(TAG, "Vacuum map upload requested, responding that it is not enabled.");
310 break;
312 uint8_t wifi_status = this->get_wifi_status_code_();
313
314 this->send_command_(
315 TuyaCommand{.cmd = TuyaCommandType::GET_NETWORK_STATUS, .payload = std::vector<uint8_t>{wifi_status}});
316 ESP_LOGV(TAG, "Network status requested, reported as %i", wifi_status);
317 break;
318 }
320 uint8_t subcommand = buffer[0];
321 switch ((TuyaExtendedServicesCommandType) subcommand) {
323 this->send_command_(
325 .payload = std::vector<uint8_t>{
326 static_cast<uint8_t>(TuyaExtendedServicesCommandType::RESET_NOTIFICATION), 0x00}});
327 ESP_LOGV(TAG, "Reset status notification enabled");
328 break;
329 }
331 ESP_LOGE(TAG, "EXTENDED_SERVICES::MODULE_RESET is not handled");
332 break;
333 }
335 ESP_LOGE(TAG, "EXTENDED_SERVICES::UPDATE_IN_PROGRESS is not handled");
336 break;
337 }
338 default:
339 ESP_LOGE(TAG, "Invalid extended services subcommand (0x%02X) received", subcommand);
340 }
341 break;
342 }
343 default:
344 ESP_LOGE(TAG, "Invalid command (0x%02X) received", command);
345 }
346}
347
348void Tuya::handle_datapoints_(const uint8_t *buffer, size_t len) {
349 while (len >= 4) {
350 TuyaDatapoint datapoint{};
351 datapoint.id = buffer[0];
352 datapoint.type = (TuyaDatapointType) buffer[1];
353 datapoint.value_uint = 0;
354
355 size_t data_size = (buffer[2] << 8) + buffer[3];
356 const uint8_t *data = buffer + 4;
357 size_t data_len = len - 4;
358 if (data_size > data_len) {
359 ESP_LOGW(TAG, "Datapoint %u is truncated and cannot be parsed (%zu > %zu)", datapoint.id, data_size, data_len);
360 return;
361 }
362
363 datapoint.len = data_size;
364
365 switch (datapoint.type) {
367 datapoint.value_raw = std::vector<uint8_t>(data, data + data_size);
368 {
369 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
370 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id,
371 format_hex_pretty_to(hex_buf, datapoint.value_raw.data(), datapoint.value_raw.size()));
372 }
373 break;
375 if (data_size != 1) {
376 ESP_LOGW(TAG, "Datapoint %u has bad boolean len %zu", datapoint.id, data_size);
377 return;
378 }
379 datapoint.value_bool = data[0];
380 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, ONOFF(datapoint.value_bool));
381 break;
383 if (data_size != 4) {
384 ESP_LOGW(TAG, "Datapoint %u has bad integer len %zu", datapoint.id, data_size);
385 return;
386 }
387 datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]);
388 ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_int);
389 break;
391 datapoint.value_string = std::string(reinterpret_cast<const char *>(data), data_size);
392 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, datapoint.value_string.c_str());
393 break;
395 if (data_size != 1) {
396 ESP_LOGW(TAG, "Datapoint %u has bad enum len %zu", datapoint.id, data_size);
397 return;
398 }
399 datapoint.value_enum = data[0];
400 ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_enum);
401 break;
403 switch (data_size) {
404 case 1:
405 datapoint.value_bitmask = encode_uint32(0, 0, 0, data[0]);
406 break;
407 case 2:
408 datapoint.value_bitmask = encode_uint32(0, 0, data[0], data[1]);
409 break;
410 case 4:
411 datapoint.value_bitmask = encode_uint32(data[0], data[1], data[2], data[3]);
412 break;
413 default:
414 ESP_LOGW(TAG, "Datapoint %u has bad bitmask len %zu", datapoint.id, data_size);
415 return;
416 }
417 ESP_LOGD(TAG, "Datapoint %u update to %#08" PRIX32, datapoint.id, datapoint.value_bitmask);
418 break;
419 default:
420 ESP_LOGW(TAG, "Datapoint %u has unknown type %#02hhX", datapoint.id, static_cast<uint8_t>(datapoint.type));
421 return;
422 }
423
424 len -= data_size + 4;
425 buffer = data + data_size;
426
427 // drop update if datapoint is in ignore_mcu_datapoint_update list
428 bool skip = false;
429 for (auto i : this->ignore_mcu_update_on_datapoints_) {
430 if (datapoint.id == i) {
431 ESP_LOGV(TAG, "Datapoint %u found in ignore_mcu_update_on_datapoints list, dropping MCU update", datapoint.id);
432 skip = true;
433 break;
434 }
435 }
436 if (skip)
437 continue;
438
439 // Update internal datapoints
440 bool found = false;
441 for (auto &other : this->datapoints_) {
442 if (other.id == datapoint.id) {
443 other = datapoint;
444 found = true;
445 }
446 }
447 if (!found) {
448 this->datapoints_.push_back(datapoint);
449 }
450
451 // Run through listeners
452 for (auto &listener : this->listeners_) {
453 if (listener.datapoint_id == datapoint.id)
454 listener.on_datapoint(datapoint);
455 }
456 }
457}
458
460 uint8_t len_hi = (uint8_t) (command.payload.size() >> 8);
461 uint8_t len_lo = (uint8_t) (command.payload.size() & 0xFF);
462 uint8_t version = 0;
463
465 switch (command.cmd) {
468 break;
471 break;
474 break;
478 break;
479 default:
480 break;
481 }
482
483#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
484 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
485 ESP_LOGV(TAG, "Sending Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", static_cast<uint8_t>(command.cmd),
486 version, format_hex_pretty_to(hex_buf, command.payload.data(), command.payload.size()),
487 static_cast<uint8_t>(this->init_state_));
488#endif
489
490 this->write_array({0x55, 0xAA, version, (uint8_t) command.cmd, len_hi, len_lo});
491 if (!command.payload.empty())
492 this->write_array(command.payload.data(), command.payload.size());
493
494 uint8_t checksum = 0x55 + 0xAA + (uint8_t) command.cmd + len_hi + len_lo;
495 for (auto &data : command.payload)
496 checksum += data;
497 this->write_byte(checksum);
498}
499
501 uint32_t now = millis();
502 uint32_t delay = now - this->last_command_timestamp_;
503
504 if (now - this->last_rx_char_timestamp_ > RECEIVE_TIMEOUT) {
505 this->rx_message_.clear();
506 }
507
508 if (this->expected_response_.has_value() && delay > RECEIVE_TIMEOUT) {
511 if (++this->init_retries_ >= MAX_RETRIES) {
512 this->init_failed_ = true;
513 ESP_LOGE(TAG, "Initialization failed at init_state %u", static_cast<uint8_t>(this->init_state_));
514 this->command_queue_.erase(command_queue_.begin());
515 this->init_retries_ = 0;
516 }
517 } else {
518 this->command_queue_.erase(command_queue_.begin());
519 }
520 }
521
522 // Left check of delay since last command in case there's ever a command sent by calling send_raw_command_ directly
523 if (delay > COMMAND_DELAY && !this->command_queue_.empty() && this->rx_message_.empty() &&
524 !this->expected_response_.has_value()) {
525 this->send_raw_command_(command_queue_.front());
526 if (!this->expected_response_.has_value())
527 this->command_queue_.erase(command_queue_.begin());
528 }
529}
530
531void Tuya::send_command_(const TuyaCommand &command) {
532 command_queue_.push_back(command);
534}
535
537 send_command_(TuyaCommand{.cmd = command, .payload = std::vector<uint8_t>{}});
538}
539
541 bool is_network_ready = network::is_connected() && remote_is_connected();
542 this->status_pin_->digital_write(is_network_ready);
543}
544
546 uint8_t status = 0x02;
547
548 if (network::is_connected()) {
549 status = 0x03;
550
551 // Protocol version 3 also supports specifying when connected to "the cloud"
552 if (this->protocol_version_ >= 0x03 && remote_is_connected()) {
553 status = 0x04;
554 }
555 } else {
556#ifdef USE_CAPTIVE_PORTAL
558 status = 0x01;
559 }
560#endif
561 };
562
563 return status;
564}
565
567#ifdef USE_WIFI
568 if (wifi::global_wifi_component != nullptr)
570#endif
571
572 return 0;
573}
574
576 uint8_t status = this->get_wifi_status_code_();
577
578 if (status == this->wifi_status_) {
579 return;
580 }
581
582 ESP_LOGD(TAG, "Sending WiFi Status");
583 this->wifi_status_ = status;
584 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::WIFI_STATE, .payload = std::vector<uint8_t>{status}});
585}
586
587#ifdef USE_TIME
589 std::vector<uint8_t> payload;
590 ESPTime now = this->time_id_->now();
591 if (now.is_valid()) {
592 uint8_t year = now.year - 2000;
593 uint8_t month = now.month;
594 uint8_t day_of_month = now.day_of_month;
595 uint8_t hour = now.hour;
596 uint8_t minute = now.minute;
597 uint8_t second = now.second;
598 // Tuya days starts from Monday, esphome uses Sunday as day 1
599 uint8_t day_of_week = now.day_of_week - 1;
600 if (day_of_week == 0) {
601 day_of_week = 7;
602 }
603 ESP_LOGD(TAG, "Sending local time");
604 payload = std::vector<uint8_t>{0x01, year, month, day_of_month, hour, minute, second, day_of_week};
605 } else {
606 // By spec we need to notify MCU that the time was not obtained if this is a response to a query
607 ESP_LOGW(TAG, "Sending missing local time");
608 payload = std::vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
609 }
610 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::LOCAL_TIME_QUERY, .payload = payload});
611}
612#endif
613
614void Tuya::set_raw_datapoint_value(uint8_t datapoint_id, const std::vector<uint8_t> &value) {
615 this->set_raw_datapoint_value_(datapoint_id, value, false);
616}
617
618void Tuya::set_boolean_datapoint_value(uint8_t datapoint_id, bool value) {
619 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::BOOLEAN, value, 1, false);
620}
621
622void Tuya::set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value) {
623 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::INTEGER, value, 4, false);
624}
625
626void Tuya::set_string_datapoint_value(uint8_t datapoint_id, const std::string &value) {
627 this->set_string_datapoint_value_(datapoint_id, value, false);
628}
629
630void Tuya::set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value) {
631 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::ENUM, value, 1, false);
632}
633
634void Tuya::set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length) {
635 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::BITMASK, value, length, false);
636}
637
638void Tuya::force_set_raw_datapoint_value(uint8_t datapoint_id, const std::vector<uint8_t> &value) {
639 this->set_raw_datapoint_value_(datapoint_id, value, true);
640}
641
642void Tuya::force_set_boolean_datapoint_value(uint8_t datapoint_id, bool value) {
643 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::BOOLEAN, value, 1, true);
644}
645
646void Tuya::force_set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value) {
647 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::INTEGER, value, 4, true);
648}
649
650void Tuya::force_set_string_datapoint_value(uint8_t datapoint_id, const std::string &value) {
651 this->set_string_datapoint_value_(datapoint_id, value, true);
652}
653
654void Tuya::force_set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value) {
655 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::ENUM, value, 1, true);
656}
657
658void Tuya::force_set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length) {
659 this->set_numeric_datapoint_value_(datapoint_id, TuyaDatapointType::BITMASK, value, length, true);
660}
661
663 for (auto &datapoint : this->datapoints_) {
664 if (datapoint.id == datapoint_id)
665 return datapoint;
666 }
667 return {};
668}
669
670void Tuya::set_numeric_datapoint_value_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, const uint32_t value,
671 uint8_t length, bool forced) {
672 ESP_LOGD(TAG, "Setting datapoint %u to %" PRIu32, datapoint_id, value);
673 optional<TuyaDatapoint> datapoint = this->get_datapoint_(datapoint_id);
674 if (!datapoint.has_value()) {
675 ESP_LOGW(TAG, "Setting unknown datapoint %u", datapoint_id);
676 } else if (datapoint->type != datapoint_type) {
677 ESP_LOGE(TAG, "Attempt to set datapoint %u with incorrect type", datapoint_id);
678 return;
679 } else if (!forced && datapoint->value_uint == value) {
680 ESP_LOGV(TAG, "Not sending unchanged value");
681 return;
682 }
683
684 std::vector<uint8_t> data;
685 switch (length) {
686 case 4:
687 data.push_back(value >> 24);
688 data.push_back(value >> 16);
689 case 2:
690 data.push_back(value >> 8);
691 case 1:
692 data.push_back(value >> 0);
693 break;
694 default:
695 ESP_LOGE(TAG, "Unexpected datapoint length %u", length);
696 return;
697 }
698 this->send_datapoint_command_(datapoint_id, datapoint_type, data);
699}
700
701void Tuya::set_raw_datapoint_value_(uint8_t datapoint_id, const std::vector<uint8_t> &value, bool forced) {
702 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
703 ESP_LOGD(TAG, "Setting datapoint %u to %s", datapoint_id, format_hex_pretty_to(hex_buf, value.data(), value.size()));
704 optional<TuyaDatapoint> datapoint = this->get_datapoint_(datapoint_id);
705 if (!datapoint.has_value()) {
706 ESP_LOGW(TAG, "Setting unknown datapoint %u", datapoint_id);
707 } else if (datapoint->type != TuyaDatapointType::RAW) {
708 ESP_LOGE(TAG, "Attempt to set datapoint %u with incorrect type", datapoint_id);
709 return;
710 } else if (!forced && datapoint->value_raw == value) {
711 ESP_LOGV(TAG, "Not sending unchanged value");
712 return;
713 }
714 this->send_datapoint_command_(datapoint_id, TuyaDatapointType::RAW, value);
715}
716
717void Tuya::set_string_datapoint_value_(uint8_t datapoint_id, const std::string &value, bool forced) {
718 ESP_LOGD(TAG, "Setting datapoint %u to %s", datapoint_id, value.c_str());
719 optional<TuyaDatapoint> datapoint = this->get_datapoint_(datapoint_id);
720 if (!datapoint.has_value()) {
721 ESP_LOGW(TAG, "Setting unknown datapoint %u", datapoint_id);
722 } else if (datapoint->type != TuyaDatapointType::STRING) {
723 ESP_LOGE(TAG, "Attempt to set datapoint %u with incorrect type", datapoint_id);
724 return;
725 } else if (!forced && datapoint->value_string == value) {
726 ESP_LOGV(TAG, "Not sending unchanged value");
727 return;
728 }
729 std::vector<uint8_t> data;
730 for (char const &c : value) {
731 data.push_back(c);
732 }
733 this->send_datapoint_command_(datapoint_id, TuyaDatapointType::STRING, data);
734}
735
736void Tuya::send_datapoint_command_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, std::vector<uint8_t> data) {
737 std::vector<uint8_t> buffer;
738 buffer.push_back(datapoint_id);
739 buffer.push_back(static_cast<uint8_t>(datapoint_type));
740 buffer.push_back(data.size() >> 8);
741 buffer.push_back(data.size() >> 0);
742 buffer.insert(buffer.end(), data.begin(), data.end());
743
744 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::DATAPOINT_DELIVER, .payload = buffer});
745}
746
747void Tuya::register_listener(uint8_t datapoint_id, const std::function<void(TuyaDatapoint)> &func) {
748 auto listener = TuyaDatapointListener{
749 .datapoint_id = datapoint_id,
750 .on_datapoint = func,
751 };
752 this->listeners_.push_back(listener);
753
754 // Run through existing datapoints
755 for (auto &datapoint : this->datapoints_) {
756 if (datapoint.id == datapoint_id)
757 func(datapoint);
758 }
759}
760
762
763} // namespace tuya
764} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
uint8_t status
Definition bl0942.h:8
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:429
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:336
virtual void digital_write(bool value)=0
virtual uint8_t get_pin() const =0
bool has_value() const
Definition optional.h:92
void add_on_time_sync_callback(std::function< void()> &&callback)
ESPTime now()
Get the time in the currently defined timezone.
void send_datapoint_command_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, std::vector< uint8_t > data)
Definition tuya.cpp:736
void set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
Definition tuya.cpp:626
optional< TuyaDatapoint > get_datapoint_(uint8_t datapoint_id)
Definition tuya.cpp:662
void send_empty_command_(TuyaCommandType command)
Definition tuya.cpp:536
void setup() override
Definition tuya.cpp:26
uint8_t get_wifi_rssi_()
Definition tuya.cpp:566
uint8_t get_wifi_status_code_()
Definition tuya.cpp:545
void dump_config() override
Definition tuya.cpp:51
time::RealTimeClock * time_id_
Definition tuya.h:142
CallbackManager< void()> initialized_callback_
Definition tuya.h:162
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:618
TuyaInitState init_state_
Definition tuya.h:145
int status_pin_reported_
Definition tuya.h:150
void set_raw_datapoint_value_(uint8_t datapoint_id, const std::vector< uint8_t > &value, bool forced)
Definition tuya.cpp:701
bool validate_message_()
Definition tuya.cpp:90
InternalGPIOPin * status_pin_
Definition tuya.h:149
void force_set_raw_datapoint_value(uint8_t datapoint_id, const std::vector< uint8_t > &value)
Definition tuya.cpp:638
uint8_t protocol_version_
Definition tuya.h:148
void set_string_datapoint_value_(uint8_t datapoint_id, const std::string &value, bool forced)
Definition tuya.cpp:717
void force_set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:646
uint32_t last_rx_char_timestamp_
Definition tuya.h:153
void process_command_queue_()
Definition tuya.cpp:500
uint32_t last_command_timestamp_
Definition tuya.h:152
std::vector< TuyaDatapointListener > listeners_
Definition tuya.h:155
std::vector< TuyaCommand > command_queue_
Definition tuya.h:159
std::vector< TuyaDatapoint > datapoints_
Definition tuya.h:156
void send_local_time_()
Definition tuya.cpp:588
std::vector< uint8_t > ignore_mcu_update_on_datapoints_
Definition tuya.h:158
std::vector< uint8_t > rx_message_
Definition tuya.h:157
void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len)
Definition tuya.cpp:158
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:630
void set_numeric_datapoint_value_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, uint32_t value, uint8_t length, bool forced)
Definition tuya.cpp:670
TuyaInitState get_init_state()
Definition tuya.cpp:761
void set_status_pin_()
Definition tuya.cpp:540
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:747
void handle_char_(uint8_t c)
Definition tuya.cpp:149
void force_set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length)
Definition tuya.cpp:658
void handle_datapoints_(const uint8_t *buffer, size_t len)
Definition tuya.cpp:348
std::string product_
Definition tuya.h:154
void send_wifi_status_()
Definition tuya.cpp:575
void send_command_(const TuyaCommand &command)
Definition tuya.cpp:531
void force_set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:642
void set_raw_datapoint_value(uint8_t datapoint_id, const std::vector< uint8_t > &value)
Definition tuya.cpp:614
optional< TuyaCommandType > expected_response_
Definition tuya.h:160
void send_raw_command_(TuyaCommand command)
Definition tuya.cpp:459
void force_set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:654
void force_set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
Definition tuya.cpp:650
bool time_sync_callback_registered_
Definition tuya.h:143
void set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length)
Definition tuya.cpp:634
int reset_pin_reported_
Definition tuya.h:151
uint8_t wifi_status_
Definition tuya.h:161
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:622
void loop() override
Definition tuya.cpp:33
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t second
uint8_t minute
uint8_t hour
CaptivePortal * global_captive_portal
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.cpp:26
const char *const TAG
Definition spi.cpp:7
TuyaExtendedServicesCommandType
Definition tuya.h:68
TuyaCommandType
Definition tuya.h:48
TuyaDatapointType
Definition tuya.h:19
WiFiComponent * global_wifi_component
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool remote_is_connected()
Return whether the node has any form of "remote" connection via the API or to an MQTT broker.
Definition util.cpp:35
std::string size_t len
Definition helpers.h:692
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:353
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:978
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:536
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
A more user-friendly version of struct tm from time.h.
Definition time.h:17
uint8_t minute
minutes after the hour [0-59]
Definition time.h:26
uint8_t second
seconds after the minute [0-60]
Definition time.h:24
uint8_t hour
hours since midnight [0-23]
Definition time.h:28
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:74
uint8_t day_of_month
day of the month [1-31]
Definition time.h:32
uint16_t year
year
Definition time.h:38
uint8_t month
month; january=1 [1-12]
Definition time.h:36
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:30
TuyaCommandType cmd
Definition tuya.h:84
std::vector< uint8_t > payload
Definition tuya.h:85
uint8_t ack
uint16_t length
Definition tt21100.cpp:0