From 051a1e4772945cdc90051bc13d84bdfa7790db05 Mon Sep 17 00:00:00 2001 From: Connor Prussin Date: Thu, 5 Nov 2020 15:53:25 -0800 Subject: [PATCH] Add a datapoint to sync the Tuya MCU minimum brightness (#1347) The Tuya MCU keeps its own internal minimum brightness setting. This value may not match the minimum brightness setting for ESPHome, leading to some issues: 1. Dimming is limited--on some devices the default minimum is as high as 10%, meaning the dimmest ESPHome will go is still quite bright. 2. HA will allow a user to set a value below the MCU minimum, but the MCU will reject it and keep the previous setting, so the UI is confusing. This PR adds a setting to configure the datapoint for setting the MCU minimum brightness. If the setting is set, then ESPHome will synchronize the MCU's minimum brightness with the one configured for ESPHome on startup. --- esphome/components/tuya/light/__init__.py | 4 ++++ esphome/components/tuya/light/tuya_light.cpp | 7 +++++++ esphome/components/tuya/light/tuya_light.h | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/esphome/components/tuya/light/__init__.py b/esphome/components/tuya/light/__init__.py index d014f8a763..05605822cb 100644 --- a/esphome/components/tuya/light/__init__.py +++ b/esphome/components/tuya/light/__init__.py @@ -8,6 +8,7 @@ from .. import tuya_ns, CONF_TUYA_ID, Tuya DEPENDENCIES = ['tuya'] CONF_DIMMER_DATAPOINT = "dimmer_datapoint" +CONF_MIN_VALUE_DATAPOINT = "min_value_datapoint" TuyaLight = tuya_ns.class_('TuyaLight', light.LightOutput, cg.Component) @@ -15,6 +16,7 @@ CONFIG_SCHEMA = cv.All(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.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.Optional(CONF_MIN_VALUE): cv.int_, cv.Optional(CONF_MAX_VALUE): cv.int_, @@ -34,6 +36,8 @@ def to_code(config): if CONF_DIMMER_DATAPOINT in config: cg.add(var.set_dimmer_id(config[CONF_DIMMER_DATAPOINT])) + if CONF_MIN_VALUE_DATAPOINT in 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_MIN_VALUE in config: diff --git a/esphome/components/tuya/light/tuya_light.cpp b/esphome/components/tuya/light/tuya_light.cpp index 9696252049..e7b44882a1 100644 --- a/esphome/components/tuya/light/tuya_light.cpp +++ b/esphome/components/tuya/light/tuya_light.cpp @@ -21,6 +21,13 @@ void TuyaLight::setup() { call.perform(); }); } + if (min_value_datapoint_id_.has_value()) { + TuyaDatapoint datapoint{}; + datapoint.id = *this->min_value_datapoint_id_; + datapoint.type = TuyaDatapointType::INTEGER; + datapoint.value_int = this->min_value_; + parent_->set_datapoint_value(datapoint); + } } void TuyaLight::dump_config() { diff --git a/esphome/components/tuya/light/tuya_light.h b/esphome/components/tuya/light/tuya_light.h index 581512c29c..896c0cc7ef 100644 --- a/esphome/components/tuya/light/tuya_light.h +++ b/esphome/components/tuya/light/tuya_light.h @@ -12,6 +12,9 @@ class TuyaLight : public Component, public light::LightOutput { void setup() override; void dump_config() override; void set_dimmer_id(uint8_t dimmer_id) { this->dimmer_id_ = dimmer_id; } + void set_min_value_datapoint_id(uint8_t min_value_datapoint_id) { + this->min_value_datapoint_id_ = min_value_datapoint_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; } @@ -26,6 +29,7 @@ class TuyaLight : public Component, public light::LightOutput { Tuya *parent_; optional dimmer_id_{}; + optional min_value_datapoint_id_{}; optional switch_id_{}; uint32_t min_value_ = 0; uint32_t max_value_ = 255;