ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
script.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <tuple>
5#include <forward_list>
9#include "esphome/core/log.h"
10namespace esphome {
11namespace script {
12
14 protected:
15#ifdef USE_STORE_LOG_STR_IN_FLASH
16 void esp_logw_(int line, const __FlashStringHelper *format, const char *param) {
17 esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
18 }
19 void esp_logd_(int line, const __FlashStringHelper *format, const char *param) {
20 esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
21 }
22 void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param);
23#else
24 void esp_logw_(int line, const char *format, const char *param) {
25 esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
26 }
27 void esp_logd_(int line, const char *format, const char *param) {
28 esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
29 }
30 void esp_log_(int level, int line, const char *format, const char *param);
31#endif
32};
33
35template<typename... Ts> class Script : public ScriptLogger, public Trigger<Ts...> {
36 public:
41 virtual void execute(Ts...) = 0;
43 virtual bool is_running() { return this->is_action_running(); }
45 virtual void stop() { this->stop_action(); }
46
47 // execute this script using a tuple that contains the arguments
48 void execute_tuple(const std::tuple<Ts...> &tuple) {
49 this->execute_tuple_(tuple, typename gens<sizeof...(Ts)>::type());
50 }
51
52 // Internal function to give scripts readable names.
53 void set_name(const LogString *name) { name_ = name; }
54
55 protected:
56 template<int... S> void execute_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
57 this->execute(std::get<S>(tuple)...);
58 }
59
60 const LogString *name_{nullptr};
61};
62
68template<typename... Ts> class SingleScript : public Script<Ts...> {
69 public:
70 void execute(Ts... x) override {
71 if (this->is_action_running()) {
72 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' is already running! (mode: single)"),
73 LOG_STR_ARG(this->name_));
74 return;
75 }
76
77 this->trigger(x...);
78 }
79};
80
86template<typename... Ts> class RestartScript : public Script<Ts...> {
87 public:
88 void execute(Ts... x) override {
89 if (this->is_action_running()) {
90 this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' restarting (mode: restart)"), LOG_STR_ARG(this->name_));
91 this->stop_action();
92 }
93
94 this->trigger(x...);
95 }
96};
97
112template<typename... Ts> class QueueingScript : public Script<Ts...>, public Component {
113 public:
114 void execute(Ts... x) override {
115 if (this->is_action_running() || this->num_queued_ > 0) {
116 // num_queued_ is the number of *queued* instances (waiting, not including currently running)
117 // max_runs_ is the maximum *total* instances (running + queued)
118 // So we reject when num_queued_ + 1 >= max_runs_ (queued + running >= max)
119 if (this->num_queued_ + 1 >= this->max_runs_) {
120 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' max instances (running + queued) reached!"),
121 LOG_STR_ARG(this->name_));
122 return;
123 }
124
125 // Initialize queue on first queued item (after capacity check)
126 this->lazy_init_queue_();
127
128 this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' queueing new instance (mode: queued)"),
129 LOG_STR_ARG(this->name_));
130 // Ring buffer: write to (queue_front_ + num_queued_) % queue_capacity
131 const size_t queue_capacity = static_cast<size_t>(this->max_runs_ - 1);
132 size_t write_pos = (this->queue_front_ + this->num_queued_) % queue_capacity;
133 // Use std::make_unique to replace the unique_ptr
134 this->var_queue_[write_pos] = std::make_unique<std::tuple<Ts...>>(x...);
135 this->num_queued_++;
136 return;
137 }
138
139 this->trigger(x...);
140 // Check if the trigger was immediate and we can continue right away.
141 this->loop();
142 }
143
144 void stop() override {
145 // Clear all queued items to free memory immediately
146 // Resetting the array automatically destroys all unique_ptrs and their contents
147 this->var_queue_.reset();
148 this->num_queued_ = 0;
149 this->queue_front_ = 0;
151 }
152
153 void loop() override {
154 if (this->num_queued_ != 0 && !this->is_action_running()) {
155 // Dequeue: decrement count, move tuple out (frees slot), advance read position
156 this->num_queued_--;
157 const size_t queue_capacity = static_cast<size_t>(this->max_runs_ - 1);
158 auto tuple_ptr = std::move(this->var_queue_[this->queue_front_]);
159 this->queue_front_ = (this->queue_front_ + 1) % queue_capacity;
160 this->trigger_tuple_(*tuple_ptr, typename gens<sizeof...(Ts)>::type());
161 }
162 }
163
164 void set_max_runs(int max_runs) { max_runs_ = max_runs; }
165
166 protected:
167 // Lazy init queue on first use - avoids setup() ordering issues and saves memory
168 // if script is never executed during this boot cycle
169 inline void lazy_init_queue_() {
170 if (!this->var_queue_) {
171 // Allocate array of max_runs_ - 1 slots for queued items (running item is separate)
172 // unique_ptr array is zero-initialized, so all slots start as nullptr
173 this->var_queue_ = std::make_unique<std::unique_ptr<std::tuple<Ts...>>[]>(this->max_runs_ - 1);
174 }
175 }
176
177 template<int... S> void trigger_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
178 this->trigger(std::get<S>(tuple)...);
179 }
180
181 int num_queued_ = 0; // Number of queued instances (not including currently running)
182 int max_runs_ = 0; // Maximum total instances (running + queued)
183 size_t queue_front_ = 0; // Ring buffer read position (next item to execute)
184 std::unique_ptr<std::unique_ptr<std::tuple<Ts...>>[]> var_queue_; // Ring buffer of queued parameters
185};
186
192template<typename... Ts> class ParallelScript : public Script<Ts...> {
193 public:
194 void execute(Ts... x) override {
195 if (this->max_runs_ != 0 && this->automation_parent_->num_running() >= this->max_runs_) {
196 this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of parallel runs exceeded!"),
197 LOG_STR_ARG(this->name_));
198 return;
199 }
200 this->trigger(x...);
201 }
202 void set_max_runs(int max_runs) { max_runs_ = max_runs; }
203
204 protected:
205 int max_runs_ = 0;
206};
207
208template<class S, typename... Ts> class ScriptExecuteAction;
209
210template<class... As, typename... Ts> class ScriptExecuteAction<Script<As...>, Ts...> : public Action<Ts...> {
211 public:
212 ScriptExecuteAction(Script<As...> *script) : script_(script) {}
213
214 using Args = std::tuple<TemplatableValue<As, Ts...>...>;
215
216 template<typename... F> void set_args(F... x) { args_ = Args{x...}; }
217
218 void play(const Ts &...x) override { this->script_->execute_tuple(this->eval_args_(x...)); }
219
220 protected:
221 // NOTE:
222 // `eval_args_impl` functions evaluates `I`th the functions in `args` member.
223 // and then recursively calls `eval_args_impl` for the `I+1`th arg.
224 // if `I` = `N` all args have been stored, and nothing is done.
225
226 template<std::size_t N>
227 void eval_args_impl_(std::tuple<As...> & /*unused*/, std::integral_constant<std::size_t, N> /*unused*/,
228 std::integral_constant<std::size_t, N> /*unused*/, Ts... /*unused*/) {}
229
230 template<std::size_t I, std::size_t N>
231 void eval_args_impl_(std::tuple<As...> &evaled_args, std::integral_constant<std::size_t, I> /*unused*/,
232 std::integral_constant<std::size_t, N> n, Ts... x) {
233 std::get<I>(evaled_args) = std::get<I>(args_).value(x...); // NOTE: evaluate `i`th arg, and store in tuple.
234 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, I + 1>{}, n,
235 x...); // NOTE: recurse to next index.
236 }
237
238 std::tuple<As...> eval_args_(Ts... x) {
239 std::tuple<As...> evaled_args;
240 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, 0>{}, std::tuple_size<Args>{}, x...);
241 return evaled_args;
242 }
243
246};
247
248template<class C, typename... Ts> class ScriptStopAction : public Action<Ts...> {
249 public:
250 ScriptStopAction(C *script) : script_(script) {}
251
252 void play(const Ts &...x) override { this->script_->stop(); }
253
254 protected:
256};
257
258template<class C, typename... Ts> class IsRunningCondition : public Condition<Ts...> {
259 public:
260 explicit IsRunningCondition(C *parent) : parent_(parent) {}
261
262 bool check(const Ts &...x) override { return this->parent_->is_running(); }
263
264 protected:
266};
267
275template<class C, typename... Ts> class ScriptWaitAction : public Action<Ts...>, public Component {
276 public:
277 ScriptWaitAction(C *script) : script_(script) {}
278
279 void setup() override {
280 // Start with loop disabled - only enable when there's work to do
281 this->disable_loop();
282 }
283
284 void play_complex(const Ts &...x) override {
285 this->num_running_++;
286 // Check if we can continue immediately.
287 if (!this->script_->is_running()) {
288 this->play_next_(x...);
289 return;
290 }
291
292 // Store parameters for later execution
293 this->param_queue_.emplace_front(x...);
294 // Enable loop now that we have work to do
295 this->enable_loop();
296 this->loop();
297 }
298
299 void loop() override {
300 if (this->num_running_ == 0)
301 return;
302
303 if (this->script_->is_running())
304 return;
305
306 while (!this->param_queue_.empty()) {
307 auto &params = this->param_queue_.front();
308 this->play_next_tuple_(params, typename gens<sizeof...(Ts)>::type());
309 this->param_queue_.pop_front();
310 }
311 // Queue is now empty - disable loop until next play_complex
312 this->disable_loop();
313 }
314
315 void play(const Ts &...x) override { /* ignore - see play_complex */
316 }
317
318 void stop() override {
319 this->param_queue_.clear();
320 this->disable_loop();
321 }
322
323 protected:
324 template<int... S> void play_next_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) {
325 this->play_next_(std::get<S>(tuple)...);
326 }
327
329 std::forward_list<std::tuple<Ts...>> param_queue_;
330};
331
332} // namespace script
333} // namespace esphome
void play_next_(const Ts &...x)
Definition automation.h:226
int num_running()
Return the number of actions in the action part of this automation that are currently running.
Definition automation.h:323
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
Base class for all automation conditions.
Definition automation.h:148
void trigger(const Ts &...x)
Definition automation.h:169
Automation< Ts... > * automation_parent_
Definition automation.h:190
bool check(const Ts &...x) override
Definition script.h:262
A script type that executes new instances in parallel.
Definition script.h:192
void execute(Ts... x) override
Definition script.h:194
void set_max_runs(int max_runs)
Definition script.h:202
A script type that queues new instances that are created.
Definition script.h:112
void trigger_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition script.h:177
void execute(Ts... x) override
Definition script.h:114
void set_max_runs(int max_runs)
Definition script.h:164
std::unique_ptr< std::unique_ptr< std::tuple< Ts... > >[]> var_queue_
Definition script.h:184
A script type that restarts scripts from the beginning when a new instance is started.
Definition script.h:86
void execute(Ts... x) override
Definition script.h:88
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:231
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:227
std::tuple< TemplatableValue< As, Ts... >... > Args
Definition script.h:214
The abstract base class for all script types.
Definition script.h:35
void execute_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition script.h:56
virtual bool is_running()
Check if any instance of this script is currently running.
Definition script.h:43
virtual void execute(Ts...)=0
Execute a new instance of this script.
void execute_tuple(const std::tuple< Ts... > &tuple)
Definition script.h:48
virtual void stop()
Stop all instances of this script.
Definition script.h:45
void set_name(const LogString *name)
Definition script.h:53
const LogString * name_
Definition script.h:60
void esp_logd_(int line, const char *format, const char *param)
Definition script.h:27
void esp_logd_(int line, const __FlashStringHelper *format, const char *param)
Definition script.h:19
void esp_logw_(int line, const char *format, const char *param)
Definition script.h:24
void esp_logw_(int line, const __FlashStringHelper *format, const char *param)
Definition script.h:16
void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param)
Definition script.cpp:10
void play(const Ts &...x) override
Definition script.h:252
Wait for a script to finish before continuing.
Definition script.h:275
void play_next_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
Definition script.h:324
void play(const Ts &...x) override
Definition script.h:315
void play_complex(const Ts &...x) override
Definition script.h:284
std::forward_list< std::tuple< Ts... > > param_queue_
Definition script.h:329
A script type for which only a single instance at a time is allowed.
Definition script.h:68
void execute(Ts... x) override
Definition script.h:70
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