ESPHome 2026.2.4
Loading...
Searching...
No Matches
tuya_text_sensor.cpp
Go to the documentation of this file.
1#include "tuya_text_sensor.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace tuya {
7
8static const char *const TAG = "tuya.text_sensor";
9
11 this->parent_->register_listener(this->sensor_id_, [this](const TuyaDatapoint &datapoint) {
12 switch (datapoint.type) {
14 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, datapoint.value_string.c_str());
15 this->publish_state(datapoint.value_string);
16 break;
18 char hex_buf[MAX_STATE_LEN + 1];
19 const char *formatted =
20 format_hex_pretty_to(hex_buf, sizeof(hex_buf), datapoint.value_raw.data(), datapoint.value_raw.size());
21 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, formatted);
22 this->publish_state(formatted);
23 break;
24 }
26 char buf[4]; // uint8_t max is 3 digits + null
27 buf_append_printf(buf, sizeof(buf), 0, "%u", datapoint.value_enum);
28 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, buf);
29 this->publish_state(buf);
30 break;
31 }
32 default:
33 ESP_LOGW(TAG, "Unsupported data type for tuya text sensor %u: %#02hhX", datapoint.id, (uint8_t) datapoint.type);
34 break;
35 }
36 });
37}
38
40 ESP_LOGCONFIG(TAG,
41 "Tuya Text Sensor:\n"
42 " Text Sensor has datapoint ID %u",
43 this->sensor_id_);
44}
45
46} // namespace tuya
47} // namespace esphome
void publish_state(const std::string &state)
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:747
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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
std::string value_string
Definition tuya.h:39
std::vector< uint8_t > value_raw
Definition tuya.h:40
TuyaDatapointType type
Definition tuya.h:30