forked from esphome/esphome
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Tuya Sensors (esphome#1088)
* Add Tuya sensor * Add tuya sensor to test4 * Forgot id
- Loading branch information
1 parent
9cd5be4
commit 005ee19
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters