From 2e30a4953a1e88e8327902f7badc3e6b32a6ec59 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 16 Oct 2025 15:51:01 -1000 Subject: [PATCH] address review comments --- esphome/components/script/__init__.py | 2 +- esphome/components/script/script.h | 12 +++++++++--- tests/integration/test_script_queued.py | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/esphome/components/script/__init__.py b/esphome/components/script/__init__.py index d8d0f6eb94..8d69981db0 100644 --- a/esphome/components/script/__init__.py +++ b/esphome/components/script/__init__.py @@ -54,7 +54,7 @@ def check_max_runs(value): if value[CONF_MODE] not in [CONF_QUEUED, CONF_PARALLEL]: raise cv.Invalid( - "The option 'max_runs' is only valid in 'queue' and 'parallel' mode.", + "The option 'max_runs' is only valid in 'queued' and 'parallel' mode.", path=[CONF_MAX_RUNS], ) diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index 80afa154de..3a97a26985 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" @@ -137,6 +139,10 @@ template class QueueingScript : public Script, public Com } void stop() override { + // Clear all queued items to free memory immediately + for (int i = 0; i < this->max_runs_ - 1; i++) { + this->var_queue_[i].reset(); + } this->num_queued_ = 0; this->queue_front_ = 0; Script::stop(); @@ -144,11 +150,11 @@ template class QueueingScript : public Script, public Com void loop() override { if (this->num_queued_ != 0 && !this->is_action_running()) { - // Dequeue: decrement count, read from front, advance read position + // Dequeue: decrement count, move tuple out (frees slot), advance read position this->num_queued_--; - auto &vars = *this->var_queue_[this->queue_front_]; + auto tuple_ptr = std::move(this->var_queue_[this->queue_front_]); this->queue_front_ = (this->queue_front_ + 1) % (this->max_runs_ - 1); - this->trigger_tuple_(vars, typename gens::type()); + this->trigger_tuple_(*tuple_ptr, typename gens::type()); } } diff --git a/tests/integration/test_script_queued.py b/tests/integration/test_script_queued.py index 414a27ca2e..9f4bce6f31 100644 --- a/tests/integration/test_script_queued.py +++ b/tests/integration/test_script_queued.py @@ -135,7 +135,7 @@ async def test_script_queued( api_client_connected() as client, ): # Get services - entities, services = await client.list_entities_services() + _, services = await client.list_entities_services() # Test 1: Queue depth limit test_service = next((s for s in services if s.name == "test_queue_depth"), None)