From 95977bcccb96a24c855db6634b1c5aafab5a75d1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 Apr 2026 15:28:23 -0500 Subject: [PATCH 1/3] [core] Freshen loop_component_start_time_ before scheduler dispatch --- esphome/core/application.h | 6 ++++++ esphome/core/scheduler.cpp | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/esphome/core/application.h b/esphome/core/application.h index 185ee4163b..86aa461fbb 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -103,6 +103,12 @@ class Application { void set_current_component(Component *component) { this->current_component_ = component; } Component *get_current_component() { return this->current_component_; } + /// Update the cached loop component start time. Used by the scheduler before + /// dispatching a queued callback so callers reading + /// get_loop_component_start_time() inside the callback observe a fresh value + /// instead of one inherited from the prior loop iteration's last component. + void set_loop_component_start_time(uint32_t now) { this->loop_component_start_time_ = now; } + // Entity register methods (generated from entity_types.h). // Each entity type gets two overloads: // - register_(obj) — bare push_back diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index d83d67d6e4..fc02ba61f8 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -772,6 +772,11 @@ Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() { // Helper to execute a scheduler item uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) { App.set_current_component(item->component); + // Freshen the cached loop component start time so callbacks reading + // App.get_loop_component_start_time() observe the dispatch time of this + // item rather than a stale value from the prior loop iteration's last + // component phase. + App.set_loop_component_start_time(now); WarnIfComponentBlockingGuard guard{item->component, now}; item->callback(); uint32_t end = guard.finish(); From 0ba825a8f0d2628f4d09656108eafbe1becce16e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 Apr 2026 15:29:10 -0500 Subject: [PATCH 2/3] Make set_loop_component_start_time_ protected with Scheduler friend --- esphome/core/application.h | 13 +++++++------ esphome/core/scheduler.cpp | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/esphome/core/application.h b/esphome/core/application.h index 86aa461fbb..b30821923e 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -103,12 +103,6 @@ class Application { void set_current_component(Component *component) { this->current_component_ = component; } Component *get_current_component() { return this->current_component_; } - /// Update the cached loop component start time. Used by the scheduler before - /// dispatching a queued callback so callers reading - /// get_loop_component_start_time() inside the callback observe a fresh value - /// instead of one inherited from the prior loop iteration's last component. - void set_loop_component_start_time(uint32_t now) { this->loop_component_start_time_ = now; } - // Entity register methods (generated from entity_types.h). // Each entity type gets two overloads: // - register_(obj) — bare push_back @@ -383,12 +377,19 @@ class Application { protected: friend Component; + friend class Scheduler; #ifdef USE_RUNTIME_STATS friend class runtime_stats::RuntimeStatsCollector; #endif friend void ::setup(); friend void ::original_setup(); + /// Update the cached loop component start time. Used by the scheduler before + /// dispatching a queued callback so callers reading + /// get_loop_component_start_time() inside the callback observe a fresh value + /// instead of one inherited from the prior loop iteration's last component. + void set_loop_component_start_time_(uint32_t now) { this->loop_component_start_time_ = now; } + /// Walk all registered components looking for any whose component_state_ /// has the given flag set. Used by Component::status_clear_*_slow_path_() /// (which is a friend) to decide whether to clear the corresponding bit on diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index fc02ba61f8..838bf952af 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -776,7 +776,7 @@ uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) { // App.get_loop_component_start_time() observe the dispatch time of this // item rather than a stale value from the prior loop iteration's last // component phase. - App.set_loop_component_start_time(now); + App.set_loop_component_start_time_(now); WarnIfComponentBlockingGuard guard{item->component, now}; item->callback(); uint32_t end = guard.finish(); From 3ffbc4ef5433c2a37b2c977a16748f27dee07f87 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 Apr 2026 15:31:24 -0500 Subject: [PATCH 3/3] Shorten comments --- esphome/core/application.h | 5 +---- esphome/core/scheduler.cpp | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/esphome/core/application.h b/esphome/core/application.h index b30821923e..221081a0e4 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -384,10 +384,7 @@ class Application { friend void ::setup(); friend void ::original_setup(); - /// Update the cached loop component start time. Used by the scheduler before - /// dispatching a queued callback so callers reading - /// get_loop_component_start_time() inside the callback observe a fresh value - /// instead of one inherited from the prior loop iteration's last component. + /// Freshen the cached loop component start time. Called by Scheduler before each dispatch. void set_loop_component_start_time_(uint32_t now) { this->loop_component_start_time_ = now; } /// Walk all registered components looking for any whose component_state_ diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index 838bf952af..11884ce4ba 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -772,10 +772,7 @@ Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() { // Helper to execute a scheduler item uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) { App.set_current_component(item->component); - // Freshen the cached loop component start time so callbacks reading - // App.get_loop_component_start_time() observe the dispatch time of this - // item rather than a stale value from the prior loop iteration's last - // component phase. + // Freshen so callbacks reading App.get_loop_component_start_time() see this item's dispatch time. App.set_loop_component_start_time_(now); WarnIfComponentBlockingGuard guard{item->component, now}; item->callback();