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.

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