Add new component for Tuya dimmers (#743)

* Add new component for Tuya dimmers

* Update code

* Class naming

* Log output

* Fixes

* Lint

* Format

* Fix test

* log setting datapoint values

* remove in_setup_ and fix datapoint handling


Co-authored-by: Samuel Sieb <samuel@sieb.net>
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Samuel Sieb
2019-10-19 12:47:24 -07:00
committed by Otto Winter
parent af15a4e710
commit 96ff9a162c
9 changed files with 571 additions and 17 deletions
+38
View File
@@ -0,0 +1,38 @@
from esphome.components import light
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_OUTPUT_ID, CONF_MIN_VALUE, CONF_MAX_VALUE
from .. import tuya_ns, CONF_TUYA_ID, Tuya
DEPENDENCIES = ['tuya']
CONF_DIMMER_DATAPOINT = "dimmer_datapoint"
CONF_SWITCH_DATAPOINT = "switch_datapoint"
TuyaLight = tuya_ns.class_('TuyaLight', light.LightOutput, cg.Component)
CONFIG_SCHEMA = light.BRIGHTNESS_ONLY_LIGHT_SCHEMA.extend({
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(TuyaLight),
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
cv.Required(CONF_DIMMER_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_MIN_VALUE): cv.int_,
cv.Optional(CONF_MAX_VALUE): cv.int_,
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
yield cg.register_component(var, config)
yield light.register_light(var, config)
if CONF_DIMMER_DATAPOINT in config:
cg.add(var.set_dimmer_id(config[CONF_DIMMER_DATAPOINT]))
if CONF_SWITCH_DATAPOINT in config:
cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT]))
if CONF_MIN_VALUE in config:
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))
if CONF_MAX_VALUE in config:
cg.add(var.set_max_value(config[CONF_MAX_VALUE]))
paren = yield cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren))
@@ -0,0 +1,85 @@
#include "esphome/core/log.h"
#include "tuya_light.h"
namespace esphome {
namespace tuya {
static const char *TAG = "tuya.light";
void TuyaLight::setup() {
if (this->dimmer_id_.has_value()) {
this->parent_->register_listener(*this->dimmer_id_, [this](TuyaDatapoint datapoint) {
auto call = this->state_->make_call();
call.set_brightness(float(datapoint.value_uint) / this->max_value_);
call.perform();
});
}
if (switch_id_.has_value()) {
this->parent_->register_listener(*this->switch_id_, [this](TuyaDatapoint datapoint) {
auto call = this->state_->make_call();
call.set_state(datapoint.value_bool);
call.perform();
});
}
}
void TuyaLight::dump_config() {
ESP_LOGCONFIG(TAG, "Tuya Dimmer:");
if (this->dimmer_id_.has_value())
ESP_LOGCONFIG(TAG, " Dimmer has datapoint ID %u", *this->dimmer_id_);
if (this->switch_id_.has_value())
ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
}
light::LightTraits TuyaLight::get_traits() {
auto traits = light::LightTraits();
traits.set_supports_brightness(this->dimmer_id_.has_value());
return traits;
}
void TuyaLight::setup_state(light::LightState *state) { state_ = state; }
void TuyaLight::write_state(light::LightState *state) {
float brightness;
state->current_values_as_brightness(&brightness);
if (brightness == 0.0f) {
// turning off, first try via switch (if exists), then dimmer
if (switch_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->switch_id_;
datapoint.type = TuyaDatapointType::BOOLEAN;
datapoint.value_bool = false;
parent_->set_datapoint_value(datapoint);
} else if (dimmer_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->dimmer_id_;
datapoint.type = TuyaDatapointType::INTEGER;
datapoint.value_int = 0;
parent_->set_datapoint_value(datapoint);
}
return;
}
auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
brightness_int = std::max(brightness_int, this->min_value_);
if (this->dimmer_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->dimmer_id_;
datapoint.type = TuyaDatapointType::INTEGER;
datapoint.value_int = brightness_int;
parent_->set_datapoint_value(datapoint);
}
if (this->switch_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->switch_id_;
datapoint.type = TuyaDatapointType::BOOLEAN;
datapoint.value_bool = true;
parent_->set_datapoint_value(datapoint);
}
}
} // namespace tuya
} // namespace esphome
@@ -0,0 +1,36 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/tuya/tuya.h"
#include "esphome/components/light/light_output.h"
namespace esphome {
namespace tuya {
class TuyaLight : public Component, public light::LightOutput {
public:
void setup() override;
void dump_config() override;
void set_dimmer_id(uint8_t dimmer_id) { this->dimmer_id_ = dimmer_id; }
void set_switch_id(uint8_t switch_id) { this->switch_id_ = switch_id; }
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
void set_min_value(uint32_t min_value) { min_value_ = min_value; }
void set_max_value(uint32_t max_value) { max_value_ = max_value; }
light::LightTraits get_traits() override;
void setup_state(light::LightState *state) override;
void write_state(light::LightState *state) override;
protected:
void update_dimmer_(uint32_t value);
void update_switch_(uint32_t value);
Tuya *parent_;
optional<uint8_t> dimmer_id_{};
optional<uint8_t> switch_id_{};
uint32_t min_value_ = 0;
uint32_t max_value_ = 255;
light::LightState *state_{nullptr};
};
} // namespace tuya
} // namespace esphome