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.

30 lines
864 B

  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([this](light::LightColorValues values) {
  13. auto new_brightness = values.get_state() == 0 ? 0.0f : values.get_brightness();
  14. new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
  15. if (last_brightness_ != new_brightness) {
  16. this->trigger(new_brightness);
  17. last_brightness_ = new_brightness;
  18. }
  19. });
  20. }
  21. protected:
  22. float last_brightness_ = -1.0f;
  23. };
  24. } // namespace yeelight_bs2
  25. } // namespace yeelight
  26. } // namespace bs2