From d330894fdc4b72d15762917a9c0e12efd46a39ba Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Mon, 5 Apr 2021 15:44:57 +0200 Subject: [PATCH] Started my own derived LightState class to get more control over the light's transitioning behavior. --- night_light.h => color_night_light.h | 10 +++--- rgb_light.h => color_rgb_light.h | 14 +++++--- white_light.h => color_white_light.h | 14 +++++--- light.py | 35 ++++++++++--------- yeelight_bs2_light_output.h => light_output.h | 16 +++++---- light_state.h | 20 +++++++++++ 6 files changed, 71 insertions(+), 38 deletions(-) rename night_light.h => color_night_light.h (78%) rename rgb_light.h => color_rgb_light.h (99%) rename white_light.h => color_white_light.h (94%) rename yeelight_bs2_light_output.h => light_output.h (96%) create mode 100644 light_state.h diff --git a/night_light.h b/color_night_light.h similarity index 78% rename from night_light.h rename to color_night_light.h index 7b12b18..ef73e4a 100644 --- a/night_light.h +++ b/color_night_light.h @@ -4,10 +4,10 @@ #include namespace esphome { -namespace rgbww { -namespace yeelight_bs2 { +namespace yeelight { +namespace bs2 { -class NightLight +class ColorNightLight { public: // Based on measurements using the original device firmware. @@ -22,5 +22,5 @@ public: }; } // namespace yeelight_bs2 -} // namespace rgbww -} // namespace esphome +} // namespace yeelight +} // namespace bs2 diff --git a/rgb_light.h b/color_rgb_light.h similarity index 99% rename from rgb_light.h rename to color_rgb_light.h index f278a72..4e7bbfe 100644 --- a/rgb_light.h +++ b/color_rgb_light.h @@ -1,3 +1,7 @@ +/** + * This code implements the RGB light mode (based on RGB + brigtness) + * for the Yeelight Bedside Lamp 2. + */ #pragma once #include @@ -5,8 +9,8 @@ #include namespace esphome { -namespace rgbww { -namespace yeelight_bs2 { +namespace yeelight { +namespace bs2 { struct RGB { float red; @@ -239,7 +243,7 @@ static const RGBCircle rgb_circle_ {{ }} }}; -class RGBLight +class ColorRGBLight { public: float red = 0; @@ -365,6 +369,6 @@ protected: }; -} // namespace yeelight_bs2 -} // namespace rgbww +} // namespace bs2 +} // namespace yeelight } // namespace esphome diff --git a/white_light.h b/color_white_light.h similarity index 94% rename from white_light.h rename to color_white_light.h index 8c7dce3..30e7e82 100644 --- a/white_light.h +++ b/color_white_light.h @@ -1,11 +1,15 @@ +/** + * This code implements the white light mode (based on temperature + + * brigtness) for the Yeelight Bedside Lamp 2. + */ #pragma once #include #include namespace esphome { -namespace rgbww { -namespace yeelight_bs2 { +namespace yeelight { +namespace bs2 { // Same range as supported by the original Yeelight firmware. 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 } }}; -class WhiteLight +class ColorWhiteLight { public: float red = 0; @@ -114,6 +118,6 @@ protected: } }; -} // namespace yeelight_bs2 -} // namespace rgbww +} // namespace bs2 +} // namespace yeelight } // namespace esphome diff --git a/light.py b/light.py index ceb734c..3c91d84 100644 --- a/light.py +++ b/light.py @@ -7,22 +7,25 @@ from esphome.const import CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, CONF_OUTP CONF_MASTER1 = "master1" 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): var = cg.new_Pvariable(config[CONF_OUTPUT_ID]) yield light.register_light(var, config) diff --git a/yeelight_bs2_light_output.h b/light_output.h similarity index 96% rename from yeelight_bs2_light_output.h rename to light_output.h index c6ed334..f009201 100644 --- a/yeelight_bs2_light_output.h +++ b/light_output.h @@ -23,9 +23,10 @@ #define TRANSITION_TO_OFF_BUGFIX 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. 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%), // because the lowest brightness setting from Home Assistant // 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_(); return; } @@ -144,9 +145,9 @@ namespace rgbww { ledc::LEDCOutput *white_; esphome::gpio::GPIOBinaryOutput *master1_; 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 float previous_state_ = 1; float previous_brightness_ = -1; @@ -216,5 +217,6 @@ namespace rgbww { } }; -} // namespace rgbww +} // namespace bs2 +} // namespace yeelight } // namespace esphome diff --git a/light_state.h b/light_state.h new file mode 100644 index 0000000..ed9fd69 --- /dev/null +++ b/light_state.h @@ -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