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.

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