From b63ade298f03e671bc7f93d8538e1e7e2e254505 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:17:44 +1300 Subject: [PATCH] Fix queuing scripts not compiling (#4077) --- esphome/components/script/script.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index f3a83cd6ec..165f90ed11 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -4,6 +4,7 @@ #include "esphome/core/component.h" #include "esphome/core/log.h" +#include namespace esphome { namespace script { @@ -88,7 +89,7 @@ template class RestartScript : public Script { template class QueueingScript : public Script, public Component { public: void execute(Ts... x) override { - if (this->is_action_running()) { + if (this->is_action_running() || this->num_runs_ > 0) { // num_runs_ is the number of *queued* instances, so total number of instances is // num_runs_ + 1 if (this->max_runs_ != 0 && this->num_runs_ + 1 >= this->max_runs_) { @@ -98,6 +99,7 @@ template class QueueingScript : public Script, public Com this->esp_logd_(__LINE__, "Script '%s' queueing new instance (mode: queued)", this->name_.c_str()); this->num_runs_++; + this->var_queue_.push(std::make_tuple(x...)); return; } @@ -114,15 +116,22 @@ template class QueueingScript : public Script, public Com void loop() override { if (this->num_runs_ != 0 && !this->is_action_running()) { this->num_runs_--; - this->trigger(); + auto &vars = this->var_queue_.front(); + this->var_queue_.pop(); + this->trigger_tuple_(vars, typename gens::type()); } } void set_max_runs(int max_runs) { max_runs_ = max_runs; } protected: + template void trigger_tuple_(const std::tuple &tuple, seq /*unused*/) { + this->trigger(std::get(tuple)...); + } + int num_runs_ = 0; int max_runs_ = 0; + std::queue> var_queue_; }; /** A script type that executes new instances in parallel.