ESPHome  2023.11.6
tuya_text_sensor.cpp
Go to the documentation of this file.
1 #include "tuya_text_sensor.h"
2 #include "esphome/core/log.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  }
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
TuyaDatapointType type
Definition: tuya.h:30
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:671
const char *const TAG
Definition: spi.cpp:8
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7