Files
esphome-lsm6ds/esphome/components/tuya/sensor/tuya_sensor.cpp
T
Jesse Hills a76b8e745b Add support for Tuya Sensors (#1088)
* Add Tuya sensor

* Add tuya sensor to test4

* Forgot id
2020-07-11 19:46:10 -03:00

45 lines
1.5 KiB
C++

#include "esphome/core/log.h"
#include "tuya_sensor.h"
namespace esphome {
namespace tuya {
static const char *TAG = "tuya.sensor";
void TuyaSensor::setup() {
this->parent_->register_listener(this->sensor_id_, [this](TuyaDatapoint datapoint) {
if (datapoint.type == TuyaDatapointType::BOOLEAN) {
this->publish_state(datapoint.value_bool);
ESP_LOGD(TAG, "MCU reported sensor is: %s", ONOFF(datapoint.value_bool));
} else if (datapoint.type == TuyaDatapointType::INTEGER) {
this->publish_state(datapoint.value_int);
ESP_LOGD(TAG, "MCU reported sensor is: %d", datapoint.value_int);
} else if (datapoint.type == TuyaDatapointType::ENUM) {
this->publish_state(datapoint.value_enum);
ESP_LOGD(TAG, "MCU reported sensor is: %d", datapoint.value_enum);
} else if (datapoint.type == TuyaDatapointType::BITMASK) {
this->publish_state(datapoint.value_bitmask);
ESP_LOGD(TAG, "MCU reported sensor is: %x", datapoint.value_bitmask);
}
});
}
// void TuyaSensor::write_state(bool state) {
// TuyaDatapoint datapoint{};
// datapoint.id = this->sensor_id_;
// datapoint.type = TuyaDatapointType::BOOLEAN;
// datapoint.value_bool = state;
// this->parent_->set_datapoint_value(datapoint);
// ESP_LOGD(TAG, "Setting sensor: %s", ONOFF(state));
// this->publish_state(state);
// }
void TuyaSensor::dump_config() {
LOG_SENSOR("", "Tuya Sensor", this);
ESP_LOGCONFIG(TAG, " Sensor has datapoint ID %u", this->sensor_id_);
}
} // namespace tuya
} // namespace esphome