ESPHome 2025.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 {
6namespace tuya {
7
8static const char *const TAG = "tuya.sensor";
9
11 this->parent_->register_listener(this->sensor_id_, [this](const TuyaDatapoint &datapoint) {
12 if (datapoint.type == TuyaDatapointType::BOOLEAN) {
13 ESP_LOGV(TAG, "MCU reported sensor %u is: %s", datapoint.id, ONOFF(datapoint.value_bool));
14 this->publish_state(datapoint.value_bool);
15 } else if (datapoint.type == TuyaDatapointType::INTEGER) {
16 ESP_LOGV(TAG, "MCU reported sensor %u is: %d", datapoint.id, datapoint.value_int);
17 this->publish_state(datapoint.value_int);
18 } else if (datapoint.type == TuyaDatapointType::ENUM) {
19 ESP_LOGV(TAG, "MCU reported sensor %u is: %u", datapoint.id, datapoint.value_enum);
20 this->publish_state(datapoint.value_enum);
21 } else if (datapoint.type == TuyaDatapointType::BITMASK) {
22 ESP_LOGV(TAG, "MCU reported sensor %u is: %" PRIx32, datapoint.id, datapoint.value_bitmask);
23 this->publish_state(datapoint.value_bitmask);
24 }
25 });
26}
27
29 LOG_SENSOR("", "Tuya Sensor", this);
30 ESP_LOGCONFIG(TAG, " Sensor has datapoint ID %u", this->sensor_id_);
31}
32
33} // namespace tuya
34} // namespace esphome
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:697
void dump_config() override
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
TuyaDatapointType type
Definition tuya.h:30