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.

51 lines
1.5 KiB

  1. #pragma once
  2. #include <cmath>
  3. #include "esphome/core/component.h"
  4. #include "esphome/core/automation.h"
  5. #include "light_output.h"
  6. namespace esphome {
  7. namespace yeelight {
  8. namespace bs2 {
  9. class BrightnessTrigger : public Trigger<float> {
  10. public:
  11. explicit BrightnessTrigger(YeelightBS2LightOutput *parent) {
  12. parent->add_on_state_callback(
  13. [this](light::LightColorValues values, std::string light_mode) {
  14. auto new_brightness = values.get_brightness();
  15. if (values.get_state() == 0) {
  16. new_brightness = 0.0f;
  17. }
  18. new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
  19. if (last_brightness_ != new_brightness) {
  20. trigger(new_brightness);
  21. last_brightness_ = new_brightness;
  22. }
  23. }
  24. );
  25. }
  26. protected:
  27. float last_brightness_ = -1.0f;
  28. };
  29. class LightModeTrigger : public Trigger<std::string> {
  30. public:
  31. explicit LightModeTrigger(YeelightBS2LightOutput *parent) {
  32. parent->add_on_state_callback(
  33. [this](light::LightColorValues values, std::string light_mode) {
  34. if (last_light_mode_ != light_mode) {
  35. trigger(light_mode);
  36. last_light_mode_ = light_mode;
  37. }
  38. }
  39. );
  40. }
  41. protected:
  42. std::string last_light_mode_ = LIGHT_MODE_UNKNOWN;
  43. };
  44. } // namespace yeelight_bs2
  45. } // namespace yeelight
  46. } // namespace bs2