ESPHome  2022.6.3
tuya_sensor.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tuya_sensor.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.sensor";
8 
10  this->parent_->register_listener(this->sensor_id_, [this](const TuyaDatapoint &datapoint) {
11  if (datapoint.type == TuyaDatapointType::BOOLEAN) {
12  ESP_LOGV(TAG, "MCU reported sensor %u is: %s", datapoint.id, ONOFF(datapoint.value_bool));
13  this->publish_state(datapoint.value_bool);
14  } else if (datapoint.type == TuyaDatapointType::INTEGER) {
15  ESP_LOGV(TAG, "MCU reported sensor %u is: %d", datapoint.id, datapoint.value_int);
16  this->publish_state(datapoint.value_int);
17  } else if (datapoint.type == TuyaDatapointType::ENUM) {
18  ESP_LOGV(TAG, "MCU reported sensor %u is: %u", datapoint.id, datapoint.value_enum);
19  this->publish_state(datapoint.value_enum);
20  } else if (datapoint.type == TuyaDatapointType::BITMASK) {
21  ESP_LOGV(TAG, "MCU reported sensor %u is: %x", datapoint.id, datapoint.value_bitmask);
22  this->publish_state(datapoint.value_bitmask);
23  }
24  });
25 }
26 
28  LOG_SENSOR("", "Tuya Sensor", this);
29  ESP_LOGCONFIG(TAG, " Sensor has datapoint ID %u", this->sensor_id_);
30 }
31 
32 } // namespace tuya
33 } // namespace esphome
TuyaDatapointType type
Definition: tuya.h:26
void setup() override
Definition: tuya_sensor.cpp:9
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:621
void dump_config() override
Definition: tuya_sensor.cpp:27
Definition: a4988.cpp:4