ESPHome  2022.11.3
tuya_text_sensor.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tuya_text_sensor.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.text_sensor";
8 
10  this->parent_->register_listener(this->sensor_id_, [this](const TuyaDatapoint &datapoint) {
11  switch (datapoint.type) {
12  case TuyaDatapointType::STRING:
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;
16  case TuyaDatapointType::RAW: {
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  }
22  default:
23  ESP_LOGW(TAG, "Unsupported data type for tuya text sensor %u: %#02hhX", datapoint.id, (uint8_t) datapoint.type);
24  break;
25  }
26  });
27 }
28 
30  ESP_LOGCONFIG(TAG, "Tuya Text Sensor:");
31  ESP_LOGCONFIG(TAG, " Text Sensor has datapoint ID %u", this->sensor_id_);
32 }
33 
34 } // namespace tuya
35 } // namespace esphome
TuyaDatapointType type
Definition: tuya.h:26
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:621
Definition: a4988.cpp:4