Support Tuya light color temperature control (#1412)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Ryan Mounce 2021-06-02 07:02:16 +09:30 committed by GitHub
parent f968713be8
commit c3938d04f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 0 deletions

View File

@ -8,6 +8,8 @@ from esphome.const import (
CONF_GAMMA_CORRECT,
CONF_DEFAULT_TRANSITION_LENGTH,
CONF_SWITCH_DATAPOINT,
CONF_COLD_WHITE_COLOR_TEMPERATURE,
CONF_WARM_WHITE_COLOR_TEMPERATURE,
)
from .. import tuya_ns, CONF_TUYA_ID, Tuya
@ -15,6 +17,8 @@ DEPENDENCIES = ["tuya"]
CONF_DIMMER_DATAPOINT = "dimmer_datapoint"
CONF_MIN_VALUE_DATAPOINT = "min_value_datapoint"
CONF_COLOR_TEMPERATURE_DATAPOINT = "color_temperature_datapoint"
CONF_COLOR_TEMPERATURE_MAX_VALUE = "color_temperature_max_value"
TuyaLight = tuya_ns.class_("TuyaLight", light.LightOutput, cg.Component)
@ -26,8 +30,18 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_DIMMER_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_MIN_VALUE_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t,
cv.Inclusive(
CONF_COLOR_TEMPERATURE_DATAPOINT, "color_temperature"
): cv.uint8_t,
cv.Optional(CONF_MIN_VALUE): cv.int_,
cv.Optional(CONF_MAX_VALUE): cv.int_,
cv.Optional(CONF_COLOR_TEMPERATURE_MAX_VALUE): cv.int_,
cv.Inclusive(
CONF_COLD_WHITE_COLOR_TEMPERATURE, "color_temperature"
): cv.color_temperature,
cv.Inclusive(
CONF_WARM_WHITE_COLOR_TEMPERATURE, "color_temperature"
): cv.color_temperature,
# Change the default gamma_correct and default transition length settings.
# The Tuya MCU handles transitions and gamma correction on its own.
cv.Optional(CONF_GAMMA_CORRECT, default=1.0): cv.positive_float,
@ -51,9 +65,23 @@ async def to_code(config):
cg.add(var.set_min_value_datapoint_id(config[CONF_MIN_VALUE_DATAPOINT]))
if CONF_SWITCH_DATAPOINT in config:
cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT]))
if CONF_COLOR_TEMPERATURE_DATAPOINT in config:
cg.add(var.set_color_temperature_id(config[CONF_COLOR_TEMPERATURE_DATAPOINT]))
cg.add(
var.set_cold_white_temperature(config[CONF_COLD_WHITE_COLOR_TEMPERATURE])
)
cg.add(
var.set_warm_white_temperature(config[CONF_WARM_WHITE_COLOR_TEMPERATURE])
)
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]))
if CONF_COLOR_TEMPERATURE_MAX_VALUE in config:
cg.add(
var.set_color_temperature_max_value(
config[CONF_COLOR_TEMPERATURE_MAX_VALUE]
)
)
paren = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren))

View File

@ -7,6 +7,15 @@ namespace tuya {
static const char *TAG = "tuya.light";
void TuyaLight::setup() {
if (this->color_temperature_id_.has_value()) {
this->parent_->register_listener(*this->color_temperature_id_, [this](TuyaDatapoint datapoint) {
auto call = this->state_->make_call();
call.set_color_temperature(this->cold_white_temperature_ +
(this->warm_white_temperature_ - this->cold_white_temperature_) *
(float(datapoint.value_uint) / float(this->color_temperature_max_value_)));
call.perform();
});
}
if (this->dimmer_id_.has_value()) {
this->parent_->register_listener(*this->dimmer_id_, [this](TuyaDatapoint datapoint) {
auto call = this->state_->make_call();
@ -41,6 +50,11 @@ void TuyaLight::dump_config() {
light::LightTraits TuyaLight::get_traits() {
auto traits = light::LightTraits();
traits.set_supports_brightness(this->dimmer_id_.has_value());
traits.set_supports_color_temperature(this->color_temperature_id_.has_value());
if (this->color_temperature_id_.has_value()) {
traits.set_min_mireds(this->cold_white_temperature_);
traits.set_max_mireds(this->warm_white_temperature_);
}
return traits;
}
@ -69,6 +83,17 @@ void TuyaLight::write_state(light::LightState *state) {
return;
}
if (this->color_temperature_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->color_temperature_id_;
datapoint.type = TuyaDatapointType::INTEGER;
datapoint.value_int =
static_cast<uint32_t>(this->color_temperature_max_value_ *
(state->current_values.get_color_temperature() - this->cold_white_temperature_) /
(this->warm_white_temperature_ - this->cold_white_temperature_));
parent_->set_datapoint_value(datapoint);
}
auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
brightness_int = std::max(brightness_int, this->min_value_);

View File

@ -16,9 +16,19 @@ class TuyaLight : public Component, public light::LightOutput {
this->min_value_datapoint_id_ = min_value_datapoint_id;
}
void set_switch_id(uint8_t switch_id) { this->switch_id_ = switch_id; }
void set_color_temperature_id(uint8_t color_temperature_id) { this->color_temperature_id_ = color_temperature_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; }
void set_color_temperature_max_value(uint32_t color_temperature_max_value) {
this->color_temperature_max_value_ = color_temperature_max_value;
}
void set_cold_white_temperature(float cold_white_temperature) {
this->cold_white_temperature_ = cold_white_temperature;
}
void set_warm_white_temperature(float warm_white_temperature) {
this->warm_white_temperature_ = warm_white_temperature;
}
light::LightTraits get_traits() override;
void setup_state(light::LightState *state) override;
void write_state(light::LightState *state) override;
@ -31,8 +41,12 @@ class TuyaLight : public Component, public light::LightOutput {
optional<uint8_t> dimmer_id_{};
optional<uint8_t> min_value_datapoint_id_{};
optional<uint8_t> switch_id_{};
optional<uint8_t> color_temperature_id_{};
uint32_t min_value_ = 0;
uint32_t max_value_ = 255;
uint32_t color_temperature_max_value_ = 255;
float cold_white_temperature_;
float warm_white_temperature_;
light::LightState *state_{nullptr};
};

View File

@ -133,6 +133,17 @@ light:
rgb_order: GRB
default_transition_length: 0s
color_correct: [50%, 50%, 50%]
- platform: tuya
id: tuya_light
switch_datapoint: 1
dimmer_datapoint: 2
min_value_datapoint: 3
color_temperature_datapoint: 4
min_value: 1
max_value: 100
cold_white_color_temperature: 153 mireds
warm_white_color_temperature: 500 mireds
gamma_correct: 1
display:
- platform: addressable_light