Add support for Tuya Binary Sensors (#1089)

* Add Tuya binary sensor

* Add tuya binary sensor to test4

* Fix copy/paste

* Forgot id
This commit is contained in:
Jesse Hills 2020-07-12 10:50:41 +12:00 committed by GitHub
parent a76b8e745b
commit ed5f207eba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 1 deletions

View file

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

View file

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

View file

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

View file

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