ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tuya_uart.cpp
Go to the documentation of this file.
1#include "tuya_uart.h"
3#include "esphome/core/gpio.h"
5#include "esphome/core/log.h"
6#include "esphome/core/util.h"
7#include <cctype>
8#include <cstring>
9
10#ifdef USE_NETWORK
12#endif
13
14#ifdef USE_WIFI
16#endif
17
18#ifdef USE_CAPTIVE_PORTAL
20#endif
21
22namespace esphome::tuya {
23
24ESPHOME_LOG_TAG(TAG, "tuya");
25
26static constexpr uint32_t HEARTBEAT_INTERVAL_ID = 0;
27static constexpr uint32_t WIFI_STATUS_INTERVAL_ID = 1;
28static constexpr uint32_t DATAPOINT_DUMP_TIMEOUT_ID = 2;
29static constexpr uint32_t FORCE_TIME_SYNC_INTERVAL_ID = 3;
30static const int COMMAND_DELAY = 10;
31static constexpr uint32_t RECEIVE_TIMEOUT = 300;
32static const int MAX_RETRIES = 5;
33static constexpr uint32_t FORCE_TIME_SYNC_CHECK_INTERVAL = 1000;
34
35static bool network_is_connected() {
36#ifdef USE_NETWORK
37 return network::is_connected();
38#else
39 return false;
40#endif
41}
42
44 this->set_interval(HEARTBEAT_INTERVAL_ID, 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); });
45#ifdef USE_TIME
46 if (this->force_time_sync_) {
48 this->set_interval(FORCE_TIME_SYNC_INTERVAL_ID, FORCE_TIME_SYNC_CHECK_INTERVAL,
49 [this] { this->check_force_time_sync_(); });
50 }
51#endif
52 if (this->status_pin_ != nullptr) {
53 this->status_pin_->digital_write(false);
54 }
55}
56
58 auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
59 // Holds commands back for the whole read, the idle search and its log lines included
60 this->handling_frame_ = true;
61 this->read_frames(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_char_timestamp_,
62 RECEIVE_TIMEOUT);
63 this->handling_frame_ = false;
65}
66
71
72// Also logged again once the MCU reports its datapoints
74 ESP_LOGCONFIG(TAG, "Tuya:");
76 if (this->init_failed_) {
77 ESP_LOGCONFIG(TAG, " Initialization failed. Current init_state: %u", static_cast<uint8_t>(this->init_state_));
78 } else {
79 ESP_LOGCONFIG(TAG, " Configuration will be reported when setup is complete. Current init_state: %u",
80 static_cast<uint8_t>(this->init_state_));
81 }
82 ESP_LOGCONFIG(TAG, " If no further output is received, confirm that this is a supported Tuya device.");
83 return;
84 }
85 this->dump_datapoints_();
86 if ((this->status_pin_reported_ != -1) || (this->reset_pin_reported_ != -1)) {
87 ESP_LOGCONFIG(TAG, " GPIO Configuration: status: pin %d, reset: pin %d", this->status_pin_reported_,
89 }
90 LOG_PIN(" Status Pin: ", this->status_pin_);
91 ESP_LOGCONFIG(TAG, " Product: '%s'",
92 this->product_invalid_ ? LOG_STR_LITERAL(R"({"p":"INVALID"})")
93 : (this->product_ ? this->product_.get() : LOG_STR_LITERAL("")));
94#ifdef USE_TIME
95 if (this->time_id_ != nullptr) {
96 ESP_LOGCONFIG(TAG, " Force time sync: %s", YESNO(this->force_time_sync_));
97 }
98#endif
99}
100
101void warn_too_long(size_t len) {
102 ESP_LOGW(TAG, "Skipped a %u byte frame, longer than the %u byte buffer", static_cast<unsigned>(len),
103 static_cast<unsigned>(MAX_FRAME_SIZE));
104}
105
106bool TuyaUART::handle_frame_(const uint8_t *frame, size_t len) {
107 // Last byte: sum of all bytes before it, header included, modulo 256
108 const uint8_t rx_checksum = frame[len - 1];
109 const uint8_t calc_checksum = sum_bytes(frame, len - 1);
110 if (rx_checksum != calc_checksum) {
111 ESP_LOGW(TAG, "Tuya Received invalid message checksum %02X!=%02X", rx_checksum, calc_checksum);
112 return false;
113 }
114 const uint8_t version = frame[2];
115 const uint8_t command = frame[3];
116 const uint8_t *message_data = frame + TuyaLength::PAYLOAD_AT;
117 const size_t length = len - TuyaLength::PAYLOAD_AT - 1;
118#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
119 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
120 ESP_LOGV(TAG, "Received Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", command, version,
121 format_hex_pretty_to(hex_buf, message_data, length), static_cast<uint8_t>(this->init_state_));
122#endif
123 this->handle_command_(command, version, message_data, length);
124 return true;
125}
126
127void TuyaUART::handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len) {
128 TuyaCommandType command_type = (TuyaCommandType) command;
129
130 if (this->expected_response_.has_value() && this->expected_response_ == command_type) {
131 this->expected_response_.reset();
132 this->command_queue_.erase(command_queue_.begin());
133 this->init_retries_ = 0;
134 }
135
136 switch (command_type) {
138 ESP_LOGV(TAG, "MCU Heartbeat (0x%02X)", buffer[0]);
139 this->protocol_version_ = version;
140 if (buffer[0] == 0) {
141 ESP_LOGI(TAG, "MCU restarted");
143 }
147 }
148 break;
150 // check it is a valid string made up of printable characters
151 bool valid = true;
152 for (size_t i = 0; i < len; i++) {
153 if (!std::isprint(buffer[i])) {
154 valid = false;
155 break;
156 }
157 }
158 if (valid) {
159 this->product_ = std::make_unique<char[]>(len + 1);
160 memcpy(this->product_.get(), buffer, len);
161 } else {
162 this->product_.reset();
163 }
164 this->product_invalid_ = !valid;
168 }
169 break;
170 }
172 if (len >= 2) {
173 this->status_pin_reported_ = buffer[0];
174 this->reset_pin_reported_ = buffer[1];
175 }
177 // If mcu returned status gpio, then we can omit sending wifi state
178 if (this->status_pin_reported_ != -1) {
181 if (this->status_pin_ != nullptr) {
182 if (this->status_pin_->get_pin() != this->status_pin_reported_) {
183 ESP_LOGW(TAG, "Supplied status_pin does not equal the reported pin %i. Using supplied pin anyway.",
185 }
186 ESP_LOGV(TAG, "Configured status pin %i", this->status_pin_->get_pin());
187 this->set_interval(WIFI_STATUS_INTERVAL_ID, 1000, [this] { this->set_status_pin_(); });
188 } else {
189 ESP_LOGW(TAG, "MCU reported status_pin %i but no status_pin was configured; running in limited mode.",
191 }
192 } else {
194 ESP_LOGV(TAG, "Configured WIFI_STATE periodic send");
195 this->set_interval(WIFI_STATUS_INTERVAL_ID, 1000, [this] { this->send_wifi_status_(); });
196 }
197 }
198 break;
199 }
204 }
205 break;
208 const bool is_select = (len >= 1);
209 // Send WIFI_SELECT ACK
212 ack.payload.clear();
213 this->send_command_(ack);
214 // Establish pairing mode for correct first WIFI_STATE byte, EZ (0x00) default
215 uint8_t first = 0x00;
216 const char *mode_str = "EZ";
217 if (is_select && buffer[0] == 0x01) {
218 first = 0x01;
219 mode_str = "AP";
220 }
221 // Send WIFI_STATE response, MCU exits pairing mode
222 TuyaCommand st;
224 st.payload.resize(1);
225 st.payload[0] = first;
226 this->send_command_(st);
227 st.payload[0] = 0x02;
228 this->send_command_(st);
229 st.payload[0] = 0x03;
230 this->send_command_(st);
231 st.payload[0] = 0x04;
232 this->send_command_(st);
233 ESP_LOGI(TAG, "%s received (%s), replied with WIFI_STATE confirming connection established",
234 is_select ? LOG_STR_LITERAL("WIFI_SELECT") : LOG_STR_LITERAL("WIFI_RESET"), mode_str);
235 break;
236 }
238 break;
243 this->set_timeout(DATAPOINT_DUMP_TIMEOUT_ID, 1000, [this] { this->dump_mcu_config_(); });
244 this->mark_initialized_();
245 }
246 this->handle_datapoints_(buffer, len);
247
248 if (command_type == TuyaCommandType::DATAPOINT_REPORT_SYNC) {
249 this->send_command_(
250 TuyaCommand{.cmd = TuyaCommandType::DATAPOINT_REPORT_ACK, .payload = std::vector<uint8_t>{0x01}});
251 }
252 break;
254 break;
256 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::WIFI_TEST, .payload = std::vector<uint8_t>{0x00, 0x00}});
257 break;
259 this->send_command_(
260 TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector<uint8_t>{get_wifi_rssi_()}});
261 break;
263#ifdef USE_TIME
264 if (this->time_id_ != nullptr) {
265 this->send_local_time_();
267 } else
268#endif
269 {
270 ESP_LOGW(TAG, "LOCAL_TIME_QUERY is not handled because time is not configured");
271 }
272 break;
274#ifdef USE_TIME
275 if (this->time_id_ != nullptr) {
276 this->send_gmt_time_();
277
279 // tuya mcu supports time, so we let them know when our time changed
280 this->time_id_->add_on_time_sync_callback([this] { this->send_gmt_time_(); });
282 }
283 } else
284#endif
285 {
286 ESP_LOGW(TAG, "GMT_TIME_QUERY is not handled because time is not configured");
287 }
288 break;
290 this->send_command_(
291 TuyaCommand{.cmd = TuyaCommandType::VACUUM_MAP_UPLOAD, .payload = std::vector<uint8_t>{0x01}});
292 ESP_LOGW(TAG, "Vacuum map upload requested, responding that it is not enabled.");
293 break;
295 uint8_t wifi_status = this->get_wifi_status_code_();
296
297 this->send_command_(
298 TuyaCommand{.cmd = TuyaCommandType::GET_NETWORK_STATUS, .payload = std::vector<uint8_t>{wifi_status}});
299 ESP_LOGV(TAG, "Network status requested, reported as %i", wifi_status);
300 break;
301 }
303 uint8_t subcommand = buffer[0];
304 switch ((TuyaExtendedServicesCommandType) subcommand) {
306 this->send_command_(
308 .payload = std::vector<uint8_t>{
309 static_cast<uint8_t>(TuyaExtendedServicesCommandType::RESET_NOTIFICATION), 0x00}});
310 ESP_LOGV(TAG, "Reset status notification enabled");
311 break;
312 }
314 ESP_LOGE(TAG, "EXTENDED_SERVICES::MODULE_RESET is not handled");
315 break;
316 }
318 ESP_LOGE(TAG, "EXTENDED_SERVICES::UPDATE_IN_PROGRESS is not handled");
319 break;
320 }
321 default:
322 ESP_LOGE(TAG, "Invalid extended services subcommand (0x%02X) received", subcommand);
323 }
324 break;
325 }
326 default:
327 ESP_LOGE(TAG, "Invalid command (0x%02X) received", command);
328 }
329}
330
331void TuyaUART::handle_datapoints_(const uint8_t *buffer, size_t len) {
332 while (len >= 4) {
333 TuyaDatapoint datapoint{};
334 datapoint.id = buffer[0];
335 datapoint.type = (TuyaDatapointType) buffer[1];
336 datapoint.value_uint = 0;
337
338 size_t data_size = encode_uint16(buffer[2], buffer[3]);
339 const uint8_t *data = buffer + 4;
340 size_t data_len = len - 4;
341 if (data_size > data_len) {
342 ESP_LOGW(TAG, "Datapoint %u is truncated and cannot be parsed (%zu > %zu)", datapoint.id, data_size, data_len);
343 return;
344 }
345
346 datapoint.len = data_size;
347
348 switch (datapoint.type) {
350 datapoint.value_raw = std::vector<uint8_t>(data, data + data_size);
351 {
352 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
353 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id,
354 format_hex_pretty_to(hex_buf, datapoint.value_raw.data(), datapoint.value_raw.size()));
355 }
356 break;
358 if (data_size != 1) {
359 ESP_LOGW(TAG, "Datapoint %u has bad boolean len %zu", datapoint.id, data_size);
360 return;
361 }
362 datapoint.value_bool = data[0];
363 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, ONOFF(datapoint.value_bool));
364 break;
366 if (data_size != 4) {
367 ESP_LOGW(TAG, "Datapoint %u has bad integer len %zu", datapoint.id, data_size);
368 return;
369 }
370 datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]);
371 ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_int);
372 break;
374 datapoint.value_string = std::string(reinterpret_cast<const char *>(data), data_size);
375 ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, datapoint.value_string.c_str());
376 break;
378 if (data_size != 1) {
379 ESP_LOGW(TAG, "Datapoint %u has bad enum len %zu", datapoint.id, data_size);
380 return;
381 }
382 datapoint.value_enum = data[0];
383 ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_enum);
384 break;
386 switch (data_size) {
387 case 1:
388 datapoint.value_bitmask = encode_uint32(0, 0, 0, data[0]);
389 break;
390 case 2:
391 datapoint.value_bitmask = encode_uint32(0, 0, data[0], data[1]);
392 break;
393 case 4:
394 datapoint.value_bitmask = encode_uint32(data[0], data[1], data[2], data[3]);
395 break;
396 default:
397 ESP_LOGW(TAG, "Datapoint %u has bad bitmask len %zu", datapoint.id, data_size);
398 return;
399 }
400 ESP_LOGD(TAG, "Datapoint %u update to %#08" PRIX32, datapoint.id, datapoint.value_bitmask);
401 break;
402 default:
403 ESP_LOGW(TAG, "Datapoint %u has unknown type %#02hhX", datapoint.id, static_cast<uint8_t>(datapoint.type));
404 return;
405 }
406
407 len -= data_size + 4;
408 buffer = data + data_size;
409
410 this->handle_datapoint_(datapoint);
411 }
412}
413
415 uint8_t len_hi = (uint8_t) (command.payload.size() >> 8);
416 uint8_t len_lo = (uint8_t) (command.payload.size() & 0xFF);
417 uint8_t version = 0;
418
420 switch (command.cmd) {
423 break;
426 break;
429 break;
433 break;
434 default:
435 break;
436 }
437
438#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
439 char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
440 ESP_LOGV(TAG, "Sending Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", static_cast<uint8_t>(command.cmd),
441 version, format_hex_pretty_to(hex_buf, command.payload.data(), command.payload.size()),
442 static_cast<uint8_t>(this->init_state_));
443#endif
444
445 this->write_array({0x55, 0xAA, version, (uint8_t) command.cmd, len_hi, len_lo});
446 if (!command.payload.empty())
447 this->write_array(command.payload.data(), command.payload.size());
448
449 const uint8_t header_sum = 0x55 + 0xAA + (uint8_t) command.cmd + len_hi + len_lo;
450 this->write_byte(sum_bytes(command.payload.data(), command.payload.size(), header_sum));
451}
452
454 uint32_t now = millis();
456
457 // A reply still coming in, or held behind a corrupt length, is waited for
458 if (this->expected_response_.has_value() && delay > RECEIVE_TIMEOUT && !this->reader_.receiving()) {
459 this->expected_response_.reset();
461 if (++this->init_retries_ >= MAX_RETRIES) {
462 this->init_failed_ = true;
463 ESP_LOGE(TAG, "Initialization failed at init_state %u", static_cast<uint8_t>(this->init_state_));
464 this->command_queue_.erase(command_queue_.begin());
465 this->init_retries_ = 0;
466 }
467 } else {
468 this->command_queue_.erase(command_queue_.begin());
469 }
470 }
471
472 // Left check of delay since last command in case there's ever a command sent by calling send_raw_command_ directly
473 // A partial frame kept by an idle flush does not hold commands back; a command clears it, so it cannot merge into
474 // the reply
475 if (delay > COMMAND_DELAY && !this->command_queue_.empty() && !this->reader_.receiving() && !this->handling_frame_ &&
476 !this->expected_response_.has_value()) {
477 this->reader_.drop_kept();
478 this->send_raw_command_(command_queue_.front());
479 if (!this->expected_response_.has_value())
480 this->command_queue_.erase(command_queue_.begin());
481 }
482}
483
485 command_queue_.push_back(std::move(command));
487}
488
490 send_command_(TuyaCommand{.cmd = command, .payload = std::vector<uint8_t>{}});
491}
492
493void TuyaUART::send_datapoint_command(uint8_t datapoint_id, TuyaDatapointType datapoint_type, const uint8_t *data,
494 size_t len) {
495 std::vector<uint8_t> buffer;
496 buffer.reserve(4 + len);
497 buffer.push_back(datapoint_id);
498 buffer.push_back(static_cast<uint8_t>(datapoint_type));
499 buffer.push_back(len >> 8);
500 buffer.push_back(len >> 0);
501 buffer.insert(buffer.end(), data, data + len);
502
503 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::DATAPOINT_DELIVER, .payload = std::move(buffer)});
504}
505
507 bool is_network_ready = network_is_connected() && remote_is_connected();
508 this->status_pin_->digital_write(is_network_ready);
509}
510
512 uint8_t status = 0x02;
513
514 if (network_is_connected()) {
515 status = 0x03;
516
517 // Protocol version 3 also supports specifying when connected to "the cloud"
518 if (this->protocol_version_ >= 0x03 && remote_is_connected()) {
519 status = 0x04;
520 }
521 } else {
522#ifdef USE_CAPTIVE_PORTAL
524 status = 0x01;
525 }
526#endif
527 };
528
529 return status;
530}
531
533#ifdef USE_WIFI
534 if (wifi::global_wifi_component != nullptr)
536#endif
537
538 return 0;
539}
540
542 uint8_t status = this->get_wifi_status_code_();
543
544 if (status == this->wifi_status_) {
545 return;
546 }
547
548 ESP_LOGD(TAG, "Sending WiFi Status");
549 this->wifi_status_ = status;
550 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::WIFI_STATE, .payload = std::vector<uint8_t>{status}});
551}
552
553#ifdef USE_TIME
556 return;
557 }
558 this->time_id_->add_on_time_sync_callback([this] { this->send_local_time_(); });
560}
561
563 ESPTime now = this->time_id_->now();
564 if (!now.is_valid() || now.minute == this->last_force_time_sync_minute_)
565 return;
567 this->send_local_time_(true);
568}
569
570void TuyaUART::send_local_time_(bool periodic) {
571 std::vector<uint8_t> payload;
572 ESPTime now = this->time_id_->now();
573 if (now.is_valid()) {
574 uint8_t year = now.year - 2000;
575 uint8_t month = now.month;
576 uint8_t day_of_month = now.day_of_month;
577 uint8_t hour = now.hour;
578 uint8_t minute = now.minute;
579 uint8_t second = now.second;
580 // Tuya days starts from Monday, esphome uses Sunday as day 1
581 uint8_t day_of_week = now.day_of_week - 1;
582 if (day_of_week == 0) {
583 day_of_week = 7;
584 }
585 // The forced send repeats every minute, so it stays out of the debug log
586 if (periodic) {
587 ESP_LOGV(TAG, "Sending local time");
588 } else {
589 ESP_LOGD(TAG, "Sending local time");
590 }
591 payload = std::vector<uint8_t>{0x01, year, month, day_of_month, hour, minute, second, day_of_week};
592 } else {
593 // By spec we need to notify MCU that the time was not obtained if this is a response to a query
594 ESP_LOGW(TAG, "Sending missing local time");
595 payload = std::vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
596 }
597 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::LOCAL_TIME_QUERY, .payload = payload});
598}
600 std::vector<uint8_t> payload;
601 ESPTime now = this->time_id_->utcnow();
602 if (now.is_valid()) {
603 uint8_t year = now.year - 2000;
604 uint8_t month = now.month;
605 uint8_t day_of_month = now.day_of_month;
606 uint8_t hour = now.hour;
607 uint8_t minute = now.minute;
608 uint8_t second = now.second;
609 ESP_LOGD(TAG, "Sending gmt time");
610 payload = std::vector<uint8_t>{0x01, year, month, day_of_month, hour, minute, second};
611 } else {
612 // By spec we need to notify MCU that the time was not obtained if this is a response to a query
613 ESP_LOGW(TAG, "Sending missing gmt time");
614 payload = std::vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
615 }
616 this->send_command_(TuyaCommand{.cmd = TuyaCommandType::GMT_TIME_QUERY, .payload = payload});
617}
618#endif
619
621
622} // namespace esphome::tuya
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void digital_write(bool value) override
Definition gpio.cpp:192
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void add_on_time_sync_callback(F &&callback)
void mark_initialized_()
Definition tuya.h:90
void dump_config() override
Prints each child entity's dump; a transport that overrides it prints its own block first,...
Definition tuya.h:53
void dump_datapoints_()
Definition tuya.cpp:10
void handle_datapoint_(const TuyaDatapoint &datapoint)
Store a datapoint received from the MCU and notify listeners.
Definition tuya.cpp:32
bool gmt_time_sync_callback_registered_
Definition tuya_uart.h:108
time::RealTimeClock * time_id_
Definition tuya_uart.h:104
uint8_t last_force_time_sync_minute_
Definition tuya_uart.h:106
void send_command_(TuyaCommand command)
optional< TuyaCommandType > expected_response_
Definition tuya_uart.h:121
void register_local_time_sync_callback_()
void send_datapoint_command(uint8_t datapoint_id, TuyaDatapointType datapoint_type, const uint8_t *data, size_t len) override
void send_local_time_(bool periodic=false)
void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len)
void send_raw_command_(TuyaCommand command)
TuyaInitState get_init_state()
TuyaInitState init_state_
Definition tuya_uart.h:110
InternalGPIOPin * status_pin_
Definition tuya_uart.h:114
void setup() override
Definition tuya_uart.cpp:43
void loop() override
Definition tuya_uart.cpp:57
LengthFrameReader< MAX_FRAME_SIZE, TuyaFrame > reader_
Definition tuya_uart.h:126
void dump_config() override
Definition tuya_uart.cpp:67
void send_empty_command_(TuyaCommandType command)
bool handle_frame_(const uint8_t *frame, size_t len)
Returns false for a frame that fails the checksum.
std::vector< TuyaCommand > command_queue_
Definition tuya_uart.h:120
void handle_datapoints_(const uint8_t *buffer, size_t len)
uint32_t last_command_timestamp_
Definition tuya_uart.h:117
std::unique_ptr< char[]> product_
Definition tuya_uart.h:119
uint8_t get_wifi_status_code_()
void write_byte(uint8_t data)
Definition uart.h:22
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms=250)
Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx,...
Definition uart.h:96
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
uint32_t buffer
Definition gif.cpp:50
CaptivePortal * global_captive_portal
ESPHOME_LOG_TAG(TAG, "cover")
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:36
TuyaExtendedServicesCommandType
Definition tuya_uart.h:48
void warn_too_long(size_t len)
TuyaDatapointType
Definition tuya.h:15
WiFiComponent * global_wifi_component
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:13
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:1086
const void size_t len
Definition hal.h:68
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:529
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:1920
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:1235
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
bool valid
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:25
uint8_t minute
minutes after the hour [0-59]
Definition time.h:34
uint8_t second
seconds after the minute [0-60]
Definition time.h:32
uint8_t hour
hours since midnight [0-23]
Definition time.h:36
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= internal::MIN_VALID_YEAR and the requested fields are in rang...
Definition time.h:84
uint8_t day_of_month
day of the month [1-31]
Definition time.h:40
uint16_t year
year
Definition time.h:46
uint8_t month
month; january=1 [1-12]
Definition time.h:44
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:38
TuyaCommandType cmd
Definition tuya_uart.h:64
std::vector< uint8_t > payload
Definition tuya_uart.h:65
uint8_t ack