From 34adbf058800a98d364753069e872a0fab9b0b5a Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Tue, 21 Jun 2022 01:26:34 +0200 Subject: [PATCH] Fix / Reverse order shutdown (#3585) --- esphome/core/application.cpp | 19 +++++++++++++------ esphome/core/application.h | 9 +-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index a423397453..c012195f34 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -125,19 +125,26 @@ void IRAM_ATTR HOT Application::feed_wdt() { } void Application::reboot() { ESP_LOGI(TAG, "Forcing a reboot..."); - for (auto *comp : this->components_) - comp->on_shutdown(); + for (auto it = this->components_.rbegin(); it != this->components_.rend(); ++it) { + (*it)->on_shutdown(); + } arch_restart(); } void Application::safe_reboot() { ESP_LOGI(TAG, "Rebooting safely..."); - for (auto *comp : this->components_) - comp->on_safe_shutdown(); - for (auto *comp : this->components_) - comp->on_shutdown(); + run_safe_shutdown_hooks(); arch_restart(); } +void Application::run_safe_shutdown_hooks() { + for (auto it = this->components_.rbegin(); it != this->components_.rend(); ++it) { + (*it)->on_safe_shutdown(); + } + for (auto it = this->components_.rbegin(); it != this->components_.rend(); ++it) { + (*it)->on_shutdown(); + } +} + void Application::calculate_looping_components_() { for (auto *obj : this->components_) { if (obj->has_overridden_loop()) diff --git a/esphome/core/application.h b/esphome/core/application.h index 453b15822e..6376987f66 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -161,14 +161,7 @@ class Application { void safe_reboot(); - void run_safe_shutdown_hooks() { - for (auto *comp : this->components_) { - comp->on_safe_shutdown(); - } - for (auto *comp : this->components_) { - comp->on_shutdown(); - } - } + void run_safe_shutdown_hooks(); uint32_t get_app_state() const { return this->app_state_; }