From c9312d5c2799a213d338981527907d475bb7844a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Oct 2025 09:42:17 -1000 Subject: [PATCH 1/9] [script] Fix unbounded queue growth, optimize queued mode (default max_runs=5) (#11308) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- esphome/components/script/__init__.py | 17 +- esphome/components/script/script.h | 72 +++++-- tests/integration/fixtures/script_queued.yaml | 170 +++++++++++++++ tests/integration/test_script_queued.py | 203 ++++++++++++++++++ 4 files changed, 442 insertions(+), 20 deletions(-) create mode 100644 tests/integration/fixtures/script_queued.yaml create mode 100644 tests/integration/test_script_queued.py diff --git a/esphome/components/script/__init__.py b/esphome/components/script/__init__.py index e8a8aa5671..8d69981db0 100644 --- a/esphome/components/script/__init__.py +++ b/esphome/components/script/__init__.py @@ -45,13 +45,26 @@ def get_script(script_id): def check_max_runs(value): + # Set default for queued mode to prevent unbounded queue growth + if CONF_MAX_RUNS not in value and value[CONF_MODE] == CONF_QUEUED: + value[CONF_MAX_RUNS] = 5 + if CONF_MAX_RUNS not in value: return 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], ) + + # Queued mode must have bounded queue (min 1), parallel mode can be unlimited (0) + if value[CONF_MODE] == CONF_QUEUED and value[CONF_MAX_RUNS] < 1: + raise cv.Invalid( + "The option 'max_runs' must be at least 1 for queued mode.", + path=[CONF_MAX_RUNS], + ) + return value @@ -106,7 +119,7 @@ CONFIG_SCHEMA = automation.validate_automation( cv.Optional(CONF_MODE, default=CONF_SINGLE): cv.one_of( *SCRIPT_MODES, lower=True ), - cv.Optional(CONF_MAX_RUNS): cv.positive_int, + cv.Optional(CONF_MAX_RUNS): cv.int_range(min=0, max=100), cv.Optional(CONF_PARAMETERS, default={}): cv.Schema( { validate_parameter_name: validate_parameter_type, diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index b87402f52e..58fb67a3ea 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -1,10 +1,11 @@ #pragma once +#include +#include #include "esphome/core/automation.h" #include "esphome/core/component.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" - -#include namespace esphome { namespace script { @@ -96,23 +97,41 @@ template class RestartScript : public Script { /** A script type that queues new instances that are created. * * Only one instance of the script can be active at a time. + * + * Ring buffer implementation: + * - num_queued_ tracks the number of queued (waiting) instances, NOT including the currently running one + * - queue_front_ points to the next item to execute (read position) + * - Buffer size is max_runs_ - 1 (max total instances minus the running one) + * - Write position is calculated as: (queue_front_ + num_queued_) % (max_runs_ - 1) + * - When an item finishes, queue_front_ advances: (queue_front_ + 1) % (max_runs_ - 1) + * - First execute() runs immediately without queuing (num_queued_ stays 0) + * - Subsequent executes while running are queued starting at position 0 + * - Maximum total instances = max_runs_ (includes 1 running + (max_runs_ - 1) queued) */ template class QueueingScript : public Script, public Component { public: void execute(Ts... x) override { - 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_) { - this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of queued runs exceeded!"), + if (this->is_action_running() || this->num_queued_ > 0) { + // num_queued_ is the number of *queued* instances (waiting, not including currently running) + // max_runs_ is the maximum *total* instances (running + queued) + // So we reject when num_queued_ + 1 >= max_runs_ (queued + running >= max) + if (this->num_queued_ + 1 >= this->max_runs_) { + this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' max instances (running + queued) reached!"), LOG_STR_ARG(this->name_)); return; } + // Initialize queue on first queued item (after capacity check) + this->lazy_init_queue_(); + this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' queueing new instance (mode: queued)"), LOG_STR_ARG(this->name_)); - this->num_runs_++; - this->var_queue_.push(std::make_tuple(x...)); + // Ring buffer: write to (queue_front_ + num_queued_) % queue_capacity + const size_t queue_capacity = static_cast(this->max_runs_ - 1); + size_t write_pos = (this->queue_front_ + this->num_queued_) % queue_capacity; + // Use std::make_unique to replace the unique_ptr + this->var_queue_[write_pos] = std::make_unique>(x...); + this->num_queued_++; return; } @@ -122,29 +141,46 @@ template class QueueingScript : public Script, public Com } void stop() override { - this->num_runs_ = 0; + // Clear all queued items to free memory immediately + // Resetting the array automatically destroys all unique_ptrs and their contents + this->var_queue_.reset(); + this->num_queued_ = 0; + this->queue_front_ = 0; Script::stop(); } void loop() override { - if (this->num_runs_ != 0 && !this->is_action_running()) { - this->num_runs_--; - auto &vars = this->var_queue_.front(); - this->var_queue_.pop(); - this->trigger_tuple_(vars, typename gens::type()); + if (this->num_queued_ != 0 && !this->is_action_running()) { + // Dequeue: decrement count, move tuple out (frees slot), advance read position + this->num_queued_--; + const size_t queue_capacity = static_cast(this->max_runs_ - 1); + auto tuple_ptr = std::move(this->var_queue_[this->queue_front_]); + this->queue_front_ = (this->queue_front_ + 1) % queue_capacity; + this->trigger_tuple_(*tuple_ptr, typename gens::type()); } } void set_max_runs(int max_runs) { max_runs_ = max_runs; } protected: + // Lazy init queue on first use - avoids setup() ordering issues and saves memory + // if script is never executed during this boot cycle + inline void lazy_init_queue_() { + if (!this->var_queue_) { + // Allocate array of max_runs_ - 1 slots for queued items (running item is separate) + // unique_ptr array is zero-initialized, so all slots start as nullptr + this->var_queue_ = std::make_unique>[]>(this->max_runs_ - 1); + } + } + 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_; + int num_queued_ = 0; // Number of queued instances (not including currently running) + int max_runs_ = 0; // Maximum total instances (running + queued) + size_t queue_front_ = 0; // Ring buffer read position (next item to execute) + std::unique_ptr>[]> var_queue_; // Ring buffer of queued parameters }; /** A script type that executes new instances in parallel. diff --git a/tests/integration/fixtures/script_queued.yaml b/tests/integration/fixtures/script_queued.yaml new file mode 100644 index 0000000000..996dd6436f --- /dev/null +++ b/tests/integration/fixtures/script_queued.yaml @@ -0,0 +1,170 @@ +esphome: + name: test-script-queued + +host: +api: + actions: + # Test 1: Queue depth with default max_runs=5 + - action: test_queue_depth + then: + - logger.log: "=== TEST 1: Queue depth (max_runs=5 means 5 total, reject 6-7) ===" + - script.execute: + id: queue_depth_script + value: 1 + - script.execute: + id: queue_depth_script + value: 2 + - script.execute: + id: queue_depth_script + value: 3 + - script.execute: + id: queue_depth_script + value: 4 + - script.execute: + id: queue_depth_script + value: 5 + - script.execute: + id: queue_depth_script + value: 6 + - script.execute: + id: queue_depth_script + value: 7 + + # Test 2: Ring buffer wrap test + - action: test_ring_buffer + then: + - logger.log: "=== TEST 2: Ring buffer wrap (should process A, B, C in order) ===" + - script.execute: + id: wrap_script + msg: "A" + - script.execute: + id: wrap_script + msg: "B" + - script.execute: + id: wrap_script + msg: "C" + + # Test 3: Stop clears queue + - action: test_stop_clears + then: + - logger.log: "=== TEST 3: Stop clears queue (should only see 1, then 'STOPPED') ===" + - script.execute: + id: stop_script + num: 1 + - script.execute: + id: stop_script + num: 2 + - script.execute: + id: stop_script + num: 3 + - delay: 50ms + - logger.log: "STOPPING script now" + - script.stop: stop_script + + # Test 4: Verify rejection (max_runs=3) + - action: test_rejection + then: + - logger.log: "=== TEST 4: Verify rejection (max_runs=3 means 3 total, reject 4-8) ===" + - script.execute: + id: rejection_script + val: 1 + - script.execute: + id: rejection_script + val: 2 + - script.execute: + id: rejection_script + val: 3 + - script.execute: + id: rejection_script + val: 4 + - script.execute: + id: rejection_script + val: 5 + - script.execute: + id: rejection_script + val: 6 + - script.execute: + id: rejection_script + val: 7 + - script.execute: + id: rejection_script + val: 8 + + # Test 5: No parameters test + - action: test_no_params + then: + - logger.log: "=== TEST 5: No params (should process 3 times) ===" + - script.execute: no_params_script + - script.execute: no_params_script + - script.execute: no_params_script + +logger: + level: DEBUG + +script: + # Test script 1: Queue depth test (default max_runs=5) + - id: queue_depth_script + mode: queued + parameters: + value: int + then: + - logger.log: + format: "Queue test: START item %d" + args: ['value'] + - delay: 100ms + - logger.log: + format: "Queue test: END item %d" + args: ['value'] + + # Test script 2: Ring buffer wrap test (max_runs=3) + - id: wrap_script + mode: queued + max_runs: 3 + parameters: + msg: string + then: + - logger.log: + format: "Ring buffer: START '%s'" + args: ['msg.c_str()'] + - delay: 50ms + - logger.log: + format: "Ring buffer: END '%s'" + args: ['msg.c_str()'] + + # Test script 3: Stop test + - id: stop_script + mode: queued + max_runs: 5 + parameters: + num: int + then: + - logger.log: + format: "Stop test: START %d" + args: ['num'] + - delay: 100ms + - logger.log: + format: "Stop test: END %d" + args: ['num'] + + # Test script 4: Rejection test (max_runs=3) + - id: rejection_script + mode: queued + max_runs: 3 + parameters: + val: int + then: + - logger.log: + format: "Rejection test: START %d" + args: ['val'] + - delay: 200ms + - logger.log: + format: "Rejection test: END %d" + args: ['val'] + + # Test script 5: No parameters + - id: no_params_script + mode: queued + then: + - logger.log: "No params: START" + - delay: 50ms + - logger.log: "No params: END" diff --git a/tests/integration/test_script_queued.py b/tests/integration/test_script_queued.py new file mode 100644 index 0000000000..ce1c25b649 --- /dev/null +++ b/tests/integration/test_script_queued.py @@ -0,0 +1,203 @@ +"""Test ESPHome queued script functionality.""" + +from __future__ import annotations + +import asyncio +import re + +import pytest + +from .types import APIClientConnectedFactory, RunCompiledFunction + + +@pytest.mark.asyncio +async def test_script_queued( + yaml_config: str, + run_compiled: RunCompiledFunction, + api_client_connected: APIClientConnectedFactory, +) -> None: + """Test comprehensive queued script functionality.""" + loop = asyncio.get_running_loop() + + # Track all test results + test_results = { + "queue_depth": {"processed": [], "rejections": 0}, + "ring_buffer": {"start_order": [], "end_order": []}, + "stop": {"processed": [], "stop_logged": False}, + "rejection": {"processed": [], "rejections": 0}, + "no_params": {"executions": 0}, + } + + # Patterns for Test 1: Queue depth + queue_start = re.compile(r"Queue test: START item (\d+)") + queue_end = re.compile(r"Queue test: END item (\d+)") + queue_reject = re.compile(r"Script 'queue_depth_script' max instances") + + # Patterns for Test 2: Ring buffer + ring_start = re.compile(r"Ring buffer: START '([A-Z])'") + ring_end = re.compile(r"Ring buffer: END '([A-Z])'") + + # Patterns for Test 3: Stop + stop_start = re.compile(r"Stop test: START (\d+)") + stop_log = re.compile(r"STOPPING script now") + + # Patterns for Test 4: Rejection + reject_start = re.compile(r"Rejection test: START (\d+)") + reject_end = re.compile(r"Rejection test: END (\d+)") + reject_reject = re.compile(r"Script 'rejection_script' max instances") + + # Patterns for Test 5: No params + no_params_end = re.compile(r"No params: END") + + # Test completion futures + test1_complete = loop.create_future() + test2_complete = loop.create_future() + test3_complete = loop.create_future() + test4_complete = loop.create_future() + test5_complete = loop.create_future() + + def check_output(line: str) -> None: + """Check log output for all test messages.""" + # Test 1: Queue depth + if match := queue_start.search(line): + item = int(match.group(1)) + if item not in test_results["queue_depth"]["processed"]: + test_results["queue_depth"]["processed"].append(item) + + if match := queue_end.search(line): + item = int(match.group(1)) + if item == 5 and not test1_complete.done(): + test1_complete.set_result(True) + + if queue_reject.search(line): + test_results["queue_depth"]["rejections"] += 1 + + # Test 2: Ring buffer + if match := ring_start.search(line): + msg = match.group(1) + test_results["ring_buffer"]["start_order"].append(msg) + + if match := ring_end.search(line): + msg = match.group(1) + test_results["ring_buffer"]["end_order"].append(msg) + if ( + len(test_results["ring_buffer"]["end_order"]) == 3 + and not test2_complete.done() + ): + test2_complete.set_result(True) + + # Test 3: Stop + if match := stop_start.search(line): + item = int(match.group(1)) + if item not in test_results["stop"]["processed"]: + test_results["stop"]["processed"].append(item) + + if stop_log.search(line): + test_results["stop"]["stop_logged"] = True + # Give time for any queued items to be cleared + if not test3_complete.done(): + loop.call_later( + 0.3, + lambda: test3_complete.set_result(True) + if not test3_complete.done() + else None, + ) + + # Test 4: Rejection + if match := reject_start.search(line): + item = int(match.group(1)) + if item not in test_results["rejection"]["processed"]: + test_results["rejection"]["processed"].append(item) + + if match := reject_end.search(line): + item = int(match.group(1)) + if item == 3 and not test4_complete.done(): + test4_complete.set_result(True) + + if reject_reject.search(line): + test_results["rejection"]["rejections"] += 1 + + # Test 5: No params + if no_params_end.search(line): + test_results["no_params"]["executions"] += 1 + if ( + test_results["no_params"]["executions"] == 3 + and not test5_complete.done() + ): + test5_complete.set_result(True) + + async with ( + run_compiled(yaml_config, line_callback=check_output), + api_client_connected() as client, + ): + # Get 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) + assert test_service is not None, "test_queue_depth service not found" + client.execute_service(test_service, {}) + await asyncio.wait_for(test1_complete, timeout=2.0) + await asyncio.sleep(0.1) # Give time for rejections + + # Verify Test 1 + assert sorted(test_results["queue_depth"]["processed"]) == [1, 2, 3, 4, 5], ( + f"Test 1: Expected to process items 1-5 (max_runs=5 means 5 total), got {sorted(test_results['queue_depth']['processed'])}" + ) + assert test_results["queue_depth"]["rejections"] >= 2, ( + "Test 1: Expected at least 2 rejection warnings (items 6-7 should be rejected)" + ) + + # Test 2: Ring buffer order + test_service = next((s for s in services if s.name == "test_ring_buffer"), None) + assert test_service is not None, "test_ring_buffer service not found" + client.execute_service(test_service, {}) + await asyncio.wait_for(test2_complete, timeout=2.0) + + # Verify Test 2 + assert test_results["ring_buffer"]["start_order"] == ["A", "B", "C"], ( + f"Test 2: Expected start order [A, B, C], got {test_results['ring_buffer']['start_order']}" + ) + assert test_results["ring_buffer"]["end_order"] == ["A", "B", "C"], ( + f"Test 2: Expected end order [A, B, C], got {test_results['ring_buffer']['end_order']}" + ) + + # Test 3: Stop clears queue + test_service = next((s for s in services if s.name == "test_stop_clears"), None) + assert test_service is not None, "test_stop_clears service not found" + client.execute_service(test_service, {}) + await asyncio.wait_for(test3_complete, timeout=2.0) + + # Verify Test 3 + assert test_results["stop"]["stop_logged"], ( + "Test 3: Stop command was not logged" + ) + assert test_results["stop"]["processed"] == [1], ( + f"Test 3: Expected only item 1 to process, got {test_results['stop']['processed']}" + ) + + # Test 4: Rejection enforcement (max_runs=3) + test_service = next((s for s in services if s.name == "test_rejection"), None) + assert test_service is not None, "test_rejection service not found" + client.execute_service(test_service, {}) + await asyncio.wait_for(test4_complete, timeout=2.0) + await asyncio.sleep(0.1) # Give time for rejections + + # Verify Test 4 + assert sorted(test_results["rejection"]["processed"]) == [1, 2, 3], ( + f"Test 4: Expected to process items 1-3 (max_runs=3 means 3 total), got {sorted(test_results['rejection']['processed'])}" + ) + assert test_results["rejection"]["rejections"] == 5, ( + f"Test 4: Expected 5 rejections (items 4-8), got {test_results['rejection']['rejections']}" + ) + + # Test 5: No parameters + test_service = next((s for s in services if s.name == "test_no_params"), None) + assert test_service is not None, "test_no_params service not found" + client.execute_service(test_service, {}) + await asyncio.wait_for(test5_complete, timeout=2.0) + + # Verify Test 5 + assert test_results["no_params"]["executions"] == 3, ( + f"Test 5: Expected 3 executions, got {test_results['no_params']['executions']}" + ) From b0ea3f57de61f7761f1bdca274b631be4d369500 Mon Sep 17 00:00:00 2001 From: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> Date: Sun, 19 Oct 2025 15:49:05 -0400 Subject: [PATCH 2/9] [esp32] Fix OTA rollback (#11300) Co-authored-by: J. Nick Koston --- esphome/components/esp32/core.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/esphome/components/esp32/core.cpp b/esphome/components/esp32/core.cpp index f3bdfea2a0..3427c96e70 100644 --- a/esphome/components/esp32/core.cpp +++ b/esphome/components/esp32/core.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,16 @@ void arch_init() { disableCore1WDT(); #endif #endif + + // If the bootloader was compiled with CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE the current + // partition will get rolled back unless it is marked as valid. + esp_ota_img_states_t state; + const esp_partition_t *running = esp_ota_get_running_partition(); + if (esp_ota_get_state_partition(running, &state) == ESP_OK) { + if (state == ESP_OTA_IMG_PENDING_VERIFY) { + esp_ota_mark_app_valid_cancel_rollback(); + } + } } void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); } From 3bdd351d49b399712f701790632968f2931aecb4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Oct 2025 09:52:33 -1000 Subject: [PATCH 3/9] [wifi] Convert fast_connect to compile-time define, save 156-1024 bytes flash (#11328) --- esphome/components/wifi/__init__.py | 3 +- esphome/components/wifi/wifi_component.cpp | 88 ++++++++++++---------- esphome/components/wifi/wifi_component.h | 10 ++- esphome/core/defines.h | 1 + tests/components/wifi/common-eap.yaml | 1 + 5 files changed, 60 insertions(+), 43 deletions(-) diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index 1f742dc1a8..494470cb48 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -407,7 +407,8 @@ async def to_code(config): cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) cg.add(var.set_power_save_mode(config[CONF_POWER_SAVE_MODE])) - cg.add(var.set_fast_connect(config[CONF_FAST_CONNECT])) + if config[CONF_FAST_CONNECT]: + cg.add_define("USE_WIFI_FAST_CONNECT") cg.add(var.set_passive_scan(config[CONF_PASSIVE_SCAN])) if CONF_OUTPUT_POWER in config: cg.add(var.set_output_power(config[CONF_OUTPUT_POWER])) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 612b11a50f..c89384d742 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -84,9 +84,9 @@ void WiFiComponent::start() { uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL; this->pref_ = global_preferences->make_preference(hash, true); - if (this->fast_connect_) { - this->fast_connect_pref_ = global_preferences->make_preference(hash + 1, false); - } +#ifdef USE_WIFI_FAST_CONNECT + this->fast_connect_pref_ = global_preferences->make_preference(hash + 1, false); +#endif SavedWifiSettings save{}; if (this->pref_.load(&save)) { @@ -108,16 +108,16 @@ void WiFiComponent::start() { ESP_LOGV(TAG, "Setting Power Save Option failed"); } - if (this->fast_connect_) { - this->trying_loaded_ap_ = this->load_fast_connect_settings_(); - if (!this->trying_loaded_ap_) { - this->ap_index_ = 0; - this->selected_ap_ = this->sta_[this->ap_index_]; - } - this->start_connecting(this->selected_ap_, false); - } else { - this->start_scanning(); +#ifdef USE_WIFI_FAST_CONNECT + this->trying_loaded_ap_ = this->load_fast_connect_settings_(); + if (!this->trying_loaded_ap_) { + this->ap_index_ = 0; + this->selected_ap_ = this->sta_[this->ap_index_]; } + this->start_connecting(this->selected_ap_, false); +#else + this->start_scanning(); +#endif #ifdef USE_WIFI_AP } else if (this->has_ap()) { this->setup_ap_config_(); @@ -168,13 +168,20 @@ void WiFiComponent::loop() { case WIFI_COMPONENT_STATE_COOLDOWN: { this->status_set_warning(LOG_STR("waiting to reconnect")); if (millis() - this->action_started_ > 5000) { - if (this->fast_connect_ || this->retry_hidden_) { +#ifdef USE_WIFI_FAST_CONNECT + // NOTE: This check may not make sense here as it could interfere with AP cycling + if (!this->selected_ap_.get_bssid().has_value()) + this->selected_ap_ = this->sta_[0]; + this->start_connecting(this->selected_ap_, false); +#else + if (this->retry_hidden_) { if (!this->selected_ap_.get_bssid().has_value()) this->selected_ap_ = this->sta_[0]; this->start_connecting(this->selected_ap_, false); } else { this->start_scanning(); } +#endif } break; } @@ -244,7 +251,6 @@ WiFiComponent::WiFiComponent() { global_wifi_component = this; } bool WiFiComponent::has_ap() const { return this->has_ap_; } bool WiFiComponent::has_sta() const { return !this->sta_.empty(); } -void WiFiComponent::set_fast_connect(bool fast_connect) { this->fast_connect_ = fast_connect; } #ifdef USE_WIFI_11KV_SUPPORT void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; } void WiFiComponent::set_rrm(bool rrm) { this->rrm_ = rrm; } @@ -723,9 +729,9 @@ void WiFiComponent::check_connecting_finished() { this->scan_result_.shrink_to_fit(); } - if (this->fast_connect_) { - this->save_fast_connect_settings_(); - } +#ifdef USE_WIFI_FAST_CONNECT + this->save_fast_connect_settings_(); +#endif return; } @@ -773,31 +779,31 @@ void WiFiComponent::retry_connect() { delay(10); if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() && (this->num_retried_ > 3 || this->error_from_callback_)) { - if (this->fast_connect_) { - if (this->trying_loaded_ap_) { - this->trying_loaded_ap_ = false; - this->ap_index_ = 0; // Retry from the first configured AP - } else if (this->ap_index_ >= this->sta_.size() - 1) { - ESP_LOGW(TAG, "No more APs to try"); - this->ap_index_ = 0; - this->restart_adapter(); - } else { - // Try next AP - this->ap_index_++; - } - this->num_retried_ = 0; - this->selected_ap_ = this->sta_[this->ap_index_]; +#ifdef USE_WIFI_FAST_CONNECT + if (this->trying_loaded_ap_) { + this->trying_loaded_ap_ = false; + this->ap_index_ = 0; // Retry from the first configured AP + } else if (this->ap_index_ >= this->sta_.size() - 1) { + ESP_LOGW(TAG, "No more APs to try"); + this->ap_index_ = 0; + this->restart_adapter(); } else { - if (this->num_retried_ > 5) { - // If retry failed for more than 5 times, let's restart STA - this->restart_adapter(); - } else { - // Try hidden networks after 3 failed retries - ESP_LOGD(TAG, "Retrying with hidden networks"); - this->retry_hidden_ = true; - this->num_retried_++; - } + // Try next AP + this->ap_index_++; } + this->num_retried_ = 0; + this->selected_ap_ = this->sta_[this->ap_index_]; +#else + if (this->num_retried_ > 5) { + // If retry failed for more than 5 times, let's restart STA + this->restart_adapter(); + } else { + // Try hidden networks after 3 failed retries + ESP_LOGD(TAG, "Retrying with hidden networks"); + this->retry_hidden_ = true; + this->num_retried_++; + } +#endif } else { this->num_retried_++; } @@ -843,6 +849,7 @@ bool WiFiComponent::is_esp32_improv_active_() { #endif } +#ifdef USE_WIFI_FAST_CONNECT bool WiFiComponent::load_fast_connect_settings_() { SavedWifiFastConnectSettings fast_connect_save{}; @@ -877,6 +884,7 @@ void WiFiComponent::save_fast_connect_settings_() { ESP_LOGD(TAG, "Saved fast_connect settings"); } } +#endif void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; } void WiFiAP::set_bssid(bssid_t bssid) { this->bssid_ = bssid; } diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 508024a235..10aa82a065 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -240,7 +240,6 @@ class WiFiComponent : public Component { void start_scanning(); void check_scanning_finished(); void start_connecting(const WiFiAP &ap, bool two); - void set_fast_connect(bool fast_connect); void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; } void check_connecting_finished(); @@ -364,8 +363,10 @@ class WiFiComponent : public Component { bool is_captive_portal_active_(); bool is_esp32_improv_active_(); +#ifdef USE_WIFI_FAST_CONNECT bool load_fast_connect_settings_(); void save_fast_connect_settings_(); +#endif #ifdef USE_ESP8266 static void wifi_event_callback(System_Event_t *event); @@ -399,7 +400,9 @@ class WiFiComponent : public Component { WiFiAP ap_; optional output_power_; ESPPreferenceObject pref_; +#ifdef USE_WIFI_FAST_CONNECT ESPPreferenceObject fast_connect_pref_; +#endif // Group all 32-bit integers together uint32_t action_started_; @@ -411,14 +414,17 @@ class WiFiComponent : public Component { WiFiComponentState state_{WIFI_COMPONENT_STATE_OFF}; WiFiPowerSaveMode power_save_{WIFI_POWER_SAVE_NONE}; uint8_t num_retried_{0}; +#ifdef USE_WIFI_FAST_CONNECT uint8_t ap_index_{0}; +#endif #if USE_NETWORK_IPV6 uint8_t num_ipv6_addresses_{0}; #endif /* USE_NETWORK_IPV6 */ // Group all boolean values together - bool fast_connect_{false}; +#ifdef USE_WIFI_FAST_CONNECT bool trying_loaded_ap_{false}; +#endif bool retry_hidden_{false}; bool has_ap_{false}; bool handled_connected_state_{false}; diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 1afb296fc0..b1bd7f92d7 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -199,6 +199,7 @@ #define USE_WEBSERVER_PORT 80 // NOLINT #define USE_WEBSERVER_SORTING #define USE_WIFI_11KV_SUPPORT +#define USE_WIFI_FAST_CONNECT #define USB_HOST_MAX_REQUESTS 16 #ifdef USE_ARDUINO diff --git a/tests/components/wifi/common-eap.yaml b/tests/components/wifi/common-eap.yaml index 779cd6b49a..52319fa5a1 100644 --- a/tests/components/wifi/common-eap.yaml +++ b/tests/components/wifi/common-eap.yaml @@ -1,4 +1,5 @@ wifi: + fast_connect: true networks: - ssid: MySSID eap: From 70cb1793f38111e3ce81c492c9708f5928f37409 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Oct 2025 09:53:05 -1000 Subject: [PATCH 4/9] [wifi] Optimize WiFi scan results with in-place construction (#11330) --- esphome/components/wifi/wifi_component_esp8266.cpp | 8 ++++---- esphome/components/wifi/wifi_component_esp_idf.cpp | 4 ++-- esphome/components/wifi/wifi_component_libretiny.cpp | 6 +++--- esphome/core/helpers.h | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index 59909b2cb5..4e17c42f41 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -706,10 +706,10 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) { this->scan_result_.init(count); for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) { - WiFiScanResult res({it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, - std::string(reinterpret_cast(it->ssid), it->ssid_len), it->channel, it->rssi, - it->authmode != AUTH_OPEN, it->is_hidden != 0); - this->scan_result_.push_back(res); + this->scan_result_.emplace_back( + bssid_t{it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, + std::string(reinterpret_cast(it->ssid), it->ssid_len), it->channel, it->rssi, it->authmode != AUTH_OPEN, + it->is_hidden != 0); } this->scan_done_ = true; } diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index ce1cc961d0..08ecba3598 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -789,8 +789,8 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) { bssid_t bssid; std::copy(record.bssid, record.bssid + 6, bssid.begin()); std::string ssid(reinterpret_cast(record.ssid)); - WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty()); - scan_result_.push_back(result); + scan_result_.emplace_back(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, + ssid.empty()); } } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) { diff --git a/esphome/components/wifi/wifi_component_libretiny.cpp b/esphome/components/wifi/wifi_component_libretiny.cpp index cb179d9022..45e2fba82a 100644 --- a/esphome/components/wifi/wifi_component_libretiny.cpp +++ b/esphome/components/wifi/wifi_component_libretiny.cpp @@ -419,9 +419,9 @@ void WiFiComponent::wifi_scan_done_callback_() { uint8_t *bssid = WiFi.BSSID(i); int32_t channel = WiFi.channel(i); - WiFiScanResult scan({bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]}, std::string(ssid.c_str()), - channel, rssi, authmode != WIFI_AUTH_OPEN, ssid.length() == 0); - this->scan_result_.push_back(scan); + this->scan_result_.emplace_back(bssid_t{bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]}, + std::string(ssid.c_str()), channel, rssi, authmode != WIFI_AUTH_OPEN, + ssid.length() == 0); } WiFi.scanDelete(); this->scan_done_ = true; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 326718e974..37a64d46b2 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -281,13 +281,13 @@ template class FixedVector { } } - /// Emplace element without bounds checking - constructs in-place + /// Emplace element without bounds checking - constructs in-place with arguments /// Caller must ensure sufficient capacity was allocated via init() /// Returns reference to the newly constructed element /// NOTE: Caller MUST ensure size_ < capacity_ before calling - T &emplace_back() { - // Use placement new to default-construct the object in pre-allocated memory - new (&data_[size_]) T(); + template T &emplace_back(Args &&...args) { + // Use placement new to construct the object in pre-allocated memory + new (&data_[size_]) T(std::forward(args)...); size_++; return data_[size_ - 1]; } From 1a2057df3011a6e5de32ffb36b7efc62f451a9cd Mon Sep 17 00:00:00 2001 From: Juan Antonio Aldea Date: Sun, 19 Oct 2025 23:15:17 +0200 Subject: [PATCH 5/9] Migrate from hexencode() to format_hex_pretty() in Kuntze component (#11372) --- esphome/components/kuntze/kuntze.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/kuntze/kuntze.cpp b/esphome/components/kuntze/kuntze.cpp index 42545d9d54..30f98aaa99 100644 --- a/esphome/components/kuntze/kuntze.cpp +++ b/esphome/components/kuntze/kuntze.cpp @@ -14,7 +14,7 @@ void Kuntze::on_modbus_data(const std::vector &data) { auto get_16bit = [&](int i) -> uint16_t { return (uint16_t(data[i * 2]) << 8) | uint16_t(data[i * 2 + 1]); }; this->waiting_ = false; - ESP_LOGV(TAG, "Data: %s", hexencode(data).c_str()); + ESP_LOGV(TAG, "Data: %s", format_hex_pretty(data).c_str()); float value = (float) get_16bit(0); for (int i = 0; i < data[3]; i++) From 1e1fefbd0a865fe2858441ab9d35d73b71347a89 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Sun, 19 Oct 2025 23:31:25 +0200 Subject: [PATCH 6/9] [substitutions] !extend and !remove now support substitutions and jinja (#11203) --- esphome/config.py | 109 ++++++++++++++---- esphome/config_helpers.py | 86 ++++---------- esphome/config_validation.py | 7 -- .../component_tests/packages/test_packages.py | 71 +++++------- .../05-extend-remove.approved.yaml | 9 ++ .../substitutions/05-extend-remove.input.yaml | 22 ++++ tests/unit_tests/test_substitutions.py | 3 + 7 files changed, 171 insertions(+), 136 deletions(-) create mode 100644 tests/unit_tests/fixtures/substitutions/05-extend-remove.approved.yaml create mode 100644 tests/unit_tests/fixtures/substitutions/05-extend-remove.input.yaml diff --git a/esphome/config.py b/esphome/config.py index 6adecb5c65..634dba8dad 100644 --- a/esphome/config.py +++ b/esphome/config.py @@ -12,7 +12,7 @@ from typing import Any import voluptuous as vol from esphome import core, loader, pins, yaml_util -from esphome.config_helpers import Extend, Remove, merge_dicts_ordered +from esphome.config_helpers import Extend, Remove, merge_config, merge_dicts_ordered import esphome.config_validation as cv from esphome.const import ( CONF_ESPHOME, @@ -324,13 +324,7 @@ def iter_ids(config, path=None): yield from iter_ids(value, path + [key]) -def recursive_check_replaceme(value): - if isinstance(value, list): - return cv.Schema([recursive_check_replaceme])(value) - if isinstance(value, dict): - return cv.Schema({cv.valid: recursive_check_replaceme})(value) - if isinstance(value, ESPLiteralValue): - pass +def check_replaceme(value): if isinstance(value, str) and value == "REPLACEME": raise cv.Invalid( "Found 'REPLACEME' in configuration, this is most likely an error. " @@ -339,7 +333,86 @@ def recursive_check_replaceme(value): "If you want to use the literal REPLACEME string, " 'please use "!literal REPLACEME"' ) - return value + + +def _build_list_index(lst): + index = OrderedDict() + extensions, removals = [], set() + for item in lst: + if item is None: + removals.add(None) + continue + item_id = None + if isinstance(item, dict) and (item_id := item.get(CONF_ID)): + if isinstance(item_id, Extend): + extensions.append(item) + continue + if isinstance(item_id, Remove): + removals.add(item_id.value) + continue + if not item_id or item_id in index: + # no id or duplicate -> pass through with identity-based key + item_id = id(item) + index[item_id] = item + return index, extensions, removals + + +def resolve_extend_remove(value, is_key=None): + if isinstance(value, ESPLiteralValue): + return # do not check inside literal blocks + if isinstance(value, list): + index, extensions, removals = _build_list_index(value) + if extensions or removals: + # Rebuild the original list after + # processing all extensions and removals + for item in extensions: + item_id = item[CONF_ID].value + if item_id in removals: + continue + old = index.get(item_id) + if old is None: + # Failed to find source for extension + # Find index of item to show error at correct position + i = next( + ( + i + for i, d in enumerate(value) + if d.get(CONF_ID) == item[CONF_ID] + ) + ) + with cv.prepend_path(i): + raise cv.Invalid( + f"Source for extension of ID '{item_id}' was not found." + ) + item[CONF_ID] = item_id + index[item_id] = merge_config(old, item) + for item_id in removals: + index.pop(item_id, None) + + value[:] = index.values() + + for i, item in enumerate(value): + with cv.prepend_path(i): + resolve_extend_remove(item, False) + return + if isinstance(value, dict): + removals = [] + for k, v in value.items(): + with cv.prepend_path(k): + if isinstance(v, Remove): + removals.append(k) + continue + resolve_extend_remove(k, True) + resolve_extend_remove(v, False) + for k in removals: + value.pop(k, None) + return + if is_key: + return # do not check keys (yet) + + check_replaceme(value) + + return class ConfigValidationStep(abc.ABC): @@ -437,19 +510,6 @@ class LoadValidationStep(ConfigValidationStep): continue p_name = p_config.get("platform") if p_name is None: - p_id = p_config.get(CONF_ID) - if isinstance(p_id, Extend): - result.add_str_error( - f"Source for extension of ID '{p_id.value}' was not found.", - path + [CONF_ID], - ) - continue - if isinstance(p_id, Remove): - result.add_str_error( - f"Source for removal of ID '{p_id.value}' was not found.", - path + [CONF_ID], - ) - continue result.add_str_error( f"'{self.domain}' requires a 'platform' key but it was not specified.", path, @@ -934,9 +994,10 @@ def validate_config( CORE.raw_config = config - # 1.1. Check for REPLACEME special value + # 1.1. Resolve !extend and !remove and check for REPLACEME + # After this step, there will not be any Extend or Remove values in the config anymore try: - recursive_check_replaceme(config) + resolve_extend_remove(config) except vol.Invalid as err: result.add_error(err) diff --git a/esphome/config_helpers.py b/esphome/config_helpers.py index 88cfa49fdc..c0a3b99968 100644 --- a/esphome/config_helpers.py +++ b/esphome/config_helpers.py @@ -1,7 +1,6 @@ from collections.abc import Callable from esphome.const import ( - CONF_ID, CONF_LEVEL, CONF_LOGGER, KEY_CORE, @@ -75,73 +74,28 @@ class Remove: return isinstance(b, Remove) and self.value == b.value -def merge_config(full_old, full_new): - def merge(old, new): - if isinstance(new, dict): - if not isinstance(old, dict): - return new - # Preserve OrderedDict type by copying to OrderedDict if either input is OrderedDict - if isinstance(old, OrderedDict) or isinstance(new, OrderedDict): - res = OrderedDict(old) - else: - res = old.copy() - for k, v in new.items(): - if isinstance(v, Remove) and k in old: - del res[k] - else: - res[k] = merge(old[k], v) if k in old else v - return res - if isinstance(new, list): - if not isinstance(old, list): - return new - res = old.copy() - ids = { - v_id: i - for i, v in enumerate(res) - if isinstance(v, dict) - and (v_id := v.get(CONF_ID)) - and isinstance(v_id, str) - } - extend_ids = { - v_id.value: i - for i, v in enumerate(res) - if isinstance(v, dict) - and (v_id := v.get(CONF_ID)) - and isinstance(v_id, Extend) - } - - ids_to_delete = [] - for v in new: - if isinstance(v, dict) and (new_id := v.get(CONF_ID)): - if isinstance(new_id, Extend): - new_id = new_id.value - if new_id in ids: - v[CONF_ID] = new_id - res[ids[new_id]] = merge(res[ids[new_id]], v) - continue - elif isinstance(new_id, Remove): - new_id = new_id.value - if new_id in ids: - ids_to_delete.append(ids[new_id]) - continue - elif ( - new_id in extend_ids - ): # When a package is extending a non-packaged item - extend_res = res[extend_ids[new_id]] - extend_res[CONF_ID] = new_id - new_v = merge(v, extend_res) - res[extend_ids[new_id]] = new_v - continue - else: - ids[new_id] = len(res) - res.append(v) - return [v for i, v in enumerate(res) if i not in ids_to_delete] - if new is None: - return old - +def merge_config(old, new): + if isinstance(new, Remove): return new + if isinstance(new, dict): + if not isinstance(old, dict): + return new + # Preserve OrderedDict type by copying to OrderedDict if either input is OrderedDict + if isinstance(old, OrderedDict) or isinstance(new, OrderedDict): + res = OrderedDict(old) + else: + res = old.copy() + for k, v in new.items(): + res[k] = merge_config(old.get(k), v) + return res + if isinstance(new, list): + if not isinstance(old, list): + return new + return old + new + if new is None: + return old - return merge(full_old, full_new) + return new def filter_source_files_from_platform( diff --git a/esphome/config_validation.py b/esphome/config_validation.py index e2f0b835c9..c613a984c4 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -24,7 +24,6 @@ import voluptuous as vol from esphome import core import esphome.codegen as cg -from esphome.config_helpers import Extend, Remove from esphome.const import ( ALLOWED_NAME_CHARS, CONF_AVAILABILITY, @@ -624,12 +623,6 @@ def declare_id(type): if value is None: return core.ID(None, is_declaration=True, type=type) - if isinstance(value, Extend): - raise Invalid(f"Source for extension of ID '{value.value}' was not found.") - - if isinstance(value, Remove): - raise Invalid(f"Source for Removal of ID '{value.value}' was not found.") - return core.ID(validate_id_name(value), is_declaration=True, type=type) return validator diff --git a/tests/component_tests/packages/test_packages.py b/tests/component_tests/packages/test_packages.py index 4712daad0d..d66ca58a69 100644 --- a/tests/component_tests/packages/test_packages.py +++ b/tests/component_tests/packages/test_packages.py @@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch import pytest from esphome.components.packages import do_packages_pass +from esphome.config import resolve_extend_remove from esphome.config_helpers import Extend, Remove import esphome.config_validation as cv from esphome.const import ( @@ -64,13 +65,20 @@ def fixture_basic_esphome(): return {CONF_NAME: TEST_DEVICE_NAME, CONF_PLATFORM: TEST_PLATFORM} +def packages_pass(config): + """Wrapper around packages_pass that also resolves Extend and Remove.""" + config = do_packages_pass(config) + resolve_extend_remove(config) + return config + + def test_package_unused(basic_esphome, basic_wifi): """ Ensures do_package_pass does not change a config if packages aren't used. """ config = {CONF_ESPHOME: basic_esphome, CONF_WIFI: basic_wifi} - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == config @@ -83,7 +91,7 @@ def test_package_invalid_dict(basic_esphome, basic_wifi): config = {CONF_ESPHOME: basic_esphome, CONF_PACKAGES: basic_wifi | {CONF_URL: ""}} with pytest.raises(cv.Invalid): - do_packages_pass(config) + packages_pass(config) def test_package_include(basic_wifi, basic_esphome): @@ -99,7 +107,7 @@ def test_package_include(basic_wifi, basic_esphome): expected = {CONF_ESPHOME: basic_esphome, CONF_WIFI: basic_wifi} - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -124,7 +132,7 @@ def test_package_append(basic_wifi, basic_esphome): }, } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -148,7 +156,7 @@ def test_package_override(basic_wifi, basic_esphome): }, } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -177,7 +185,7 @@ def test_multiple_package_order(): }, } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -233,7 +241,7 @@ def test_package_list_merge(): ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -311,7 +319,7 @@ def test_package_list_merge_by_id(): ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -350,13 +358,13 @@ def test_package_merge_by_id_with_list(): ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected def test_package_merge_by_missing_id(): """ - Ensures that components with missing IDs are not merged. + Ensures that a validation error is thrown when trying to extend a missing ID. """ config = { @@ -379,25 +387,15 @@ def test_package_merge_by_missing_id(): ], } - expected = { - CONF_SENSOR: [ - { - CONF_ID: TEST_SENSOR_ID_1, - CONF_FILTERS: [{CONF_MULTIPLY: 42.0}], - }, - { - CONF_ID: TEST_SENSOR_ID_1, - CONF_FILTERS: [{CONF_MULTIPLY: 10.0}], - }, - { - CONF_ID: Extend(TEST_SENSOR_ID_2), - CONF_FILTERS: [{CONF_OFFSET: 146.0}], - }, - ] - } + error_raised = False + try: + packages_pass(config) + assert False, "Expected validation error for missing ID" + except cv.Invalid as err: + error_raised = True + assert err.path == [CONF_SENSOR, 2] - actual = do_packages_pass(config) - assert actual == expected + assert error_raised def test_package_list_remove_by_id(): @@ -447,7 +445,7 @@ def test_package_list_remove_by_id(): ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -493,7 +491,7 @@ def test_multiple_package_list_remove_by_id(): ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -514,7 +512,7 @@ def test_package_dict_remove_by_id(basic_wifi, basic_esphome): CONF_ESPHOME: basic_esphome, } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -545,7 +543,6 @@ def test_package_remove_by_missing_id(): } expected = { - "missing_key": Remove(), CONF_SENSOR: [ { CONF_ID: TEST_SENSOR_ID_1, @@ -555,14 +552,10 @@ def test_package_remove_by_missing_id(): CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 10.0}], }, - { - CONF_ID: Remove(TEST_SENSOR_ID_2), - CONF_FILTERS: [{CONF_OFFSET: 146.0}], - }, ], } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -634,7 +627,7 @@ def test_remote_packages_with_files_list( ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected @@ -730,5 +723,5 @@ def test_remote_packages_with_files_and_vars( ] } - actual = do_packages_pass(config) + actual = packages_pass(config) assert actual == expected diff --git a/tests/unit_tests/fixtures/substitutions/05-extend-remove.approved.yaml b/tests/unit_tests/fixtures/substitutions/05-extend-remove.approved.yaml new file mode 100644 index 0000000000..a479370f4b --- /dev/null +++ b/tests/unit_tests/fixtures/substitutions/05-extend-remove.approved.yaml @@ -0,0 +1,9 @@ +substitutions: + A: component1 + B: component2 + C: component3 +some_component: + - id: component1 + value: 2 + - id: component2 + value: 5 diff --git a/tests/unit_tests/fixtures/substitutions/05-extend-remove.input.yaml b/tests/unit_tests/fixtures/substitutions/05-extend-remove.input.yaml new file mode 100644 index 0000000000..2e0e60798d --- /dev/null +++ b/tests/unit_tests/fixtures/substitutions/05-extend-remove.input.yaml @@ -0,0 +1,22 @@ +substitutions: + A: component1 + B: component2 + C: component3 + +packages: + - some_component: + - id: component1 + value: 1 + - id: !extend ${B} + value: 4 + - id: !extend ${B} + value: 5 + - id: component3 + value: 6 + +some_component: + - id: !extend ${A} + value: 2 + - id: component2 + value: 3 + - id: !remove ${C} diff --git a/tests/unit_tests/test_substitutions.py b/tests/unit_tests/test_substitutions.py index 59396a4a83..beb1ebc73e 100644 --- a/tests/unit_tests/test_substitutions.py +++ b/tests/unit_tests/test_substitutions.py @@ -4,6 +4,7 @@ from pathlib import Path from esphome import config as config_module, yaml_util from esphome.components import substitutions +from esphome.config import resolve_extend_remove from esphome.config_helpers import merge_config from esphome.const import CONF_PACKAGES, CONF_SUBSTITUTIONS from esphome.core import CORE @@ -81,6 +82,8 @@ def test_substitutions_fixtures(fixture_path): substitutions.do_substitution_pass(config, None) + resolve_extend_remove(config) + # Also load expected using ESPHome's loader, or use {} if missing and DEV_MODE if expected_path.is_file(): expected = yaml_util.load_yaml(expected_path) From afbd3f77af222968161f7bc923b392e29fe00879 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 20 Oct 2025 11:08:30 +1300 Subject: [PATCH 7/9] [light] Clean up deprecated functions from 1.21 (#11389) --- esphome/components/light/addressable_light.h | 8 +++---- esphome/components/light/light_traits.h | 22 +------------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/esphome/components/light/addressable_light.h b/esphome/components/light/addressable_light.h index baa4507d2f..3e94a39745 100644 --- a/esphome/components/light/addressable_light.h +++ b/esphome/components/light/addressable_light.h @@ -1,11 +1,11 @@ #pragma once -#include "esphome/core/component.h" -#include "esphome/core/defines.h" -#include "esphome/core/color.h" #include "esp_color_correction.h" #include "esp_color_view.h" #include "esp_range_view.h" +#include "esphome/core/color.h" +#include "esphome/core/component.h" +#include "esphome/core/defines.h" #include "light_output.h" #include "light_state.h" #include "transformers.h" @@ -17,8 +17,6 @@ namespace esphome { namespace light { -using ESPColor ESPDEPRECATED("esphome::light::ESPColor is deprecated, use esphome::Color instead.", "v1.21") = Color; - /// Convert the color information from a `LightColorValues` object to a `Color` object (does not apply brightness). Color color_from_light_color_values(LightColorValues val); diff --git a/esphome/components/light/light_traits.h b/esphome/components/light/light_traits.h index c83d8ad2a9..4532edca83 100644 --- a/esphome/components/light/light_traits.h +++ b/esphome/components/light/light_traits.h @@ -1,7 +1,7 @@ #pragma once -#include "esphome/core/helpers.h" #include "color_mode.h" +#include "esphome/core/helpers.h" namespace esphome { @@ -31,26 +31,6 @@ class LightTraits { return this->supported_color_modes_.has_capability(color_capability); } - ESPDEPRECATED("get_supports_brightness() is deprecated, use color modes instead.", "v1.21") - bool get_supports_brightness() const { return this->supports_color_capability(ColorCapability::BRIGHTNESS); } - ESPDEPRECATED("get_supports_rgb() is deprecated, use color modes instead.", "v1.21") - bool get_supports_rgb() const { return this->supports_color_capability(ColorCapability::RGB); } - ESPDEPRECATED("get_supports_rgb_white_value() is deprecated, use color modes instead.", "v1.21") - bool get_supports_rgb_white_value() const { - return this->supports_color_mode(ColorMode::RGB_WHITE) || - this->supports_color_mode(ColorMode::RGB_COLOR_TEMPERATURE); - } - ESPDEPRECATED("get_supports_color_temperature() is deprecated, use color modes instead.", "v1.21") - bool get_supports_color_temperature() const { - return this->supports_color_capability(ColorCapability::COLOR_TEMPERATURE); - } - ESPDEPRECATED("get_supports_color_interlock() is deprecated, use color modes instead.", "v1.21") - bool get_supports_color_interlock() const { - return this->supports_color_mode(ColorMode::RGB) && - (this->supports_color_mode(ColorMode::WHITE) || this->supports_color_mode(ColorMode::COLD_WARM_WHITE) || - this->supports_color_mode(ColorMode::COLOR_TEMPERATURE)); - } - float get_min_mireds() const { return this->min_mireds_; } void set_min_mireds(float min_mireds) { this->min_mireds_ = min_mireds; } float get_max_mireds() const { return this->max_mireds_; } From 9c146a70708899fc01522849bf8e018919c8f841 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 20 Oct 2025 11:11:35 +1300 Subject: [PATCH 8/9] [climate] Clean up deprecated functions from 1.20 (#11388) --- esphome/components/climate/climate_traits.h | 46 +-------------------- 1 file changed, 2 insertions(+), 44 deletions(-) diff --git a/esphome/components/climate/climate_traits.h b/esphome/components/climate/climate_traits.h index 50c1e79ad2..2962a147d7 100644 --- a/esphome/components/climate/climate_traits.h +++ b/esphome/components/climate/climate_traits.h @@ -1,8 +1,8 @@ #pragma once -#include "esphome/core/helpers.h" -#include "climate_mode.h" #include +#include "climate_mode.h" +#include "esphome/core/helpers.h" namespace esphome { @@ -109,44 +109,12 @@ class ClimateTraits { void set_supported_modes(std::set modes) { this->supported_modes_ = std::move(modes); } void add_supported_mode(ClimateMode mode) { this->supported_modes_.insert(mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_auto_mode(bool supports_auto_mode) { set_mode_support_(CLIMATE_MODE_AUTO, supports_auto_mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_cool_mode(bool supports_cool_mode) { set_mode_support_(CLIMATE_MODE_COOL, supports_cool_mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_heat_mode(bool supports_heat_mode) { set_mode_support_(CLIMATE_MODE_HEAT, supports_heat_mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_heat_cool_mode(bool supported) { set_mode_support_(CLIMATE_MODE_HEAT_COOL, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_fan_only_mode(bool supports_fan_only_mode) { - set_mode_support_(CLIMATE_MODE_FAN_ONLY, supports_fan_only_mode); - } - ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20") - void set_supports_dry_mode(bool supports_dry_mode) { set_mode_support_(CLIMATE_MODE_DRY, supports_dry_mode); } bool supports_mode(ClimateMode mode) const { return this->supported_modes_.count(mode); } const std::set &get_supported_modes() const { return this->supported_modes_; } void set_supported_fan_modes(std::set modes) { this->supported_fan_modes_ = std::move(modes); } void add_supported_fan_mode(ClimateFanMode mode) { this->supported_fan_modes_.insert(mode); } void add_supported_custom_fan_mode(const std::string &mode) { this->supported_custom_fan_modes_.insert(mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_on(bool supported) { set_fan_mode_support_(CLIMATE_FAN_ON, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_off(bool supported) { set_fan_mode_support_(CLIMATE_FAN_OFF, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_auto(bool supported) { set_fan_mode_support_(CLIMATE_FAN_AUTO, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_low(bool supported) { set_fan_mode_support_(CLIMATE_FAN_LOW, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_medium(bool supported) { set_fan_mode_support_(CLIMATE_FAN_MEDIUM, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_high(bool supported) { set_fan_mode_support_(CLIMATE_FAN_HIGH, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_middle(bool supported) { set_fan_mode_support_(CLIMATE_FAN_MIDDLE, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_focus(bool supported) { set_fan_mode_support_(CLIMATE_FAN_FOCUS, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead", "v1.20") - void set_supports_fan_mode_diffuse(bool supported) { set_fan_mode_support_(CLIMATE_FAN_DIFFUSE, supported); } bool supports_fan_mode(ClimateFanMode fan_mode) const { return this->supported_fan_modes_.count(fan_mode); } bool get_supports_fan_modes() const { return !this->supported_fan_modes_.empty() || !this->supported_custom_fan_modes_.empty(); @@ -178,16 +146,6 @@ class ClimateTraits { void set_supported_swing_modes(std::set modes) { this->supported_swing_modes_ = std::move(modes); } void add_supported_swing_mode(ClimateSwingMode mode) { this->supported_swing_modes_.insert(mode); } - ESPDEPRECATED("This method is deprecated, use set_supported_swing_modes() instead", "v1.20") - void set_supports_swing_mode_off(bool supported) { set_swing_mode_support_(CLIMATE_SWING_OFF, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_swing_modes() instead", "v1.20") - void set_supports_swing_mode_both(bool supported) { set_swing_mode_support_(CLIMATE_SWING_BOTH, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_swing_modes() instead", "v1.20") - void set_supports_swing_mode_vertical(bool supported) { set_swing_mode_support_(CLIMATE_SWING_VERTICAL, supported); } - ESPDEPRECATED("This method is deprecated, use set_supported_swing_modes() instead", "v1.20") - void set_supports_swing_mode_horizontal(bool supported) { - set_swing_mode_support_(CLIMATE_SWING_HORIZONTAL, supported); - } bool supports_swing_mode(ClimateSwingMode swing_mode) const { return this->supported_swing_modes_.count(swing_mode); } bool get_supports_swing_modes() const { return !this->supported_swing_modes_.empty(); } const std::set &get_supported_swing_modes() const { return this->supported_swing_modes_; } From 020cea80b205a6a7104a928f3f5142a09171748d Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 20 Oct 2025 11:16:50 +1300 Subject: [PATCH 9/9] [nextion] Clean up deprecated code from 1.20 (#11393) --- esphome/components/nextion/nextion.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/esphome/components/nextion/nextion.cpp b/esphome/components/nextion/nextion.cpp index 0ce9d02e97..fc152ece1e 100644 --- a/esphome/components/nextion/nextion.cpp +++ b/esphome/components/nextion/nextion.cpp @@ -1291,9 +1291,6 @@ void Nextion::check_pending_waveform_() { void Nextion::set_writer(const nextion_writer_t &writer) { this->writer_ = writer; } -ESPDEPRECATED("set_wait_for_ack(bool) deprecated, no effect", "v1.20") -void Nextion::set_wait_for_ack(bool wait_for_ack) { ESP_LOGE(TAG, "Deprecated"); } - bool Nextion::is_updating() { return this->connection_state_.is_updating_; } } // namespace nextion