From 92d935b0b47ba4a4457235acf28a37d3e03ee8be Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Mon, 5 Apr 2021 22:57:11 +0200 Subject: [PATCH] Experimental class structure to give my LightOutput class access to some protected data in the LightState class. --- color_rgb_light.h | 2 +- color_white_light.h | 2 +- light_output.h | 65 ++++++++++++++++++++++++++++++++++++++------- light_state.h | 20 -------------- 4 files changed, 57 insertions(+), 32 deletions(-) delete mode 100644 light_state.h diff --git a/color_rgb_light.h b/color_rgb_light.h index 4e7bbfe..4214a8f 100644 --- a/color_rgb_light.h +++ b/color_rgb_light.h @@ -1,5 +1,5 @@ /** - * This code implements the RGB light mode (based on RGB + brigtness) + * This code implements the RGB light mode (based on RGB + brightness) * for the Yeelight Bedside Lamp 2. */ #pragma once diff --git a/color_white_light.h b/color_white_light.h index 30e7e82..28226ef 100644 --- a/color_white_light.h +++ b/color_white_light.h @@ -1,6 +1,6 @@ /** * This code implements the white light mode (based on temperature + - * brigtness) for the Yeelight Bedside Lamp 2. + * brightness) for the Yeelight Bedside Lamp 2. */ #pragma once diff --git a/light_output.h b/light_output.h index f009201..a94e749 100644 --- a/light_output.h +++ b/light_output.h @@ -1,10 +1,6 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/components/ledc/ledc_output.h" -#include "esphome/components/light/light_output.h" -#include "esphome/components/gpio/output/gpio_binary_output.h" +#pragma once +#include "light_output.h" // What seems to be a bug in ESPHome transitioning: when turning on // the device, the brightness is scaled along with the state (which @@ -31,9 +27,16 @@ namespace bs2 { // Same range as supported by the original Yeelight firmware. static const int HOME_ASSISTANT_MIRED_MIN = 153; static const int HOME_ASSISTANT_MIRED_MAX = 588; - - class YeelightBS2LightOutput : public Component, public light::LightOutput - { + + class LightStateDataExposer { + public: + virtual bool has_active_transformer() = 0; + virtual light::LightColorValues get_transformer_values() = 0; + virtual light::LightColorValues get_transformer_end_values() = 0; + virtual float get_transformer_progress() = 0; + }; + + class YeelightBS2LightOutput : public Component, public light::LightOutput { public: light::LightTraits get_traits() override { @@ -72,8 +75,25 @@ namespace bs2 { master2_ = master2; } - void write_state(light::LightState *state) override + void set_light_state_data_exposer(LightStateDataExposer *exposer) { + state_exposer_ = exposer; + } + + void write_state(light::LightState *state) { + // Experimental access to protected LightState data. + if (state_exposer_->has_active_transformer()) { + auto progress = state_exposer_->get_transformer_progress(); + auto s = state_exposer_->get_transformer_values(); + auto t = state_exposer_->get_transformer_end_values(); + //ESP_LOGD(TAG, "TRFRM %f vals [%f,%f,%f,%f,%f] new [%f,%f,%f,%f,%f]", + // progress, + // s.get_red(), s.get_green(), s.get_blue(), + // s.get_brightness(), s.get_color_temperature(), + // t.get_red(), t.get_green(), t.get_blue(), + // t.get_brightness(), t.get_color_temperature()); + } + auto values = state->current_values; // Power down the light when its state is 'off'. @@ -145,6 +165,7 @@ namespace bs2 { ledc::LEDCOutput *white_; esphome::gpio::GPIOBinaryOutput *master1_; esphome::gpio::GPIOBinaryOutput *master2_; + LightStateDataExposer *state_exposer_; ColorWhiteLight white_light_; ColorRGBLight rgb_light_; ColorNightLight night_light_; @@ -217,6 +238,30 @@ namespace bs2 { } }; + class YeelightBS2LightState : public light::LightState, public LightStateDataExposer + { + public: + YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) { + output->set_light_state_data_exposer(this); + } + + bool has_active_transformer() { + return this->transformer_ != nullptr; + } + + light::LightColorValues get_transformer_values() { + return this->transformer_->get_values(); + } + + light::LightColorValues get_transformer_end_values() { + return this->transformer_->get_end_values(); + } + + float get_transformer_progress() { + return this->transformer_->get_progress(); + } + }; + } // namespace bs2 } // namespace yeelight } // namespace esphome diff --git a/light_state.h b/light_state.h deleted file mode 100644 index ed9fd69..0000000 --- a/light_state.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/components/light/light_state.h" -#include "esphome/components/light/light_output.h" - - -namespace esphome { -namespace yeelight { -namespace bs2 { - - class YeelightBS2LightState : public light::LightState - { - public: - YeelightBS2LightState(const std::string &name, light::LightOutput *output) : light::LightState(name, output) {} - }; - -} // namespace bs2 -} // namespace yeelight -} // namespace esphome