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.9 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. if (operation == "next_group") {
  37. presets_->activate_next_group();
  38. } else if (operation == "next_preset") {
  39. presets_->activate_next_preset();
  40. } else if (operation == "activate_group") {
  41. auto group = this->group_.value(x...);
  42. presets_->activate_group(group);
  43. } else if (operation == "activate_preset") {
  44. auto group = this->group_.value(x...);
  45. auto preset = this->preset_.value(x...);
  46. presets_->activate_preset(group, preset);
  47. }
  48. }
  49. protected:
  50. PresetsContainer *presets_;
  51. };
  52. } // namespace bslamp2
  53. } // namespace xiaomi
  54. } // namespace esphome