ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tuya_uart.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4#include <memory>
5#include <vector>
6
7#include "tuya.h"
11
12#ifdef USE_TIME
14#include "esphome/core/time.h"
15#endif
16
17namespace esphome::tuya {
18
19// Longest frame accepted, as in Tasmota; keeps the reader's positions 8 bit
20static constexpr size_t MAX_FRAME_SIZE = 255;
21
22// 55 AA, version, command, payload length (2, BE), payload, checksum
23using TuyaLength = PayloadLengthU16BE<LengthField{.offset = 4, .trailer = 1}>;
24void warn_too_long(size_t len);
26
27enum class TuyaCommandType : uint8_t {
28 HEARTBEAT = 0x00,
29 PRODUCT_QUERY = 0x01,
30 CONF_QUERY = 0x02,
31 WIFI_STATE = 0x03,
32 WIFI_RESET = 0x04,
33 WIFI_SELECT = 0x05,
34 DATAPOINT_DELIVER = 0x06,
36 DATAPOINT_QUERY = 0x08,
37 GMT_TIME_QUERY = 0x0C,
38 WIFI_TEST = 0x0E,
39 LOCAL_TIME_QUERY = 0x1C,
42 WIFI_RSSI = 0x24,
43 VACUUM_MAP_UPLOAD = 0x28,
44 GET_NETWORK_STATUS = 0x2B,
45 EXTENDED_SERVICES = 0x34,
46};
47
48enum class TuyaExtendedServicesCommandType : uint8_t {
49 RESET_NOTIFICATION = 0x04,
50 MODULE_RESET = 0x05,
51 UPDATE_IN_PROGRESS = 0x0A,
52};
53
54enum class TuyaInitState : uint8_t {
55 INIT_HEARTBEAT = 0x00,
61};
62
65 std::vector<uint8_t> payload;
66};
67
69class TuyaUART final : public Tuya, public uart::UARTDevice {
70 public:
71 void setup() override;
72 void loop() override;
73 void dump_config() override;
74 void set_status_pin(InternalGPIOPin *status_pin) { this->status_pin_ = status_pin; }
76#ifdef USE_TIME
77 void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
78 void set_force_time_sync(bool force_time_sync) { this->force_time_sync_ = force_time_sync; }
79#endif
80
81 protected:
82 void dump_mcu_config_();
84 bool handle_frame_(const uint8_t *frame, size_t len);
85 void handle_datapoints_(const uint8_t *buffer, size_t len);
86
87 void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len);
88 void send_raw_command_(TuyaCommand command);
90 void send_command_(TuyaCommand command);
92 void send_datapoint_command(uint8_t datapoint_id, TuyaDatapointType datapoint_type, const uint8_t *data,
93 size_t len) override;
94 void set_status_pin_();
95 void send_wifi_status_();
96 uint8_t get_wifi_status_code_();
97 uint8_t get_wifi_rssi_();
98
99#ifdef USE_TIME
102 void send_local_time_(bool periodic = false);
103 void send_gmt_time_();
105 bool force_time_sync_{false};
106 uint8_t last_force_time_sync_minute_{0xFF}; // 0xFF: no minute seen yet
109#endif
111 bool init_failed_{false};
113 uint8_t protocol_version_ = -1;
119 std::unique_ptr<char[]> product_;
120 std::vector<TuyaCommand> command_queue_;
121 optional<TuyaCommandType> expected_response_{};
122 uint8_t wifi_status_ = -1;
123 bool product_invalid_{false};
124 // Holds commands back while a received frame is handled
125 bool handling_frame_{false};
127};
128
129} // namespace esphome::tuya
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Transport-agnostic Tuya controller.
Definition tuya.h:49
Tuya MCU controller connected over UART (the standard Tuya serial protocol).
Definition tuya_uart.h:69
bool gmt_time_sync_callback_registered_
Definition tuya_uart.h:108
time::RealTimeClock * time_id_
Definition tuya_uart.h:104
void set_force_time_sync(bool force_time_sync)
Definition tuya_uart.h:78
uint8_t last_force_time_sync_minute_
Definition tuya_uart.h:106
void send_command_(TuyaCommand command)
uint32_t last_rx_char_timestamp_
Definition tuya_uart.h:118
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 set_status_pin(InternalGPIOPin *status_pin)
Definition tuya_uart.h:74
void set_time_id(time::RealTimeClock *time_id)
Definition tuya_uart.h:77
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_()
uint32_t buffer
Definition gif.cpp:50
TuyaExtendedServicesCommandType
Definition tuya_uart.h:48
PayloadLengthU16BE< LengthField{.offset=4,.trailer=1}> TuyaLength
Definition tuya_uart.h:23
void warn_too_long(size_t len)
TuyaDatapointType
Definition tuya.h:15
const void size_t len
Definition hal.h:68
static void uint32_t
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
offset: where the length field starts, counted from the first header byte.
FrameFormat option: a frame longer than MAX calls WARN(length) and is skipped; a replay can warn agai...
TuyaCommandType cmd
Definition tuya_uart.h:64
std::vector< uint8_t > payload
Definition tuya_uart.h:65