Add support for Tuya Sensors (#1088)

* Add Tuya sensor

* Add tuya sensor to test4

* Forgot id
This commit is contained in:
Jesse Hills
2020-07-12 10:46:10 +12:00
committed by GitHub
parent 68d29c5af5
commit a76b8e745b
4 changed files with 99 additions and 1 deletions
@@ -0,0 +1,44 @@
#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