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.

54 lines
1.9 KiB

  1. #pragma once
  2. #include "common.h"
  3. namespace esphome {
  4. namespace yeelight {
  5. namespace bs2 {
  6. class ColorNightLight : public GPIOOutputs {
  7. public:
  8. bool set_light_color_values(light::LightColorValues v) {
  9. // This class can handle the GPIO outputs for the night light mode.
  10. //
  11. // At the lowest brightness setting, switch to night light mode.
  12. // In the Yeelight integration in Home Assistant, this feature is
  13. // exposed trough a separate switch. I have found that the switch
  14. // is both confusing and made me run into issues when automating
  15. // the lights.
  16. // I don't simply check for a brightness at or below 0.01 (1%),
  17. // because the lowest brightness setting from Home Assistant
  18. // turns up as 0.011765 in here (which is 3/255).
  19. if (v.get_brightness() >= 0.012f) {
  20. return false;
  21. }
  22. values = v;
  23. // This night light mode is activated when white light is selected.
  24. // Based on measurements using the original device firmware, so it
  25. // matches the night light of the original firmware.
  26. if (v.get_white() > 0) {
  27. red = 0.968f;
  28. green = 0.968f;
  29. blue = 0.972f;
  30. white = 0.0f;
  31. }
  32. // In RGB mode, the selected color is used to give the night light
  33. // a specific color, instead of the default. This is a nice extra
  34. // for this firmware, as the original firmware does not support it.
  35. else {
  36. red = 0.9997f - v.get_red() * (0.9997f - 0.9680f);
  37. green = 0.9997f - v.get_green() * (0.9997f - 0.9680f);
  38. auto blue_on = 0.9720f + (0.9680f - 0.9720f) * (1.0f - (v.get_red() + v.get_green())/2.0f);
  39. blue = 0.9997f - v.get_blue() * (0.9997f - blue_on);
  40. white = 0.0f;
  41. }
  42. return true;
  43. }
  44. };
  45. } // namespace yeelight_bs2
  46. } // namespace yeelight
  47. } // namespace bs2