36#ifdef USE_SCRIPT_ON_STOP
51 this->
execute_tuple_(tuple, std::make_index_sequence<
sizeof...(Ts)>{});
58 template<
size_t... S>
void execute_tuple_(
const std::tuple<Ts...> &tuple, std::index_sequence<S...> ) {
59 this->
execute(std::get<S>(tuple)...);
73#ifdef USE_SCRIPT_ON_STOP
78 const LogString *
name_{
nullptr};
79#ifdef USE_SCRIPT_ON_STOP
93 this->
esp_logw_(__LINE__, ESPHOME_LOG_FORMAT(
"Script '%s' is already running! (mode: single)"),
94 LOG_STR_ARG(this->
name_));
111 this->
esp_logd_(__LINE__, ESPHOME_LOG_FORMAT(
"Script '%s' restarting (mode: restart)"), LOG_STR_ARG(this->
name_));
113#ifdef USE_SCRIPT_ON_STOP
146 this->
esp_logw_(__LINE__, ESPHOME_LOG_FORMAT(
"Script '%s' max instances (running + queued) reached!"),
147 LOG_STR_ARG(this->
name_));
154 this->
esp_logd_(__LINE__, ESPHOME_LOG_FORMAT(
"Script '%s' queueing new instance (mode: queued)"),
155 LOG_STR_ARG(this->
name_));
157 const size_t queue_capacity =
static_cast<size_t>(this->
max_runs_ - 1);
160 this->
var_queue_[write_pos] = std::make_unique<std::tuple<Ts...>>(
x...);
174#ifdef USE_SCRIPT_ON_STOP
183#ifdef USE_SCRIPT_ON_STOP
195 const size_t queue_capacity =
static_cast<size_t>(this->
max_runs_ - 1);
197 this->queue_front_ = (this->queue_front_ + 1) % queue_capacity;
198 this->
trigger_tuple_(*tuple_ptr, std::make_index_sequence<
sizeof...(Ts)>{});
220 this->
var_queue_ = std::make_unique<std::unique_ptr<std::tuple<Ts...>>[]>(this->
max_runs_ - 1);
224 template<
size_t... S>
void trigger_tuple_(
const std::tuple<Ts...> &tuple, std::index_sequence<S...> ) {
231 std::unique_ptr<std::unique_ptr<std::tuple<Ts...>>[]>
var_queue_;
243 this->
esp_logw_(__LINE__, ESPHOME_LOG_FORMAT(
"Script '%s' maximum number of parallel runs exceeded!"),
244 LOG_STR_ARG(this->
name_));
265 void play(
const Ts &...
x)
override { this->script_->execute_tuple(this->eval_args_(
x...)); }
273 template<std::
size_t N>
275 std::integral_constant<std::size_t, N> , Ts... ) {}
277 template<std::
size_t I, std::
size_t N>
278 void eval_args_impl_(std::tuple<As...> &evaled_args, std::integral_constant<std::size_t, I> ,
279 std::integral_constant<std::size_t, N> n, Ts...
x) {
280 std::get<I>(evaled_args) = std::get<I>(args_).value(
x...);
281 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, I + 1>{}, n,
286 std::tuple<As...> evaled_args;
287 eval_args_impl_(evaled_args, std::integral_constant<std::size_t, 0>{}, std::tuple_size<Args>{},
x...);
320 if (!this->
script_->is_running()) {
339 if (this->
script_->is_running())
349 std::apply([
this](
auto &...args) { this->
play_next_(args...); }, params);
356 void play(
const Ts &...
x)
override { }
void play_next_(const Ts &...x)
int num_running()
Return the number of actions in the action part of this automation that are currently running.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
bool is_idle() const
Check if this component is idle.
RAII guard that publishes a current source (e.g.
Function-pointer-only templatable storage (4 bytes on 32-bit).
Automation< Ts... > * automation_parent_
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
A script type that executes new instances in parallel.
void execute(Ts... x) override
void set_max_runs(int max_runs)
A script type that queues new instances that are created.
void trigger_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
void execute(Ts... x) override
void set_max_runs(int max_runs)
std::unique_ptr< std::unique_ptr< std::tuple< Ts... > >[]> var_queue_
A script type that restarts scripts from the beginning when a new instance is started.
void execute(Ts... x) override
void play(const Ts &...x) override
Script< As... > * script_
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)
void eval_args_impl_(std::tuple< As... > &, std::integral_constant< std::size_t, N >, std::integral_constant< std::size_t, N >, Ts...)
std::tuple< As... > eval_args_(Ts... x)
ScriptExecuteAction(Script< As... > *script)
std::tuple< TemplatableFn< As, Ts... >... > Args
The abstract base class for all script types.
virtual bool is_running()
Check if any instance of this script is currently running.
virtual void execute(Ts...)=0
Execute a new instance of this script.
void execute_tuple(const std::tuple< Ts... > &tuple)
Trigger * get_stop_trigger()
virtual void stop()
Stop all instances of this script. Fires the on_stop trigger once if any instance was running.
void run_actions_(const Ts &...x) ESPHOME_ALWAYS_INLINE
void set_name(const LogString *name)
void execute_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
void esp_logw_(int line, ProgmemStr format, const char *param)
void esp_logd_(int line, ProgmemStr format, const char *param)
void esp_log_(int level, int line, ProgmemStr format, const char *param)
Wait for a script to finish before continuing.
void play(const Ts &...x) override
ScriptWaitAction(C *script)
std::list< std::tuple< stored_trigger_arg_t< Ts >... > > param_queue_
void play_complex(const Ts &...x) override
A script type for which only a single instance at a time is allowed.
void execute(Ts... x) override
const __FlashStringHelper * ProgmemStr