ESPHome  2024.10.2
script.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/log.h"
6 
7 #include <queue>
8 namespace esphome {
9 namespace script {
10 
11 class ScriptLogger {
12  protected:
13  void esp_logw_(int line, const char *format, const char *param) {
14  esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
15  }
16  void esp_logd_(int line, const char *format, const char *param) {
17  esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
18  }
19  void esp_log_(int level, int line, const char *format, const char *param);
20 };
21 
23 template<typename... Ts> class Script : public ScriptLogger, public Trigger<Ts...> {
24  public:
29  virtual void execute(Ts...) = 0;
31  virtual bool is_running() { return this->is_action_running(); }
33  virtual void stop() { this->stop_action(); }
34 
35  // execute this script using a tuple that contains the arguments
36  void execute_tuple(const std::tuple<Ts...> &tuple) {
37  this->execute_tuple_(tuple, typename gens<sizeof...(Ts)>::type());
38  }
39 
40  // Internal function to give scripts readable names.
41  void set_name(const std::string &name) { name_ = name; }
42 
43  protected:
44  template<int... S> void execute_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
45  this->execute(std::get<S>(tuple)...);
46  }
47 
48  std::string name_;
49 };
50 
56 template<typename... Ts> class SingleScript : public Script<Ts...> {
57  public:
58  void execute(Ts... x) override {
59  if (this->is_action_running()) {
60  this->esp_logw_(__LINE__, "Script '%s' is already running! (mode: single)", this->name_.c_str());
61  return;
62  }
63 
64  this->trigger(x...);
65  }
66 };
67 
73 template<typename... Ts> class RestartScript : public Script<Ts...> {
74  public:
75  void execute(Ts... x) override {
76  if (this->is_action_running()) {
77  this->esp_logd_(__LINE__, "Script '%s' restarting (mode: restart)", this->name_.c_str());
78  this->stop_action();
79  }
80 
81  this->trigger(x...);
82  }
83 };
84 
89 template<typename... Ts> class QueueingScript : public Script<Ts...>, public Component {
90  public:
91  void execute(Ts... x) override {
92  if (this->is_action_running() || this->num_runs_ > 0) {
93  // num_runs_ is the number of *queued* instances, so total number of instances is
94  // num_runs_ + 1
95  if (this->max_runs_ != 0 && this->num_runs_ + 1 >= this->max_runs_) {
96  this->esp_logw_(__LINE__, "Script '%s' maximum number of queued runs exceeded!", this->name_.c_str());
97  return;
98  }
99 
100  this->esp_logd_(__LINE__, "Script '%s' queueing new instance (mode: queued)", this->name_.c_str());
101  this->num_runs_++;
102  this->var_queue_.push(std::make_tuple(x...));
103  return;
104  }
105 
106  this->trigger(x...);
107  // Check if the trigger was immediate and we can continue right away.
108  this->loop();
109  }
110 
111  void stop() override {
112  this->num_runs_ = 0;
114  }
115 
116  void loop() override {
117  if (this->num_runs_ != 0 && !this->is_action_running()) {
118  this->num_runs_--;
119  auto &vars = this->var_queue_.front();
120  this->var_queue_.pop();
121  this->trigger_tuple_(vars, typename gens<sizeof...(Ts)>::type());
122  }
123  }
124 
125  void set_max_runs(int max_runs) { max_runs_ = max_runs; }
126 
127  protected:
128  template<int... S> void trigger_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
129  this->trigger(std::get<S>(tuple)...);
130  }
131 
132  int num_runs_ = 0;
133  int max_runs_ = 0;
134  std::queue<std::tuple<Ts...>> var_queue_;
135 };
136 
142 template<typename... Ts> class ParallelScript : public Script<Ts...> {
143  public:
144  void execute(Ts... x) override {
145  if (this->max_runs_ != 0 && this->automation_parent_->num_running() >= this->max_runs_) {
146  this->esp_logw_(__LINE__, "Script '%s' maximum number of parallel runs exceeded!", this->name_.c_str());
147  return;
148  }
149  this->trigger(x...);
150  }
151  void set_max_runs(int max_runs) { max_runs_ = max_runs; }
152 
153  protected:
154  int max_runs_ = 0;
155 };
156 
157 template<class S, typename... Ts> class ScriptExecuteAction;
158 
159 template<class... As, typename... Ts> class ScriptExecuteAction<Script<As...>, Ts...> : public Action<Ts...> {
160  public:
161  ScriptExecuteAction(Script<As...> *script) : script_(script) {}
162 
163  using Args = std::tuple<TemplatableValue<As, Ts...>...>;
164 
165  template<typename... F> void set_args(F... x) { args_ = Args{x...}; }
166 
167  void play(Ts... x) override { this->script_->execute_tuple(this->eval_args_(x...)); }
168 
169  protected:
170  // NOTE:
171  // `eval_args_impl` functions evaluates `I`th the functions in `args` member.
172  // and then recursively calls `eval_args_impl` for the `I+1`th arg.
173  // if `I` = `N` all args have been stored, and nothing is done.
174 
175  template<std::size_t N>
176  void eval_args_impl_(std::tuple<As...> & /*unused*/, std::integral_constant<std::size_t, N> /*unused*/,
177  std::integral_constant<std::size_t, N> /*unused*/, Ts... /*unused*/) {}
178 
179  template<std::size_t I, std::size_t N>
180  void eval_args_impl_(std::tuple<As...> &evaled_args, std::integral_constant<std::size_t, I> /*unused*/,
181  std::integral_constant<std::size_t, N> n, Ts... x) {
182  std::get<I>(evaled_args) = std::get<I>(args_).value(x...); // NOTE: evaluate `i`th arg, and store in tuple.
183  eval_args_impl_(evaled_args, std::integral_constant<std::size_t, I + 1>{}, n,
184  x...); // NOTE: recurse to next index.
185  }
186 
187  std::tuple<As...> eval_args_(Ts... x) {
188  std::tuple<As...> evaled_args;
189  eval_args_impl_(evaled_args, std::integral_constant<std::size_t, 0>{}, std::tuple_size<Args>{}, x...);
190  return evaled_args;
191  }
192 
193  Script<As...> *script_;
195 };
196 
197 template<class C, typename... Ts> class ScriptStopAction : public Action<Ts...> {
198  public:
199  ScriptStopAction(C *script) : script_(script) {}
200 
201  void play(Ts... x) override { this->script_->stop(); }
202 
203  protected:
205 };
206 
207 template<class C, typename... Ts> class IsRunningCondition : public Condition<Ts...> {
208  public:
209  explicit IsRunningCondition(C *parent) : parent_(parent) {}
210 
211  bool check(Ts... x) override { return this->parent_->is_running(); }
212 
213  protected:
215 };
216 
217 template<class C, typename... Ts> class ScriptWaitAction : public Action<Ts...>, public Component {
218  public:
219  ScriptWaitAction(C *script) : script_(script) {}
220 
221  void play_complex(Ts... x) override {
222  this->num_running_++;
223  // Check if we can continue immediately.
224  if (!this->script_->is_running()) {
225  this->play_next_(x...);
226  return;
227  }
228  this->var_ = std::make_tuple(x...);
229  this->loop();
230  }
231 
232  void loop() override {
233  if (this->num_running_ == 0)
234  return;
235 
236  if (this->script_->is_running())
237  return;
238 
239  this->play_next_tuple_(this->var_);
240  }
241 
242  float get_setup_priority() const override { return setup_priority::DATA; }
243 
244  void play(Ts... x) override { /* ignore - see play_complex */
245  }
246 
247  protected:
249  std::tuple<Ts...> var_{};
250 };
251 
252 } // namespace script
253 } // namespace esphome
const char * name
Definition: stm32flash.h:78
void loop()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
A script type that queues new instances that are created.
Definition: script.h:89
void execute(Ts... x) override
Definition: script.h:75
void set_max_runs(int max_runs)
Definition: script.h:125
bool check(Ts... x) override
Definition: script.h:211
uint16_t x
Definition: tt21100.cpp:17
void execute_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition: script.h:44
A script type that restarts scripts from the beginning when a new instance is started.
Definition: script.h:73
std::queue< std::tuple< Ts... > > var_queue_
Definition: script.h:134
virtual bool is_running()
Check if any instance of this script is currently running.
Definition: script.h:31
void trigger_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition: script.h:128
virtual void stop()
Stop all instances of this script.
Definition: script.h:33
void execute(Ts... x) override
Definition: script.h:91
A script type that executes new instances in parallel.
Definition: script.h:142
void esp_logw_(int line, const char *format, const char *param)
Definition: script.h:13
void play(Ts... x) override
Definition: script.h:244
void eval_args_impl_(std::tuple< As... > &evaled_args, std::integral_constant< std::size_t, I >, std::integral_constant< std::size_t, N > n, Ts... x)
Definition: script.h:180
void execute(Ts... x) override
Definition: script.h:144
std::tuple< TemplatableValue< As, Ts... >... > Args
Definition: script.h:163
Base class for all automation conditions.
Definition: automation.h:74
void set_name(const std::string &name)
Definition: script.h:41
uint8_t type
void execute_tuple(const std::tuple< Ts... > &tuple)
Definition: script.h:36
A script type for which only a single instance at a time is allowed.
Definition: script.h:56
The abstract base class for all script types.
Definition: script.h:23
void set_max_runs(int max_runs)
Definition: script.h:151
float get_setup_priority() const override
Definition: script.h:242
void play(Ts... x) override
Definition: script.h:201
void esp_log_(int level, int line, const char *format, const char *param)
Definition: script.cpp:9
std::string name_
Definition: script.h:48
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void play_complex(Ts... x) override
Definition: script.h:221
void esp_logd_(int line, const char *format, const char *param)
Definition: script.h:16
void execute(Ts... x) override
Definition: script.h:58
void eval_args_impl_(std::tuple< As... > &, std::integral_constant< std::size_t, N >, std::integral_constant< std::size_t, N >, Ts...)
Definition: script.h:176