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.

52 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. #include "presets.h"
  7. namespace esphome {
  8. namespace xiaomi {
  9. namespace bslamp2 {
  10. class BrightnessTrigger : public Trigger<float> {
  11. public:
  12. explicit BrightnessTrigger(XiaomiBslamp2LightOutput *parent) {
  13. parent->add_on_state_callback([this](light::LightColorValues values) {
  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. protected:
  26. float last_brightness_ = -1.0f;
  27. };
  28. template<typename... Ts> class ActivatePresetAction : public Action<Ts...> {
  29. public:
  30. explicit ActivatePresetAction(PresetsContainer *presets) : presets_(presets) {}
  31. TEMPLATABLE_VALUE(std::string, operation);
  32. TEMPLATABLE_VALUE(std::string, group);
  33. TEMPLATABLE_VALUE(std::string, preset);
  34. void play(Ts... x) override {
  35. auto operation = this->operation_.value(x...);
  36. auto group = this->group_.optional_value(x...);
  37. auto preset = this->preset_.optional_value(x...);
  38. }
  39. protected:
  40. PresetsContainer *presets_;
  41. };
  42. } // namespace bslamp2
  43. } // namespace xiaomi
  44. } // namespace esphome