ESPHome  1.15.3
tuya.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace esphome {
7 namespace tuya {
8 
9 enum class TuyaDatapointType : uint8_t {
10  RAW = 0x00, // variable length
11  BOOLEAN = 0x01, // 1 byte (0/1)
12  INTEGER = 0x02, // 4 byte
13  STRING = 0x03, // variable length
14  ENUM = 0x04, // 1 byte
15  BITMASK = 0x05, // 2 bytes
16 };
17 
18 struct TuyaDatapoint {
19  uint8_t id;
21  union {
22  bool value_bool;
23  int value_int;
24  uint32_t value_uint;
25  uint8_t value_enum;
26  uint16_t value_bitmask;
27  };
28 };
29 
31  uint8_t datapoint_id;
32  std::function<void(TuyaDatapoint)> on_datapoint;
33 };
34 
35 enum class TuyaCommandType : uint8_t {
36  HEARTBEAT = 0x00,
37  PRODUCT_QUERY = 0x01,
38  CONF_QUERY = 0x02,
39  WIFI_STATE = 0x03,
40  WIFI_RESET = 0x04,
41  WIFI_SELECT = 0x05,
42  DATAPOINT_DELIVER = 0x06,
43  DATAPOINT_REPORT = 0x07,
44  DATAPOINT_QUERY = 0x08,
45  WIFI_TEST = 0x0E,
46 };
47 
48 enum class TuyaInitState : uint8_t {
49  INIT_HEARTBEAT = 0x00,
51  INIT_CONF,
52  INIT_WIFI,
54  INIT_DONE,
55 };
56 
57 class Tuya : public Component, public uart::UARTDevice {
58  public:
59  float get_setup_priority() const override { return setup_priority::LATE; }
60  void setup() override;
61  void loop() override;
62  void dump_config() override;
63  void register_listener(uint8_t datapoint_id, const std::function<void(TuyaDatapoint)> &func);
64  void set_datapoint_value(TuyaDatapoint datapoint);
65 
66  protected:
67  void handle_char_(uint8_t c);
68  void handle_datapoint_(const uint8_t *buffer, size_t len);
69  bool validate_message_();
70 
71  void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len);
72  void send_command_(TuyaCommandType command, const uint8_t *buffer, uint16_t len);
73  void send_empty_command_(TuyaCommandType command) { this->send_command_(command, nullptr, 0); }
74 
76  int gpio_status_ = -1;
77  int gpio_reset_ = -1;
78  std::string product_ = "";
79  std::vector<TuyaDatapointListener> listeners_;
80  std::vector<TuyaDatapoint> datapoints_;
81  std::vector<uint8_t> rx_message_;
82 };
83 
84 } // namespace tuya
85 } // namespace esphome
TuyaDatapointType type
Definition: tuya.h:20
TuyaCommandType
Definition: tuya.h:35
float get_setup_priority() const override
Definition: tuya.h:59
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:21
TuyaDatapointType
Definition: tuya.h:9
std::vector< TuyaDatapointListener > listeners_
Definition: tuya.h:79
std::vector< TuyaDatapoint > datapoints_
Definition: tuya.h:80
std::vector< uint8_t > rx_message_
Definition: tuya.h:81
std::function< void(TuyaDatapoint)> on_datapoint
Definition: tuya.h:32
Definition: a4988.cpp:4
void send_empty_command_(TuyaCommandType command)
Definition: tuya.h:73
TuyaInitState
Definition: tuya.h:48