11 static const char *
const TAG =
"tuya";
12 static const int COMMAND_DELAY = 10;
13 static const int RECEIVE_TIMEOUT = 300;
14 static const int MAX_RETRIES = 5;
33 ESP_LOGCONFIG(TAG,
"Tuya:");
36 ESP_LOGCONFIG(TAG,
" Initialization failed. Current init_state: %u", static_cast<uint8_t>(this->
init_state_));
38 ESP_LOGCONFIG(TAG,
" Configuration will be reported when setup is complete. Current init_state: %u",
41 ESP_LOGCONFIG(TAG,
" If no further output is received, confirm that this is a supported Tuya device.");
46 ESP_LOGCONFIG(TAG,
" Datapoint %u: raw (value: %s)", info.id,
format_hex_pretty(info.value_raw).c_str());
48 ESP_LOGCONFIG(TAG,
" Datapoint %u: switch (value: %s)", info.id, ONOFF(info.value_bool));
50 ESP_LOGCONFIG(TAG,
" Datapoint %u: int value (value: %d)", info.id, info.value_int);
52 ESP_LOGCONFIG(TAG,
" Datapoint %u: string value (value: %s)", info.id, info.value_string.c_str());
54 ESP_LOGCONFIG(TAG,
" Datapoint %u: enum (value: %d)", info.id, info.value_enum);
56 ESP_LOGCONFIG(TAG,
" Datapoint %u: bitmask (value: %x)", info.id, info.value_bitmask);
58 ESP_LOGCONFIG(TAG,
" Datapoint %u: unknown", info.id);
62 ESP_LOGCONFIG(TAG,
" GPIO Configuration: status: pin %d, reset: pin %d", this->
status_pin_reported_,
66 LOG_PIN(
" Status Pin: ", this->
status_pin_.value());
68 ESP_LOGCONFIG(TAG,
" Product: '%s'", this->
product_.c_str());
75 uint8_t new_byte = data[at];
79 return new_byte == 0x55;
82 return new_byte == 0xAA;
90 uint8_t command = data[3];
101 uint16_t length = (uint16_t(data[4]) << 8) | (uint16_t(data[5]));
108 uint8_t rx_checksum = new_byte;
109 uint8_t calc_checksum = 0;
110 for (uint32_t i = 0; i < 6 + length; i++)
111 calc_checksum += data[i];
113 if (rx_checksum != calc_checksum) {
114 ESP_LOGW(TAG,
"Tuya Received invalid message checksum %02X!=%02X", rx_checksum, calc_checksum);
119 const uint8_t *message_data = data + 6;
120 ESP_LOGV(TAG,
"Received Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", command, version,
146 switch (command_type) {
148 ESP_LOGV(TAG,
"MCU Heartbeat (0x%02X)", buffer[0]);
150 if (buffer[0] == 0) {
151 ESP_LOGI(TAG,
"MCU restarted");
162 for (
size_t i = 0; i <
len; i++) {
163 if (!std::isprint(buffer[i])) {
169 this->
product_ = std::string(reinterpret_cast<const char *>(buffer), len);
171 this->
product_ = R
"({"p":"INVALID"})"; 196 ESP_LOGW(TAG,
"Supplied status_pin does not equals the reported pin %i. TuyaMcu will work in limited mode.",
201 ESP_LOGV(TAG,
"Configured WIFI_STATE periodic send");
214 ESP_LOGE(TAG,
"WIFI_RESET is not handled");
217 ESP_LOGE(TAG,
"WIFI_SELECT is not handled");
241 ESP_LOGW(TAG,
"LOCAL_TIME_QUERY is not handled because time is not configured");
244 ESP_LOGE(TAG,
"LOCAL_TIME_QUERY is not handled");
248 ESP_LOGE(TAG,
"Invalid command (0x%02X) received", command);
255 datapoint.
id = buffer[0];
257 datapoint.value_uint = 0;
259 size_t data_size = (buffer[2] << 8) + buffer[3];
260 const uint8_t *data = buffer + 4;
261 size_t data_len = len - 4;
262 if (data_size > data_len) {
263 ESP_LOGW(TAG,
"Datapoint %u is truncated and cannot be parsed (%zu > %zu)", datapoint.id, data_size, data_len);
267 datapoint.len = data_size;
269 switch (datapoint.type) {
271 datapoint.value_raw = std::vector<uint8_t>(data, data + data_size);
272 ESP_LOGD(TAG,
"Datapoint %u update to %s", datapoint.id,
format_hex_pretty(datapoint.value_raw).c_str());
275 if (data_size != 1) {
276 ESP_LOGW(TAG,
"Datapoint %u has bad boolean len %zu", datapoint.id, data_size);
279 datapoint.value_bool = data[0];
280 ESP_LOGD(TAG,
"Datapoint %u update to %s", datapoint.id, ONOFF(datapoint.value_bool));
283 if (data_size != 4) {
284 ESP_LOGW(TAG,
"Datapoint %u has bad integer len %zu", datapoint.id, data_size);
287 datapoint.value_uint =
encode_uint32(data[0], data[1], data[2], data[3]);
288 ESP_LOGD(TAG,
"Datapoint %u update to %d", datapoint.id, datapoint.value_int);
291 datapoint.value_string = std::string(reinterpret_cast<const char *>(data), data_size);
292 ESP_LOGD(TAG,
"Datapoint %u update to %s", datapoint.id, datapoint.value_string.c_str());
295 if (data_size != 1) {
296 ESP_LOGW(TAG,
"Datapoint %u has bad enum len %zu", datapoint.id, data_size);
299 datapoint.value_enum = data[0];
300 ESP_LOGD(TAG,
"Datapoint %u update to %d", datapoint.id, datapoint.value_enum);
308 datapoint.value_bitmask =
encode_uint32(0, 0, data[0], data[1]);
311 datapoint.value_bitmask =
encode_uint32(data[0], data[1], data[2], data[3]);
314 ESP_LOGW(TAG,
"Datapoint %u has bad bitmask len %zu", datapoint.id, data_size);
317 ESP_LOGD(TAG,
"Datapoint %u update to %#08X", datapoint.id, datapoint.value_bitmask);
320 ESP_LOGW(TAG,
"Datapoint %u has unknown type %#02hhX", datapoint.id, static_cast<uint8_t>(datapoint.type));
324 len -= data_size + 4;
325 buffer = data + data_size;
330 if (datapoint.id == i) {
331 ESP_LOGV(TAG,
"Datapoint %u found in ignore_mcu_update_on_datapoints list, dropping MCU update", datapoint.id);
342 if (other.id == datapoint.id) {
348 this->datapoints_.push_back(datapoint);
353 if (listener.datapoint_id == datapoint.id)
354 listener.on_datapoint(datapoint);
360 uint8_t len_hi = (uint8_t)(command.
payload.size() >> 8);
361 uint8_t len_lo = (uint8_t)(command.
payload.size() & 0xFF);
365 switch (command.
cmd) {
383 ESP_LOGV(TAG,
"Sending Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", static_cast<uint8_t>(command.
cmd),
390 uint8_t
checksum = 0x55 + 0xAA + (uint8_t) command.
cmd + len_hi + len_lo;
391 for (
auto &data : command.
payload)
409 ESP_LOGE(TAG,
"Initialization failed at init_state %u", static_cast<uint8_t>(this->
init_state_));
438 this->
status_pin_.value()->digital_write(is_network_ready);
442 uint8_t status = 0x02;
456 ESP_LOGD(TAG,
"Sending WiFi Status");
463 std::vector<uint8_t> payload;
467 uint8_t year = now.
year - 2000;
468 uint8_t month = now.
month;
470 uint8_t hour = now.
hour;
471 uint8_t minute = now.
minute;
472 uint8_t second = now.
second;
475 if (day_of_week == 0) {
478 ESP_LOGD(TAG,
"Sending local time");
479 payload = std::vector<uint8_t>{0x01, year, month, day_of_month, hour, minute, second, day_of_week};
482 ESP_LOGW(TAG,
"Sending missing local time");
483 payload = std::vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
539 if (datapoint.id == datapoint_id)
546 uint8_t length,
bool forced) {
547 ESP_LOGD(TAG,
"Setting datapoint %u to %u", datapoint_id, value);
550 ESP_LOGW(TAG,
"Setting unknown datapoint %u", datapoint_id);
551 }
else if (datapoint->type != datapoint_type) {
552 ESP_LOGE(TAG,
"Attempt to set datapoint %u with incorrect type", datapoint_id);
554 }
else if (!forced && datapoint->value_uint == value) {
555 ESP_LOGV(TAG,
"Not sending unchanged value");
559 std::vector<uint8_t> data;
562 data.push_back(value >> 24);
563 data.push_back(value >> 16);
565 data.push_back(value >> 8);
567 data.push_back(value >> 0);
570 ESP_LOGE(TAG,
"Unexpected datapoint length %u", length);
577 ESP_LOGD(TAG,
"Setting datapoint %u to %s", datapoint_id,
format_hex_pretty(value).c_str());
580 ESP_LOGW(TAG,
"Setting unknown datapoint %u", datapoint_id);
582 ESP_LOGE(TAG,
"Attempt to set datapoint %u with incorrect type", datapoint_id);
584 }
else if (!forced && datapoint->value_raw == value) {
585 ESP_LOGV(TAG,
"Not sending unchanged value");
592 ESP_LOGD(TAG,
"Setting datapoint %u to %s", datapoint_id, value.c_str());
594 if (!datapoint.has_value()) {
595 ESP_LOGW(TAG,
"Setting unknown datapoint %u", datapoint_id);
597 ESP_LOGE(TAG,
"Attempt to set datapoint %u with incorrect type", datapoint_id);
599 }
else if (!forced && datapoint->value_string == value) {
600 ESP_LOGV(TAG,
"Not sending unchanged value");
603 std::vector<uint8_t> data;
604 for (
char const &c : value) {
611 std::vector<uint8_t> buffer;
612 buffer.push_back(datapoint_id);
613 buffer.push_back(static_cast<uint8_t>(datapoint_type));
614 buffer.push_back(data.size() >> 8);
615 buffer.push_back(data.size() >> 0);
616 buffer.insert(buffer.end(), data.begin(), data.end());
624 .on_datapoint = func,
630 if (datapoint.id == datapoint_id)
uint8_t month
month; january=1 [1-12]
uint8_t protocol_version_
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
void set_raw_datapoint_value(uint8_t datapoint_id, const std::vector< uint8_t > &value)
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
void write_array(const uint8_t *data, size_t len)
TuyaInitState init_state_
CallbackManager< void()> initialized_callback_
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
void write_byte(uint8_t data)
void handle_char_(uint8_t c)
void handle_datapoints_(const uint8_t *buffer, size_t len)
void set_raw_datapoint_value_(uint8_t datapoint_id, const std::vector< uint8_t > &value, bool forced)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void force_set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length)
uint32_t last_command_timestamp_
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.
void force_set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
void force_set_raw_datapoint_value(uint8_t datapoint_id, const std::vector< uint8_t > &value)
std::vector< uint8_t > ignore_mcu_update_on_datapoints_
void process_command_queue_()
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
optional< TuyaCommandType > expected_response_
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
std::vector< TuyaDatapointListener > listeners_
uint32_t IRAM_ATTR HOT millis()
void force_set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
std::vector< TuyaDatapoint > datapoints_
uint8_t minute
minutes after the hour [0-59]
void set_string_datapoint_value_(uint8_t datapoint_id, const std::string &value, bool forced)
std::vector< uint8_t > rx_message_
optional< TuyaDatapoint > get_datapoint_(uint8_t datapoint_id)
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
A more user-friendly version of struct tm from time.h.
bool read_byte(uint8_t *data)
TuyaInitState get_init_state()
bool remote_is_connected()
Return whether the node has any form of "remote" connection via the API or to an MQTT broker...
uint8_t second
seconds after the minute [0-60]
void force_set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
optional< time::RealTimeClock * > time_id_
std::vector< uint8_t > payload
uint8_t day_of_week
day of the week; sunday=1 [1-7]
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
void set_bitmask_datapoint_value(uint8_t datapoint_id, uint32_t value, uint8_t length)
void send_datapoint_command_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, std::vector< uint8_t > data)
void send_raw_command_(TuyaCommand command)
void set_string_datapoint_value(uint8_t datapoint_id, const std::string &value)
void send_command_(const TuyaCommand &command)
std::vector< TuyaCommand > command_queue_
void send_empty_command_(TuyaCommandType command)
uint8_t day_of_month
day of the month [1-31]
void set_numeric_datapoint_value_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, uint32_t value, uint8_t length, bool forced)
optional< InternalGPIOPin * > status_pin_
uint32_t last_rx_char_timestamp_
void force_set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len)
void dump_config() override
uint8_t hour
hours since midnight [0-23]
void IRAM_ATTR HOT delay(uint32_t ms)