Browse Source

Started my own derived LightState class to get more control over the light's transitioning behavior.

pull/3/head
Maurice Makaay 3 years ago
parent
commit
d330894fdc
6 changed files with 71 additions and 38 deletions
  1. +5
    -5
      color_night_light.h
  2. +9
    -5
      color_rgb_light.h
  3. +9
    -5
      color_white_light.h
  4. +19
    -16
      light.py
  5. +9
    -7
      light_output.h
  6. +20
    -0
      light_state.h

night_light.h → color_night_light.h View File

@ -4,10 +4,10 @@
#include <stdexcept> #include <stdexcept>
namespace esphome { namespace esphome {
namespace rgbww {
namespace yeelight_bs2 {
namespace yeelight {
namespace bs2 {
class NightLight
class ColorNightLight
{ {
public: public:
// Based on measurements using the original device firmware. // Based on measurements using the original device firmware.
@ -22,5 +22,5 @@ public:
}; };
} // namespace yeelight_bs2 } // namespace yeelight_bs2
} // namespace rgbww
} // namespace esphome
} // namespace yeelight
} // namespace bs2

rgb_light.h → color_rgb_light.h View File

@ -1,3 +1,7 @@
/**
* This code implements the RGB light mode (based on RGB + brigtness)
* for the Yeelight Bedside Lamp 2.
*/
#pragma once #pragma once
#include <array> #include <array>
@ -5,8 +9,8 @@
#include <cmath> #include <cmath>
namespace esphome { namespace esphome {
namespace rgbww {
namespace yeelight_bs2 {
namespace yeelight {
namespace bs2 {
struct RGB { struct RGB {
float red; float red;
@ -239,7 +243,7 @@ static const RGBCircle rgb_circle_ {{
}} }}
}}; }};
class RGBLight
class ColorRGBLight
{ {
public: public:
float red = 0; float red = 0;
@ -365,6 +369,6 @@ protected:
}; };
} // namespace yeelight_bs2
} // namespace rgbww
} // namespace bs2
} // namespace yeelight
} // namespace esphome } // namespace esphome

white_light.h → color_white_light.h View File

@ -1,11 +1,15 @@
/**
* This code implements the white light mode (based on temperature +
* brigtness) for the Yeelight Bedside Lamp 2.
*/
#pragma once #pragma once
#include <array> #include <array>
#include <stdexcept> #include <stdexcept>
namespace esphome { namespace esphome {
namespace rgbww {
namespace yeelight_bs2 {
namespace yeelight {
namespace bs2 {
// Same range as supported by the original Yeelight firmware. // Same range as supported by the original Yeelight firmware.
static const int MIRED_MAX = 153; static const int MIRED_MAX = 153;
@ -57,7 +61,7 @@ static const RGBWLevelsTable rgbw_levels_100_ {{
{ 153.0f, 1.000f, 0.000f, 0.187f, 0.335f } { 153.0f, 1.000f, 0.000f, 0.187f, 0.335f }
}}; }};
class WhiteLight
class ColorWhiteLight
{ {
public: public:
float red = 0; float red = 0;
@ -114,6 +118,6 @@ protected:
} }
}; };
} // namespace yeelight_bs2
} // namespace rgbww
} // namespace bs2
} // namespace yeelight
} // namespace esphome } // namespace esphome

+ 19
- 16
light.py View File

@ -7,22 +7,25 @@ from esphome.const import CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, CONF_OUTP
CONF_MASTER1 = "master1" CONF_MASTER1 = "master1"
CONF_MASTER2 = "master2" CONF_MASTER2 = "master2"
rgbww_ns = cg.esphome_ns.namespace("rgbww")
YeelightBS2LightOutput = rgbww_ns.class_("YeelightBS2LightOutput", light.LightOutput)
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
{
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(YeelightBS2LightOutput),
cv.Required(CONF_RED): cv.use_id(ledc),
cv.Required(CONF_GREEN): cv.use_id(ledc),
cv.Required(CONF_BLUE): cv.use_id(ledc),
cv.Required(CONF_WHITE): cv.use_id(ledc),
cv.Required(CONF_WHITE): cv.use_id(ledc),
cv.Required(CONF_MASTER1): cv.use_id(gpio_output.GPIOBinaryOutput),
cv.Required(CONF_MASTER2): cv.use_id(gpio_output.GPIOBinaryOutput),
}
)
CODEOWNERS = ["@mmakaay"]
yeelight_ns = cg.esphome_ns.namespace("yeelight")
bs2_ns = yeelight_ns.namespace("bs2")
light_state = bs2_ns.class_("YeelightBS2LightState", cg.Nameable, cg.Component)
light_output = bs2_ns.class_("YeelightBS2LightOutput", light.LightOutput)
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(light_state),
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(light_output),
cv.Required(CONF_RED): cv.use_id(ledc),
cv.Required(CONF_GREEN): cv.use_id(ledc),
cv.Required(CONF_BLUE): cv.use_id(ledc),
cv.Required(CONF_WHITE): cv.use_id(ledc),
cv.Required(CONF_WHITE): cv.use_id(ledc),
cv.Required(CONF_MASTER1): cv.use_id(gpio_output.GPIOBinaryOutput),
cv.Required(CONF_MASTER2): cv.use_id(gpio_output.GPIOBinaryOutput),
})
import json;
def to_code(config): def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID]) var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
yield light.register_light(var, config) yield light.register_light(var, config)


yeelight_bs2_light_output.h → light_output.h View File

@ -23,9 +23,10 @@
#define TRANSITION_TO_OFF_BUGFIX #define TRANSITION_TO_OFF_BUGFIX
namespace esphome { namespace esphome {
namespace rgbww {
namespace yeelight {
namespace bs2 {
static const char *TAG = "yeelight_bs2.light";
static const char *TAG = "yeelight_bs2";
// Same range as supported by the original Yeelight firmware. // Same range as supported by the original Yeelight firmware.
static const int HOME_ASSISTANT_MIRED_MIN = 153; static const int HOME_ASSISTANT_MIRED_MIN = 153;
@ -113,7 +114,7 @@ namespace rgbww {
// I don't simply check for a brightness at or below 0.01 (1%), // I don't simply check for a brightness at or below 0.01 (1%),
// because the lowest brightness setting from Home Assistant // because the lowest brightness setting from Home Assistant
// turns up as 0.011765 in here (which is 3/255). // turns up as 0.011765 in here (which is 3/255).
if (brightness < 0.012f) {
if (brightness < 0.012f && values.get_state() == 1) {
turn_on_in_night_light_mode_(); turn_on_in_night_light_mode_();
return; return;
} }
@ -144,9 +145,9 @@ namespace rgbww {
ledc::LEDCOutput *white_; ledc::LEDCOutput *white_;
esphome::gpio::GPIOBinaryOutput *master1_; esphome::gpio::GPIOBinaryOutput *master1_;
esphome::gpio::GPIOBinaryOutput *master2_; esphome::gpio::GPIOBinaryOutput *master2_;
esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
esphome::rgbww::yeelight_bs2::RGBLight rgb_light_;
esphome::rgbww::yeelight_bs2::NightLight night_light_;
ColorWhiteLight white_light_;
ColorRGBLight rgb_light_;
ColorNightLight night_light_;
#ifdef TRANSITION_TO_OFF_BUGFIX #ifdef TRANSITION_TO_OFF_BUGFIX
float previous_state_ = 1; float previous_state_ = 1;
float previous_brightness_ = -1; float previous_brightness_ = -1;
@ -216,5 +217,6 @@ namespace rgbww {
} }
}; };
} // namespace rgbww
} // namespace bs2
} // namespace yeelight
} // namespace esphome } // namespace esphome

+ 20
- 0
light_state.h View File

@ -0,0 +1,20 @@
#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