ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
script.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/log.h"
6
7#include <queue>
8namespace esphome {
9namespace script {
10
12 protected:
13#ifdef USE_STORE_LOG_STR_IN_FLASH
14 void esp_logw_(int line, const __FlashStringHelper *format, const char *param) {
15 esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
16 }
17 void esp_logd_(int line, const __FlashStringHelper *format, const char *param) {
18 esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
19 }
20 void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param);
21#else
22 void esp_logw_(int line, const char *format, const char *param) {
23 esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
24 }
25 void esp_logd_(int line, const char *format, const char *param) {
26 esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
27 }
28 void esp_log_(int level, int line, const char *format, const char *param);
29#endif
30};
31
33template<typename... Ts> class Script : public ScriptLogger, public Trigger<Ts...> {
34 public:
39 virtual void execute(Ts...) = 0;
41 virtual bool is_running() { return this->is_action_running(); }
43 virtual void stop() { this->stop_action(); }
44
45 // execute this script using a tuple that contains the arguments
46 void execute_tuple(const std::tuple<Ts...> &tuple) {
47 this->execute_tuple_(tuple, typename gens<sizeof...(Ts)>::type());
48 }
49
50 // Internal function to give scripts readable names.
51 void set_name(const LogString *name) { name_ = name; }
52
53 protected:
54 template<int... S> void execute_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
55 this->execute(std::get<S>(tuple)...);
56 }
57
58 const LogString *name_{nullptr};
59};
60
66template<typename... Ts> class SingleScript : public Script<Ts...> {
67 public:
68 void execute(Ts... x) override {
69 if (this->is_action_running()) {
70 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' is already running! (mode: single)"),
71 LOG_STR_ARG(this->name_));
72 return;
73 }
74
75 this->trigger(x...);
76 }
77};
78
84template<typename... Ts> class RestartScript : public Script<Ts...> {
85 public:
86 void execute(Ts... x) override {
87 if (this->is_action_running()) {
88 this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' restarting (mode: restart)"), LOG_STR_ARG(this->name_));
89 this->stop_action();
90 }
91
92 this->trigger(x...);
93 }
94};
95
100template<typename... Ts> class QueueingScript : public Script<Ts...>, public Component {
101 public:
102 void execute(Ts... x) override {
103 if (this->is_action_running() || this->num_runs_ > 0) {
104 // num_runs_ is the number of *queued* instances, so total number of instances is
105 // num_runs_ + 1
106 if (this->max_runs_ != 0 && this->num_runs_ + 1 >= this->max_runs_) {
107 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of queued runs exceeded!"),
108 LOG_STR_ARG(this->name_));
109 return;
110 }
111
112 this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' queueing new instance (mode: queued)"),
113 LOG_STR_ARG(this->name_));
114 this->num_runs_++;
115 this->var_queue_.push(std::make_tuple(x...));
116 return;
117 }
118
119 this->trigger(x...);
120 // Check if the trigger was immediate and we can continue right away.
121 this->loop();
122 }
123
124 void stop() override {
125 this->num_runs_ = 0;
127 }
128
129 void loop() override {
130 if (this->num_runs_ != 0 && !this->is_action_running()) {
131 this->num_runs_--;
132 auto &vars = this->var_queue_.front();
133 this->var_queue_.pop();
134 this->trigger_tuple_(vars, typename gens<sizeof...(Ts)>::type());
135 }
136 }
137
138 void set_max_runs(int max_runs) { max_runs_ = max_runs; }
139
140 protected:
141 template<int... S> void trigger_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
142 this->trigger(std::get<S>(tuple)...);
143 }
144
145 int num_runs_ = 0;
146 int max_runs_ = 0;
147 std::queue<std::tuple<Ts...>> var_queue_;
148};
149
155template<typename... Ts> class ParallelScript : public Script<Ts...> {
156 public:
157 void execute(Ts... x) override {
158 if (this->max_runs_ != 0 && this->automation_parent_->num_running() >= this->max_runs_) {
159 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of parallel runs exceeded!"),
160 LOG_STR_ARG(this->name_));
161 return;
162 }
163 this->trigger(x...);
164 }
165 void set_max_runs(int max_runs) { max_runs_ = max_runs; }
166
167 protected:
168 int max_runs_ = 0;
169};
170
171template<class S, typename... Ts> class ScriptExecuteAction;
172
173template<class... As, typename... Ts> class ScriptExecuteAction<Script<As...>, Ts...> : public Action<Ts...> {
174 public:
175 ScriptExecuteAction(Script<As...> *script) : script_(script) {}
176
177 using Args = std::tuple<TemplatableValue<As, Ts...>...>;
178
179 template<typename... F> void set_args(F... x) { args_ = Args{x...}; }
180
181 void play(Ts... x) override { this->script_->execute_tuple(this->eval_args_(x...)); }
182
183 protected:
184 // NOTE:
185 // `eval_args_impl` functions evaluates `I`th the functions in `args` member.
186 // and then recursively calls `eval_args_impl` for the `I+1`th arg.
187 // if `I` = `N` all args have been stored, and nothing is done.
188
189 template<std::size_t N>
190 void eval_args_impl_(std::tuple<As...> & /*unused*/, std::integral_constant<std::size_t, N> /*unused*/,
191 std::integral_constant<std::size_t, N> /*unused*/, Ts... /*unused*/) {}
192
193 template<std::size_t I, std::size_t N>
194 void eval_args_impl_(std::tuple<As...> &evaled_args, std::integral_constant<std::size_t, I> /*unused*/,
195 std::integral_constant<std::size_t, N> n, Ts... x) {
196 std::get<I>(evaled_args) = std::get<I>(args_).value(x...); // NOTE: evaluate `i`th arg, and store in tuple.
197 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, I + 1>{}, n,
198 x...); // NOTE: recurse to next index.
199 }
200
201 std::tuple<As...> eval_args_(Ts... x) {
202 std::tuple<As...> evaled_args;
203 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, 0>{}, std::tuple_size<Args>{}, x...);
204 return evaled_args;
205 }
206
209};
210
211template<class C, typename... Ts> class ScriptStopAction : public Action<Ts...> {
212 public:
213 ScriptStopAction(C *script) : script_(script) {}
214
215 void play(Ts... x) override { this->script_->stop(); }
216
217 protected:
219};
220
221template<class C, typename... Ts> class IsRunningCondition : public Condition<Ts...> {
222 public:
223 explicit IsRunningCondition(C *parent) : parent_(parent) {}
224
225 bool check(Ts... x) override { return this->parent_->is_running(); }
226
227 protected:
229};
230
231template<class C, typename... Ts> class ScriptWaitAction : public Action<Ts...>, public Component {
232 public:
233 ScriptWaitAction(C *script) : script_(script) {}
234
235 void play_complex(Ts... x) override {
236 this->num_running_++;
237 // Check if we can continue immediately.
238 if (!this->script_->is_running()) {
239 this->play_next_(x...);
240 return;
241 }
242 this->var_ = std::make_tuple(x...);
243 this->loop();
244 }
245
246 void loop() override {
247 if (this->num_running_ == 0)
248 return;
249
250 if (this->script_->is_running())
251 return;
252
253 this->play_next_tuple_(this->var_);
254 }
255
256 void play(Ts... x) override { /* ignore - see play_complex */
257 }
258
259 protected:
261 std::tuple<Ts...> var_{};
262};
263
264} // namespace script
265} // namespace esphome
void play_next_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition automation.h:209
void play_next_(Ts... x)
Definition automation.h:201
int num_running()
Return the number of actions in the action part of this automation that are currently running.
Definition automation.h:298
Base class for all automation conditions.
Definition automation.h:124
Automation< Ts... > * automation_parent_
Definition automation.h:166
bool check(Ts... x) override
Definition script.h:225
A script type that executes new instances in parallel.
Definition script.h:155
void execute(Ts... x) override
Definition script.h:157
void set_max_runs(int max_runs)
Definition script.h:165
A script type that queues new instances that are created.
Definition script.h:100
void trigger_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition script.h:141
void execute(Ts... x) override
Definition script.h:102
std::queue< std::tuple< Ts... > > var_queue_
Definition script.h:147
void set_max_runs(int max_runs)
Definition script.h:138
A script type that restarts scripts from the beginning when a new instance is started.
Definition script.h:84
void execute(Ts... x) override
Definition script.h:86
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:194
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:190
std::tuple< TemplatableValue< As, Ts... >... > Args
Definition script.h:177
The abstract base class for all script types.
Definition script.h:33
void execute_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition script.h:54
virtual bool is_running()
Check if any instance of this script is currently running.
Definition script.h:41
virtual void execute(Ts...)=0
Execute a new instance of this script.
void execute_tuple(const std::tuple< Ts... > &tuple)
Definition script.h:46
virtual void stop()
Stop all instances of this script.
Definition script.h:43
void set_name(const LogString *name)
Definition script.h:51
const LogString * name_
Definition script.h:58
void esp_logd_(int line, const char *format, const char *param)
Definition script.h:25
void esp_logd_(int line, const __FlashStringHelper *format, const char *param)
Definition script.h:17
void esp_logw_(int line, const char *format, const char *param)
Definition script.h:22
void esp_logw_(int line, const __FlashStringHelper *format, const char *param)
Definition script.h:14
void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param)
Definition script.cpp:10
void play(Ts... x) override
Definition script.h:215
std::tuple< Ts... > var_
Definition script.h:261
void play_complex(Ts... x) override
Definition script.h:235
void play(Ts... x) override
Definition script.h:256
A script type for which only a single instance at a time is allowed.
Definition script.h:66
void execute(Ts... x) override
Definition script.h:68
uint16_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5