mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	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:
		
							
								
								
									
										28
									
								
								esphome/components/tuya/binary_sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								esphome/components/tuya/binary_sensor/__init__.py
									
									
									
									
									
										Normal 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])) | ||||||
							
								
								
									
										22
									
								
								esphome/components/tuya/binary_sensor/tuya_binary_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								esphome/components/tuya/binary_sensor/tuya_binary_sensor.cpp
									
									
									
									
									
										Normal 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 | ||||||
							
								
								
									
										24
									
								
								esphome/components/tuya/binary_sensor/tuya_binary_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								esphome/components/tuya/binary_sensor/tuya_binary_sensor.h
									
									
									
									
									
										Normal 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 | ||||||
| @@ -77,6 +77,11 @@ sensor: | |||||||
| #    type: blue | #    type: blue | ||||||
| #    name: APDS9960 Blue | #    name: APDS9960 Blue | ||||||
|  |  | ||||||
|  | binary_sensor: | ||||||
|  |   - platform: tuya | ||||||
|  |     id: tuya_binary_sensor | ||||||
|  |     sensor_datapoint: 1 | ||||||
|  |  | ||||||
| climate: | climate: | ||||||
|   - platform: tuya |   - platform: tuya | ||||||
|     id: tuya_climate |     id: tuya_climate | ||||||
| @@ -87,4 +92,3 @@ switch: | |||||||
|   - platform: tuya |   - platform: tuya | ||||||
|     id: tuya_switch |     id: tuya_switch | ||||||
|     switch_datapoint: 1 |     switch_datapoint: 1 | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user