ESPHome 2026.6.0-dev
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::tuya {
6
7static const char *const TAG = "tuya.text_sensor";
8
10 this->parent_->register_listener(this->sensor_id_, [this](const TuyaDatapoint &datapoint) {
11 switch (datapoint.type) {
13 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, datapoint.value_string.c_str());
14 this->publish_state(datapoint.value_string);
15 break;
17 char hex_buf[MAX_STATE_LEN + 1];
18 const char *formatted =
19 format_hex_pretty_to(hex_buf, sizeof(hex_buf), datapoint.value_raw.data(), datapoint.value_raw.size());
20 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, formatted);
21 this->publish_state(formatted);
22 break;
23 }
25 char buf[4]; // uint8_t max is 3 digits + null
26 buf_append_printf(buf, sizeof(buf), 0, "%u", datapoint.value_enum);
27 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, buf);
28 this->publish_state(buf);
29 break;
30 }
31 default:
32 ESP_LOGW(TAG, "Unsupported data type for tuya text sensor %u: %#02hhX", datapoint.id, (uint8_t) datapoint.type);
33 break;
34 }
35 });
36}
37
39 ESP_LOGCONFIG(TAG,
40 "Tuya Text Sensor:\n"
41 " Text Sensor has datapoint ID %u",
42 this->sensor_id_);
43}
44
45} // namespace esphome::tuya
void publish_state(const std::string &state)
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:749
const char *const TAG
Definition spi.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:340
std::string value_string
Definition tuya.h:38
std::vector< uint8_t > value_raw
Definition tuya.h:39
TuyaDatapointType type
Definition tuya.h:29