mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Add support for Tuya Sensors (#1088)
* Add Tuya sensor * Add tuya sensor to test4 * Forgot id
This commit is contained in:
		
							
								
								
									
										28
									
								
								esphome/components/tuya/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								esphome/components/tuya/sensor/__init__.py
									
									
									
									
									
										Normal 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])) | ||||
							
								
								
									
										44
									
								
								esphome/components/tuya/sensor/tuya_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								esphome/components/tuya/sensor/tuya_sensor.cpp
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										24
									
								
								esphome/components/tuya/sensor/tuya_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								esphome/components/tuya/sensor/tuya_sensor.h
									
									
									
									
									
										Normal 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 | ||||
| @@ -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 | ||||
| # | ||||
|   | ||||
		Reference in New Issue
	
	Block a user