From ed5f207ebaf704914f0d648b465b26ddbf2a0f84 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Sun, 12 Jul 2020 10:50:41 +1200 Subject: [PATCH] Add support for Tuya Binary Sensors (#1089) * Add Tuya binary sensor * Add tuya binary sensor to test4 * Fix copy/paste * Forgot id --- .../components/tuya/binary_sensor/__init__.py | 28 +++++++++++++++++++ .../tuya/binary_sensor/tuya_binary_sensor.cpp | 22 +++++++++++++++ .../tuya/binary_sensor/tuya_binary_sensor.h | 24 ++++++++++++++++ tests/test4.yaml | 6 +++- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 esphome/components/tuya/binary_sensor/__init__.py create mode 100644 esphome/components/tuya/binary_sensor/tuya_binary_sensor.cpp create mode 100644 esphome/components/tuya/binary_sensor/tuya_binary_sensor.h diff --git a/esphome/components/tuya/binary_sensor/__init__.py b/esphome/components/tuya/binary_sensor/__init__.py new file mode 100644 index 0000000000..3ba479bf19 --- /dev/null +++ b/esphome/components/tuya/binary_sensor/__init__.py @@ -0,0 +1,28 @@ +from esphome.components import binary_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" + +TuyaBinarySensor = tuya_ns.class_('TuyaBinarySensor', binary_sensor.BinarySensor, cg.Component) + +CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_id(TuyaBinarySensor), + 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 binary_sensor.register_binary_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/binary_sensor/tuya_binary_sensor.cpp b/esphome/components/tuya/binary_sensor/tuya_binary_sensor.cpp new file mode 100644 index 0000000000..ad3d18efae --- /dev/null +++ b/esphome/components/tuya/binary_sensor/tuya_binary_sensor.cpp @@ -0,0 +1,22 @@ +#include "esphome/core/log.h" +#include "tuya_binary_sensor.h" + +namespace esphome { +namespace tuya { + +static const char *TAG = "tuya.binary_sensor"; + +void TuyaBinarySensor::setup() { + this->parent_->register_listener(this->sensor_id_, [this](TuyaDatapoint datapoint) { + this->publish_state(datapoint.value_bool); + ESP_LOGD(TAG, "MCU reported binary sensor is: %s", ONOFF(datapoint.value_bool)); + }); +} + +void TuyaBinarySensor::dump_config() { + ESP_LOGCONFIG(TAG, "Tuya Binary Sensor:"); + ESP_LOGCONFIG(TAG, " Binary Sensor has datapoint ID %u", this->sensor_id_); +} + +} // namespace tuya +} // namespace esphome diff --git a/esphome/components/tuya/binary_sensor/tuya_binary_sensor.h b/esphome/components/tuya/binary_sensor/tuya_binary_sensor.h new file mode 100644 index 0000000000..1eeeb40477 --- /dev/null +++ b/esphome/components/tuya/binary_sensor/tuya_binary_sensor.h @@ -0,0 +1,24 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/tuya/tuya.h" +#include "esphome/components/binary_sensor/binary_sensor.h" + +namespace esphome { +namespace tuya { + +class TuyaBinarySensor : public binary_sensor::BinarySensor, 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 2709941b0f..61c1e278c4 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -77,6 +77,11 @@ sensor: # type: blue # name: APDS9960 Blue +binary_sensor: + - platform: tuya + id: tuya_binary_sensor + sensor_datapoint: 1 + climate: - platform: tuya id: tuya_climate @@ -87,4 +92,3 @@ switch: - platform: tuya id: tuya_switch switch_datapoint: 1 -