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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 1 deletions

View File

@ -0,0 +1,28 @@
from esphome.components import sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
from .. import tuya_ns, CONF_TUYA_ID, Tuya
DEPENDENCIES = ['tuya']
CONF_SENSOR_DATAPOINT = "sensor_datapoint"
TuyaSensor = tuya_ns.class_('TuyaSensor', sensor.Sensor, cg.Component)
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(TuyaSensor),
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
cv.Required(CONF_SENSOR_DATAPOINT): cv.uint8_t,
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
paren = yield cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren))
cg.add(var.set_sensor_id(config[CONF_SENSOR_DATAPOINT]))

View File

@ -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

View File

@ -0,0 +1,24 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/tuya/tuya.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace tuya {
class TuyaSensor : public sensor::Sensor, public Component {
public:
void setup() override;
void dump_config() override;
void set_sensor_id(uint8_t sensor_id) { this->sensor_id_ = sensor_id; }
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
protected:
Tuya *parent_;
uint8_t sensor_id_{0};
};
} // namespace tuya
} // namespace esphome

View File

@ -49,13 +49,15 @@ web_server:
username: admin
password: admin
tuya:
sensor:
- platform: homeassistant
entity_id: sensor.hello_world
id: ha_hello_world
- platform: tuya
id: tuya_sensor
sensor_datapoint: 1
#
# platform sensor.apds9960 requires component apds9960
#