You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

  1. #pragma once
  2. #include <array>
  3. #include <stdexcept>
  4. #include "common.h"
  5. #include "color_off.h"
  6. #include "color_night_light.h"
  7. #include "color_white_light.h"
  8. #include "color_rgb_light.h"
  9. namespace esphome {
  10. namespace yeelight {
  11. namespace bs2 {
  12. /// This class translates LightColorValues into GPIO duty cycles
  13. /// for representing a requested light color.
  14. ///
  15. /// The code handles all known light modes for the device:
  16. ///
  17. /// - off: the light is off
  18. /// - night light: activated when brightness is at its lowest
  19. /// - white light: based on color temperature + brightness
  20. /// - RGB light: based on RGB values + brightness
  21. class ColorTranslator : public GPIOOutputs {
  22. public:
  23. bool set_light_color_values(light::LightColorValues v) {
  24. values = v;
  25. GPIOOutputs *delegate;
  26. // Well, not much light here! Use the off "color".
  27. if (v.get_state() == 0.0f || v.get_brightness() == 0.0f) {
  28. delegate = off_light_;
  29. }
  30. // At the lowest brightness setting, switch to night light mode.
  31. // In the Yeelight integration in Home Assistant, this feature is
  32. // exposed trough a separate switch. I have found that the switch
  33. // is both confusing and made me run into issues when automating
  34. // the lights.
  35. // I don't simply check for a brightness at or below 0.01 (1%),
  36. // because the lowest brightness setting from Home Assistant
  37. // turns up as 0.011765 in here (which is 3/255).
  38. else if (v.get_brightness() < 0.012f) {
  39. delegate = night_light_;
  40. }
  41. // When white light is requested, then use the color temperature
  42. // white light mode: temperature + brightness.
  43. else if (v.get_white() > 0.0f) {
  44. delegate = white_light_;
  45. }
  46. // Otherwise, use RGB color mode: red, green, blue + brightness.
  47. else {
  48. delegate = rgb_light_;
  49. }
  50. delegate->set_light_color_values(v);
  51. delegate->copy_to(this);
  52. return true;
  53. }
  54. protected:
  55. GPIOOutputs *off_light_ = new ColorOff();
  56. GPIOOutputs *rgb_light_ = new ColorRGBLight();
  57. GPIOOutputs *white_light_ = new ColorWhiteLight();
  58. GPIOOutputs *night_light_ = new ColorNightLight();
  59. };
  60. } // namespace yeelight_bs2
  61. } // namespace yeelight
  62. } // namespace bs2