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.

57 lines
2.0 KiB

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