ESPHome 2026.5.0
Loading...
Searching...
No Matches
tuya_sensor.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tuya_sensor.h"
3#include <cinttypes>
4
5namespace esphome::tuya {
6
7static 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: %" PRIx32, 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 esphome::tuya
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:747
void dump_config() override
const char *const TAG
Definition spi.cpp:7
TuyaDatapointType type
Definition tuya.h:29