From abe8137b42d1ad809b938918c11bf4a67a1ecc2e Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Wed, 7 Apr 2021 02:09:45 +0200 Subject: [PATCH] Transitioning to off works too now. Transitioning to on needs some more work (it is now instant, but that won't be hard to implement correctly). --- color_off.h | 6 ++-- color_translator.h | 2 +- light.h | 75 +++++++++------------------------------------- 3 files changed, 18 insertions(+), 65 deletions(-) diff --git a/color_off.h b/color_off.h index ef0a955..ef2b4ab 100644 --- a/color_off.h +++ b/color_off.h @@ -13,9 +13,9 @@ class ColorOff : public GPIOOutputs { public: bool set_light_color_values(light::LightColorValues v) { values = v; - red = 0.0f; - green = 0.0f; - blue = 0.0f; + red = 1.0f; + green = 1.0f; + blue = 1.0f; white = 0.0f; return true; diff --git a/color_translator.h b/color_translator.h index 41d8916..0e29db8 100644 --- a/color_translator.h +++ b/color_translator.h @@ -27,7 +27,7 @@ public: bool set_light_color_values(light::LightColorValues v) { values = v; - GPIOOutputs *delegate = nullptr; + GPIOOutputs *delegate; // Well, not much light here! Use the off "color". if (v.get_state() == 0.0f || v.get_brightness() == 0.0f) { diff --git a/light.h b/light.h index aef6643..e2fb9f5 100644 --- a/light.h +++ b/light.h @@ -1,21 +1,5 @@ #pragma once -// What seems to be a bug in ESPHome transitioning: when turning on -// the device, the brightness is scaled along with the state (which -// runs from 0 to 1), but when turning off the device, the brightness -// is kept the same while the state goes down from 1 to 0. As a result -// when turning off the lamp with a transition time of 1s, the light -// stays on for 1s and then turn itself off abruptly. -// -// Reported the issue + fix at: -// https://github.com/esphome/esphome/pull/1643 -// -// A work-around for this issue can be enabled using the following -// define. Note that the code provides a forward-compatible fix, so -// having this define active with a fixed ESPHome version should -// not be a problem. -#define TRANSITION_TO_OFF_BUGFIX - namespace esphome { namespace yeelight { namespace bs2 { @@ -31,7 +15,6 @@ namespace bs2 { public: virtual bool has_active_transformer() = 0; virtual bool transformer_is_transition() = 0; - virtual light::LightColorValues get_transformer_values() = 0; virtual light::LightColorValues get_transformer_end_values() = 0; virtual float get_transformer_progress() = 0; }; @@ -182,48 +165,26 @@ namespace bs2 { white_->set_level(0.0f); master2_->turn_off(); master1_->turn_off(); -#ifdef TRANSITION_TO_OFF_BUGFIX - previous_state_ = -1; - previous_brightness_ = 0; -#endif return; } + GPIOOutputs *delegate; if (transition_handler_->set_light_color_values(values)) { - master2_->turn_on(); - master1_->turn_on(); - red_->set_level(transition_handler_->red); - green_->set_level(transition_handler_->green); - blue_->set_level(transition_handler_->blue); - white_->set_level(transition_handler_->white); - return; + transition_handler_->log("TRANSITION"); + delegate = transition_handler_; + } else { + instant_handler_->set_light_color_values(values); + instant_handler_->log("INSTANT"); + delegate = instant_handler_; } -#ifdef TRANSITION_TO_OFF_BUGFIX - // Remember the brightness that is used when the light is fully ON. - auto brightness = values.get_brightness(); - if (values.get_state() == 1) { - previous_brightness_ = brightness; - } - // When transitioning towards zero brightness ... - else if (values.get_state() < previous_state_) { - // ... check if the prevous brightness is the same as the current - // brightness. If yes, then the brightness isn't being scaled ... - if (previous_brightness_ == brightness) { - // ... and we need to do that ourselves. - brightness = values.get_state() * brightness; - } - } - previous_state_ = values.get_state(); -#endif - - instant_handler_->set_light_color_values(values); + delegate->set_light_color_values(values); master2_->turn_on(); master1_->turn_on(); - red_->set_level(instant_handler_->red); - green_->set_level(instant_handler_->green); - blue_->set_level(instant_handler_->blue); - white_->set_level(instant_handler_->white); + red_->set_level(delegate->red); + green_->set_level(delegate->green); + blue_->set_level(delegate->blue); + white_->set_level(delegate->white); } protected: @@ -233,12 +194,8 @@ namespace bs2 { ledc::LEDCOutput *white_; esphome::gpio::GPIOBinaryOutput *master1_; esphome::gpio::GPIOBinaryOutput *master2_; - TransitionHandler *transition_handler_; - ColorTranslator *instant_handler_ = new ColorTranslator(); -#ifdef TRANSITION_TO_OFF_BUGFIX - float previous_state_ = 1; - float previous_brightness_ = -1; -#endif + GPIOOutputs *transition_handler_; + GPIOOutputs *instant_handler_ = new ColorTranslator(); friend class YeelightBS2LightState; @@ -264,10 +221,6 @@ namespace bs2 { return this->transformer_->is_transition(); } - light::LightColorValues get_transformer_values() { - return this->transformer_->get_values(); - } - light::LightColorValues get_transformer_end_values() { return this->transformer_->get_end_values(); }