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.

33 lines
922 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 xiaomi {
  8. namespace bslamp2 {
  9. class BrightnessTrigger : public Trigger<float> {
  10. public:
  11. explicit BrightnessTrigger(XiaomiBslamp2LightOutput *parent) {
  12. parent->add_on_state_callback([this](light::LightColorValues values) {
  13. auto new_brightness = values.get_brightness();
  14. if (values.get_state() == 0) {
  15. new_brightness = 0.0f;
  16. }
  17. new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
  18. if (last_brightness_ != new_brightness) {
  19. trigger(new_brightness);
  20. last_brightness_ = new_brightness;
  21. }
  22. });
  23. }
  24. protected:
  25. float last_brightness_ = -1.0f;
  26. };
  27. } // namespace bslamp2
  28. } // namespace xiaomi
  29. } // namespace esphome