Browse Source

Experimental class structure to give my LightOutput class access to some protected data in the LightState class.

pull/3/head
Maurice Makaay 3 years ago
parent
commit
92d935b0b4
4 changed files with 57 additions and 32 deletions
  1. +1
    -1
      color_rgb_light.h
  2. +1
    -1
      color_white_light.h
  3. +55
    -10
      light_output.h
  4. +0
    -20
      light_state.h

+ 1
- 1
color_rgb_light.h View File

@ -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


+ 1
- 1
color_white_light.h View File

@ -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


+ 55
- 10
light_output.h View File

@ -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

+ 0
- 20
light_state.h View File

@ -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

Loading…
Cancel
Save