Support inverting color temperature on tuya lights (#2277)

This commit is contained in:
irtimaled 2021-09-13 00:33:20 -07:00 committed by GitHub
parent 97a18717e6
commit f0b6aabc96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -18,6 +18,7 @@ DEPENDENCIES = ["tuya"]
CONF_DIMMER_DATAPOINT = "dimmer_datapoint"
CONF_MIN_VALUE_DATAPOINT = "min_value_datapoint"
CONF_COLOR_TEMPERATURE_DATAPOINT = "color_temperature_datapoint"
CONF_COLOR_TEMPERATURE_INVERT = "color_temperature_invert"
CONF_COLOR_TEMPERATURE_MAX_VALUE = "color_temperature_max_value"
TuyaLight = tuya_ns.class_("TuyaLight", light.LightOutput, cg.Component)
@ -33,6 +34,7 @@ CONFIG_SCHEMA = cv.All(
cv.Inclusive(
CONF_COLOR_TEMPERATURE_DATAPOINT, "color_temperature"
): cv.uint8_t,
cv.Optional(CONF_COLOR_TEMPERATURE_INVERT, default=False): cv.boolean,
cv.Optional(CONF_MIN_VALUE): cv.int_,
cv.Optional(CONF_MAX_VALUE): cv.int_,
cv.Optional(CONF_COLOR_TEMPERATURE_MAX_VALUE): cv.int_,
@ -67,6 +69,8 @@ async def to_code(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_color_temperature_invert(config[CONF_COLOR_TEMPERATURE_INVERT]))
cg.add(
var.set_cold_white_temperature(config[CONF_COLD_WHITE_COLOR_TEMPERATURE])
)

View File

@ -9,10 +9,14 @@ static const char *const TAG = "tuya.light";
void TuyaLight::setup() {
if (this->color_temperature_id_.has_value()) {
this->parent_->register_listener(*this->color_temperature_id_, [this](const TuyaDatapoint &datapoint) {
auto datapoint_value = datapoint.value_uint;
if (this->color_temperature_invert_) {
datapoint_value = this->color_temperature_max_value_ - datapoint_value;
}
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_)));
(float(datapoint_value) / this->color_temperature_max_value_));
call.perform();
});
}
@ -78,6 +82,9 @@ void TuyaLight::write_state(light::LightState *state) {
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_));
if (this->color_temperature_invert_) {
color_temp_int = this->color_temperature_max_value_ - color_temp_int;
}
parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int);
}

View File

@ -17,6 +17,9 @@ class TuyaLight : public Component, public light::LightOutput {
}
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_color_temperature_invert(bool color_temperature_invert) {
this->color_temperature_invert_ = color_temperature_invert;
}
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; }
@ -47,6 +50,7 @@ class TuyaLight : public Component, public light::LightOutput {
uint32_t color_temperature_max_value_ = 255;
float cold_white_temperature_;
float warm_white_temperature_;
bool color_temperature_invert_{false};
light::LightState *state_{nullptr};
};