Created
August 15, 2020 14:39
-
-
Save FireBall1725/553de49fe4091e51b50eee747f008e0f to your computer and use it in GitHub Desktop.
ESPHome Custom RGB+CCT Light
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| esphome: | |
| name: testled | |
| platform: ESP8266 | |
| board: d1_mini | |
| esp8266_restore_from_flash: true | |
| includes: | |
| - rgbcct.h | |
| wifi: | |
| ssid: "ssid" | |
| password: "password" | |
| logger: | |
| api: | |
| ota: | |
| light: | |
| - platform: custom | |
| lambda: |- | |
| auto light_out = new CustomRGBWWLightOutput(id(ledr), id(ledg), id(ledb), id(ledcw), id(ledww)); | |
| App.register_component(light_out); | |
| return {light_out}; | |
| lights: | |
| - name: "RGBWW Light" | |
| output: | |
| - platform: esp8266_pwm | |
| id: ledr | |
| pin: D1 | |
| - platform: esp8266_pwm | |
| id: ledg | |
| pin: D2 | |
| - platform: esp8266_pwm | |
| id: ledb | |
| pin: D3 | |
| - platform: esp8266_pwm | |
| id: ledcw | |
| pin: D5 | |
| - platform: esp8266_pwm | |
| id: ledww | |
| pin: D6 | |
| binary_sensor: | |
| - platform: gpio | |
| pin: D7 | |
| name: "RGBWW Button" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "esphome.h" | |
| class CustomRGBWWLightOutput : public Component, public LightOutput { | |
| public: | |
| CustomRGBWWLightOutput(FloatOutput *red, FloatOutput *green, FloatOutput *blue, FloatOutput *cool_white, FloatOutput *warm_white) { | |
| CustomRGBWWLightOutput(red, green, blue, cool_white, warm_white, 370, 166); | |
| } | |
| CustomRGBWWLightOutput(FloatOutput *red, FloatOutput *green, FloatOutput *blue, FloatOutput *cool_white, FloatOutput *warm_white, int min_mireds, int max_mireds) { | |
| red_ = red; | |
| green_ = green; | |
| blue_ = blue; | |
| cool_white_ = cool_white; | |
| warm_white_ = warm_white; | |
| min_mireds_ = min_mireds; | |
| max_mireds_ = max_mireds; | |
| } | |
| LightTraits get_traits() override { | |
| auto traits = LightTraits(); | |
| traits.set_supports_brightness(true); | |
| traits.set_supports_rgb(true); | |
| traits.set_supports_color_temperature(true); | |
| traits.set_max_mireds(max_mireds_); | |
| traits.set_min_mireds(min_mireds_); | |
| return traits; | |
| } | |
| void write_state(LightState *state) override { | |
| float red, green, blue, cool_white, warm_white, brightness; | |
| state -> current_values_as_rgbww(&red, &green, &blue, &cool_white, &warm_white); | |
| state -> current_values_as_brightness(&brightness); | |
| if (red == blue && red == green) { | |
| // Set RGB to 0 | |
| red_ -> set_level(0); | |
| green_ -> set_level(0); | |
| blue_ -> set_level(0); | |
| // Set white to the values | |
| cool_white_ -> set_level(cool_white * brightness); | |
| warm_white_ -> set_level(warm_white * brightness); | |
| } else { | |
| // Set RGB to the values | |
| red_ -> set_level(red); | |
| green_ -> set_level(green); | |
| blue_ -> set_level(blue); | |
| // Set white to 0 | |
| cool_white_ -> set_level(0); | |
| warm_white_ -> set_level(0); | |
| } | |
| } | |
| protected: | |
| FloatOutput *red_; | |
| FloatOutput *green_; | |
| FloatOutput *blue_; | |
| FloatOutput *cool_white_; | |
| FloatOutput *warm_white_; | |
| int min_mireds_; | |
| int max_mireds_; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment