base_automation.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. #include "esphome/core/automation.h"
  3. #include "esphome/core/component.h"
  4. namespace esphome {
  5. template<typename... Ts> class AndCondition : public Condition<Ts...> {
  6. public:
  7. explicit AndCondition(const std::vector<Condition<Ts...> *> &conditions) : conditions_(conditions) {}
  8. bool check(Ts... x) override {
  9. for (auto *condition : this->conditions_) {
  10. if (!condition->check(x...))
  11. return false;
  12. }
  13. return true;
  14. }
  15. protected:
  16. std::vector<Condition<Ts...> *> conditions_;
  17. };
  18. template<typename... Ts> class OrCondition : public Condition<Ts...> {
  19. public:
  20. explicit OrCondition(const std::vector<Condition<Ts...> *> &conditions) : conditions_(conditions) {}
  21. bool check(Ts... x) override {
  22. for (auto *condition : this->conditions_) {
  23. if (condition->check(x...))
  24. return true;
  25. }
  26. return false;
  27. }
  28. protected:
  29. std::vector<Condition<Ts...> *> conditions_;
  30. };
  31. template<typename... Ts> class NotCondition : public Condition<Ts...> {
  32. public:
  33. explicit NotCondition(Condition<Ts...> *condition) : condition_(condition) {}
  34. bool check(Ts... x) override { return !this->condition_->check(x...); }
  35. protected:
  36. Condition<Ts...> *condition_;
  37. };
  38. template<typename... Ts> class LambdaCondition : public Condition<Ts...> {
  39. public:
  40. explicit LambdaCondition(std::function<bool(Ts...)> &&f) : f_(std::move(f)) {}
  41. bool check(Ts... x) override { return this->f_(x...); }
  42. protected:
  43. std::function<bool(Ts...)> f_;
  44. };
  45. template<typename... Ts> class ForCondition : public Condition<Ts...>, public Component {
  46. public:
  47. explicit ForCondition(Condition<> *condition) : condition_(condition) {}
  48. TEMPLATABLE_VALUE(uint32_t, time);
  49. void loop() override { this->check_internal(); }
  50. float get_setup_priority() const override { return setup_priority::DATA; }
  51. bool check_internal() {
  52. bool cond = this->condition_->check();
  53. if (!cond)
  54. this->last_inactive_ = millis();
  55. return cond;
  56. }
  57. bool check(Ts... x) override {
  58. if (!this->check_internal())
  59. return false;
  60. return millis() - this->last_inactive_ >= this->time_.value(x...);
  61. }
  62. protected:
  63. Condition<> *condition_;
  64. uint32_t last_inactive_{0};
  65. };
  66. class StartupTrigger : public Trigger<>, public Component {
  67. public:
  68. explicit StartupTrigger(float setup_priority) : setup_priority_(setup_priority) {}
  69. void setup() override { this->trigger(); }
  70. float get_setup_priority() const override { return this->setup_priority_; }
  71. protected:
  72. float setup_priority_;
  73. };
  74. class ShutdownTrigger : public Trigger<>, public Component {
  75. public:
  76. void on_shutdown() override { this->trigger(); }
  77. };
  78. class LoopTrigger : public Trigger<>, public Component {
  79. public:
  80. void loop() override { this->trigger(); }
  81. float get_setup_priority() const override { return setup_priority::DATA; }
  82. };
  83. template<typename... Ts> class DelayAction : public Action<Ts...>, public Component {
  84. public:
  85. explicit DelayAction() = default;
  86. TEMPLATABLE_VALUE(uint32_t, delay)
  87. void play_complex(Ts... x) override {
  88. auto f = std::bind(&DelayAction<Ts...>::play_next_, this, x...);
  89. this->num_running_++;
  90. this->set_timeout(this->delay_.value(x...), f);
  91. }
  92. float get_setup_priority() const override { return setup_priority::HARDWARE; }
  93. void play(Ts... x) override { /* ignore - see play_complex */
  94. }
  95. void stop() override { this->cancel_timeout(""); }
  96. };
  97. template<typename... Ts> class LambdaAction : public Action<Ts...> {
  98. public:
  99. explicit LambdaAction(std::function<void(Ts...)> &&f) : f_(std::move(f)) {}
  100. void play(Ts... x) override { this->f_(x...); }
  101. protected:
  102. std::function<void(Ts...)> f_;
  103. };
  104. template<typename... Ts> class IfAction : public Action<Ts...> {
  105. public:
  106. explicit IfAction(Condition<Ts...> *condition) : condition_(condition) {}
  107. void add_then(const std::vector<Action<Ts...> *> &actions) {
  108. this->then_.add_actions(actions);
  109. this->then_.add_action(new LambdaAction<Ts...>([this](Ts... x) { this->play_next_(x...); }));
  110. }
  111. void add_else(const std::vector<Action<Ts...> *> &actions) {
  112. this->else_.add_actions(actions);
  113. this->else_.add_action(new LambdaAction<Ts...>([this](Ts... x) { this->play_next_(x...); }));
  114. }
  115. void play_complex(Ts... x) override {
  116. this->num_running_++;
  117. bool res = this->condition_->check(x...);
  118. if (res) {
  119. if (this->then_.empty()) {
  120. this->play_next_(x...);
  121. } else if (this->num_running_ > 0) {
  122. this->then_.play(x...);
  123. }
  124. } else {
  125. if (this->else_.empty()) {
  126. this->play_next_(x...);
  127. } else if (this->num_running_ > 0) {
  128. this->else_.play(x...);
  129. }
  130. }
  131. }
  132. void play(Ts... x) override { /* ignore - see play_complex */
  133. }
  134. void stop() override {
  135. this->then_.stop();
  136. this->else_.stop();
  137. }
  138. protected:
  139. Condition<Ts...> *condition_;
  140. ActionList<Ts...> then_;
  141. ActionList<Ts...> else_;
  142. };
  143. template<typename... Ts> class WhileAction : public Action<Ts...> {
  144. public:
  145. WhileAction(Condition<Ts...> *condition) : condition_(condition) {}
  146. void add_then(const std::vector<Action<Ts...> *> &actions) {
  147. this->then_.add_actions(actions);
  148. this->then_.add_action(new LambdaAction<Ts...>([this](Ts... x) {
  149. if (this->num_running_ > 0 && this->condition_->check_tuple(this->var_)) {
  150. // play again
  151. if (this->num_running_ > 0) {
  152. this->then_.play_tuple(this->var_);
  153. }
  154. } else {
  155. // condition false, play next
  156. this->play_next_tuple_(this->var_);
  157. }
  158. }));
  159. }
  160. void play_complex(Ts... x) override {
  161. this->num_running_++;
  162. // Store loop parameters
  163. this->var_ = std::make_tuple(x...);
  164. // Initial condition check
  165. if (!this->condition_->check_tuple(this->var_)) {
  166. // If new condition check failed, stop loop if running
  167. this->then_.stop();
  168. this->play_next_tuple_(this->var_);
  169. return;
  170. }
  171. if (this->num_running_ > 0) {
  172. this->then_.play_tuple(this->var_);
  173. }
  174. }
  175. void play(Ts... x) override { /* ignore - see play_complex */
  176. }
  177. void stop() override { this->then_.stop(); }
  178. protected:
  179. Condition<Ts...> *condition_;
  180. ActionList<Ts...> then_;
  181. std::tuple<Ts...> var_{};
  182. };
  183. template<typename... Ts> class WaitUntilAction : public Action<Ts...>, public Component {
  184. public:
  185. WaitUntilAction(Condition<Ts...> *condition) : condition_(condition) {}
  186. void play_complex(Ts... x) override {
  187. this->num_running_++;
  188. // Check if we can continue immediately.
  189. if (this->condition_->check(x...)) {
  190. if (this->num_running_ > 0) {
  191. this->play_next_(x...);
  192. }
  193. return;
  194. }
  195. this->var_ = std::make_tuple(x...);
  196. this->loop();
  197. }
  198. void loop() override {
  199. if (this->num_running_ == 0)
  200. return;
  201. if (!this->condition_->check_tuple(this->var_)) {
  202. return;
  203. }
  204. this->play_next_tuple_(this->var_);
  205. }
  206. float get_setup_priority() const override { return setup_priority::DATA; }
  207. void play(Ts... x) override { /* ignore - see play_complex */
  208. }
  209. protected:
  210. Condition<Ts...> *condition_;
  211. std::tuple<Ts...> var_{};
  212. };
  213. template<typename... Ts> class UpdateComponentAction : public Action<Ts...> {
  214. public:
  215. UpdateComponentAction(PollingComponent *component) : component_(component) {}
  216. void play(Ts... x) override { this->component_->update(); }
  217. protected:
  218. PollingComponent *component_;
  219. };
  220. } // namespace esphome