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.

63 lines
1.7 KiB

  1. #pragma once
  2. #include "esphome/core/automation.h"
  3. #include "esphome/core/component.h"
  4. #include "light_output.h"
  5. #include "presets.h"
  6. #include <cmath>
  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. 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. template<typename... Ts> class ActivatePresetAction : public Action<Ts...> {
  28. public:
  29. explicit ActivatePresetAction(PresetsContainer *presets) : presets_(presets) {}
  30. TEMPLATABLE_VALUE(std::string, operation);
  31. TEMPLATABLE_VALUE(std::string, group);
  32. TEMPLATABLE_VALUE(std::string, preset);
  33. void play(Ts... x) override {
  34. auto operation = this->operation_.value(x...);
  35. if (operation == "next_group") {
  36. presets_->activate_next_group();
  37. } else if (operation == "next_preset") {
  38. presets_->activate_next_preset();
  39. } else if (operation == "activate_group") {
  40. auto group = this->group_.value(x...);
  41. presets_->activate_group(group);
  42. } else if (operation == "activate_preset") {
  43. auto group = this->group_.value(x...);
  44. auto preset = this->preset_.value(x...);
  45. presets_->activate_preset(group, preset);
  46. }
  47. }
  48. protected:
  49. PresetsContainer *presets_;
  50. };
  51. } // namespace bslamp2
  52. } // namespace xiaomi
  53. } // namespace esphome