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.

109 lines
3.3 KiB

  1. #pragma once
  2. #include "esphome/core/automation.h"
  3. #include "esphome/core/component.h"
  4. #include "interfaces.h"
  5. #include "light_output.h"
  6. #include "light_state.h"
  7. #include "presets.h"
  8. #include <cmath>
  9. namespace esphome {
  10. namespace xiaomi {
  11. namespace bslamp2 {
  12. class BrightnessTrigger : public Trigger<float> {
  13. public:
  14. explicit BrightnessTrigger(XiaomiBslamp2LightOutput *parent) {
  15. parent->add_on_state_callback([this](light::LightColorValues values) {
  16. auto new_brightness = values.get_brightness();
  17. if (values.get_state() == 0)
  18. new_brightness = 0.0f;
  19. new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
  20. if (last_brightness_ != new_brightness) {
  21. trigger(new_brightness);
  22. last_brightness_ = new_brightness;
  23. }
  24. });
  25. }
  26. protected:
  27. float last_brightness_ = -1.0f;
  28. };
  29. template<typename... Ts> class DiscoAction : public Action<Ts...> {
  30. public:
  31. explicit DiscoAction(LightStateDiscoSupport *parent) : parent_(parent) {}
  32. TEMPLATABLE_VALUE(bool, disco_state)
  33. TEMPLATABLE_VALUE(bool, state)
  34. TEMPLATABLE_VALUE(uint32_t, transition_length)
  35. TEMPLATABLE_VALUE(uint32_t, flash_length)
  36. TEMPLATABLE_VALUE(float, brightness)
  37. TEMPLATABLE_VALUE(float, red)
  38. TEMPLATABLE_VALUE(float, green)
  39. TEMPLATABLE_VALUE(float, blue)
  40. TEMPLATABLE_VALUE(float, color_temperature)
  41. TEMPLATABLE_VALUE(std::string, effect)
  42. void play(Ts... x) override {
  43. if (this->disco_state_.has_value()) {
  44. auto p = this->disco_state_.optional_value(x...);
  45. if (!*p) {
  46. parent_->disco_stop();
  47. return;
  48. }
  49. }
  50. auto call = this->parent_->make_disco_call(false);
  51. call.set_state(this->state_.optional_value(x...));
  52. call.set_brightness(this->brightness_.optional_value(x...));
  53. call.set_red(this->red_.optional_value(x...));
  54. call.set_green(this->green_.optional_value(x...));
  55. call.set_blue(this->blue_.optional_value(x...));
  56. call.set_color_temperature(this->color_temperature_.optional_value(x...));
  57. call.set_effect(this->effect_.optional_value(x...));
  58. call.set_flash_length(this->flash_length_.optional_value(x...));
  59. call.set_transition_length(this->transition_length_.optional_value(x...));
  60. // Force the light to update right now, not in the next loop.
  61. call.perform();
  62. parent_->disco_apply();
  63. }
  64. protected:
  65. LightStateDiscoSupport *parent_;
  66. };
  67. template<typename... Ts> class ActivatePresetAction : public Action<Ts...> {
  68. public:
  69. explicit ActivatePresetAction(PresetsContainer *presets) : presets_(presets) {}
  70. TEMPLATABLE_VALUE(std::string, operation);
  71. TEMPLATABLE_VALUE(std::string, group);
  72. TEMPLATABLE_VALUE(std::string, preset);
  73. void play(Ts... x) override {
  74. auto operation = this->operation_.value(x...);
  75. if (operation == "next_group") {
  76. presets_->activate_next_group();
  77. } else if (operation == "next_preset") {
  78. presets_->activate_next_preset();
  79. } else if (operation == "activate_group") {
  80. auto group = this->group_.value(x...);
  81. presets_->activate_group(group);
  82. } else if (operation == "activate_preset") {
  83. auto group = this->group_.value(x...);
  84. auto preset = this->preset_.value(x...);
  85. presets_->activate_preset(group, preset);
  86. }
  87. }
  88. protected:
  89. PresetsContainer *presets_;
  90. };
  91. } // namespace bslamp2
  92. } // namespace xiaomi
  93. } // namespace esphome