From a76b8e745b3bc572c6696da56b6fe2b1a5ccda7f Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Sun, 12 Jul 2020 10:46:10 +1200 Subject: [PATCH] Add support for Tuya Sensors (#1088) * Add Tuya sensor * Add tuya sensor to test4 * Forgot id --- esphome/components/tuya/sensor/__init__.py | 28 ++++++++++++ .../components/tuya/sensor/tuya_sensor.cpp | 44 +++++++++++++++++++ esphome/components/tuya/sensor/tuya_sensor.h | 24 ++++++++++ tests/test4.yaml | 4 +- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 esphome/components/tuya/sensor/__init__.py create mode 100644 esphome/components/tuya/sensor/tuya_sensor.cpp create mode 100644 esphome/components/tuya/sensor/tuya_sensor.h diff --git a/esphome/components/tuya/sensor/__init__.py b/esphome/components/tuya/sensor/__init__.py new file mode 100644 index 0000000000..5f2ff2d674 --- /dev/null +++ b/esphome/components/tuya/sensor/__init__.py @@ -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])) diff --git a/esphome/components/tuya/sensor/tuya_sensor.cpp b/esphome/components/tuya/sensor/tuya_sensor.cpp new file mode 100644 index 0000000000..49a1af2efe --- /dev/null +++ b/esphome/components/tuya/sensor/tuya_sensor.cpp @@ -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 diff --git a/esphome/components/tuya/sensor/tuya_sensor.h b/esphome/components/tuya/sensor/tuya_sensor.h new file mode 100644 index 0000000000..8fd7cd1770 --- /dev/null +++ b/esphome/components/tuya/sensor/tuya_sensor.h @@ -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 diff --git a/tests/test4.yaml b/tests/test4.yaml index a7fbacc99d..2709941b0f 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -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 #