ESPHome 2025.5.1
Loading...
Searching...
No Matches
tuya_text_sensor.cpp
Go to the documentation of this file.
1#include "tuya_text_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace 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 std::string data = format_hex_pretty(datapoint.value_raw);
18 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, data.c_str());
19 this->publish_state(data);
20 break;
21 }
23 std::string data = to_string(datapoint.value_enum);
24 ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, data.c_str());
25 this->publish_state(data);
26 break;
27 }
28 default:
29 ESP_LOGW(TAG, "Unsupported data type for tuya text sensor %u: %#02hhX", datapoint.id, (uint8_t) datapoint.type);
30 break;
31 }
32 });
33}
34
36 ESP_LOGCONFIG(TAG, "Tuya Text Sensor:");
37 ESP_LOGCONFIG(TAG, " Text Sensor has datapoint ID %u", this->sensor_id_);
38}
39
40} // namespace tuya
41} // 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:697
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string to_string(int value)
Definition helpers.cpp:82
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.
Definition helpers.cpp:372
std::string value_string
Definition tuya.h:39
std::vector< uint8_t > value_raw
Definition tuya.h:40
TuyaDatapointType type
Definition tuya.h:30