ESPHome 2026.6.2
Loading...
Searching...
No Matches
application.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <ctime>
5#include <limits>
6#include <span>
7#include <string>
8#include <type_traits>
9#include <vector>
12
13#if defined(USE_LWIP_FAST_SELECT) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
14#include <atomic> // for std::atomic_thread_fence in Application::loop()
15#endif
16#include "esphome/core/hal.h"
23
24#ifdef USE_ESP32
25#include <sdkconfig.h> // for CONFIG_ESP_TASK_WDT_TIMEOUT_S (drives WDT_FEED_INTERVAL_MS)
26#endif
27
28#ifdef USE_DEVICES
29#include "esphome/core/device.h"
30#endif
31#ifdef USE_AREAS
32#include "esphome/core/area.h"
33#endif
34
35#ifdef USE_RUNTIME_STATS
37#endif
38#include "esphome/core/wake.h"
40
41#ifdef USE_RUNTIME_STATS
42namespace esphome::runtime_stats {
43class RuntimeStatsCollector;
44} // namespace esphome::runtime_stats
45#endif
46
47// Forward declarations for friend access from codegen-generated setup()
48void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
49void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
50
51namespace esphome {
52
56template<typename T, typename = void> struct HasLoopOverride : std::true_type {};
57template<typename T>
58struct HasLoopOverride<T, std::void_t<decltype(&T::loop)>>
59 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
60
61// Teardown timeout constant (in milliseconds)
62// For reboots, it's more important to shut down quickly than disconnect cleanly
63// since we're not entering deep sleep. The only consequence of not shutting down
64// cleanly is a warning in the log.
65static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000; // 1 second for quick reboot
66
68 public:
69#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
70 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
72 void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len) {
73 arch_init();
74 this->name_add_mac_suffix_ = true;
75 // MAC address suffix length (last 6 characters of 12-char MAC address string)
76 constexpr size_t mac_address_suffix_len = 6;
77 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
79 // Overwrite the placeholder suffix in the mutable static buffers with actual MAC
80 // name is always non-empty (validated by validate_hostname in Python config)
81 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
82 if (friendly_name_len > 0) {
83 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
84 mac_address_suffix_len);
85 }
86 this->name_ = StringRef(name, name_len);
87 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
88 }
89#else
90 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
92 void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len) {
93 arch_init();
94 this->name_add_mac_suffix_ = false;
95 this->name_ = StringRef(name, name_len);
96 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
97 }
98#endif
99
100#ifdef USE_DEVICES
101 void register_device(Device *device) { this->devices_.push_back(device); }
102#endif
103#ifdef USE_AREAS
104 void register_area(Area *area) { this->areas_.push_back(area); }
105#endif
106
108
109 // Owning script of the action chain currently executing (nullptr when none); used to attribute
110 // blocking warnings for deferred work to the script that scheduled it.
111 void set_current_source(const LogString *source) { this->current_source_ = source; }
112 const LogString *get_current_source() { return this->current_source_; }
113
114// Entity register methods (generated from entity_types.h).
115// Each entity type gets two overloads:
116// - register_<entity>(obj) — bare push_back
117// - register_<entity>(obj, name, hash, fields) — configure_entity_ + push_back
118// The 4-arg form lets codegen collapse `App.register_<entity>(obj); obj->configure_entity_(...);`
119// into a single call site, saving flash and a `main.cpp` line per entity.
120// NOLINTBEGIN(bugprone-macro-parentheses)
121#define ENTITY_TYPE_(type, singular, plural, count, upper) \
122 void register_##singular(type *obj) { this->plural##_.push_back(obj); } \
123 void register_##singular(type *obj, const char *name, uint32_t object_id_hash, uint32_t entity_fields) { \
124 obj->configure_entity_(name, object_id_hash, entity_fields); \
125 this->plural##_.push_back(obj); \
126 }
127#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
128 ENTITY_TYPE_(type, singular, plural, count, upper)
130#undef ENTITY_TYPE_
131#undef ENTITY_CONTROLLER_TYPE_
132 // NOLINTEND(bugprone-macro-parentheses)
133
134#ifdef USE_SERIAL_PROXY
136 proxy->set_instance_index(this->serial_proxies_.size());
137 this->serial_proxies_.push_back(proxy);
138 }
139#endif
140
142
144 void setup();
145
147 inline void ESPHOME_ALWAYS_INLINE loop();
148
150 const StringRef &get_name() const { return this->name_; }
151
153 const StringRef &get_friendly_name() const { return this->friendly_name_; }
154
156 const char *get_area() const {
157#ifdef USE_AREAS
158 // If we have areas registered, return the name of the first one (which is the top-level area)
159 if (!this->areas_.empty() && this->areas_[0] != nullptr) {
160 return this->areas_[0]->get_name();
161 }
162#endif
163 return "";
164 }
165
167 static constexpr size_t ESPHOME_COMMENT_SIZE_MAX = 256;
168
170 void get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer);
171
173 std::string get_comment() {
174 char buffer[ESPHOME_COMMENT_SIZE_MAX];
175 this->get_comment_string(buffer);
176 return std::string(buffer);
177 }
178
180
182 static constexpr size_t BUILD_TIME_STR_SIZE = 26;
183
186
189
191 time_t get_build_time();
192
195 void get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer);
196
198 // Remove before 2026.7.0
199 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0")
200 std::string get_compilation_time() {
201 char buf[BUILD_TIME_STR_SIZE];
202 this->get_build_time_string(buf);
203 return std::string(buf);
204 }
205
207 inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }
208
225 void set_loop_interval(uint32_t loop_interval) {
226 this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
227 }
228
229 uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
230
232
244#ifdef USE_BK72XX
245 // BDK busy-waits 200us per WDT reload (sctrl_dpll_delay200us). LibreTiny
246 // sets HW WDT to 10s; 2000ms keeps ~5x margin. See wdt_ctrl WCMD_RELOAD_PERIOD:
247 // https://github.com/libretiny-eu/framework-beken-bdk/blob/44800e7451ea30fbcbd3bb6e905315de59349fee/beken378/driver/wdt/wdt.c#L75-L87
248 static constexpr uint32_t WDT_FEED_INTERVAL_MS = 2000;
249#elif defined(USE_ESP32)
250 // Auto-scale to 1/5 of the configured ESP32 task WDT timeout so the safety
251 // margin stays constant when the user raises esp32.watchdog_timeout (default
252 // 5 s → 1000 ms feed; 10 s → 2000 ms; 60 s → 12000 ms). The esp32 component
253 // writes CONFIG_ESP_TASK_WDT_TIMEOUT_S into sdkconfig (range is validated
254 // to ≥ 5 s in esp32/__init__.py), giving us the value at compile time.
255 // esp_task_wdt_reset() takes a spinlock and walks the WDT task list, so
256 // each call costs tens of microseconds; longer intervals materially reduce
257 // the main-loop's wdt bucket. Component loops and scheduler items still
258 // feed after every op, so any op exceeding this threshold triggers a real
259 // feed naturally regardless of the rate-limit.
260 static_assert(CONFIG_ESP_TASK_WDT_TIMEOUT_S >= 5,
261 "CONFIG_ESP_TASK_WDT_TIMEOUT_S must be at least 5s for a safe WDT feed interval");
262 static constexpr uint32_t WDT_FEED_INTERVAL_MS = (CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000U) / 5U;
263#elif defined(USE_ESP8266)
264 // ESP8266 needs a tighter feed cadence than the other targets: the soft WDT
265 // is ~1.6 s and the HW WDT ~6 s, but a single long iteration (mDNS reply,
266 // wifi scan, OTA verify, lwIP TCP retransmit storm) can push the loop past
267 // a few hundred ms without giving the SDK a chance to feed. 100 ms keeps a
268 // ~16x margin to the soft WDT and ~60x to the HW WDT while still avoiding
269 // the per-iteration arch_feed_wdt() cost (this is the rate limit; component
270 // loops and scheduler items still feed after every op).
271 static constexpr uint32_t WDT_FEED_INTERVAL_MS = 100;
272#else
273 static constexpr uint32_t WDT_FEED_INTERVAL_MS = 300;
274#endif
275
278 void feed_wdt();
279
280#ifdef USE_STATUS_LED
288#endif
289
298 void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time) {
299 if (static_cast<uint32_t>(time - this->last_wdt_feed_) > WDT_FEED_INTERVAL_MS) [[unlikely]] {
300 this->feed_wdt_slow_(time);
301 }
302#ifdef USE_STATUS_LED
303 if (static_cast<uint32_t>(time - this->last_status_led_service_) > STATUS_LED_DISPATCH_INTERVAL_MS) [[unlikely]] {
304 this->service_status_led_slow_(time);
305 }
306#endif
307 }
308
309 void reboot();
310
311 void safe_reboot();
312
314
315 void run_powerdown_hooks();
316
321 void teardown_components(uint32_t timeout_ms);
322
326 uint8_t get_app_state() const { return this->app_state_ & ~APP_STATE_SETUP_COMPLETE; }
327
334 bool is_setup_complete() const { return (this->app_state_ & APP_STATE_SETUP_COMPLETE) != 0; }
335
336// Helper macro for entity getter method declarations
337#ifdef USE_DEVICES
338#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
339 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
340 for (auto *obj : this->entities_member##_) { \
341 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
342 (include_internal || !obj->is_internal())) \
343 return obj; \
344 } \
345 return nullptr; \
346 }
347 const auto &get_devices() { return this->devices_; }
348#else
349#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
350 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
351 for (auto *obj : this->entities_member##_) { \
352 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
353 return obj; \
354 } \
355 return nullptr; \
356 }
357#endif // USE_DEVICES
358#ifdef USE_AREAS
359 const auto &get_areas() { return this->areas_; }
360#endif
361// Entity getter methods (generated from entity_types.h)
362// NOLINTBEGIN(bugprone-macro-parentheses)
363#define ENTITY_TYPE_(type, singular, plural, count, upper) \
364 auto &get_##plural() const { return this->plural##_; } \
365 GET_ENTITY_METHOD(type, singular, plural)
366#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
367 ENTITY_TYPE_(type, singular, plural, count, upper)
369#undef ENTITY_TYPE_
370#undef ENTITY_CONTROLLER_TYPE_
371 // NOLINTEND(bugprone-macro-parentheses)
372
373#ifdef USE_SERIAL_PROXY
374 auto &get_serial_proxies() const { return this->serial_proxies_; }
375#endif
376
378
382
383#if defined(USE_ESP32) || defined(USE_LIBRETINY)
385 static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px) { esphome::wake_loop_isrsafe(px); }
386#elif defined(USE_ESP8266)
388 static void IRAM_ATTR ESPHOME_ALWAYS_INLINE wake_loop_isrsafe() { esphome::wake_loop_isrsafe(); }
389#elif defined(USE_ZEPHYR)
392#endif
393
396
397 protected:
398 friend Component;
399 friend class Scheduler;
400 friend class LoopBlockingGuard;
401#ifdef USE_RUNTIME_STATS
403#endif
404 friend void ::setup();
405 friend void ::original_setup();
406
409
410 // Publish the running unit's identity (component + source) and dispatch time together, so a
411 // dispatch site can't set one without the others. Friend-only (Scheduler).
412 void set_current_execution_context_(Component *component, const LogString *source, uint32_t now) {
414 this->current_source_ = source;
416 }
417
422 bool any_component_has_status_flag_(uint8_t flag) const;
423
428 template<typename T> void register_component_(T *comp, uint8_t source_index = 0) {
429 if (source_index != 0)
430 comp->set_component_source_(source_index);
432 }
433
434 void register_component_impl_(Component *comp, bool has_loop);
435
437 // FixedVector capacity was pre-initialized by codegen with the exact count
438 // of components that override loop(), computed at C++ compile time.
439
440 // Add all components with loop override that aren't already LOOP_DONE
441 // Some components (like logger) may call disable_loop() during initialization
442 // before setup runs, so we need to respect their LOOP_DONE state
445 // Then add any components that are already LOOP_DONE to the inactive section
446 // This handles components that called disable_loop() during initialization
448 }
449 void add_looping_components_by_state_(bool match_loop_done);
450
451 // These methods are called by Component::disable_loop() and Component::enable_loop()
452 // Components should not call these directly - use this->disable_loop() or this->enable_loop()
453 // to ensure component state is properly updated along with the loop partition
457 void activate_looping_component_(uint16_t index);
458 inline uint32_t ESPHOME_ALWAYS_INLINE scheduler_tick_(uint32_t now);
459
460 // RAII guard for a component loop phase. Constructor processes any pending
461 // enable_loop requests from ISRs and marks in_loop_ so reentrant
462 // modifications during component.loop() are safe; destructor clears in_loop_.
464 public:
465 inline ESPHOME_ALWAYS_INLINE explicit ComponentPhaseGuard(Application &app);
466 inline ESPHOME_ALWAYS_INLINE ~ComponentPhaseGuard() { this->app_.in_loop_ = false; }
469
470 private:
471 Application &app_;
472 };
473
477 void __attribute__((noinline)) process_dump_config_();
478
484 void feed_wdt_slow_(uint32_t time);
485
486#ifdef USE_STATUS_LED
492#endif
493
494 // === Member variables ordered by size to minimize padding ===
495
496 // Pointer-sized members first
498 const LogString *current_source_{nullptr};
499
500 // std::vector (3 pointers each: begin, end, capacity)
501 // Partitioned vector design for looping components
502 // =================================================
503 // Components are partitioned into [active | inactive] sections:
504 //
505 // looping_components_: [A, B, C, D | E, F]
506 // ^
507 // looping_components_active_end_ (4)
508 //
509 // - Components A,B,C,D are active and will be called in loop()
510 // - Components E,F are inactive (disabled/failed) and won't be called
511 // - No flag checking needed during iteration - just loop 0 to active_end_
512 // - When a component is disabled, it's swapped with the last active component
513 // and active_end_ is decremented
514 // - When a component is enabled, it's swapped with the first inactive component
515 // and active_end_ is incremented
516 // - This eliminates branch mispredictions from flag checking in the hot loop
518
519 // StringRef members (8 bytes each: pointer + size)
522
523 // 4-byte members
526 uint32_t last_wdt_feed_{0}; // millis() of most recent arch_feed_wdt(); rate-limits feed_wdt() hot path
527#ifdef USE_STATUS_LED
528 // millis() of most recent status_led dispatch; rate-limits independently of last_wdt_feed_
530#endif
531
532 // 2-byte members (grouped together for alignment)
533 uint16_t dump_config_at_{std::numeric_limits<uint16_t>::max()}; // Index into components_ for dump_config progress
534 uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
535 uint16_t looping_components_active_end_{0}; // Index marking end of active components in looping_components_
536 uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
537
538 // 1-byte members (grouped together to minimize padding)
539 uint8_t app_state_{0};
541 bool in_loop_{false};
543
544 // StaticVectors (largest members - contain actual array data inline)
546
547#ifdef USE_DEVICES
549#endif
550#ifdef USE_AREAS
552#endif
553// Entity StaticVector fields (generated from entity_types.h)
554// NOLINTBEGIN(bugprone-macro-parentheses)
555#define ENTITY_TYPE_(type, singular, plural, count, upper) StaticVector<type *, count> plural##_{};
556#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
557 ENTITY_TYPE_(type, singular, plural, count, upper)
559#undef ENTITY_TYPE_
560#undef ENTITY_CONTROLLER_TYPE_
561 // NOLINTEND(bugprone-macro-parentheses)
562
563#ifdef USE_SERIAL_PROXY
565#endif
566};
567
569extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
570
574 public:
575 explicit ScopedSourceGuard(const LogString *source) : prev_(App.get_current_source()) {
576 App.set_current_source(source);
577 }
581
582 private:
583 const LogString *prev_;
584};
585
586// Times one unit of work (a component loop() or a scheduled callback) and warns if it blocks the
587// main loop too long. The constructor publishes the unit's identity + dispatch time to App;
588// finish()/the cold warning path read them back, so the guard stores no copy.
589//
590// Guards must not nest: the constructor publishes to App but never restores on destruction, so a
591// nested guard would clobber the outer's context. Safe because the two dispatch sites (component
592// loop phase, execute_item_) run strictly sequentially and aren't re-entered from a timed callback.
594 public:
595 // Publish the unit's identity + dispatch time, then start timing. The millis start lives in App,
596 // so only the runtime-stats micros stamp is kept here.
597 LoopBlockingGuard(Component *component, const LogString *source, uint32_t now) {
599#ifdef USE_RUNTIME_STATS
600 this->started_us_ = micros();
601#endif
602 }
603
604 // Finish the timing operation and return the current time (millis)
605 // Inlined: the fast path is just millis() + subtract + compare
606 inline uint32_t HOT finish() {
607#ifdef USE_RUNTIME_STATS
608 uint32_t elapsed_us = micros() - this->started_us_;
609 // Delays have no component; accumulate into the global counter so loop() can subtract them.
611 if (component != nullptr) {
612 component->runtime_stats_.record_time(elapsed_us);
613 } else {
615 }
616#endif
617 uint32_t curr_time = MillisInternal::get();
618#ifndef USE_BENCHMARK
619 // Fast path: compare against constant threshold in ms (computed at compile time from centiseconds)
620 static constexpr uint32_t WARN_IF_BLOCKING_OVER_MS = static_cast<uint32_t>(WARN_IF_BLOCKING_OVER_CS) * 10U;
621 uint32_t blocking_time = curr_time - App.get_loop_component_start_time();
622 if (blocking_time > WARN_IF_BLOCKING_OVER_MS) [[unlikely]] {
623 warn_blocking(blocking_time);
624 }
625#endif
626 return curr_time;
627 }
628
630
631#ifdef USE_RUNTIME_STATS
632 protected:
634#endif
635
636 private:
637 // Cold path; defined in component.cpp. Reads the current component/source from App to name the culprit.
638 static void __attribute__((noinline, cold)) warn_blocking(uint32_t blocking_time);
639};
640
641// Phase A: drain wake notifications and run the scheduler. Invoked on every
642// Application::loop() tick regardless of whether a component phase runs, so
643// scheduler items fire at their requested cadence even when the caller has
644// raised loop_interval_ for power savings (see Application::loop()).
645// Returns the timestamp of the last scheduler item that ran (or `now`
646// unchanged if none ran), so the caller's WDT feed stays monotonic with the
647// per-item feeds inside scheduler.call() without an extra millis().
648inline uint32_t ESPHOME_ALWAYS_INLINE Application::scheduler_tick_(uint32_t now) {
649#ifdef USE_HOST
650 // Drain wake notifications first to clear socket for next wake.
652#endif
653 return this->scheduler.call(now);
654}
655
656// Phase B entry: only invoked when a component loop phase is about to run.
657// Processes pending enable_loop requests from ISRs and marks in_loop_ so
658// reentrant modifications during component.loop() are safe.
659inline ESPHOME_ALWAYS_INLINE Application::ComponentPhaseGuard::ComponentPhaseGuard(Application &app) : app_(app) {
660 // Process any pending enable_loop requests from ISRs
661 // This must be done before marking in_loop_ = true to avoid race conditions
662 if (this->app_.has_pending_enable_loop_requests_) {
663 // Clear flag BEFORE processing to avoid race condition
664 // If ISR sets it during processing, we'll catch it next loop iteration
665 // This is safe because:
666 // 1. Each component has its own pending_enable_loop_ flag that we check
667 // 2. If we can't process a component (wrong state), enable_pending_loops_()
668 // will set this flag back to true
669 // 3. Any new ISR requests during processing will set the flag again
670 this->app_.has_pending_enable_loop_requests_ = false;
671 this->app_.enable_pending_loops_();
672 }
673
674 // Mark that we're in the loop for safe reentrant modifications
675 this->app_.in_loop_ = true;
676}
677
678inline void ESPHOME_ALWAYS_INLINE Application::loop() {
679#if defined(USE_LWIP_FAST_SELECT) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
680 // Pairs with the TCP/IP thread's SYS_ARCH_UNPROTECT release on rcvevent so
681 // subsequent Socket::ready() checks in this iter observe the published state
682 // without a per-call memw. Wake is independent (xTaskNotifyGive/
683 // ulTaskNotifyTake), so non-losing. Skipped on MULTI_NO_ATOMICS (e.g.
684 // BK72xx) — that path keeps `volatile` in esphome_lwip_socket_has_data()
685 // instead.
686 std::atomic_thread_fence(std::memory_order_acquire);
687#endif
688#ifdef USE_RUNTIME_STATS
689 // Capture the start of the active (non-sleeping) portion of this iteration.
690 // Used to derive main-loop overhead = active time − Σ(component time) −
691 // before/tail splits recorded below.
692 uint32_t loop_active_start_us = micros();
693 // Snapshot the cumulative component-recorded time so we can subtract the
694 // slice that the scheduler spends inside its own LoopBlockingGuard
695 // (scheduler.cpp) — that time is already counted in per-component stats,
696 // so charging it again to "before" would double-count.
697 uint64_t loop_recorded_snap = ComponentRuntimeStats::global_recorded_us;
698#endif
699 // Phase A: always service the scheduler. Decouples scheduler cadence from
700 // loop_interval_ so raised intervals (for power savings) don't drag scheduled
701 // items forward. A tick that only runs the scheduler is cheap.
702 // scheduler_tick_ returns the timestamp of the last scheduler item that ran
703 // (advanced by its per-item feeds) or `now` unchanged. We adopt it as `now`
704 // so the gate check and WDT feed both reflect actual elapsed time after
705 // scheduler dispatch, without an extra millis() call.
706 uint32_t now = this->scheduler_tick_(MillisInternal::get());
707 // Guarantee one WDT feed per tick even when the scheduler had nothing to
708 // dispatch and the component phase is gated out — covers configs with no
709 // looping components and no scheduler work (setup() has its own
710 // per-component feed_wdt calls, so only do this here, not in scheduler_tick_).
711 this->feed_wdt_with_time(now);
712
713#ifdef USE_RUNTIME_STATS
714 uint32_t loop_before_end_us = micros();
715 uint64_t loop_before_scheduled_us = ComponentRuntimeStats::global_recorded_us - loop_recorded_snap;
716 // Only meaningful when do_component_phase is true; initialized to 0 so the
717 // tail bucket receives 0 on Phase A-only ticks (no component tail happened,
718 // the gate-check / stats-prefix overhead belongs to "residual", not "tail").
719 uint32_t loop_tail_start_us = 0;
720#endif
721
722 // Gate the component phase on loop_interval_, an active high-frequency
723 // request, or an explicit wake from a background producer. A scheduler-only
724 // wake (e.g. set_interval firing under a raised loop_interval_) leaves the
725 // component phase gated; an external producer that called wake_loop_*
726 // (MQTT RX, USB RX, BLE event, etc.) needs the component phase to actually
727 // run so its component's loop() can drain the queued work — that is the
728 // long-standing semantic of wake_loop_threadsafe(), and the wake_request
729 // flag preserves it. wake_request_take() exchange-clears the flag; wakes
730 // that arrive during Phase B re-set it and run Phase B again on the next
731 // iteration.
732 //
733 // wake_request_take() must always be called first since it does an
734 // atomic exchange to clear the flag, and we want to run the component phase
735 // if either the flag was set or the scheduler requested a high-frequency loop.
736 const bool do_component_phase = esphome::wake_request_take() || HighFrequencyLoopRequester::is_high_frequency() ||
737 (now - this->last_loop_ >= this->loop_interval_);
738
739 if (do_component_phase) {
740 ComponentPhaseGuard phase_guard{*this};
741
742 uint32_t last_op_end_time = now;
744 this->current_loop_index_++) {
746
747 {
748 // Guard publishes this component (no script source) + dispatch time, then times loop().
749 LoopBlockingGuard guard{component, nullptr, last_op_end_time};
750 component->loop();
751 // Use the finish method to get the current time as the end time
752 last_op_end_time = guard.finish();
753 }
754 this->feed_wdt_with_time(last_op_end_time);
755 }
756
757#ifdef USE_RUNTIME_STATS
758 loop_tail_start_us = micros();
759#endif
760 this->last_loop_ = last_op_end_time;
761 now = last_op_end_time;
762 // phase_guard destructor clears in_loop_ at scope exit
763 }
764
765#ifdef USE_RUNTIME_STATS
766 // Record per-tick timing on every loop, not just component-phase ticks.
767 // record_loop_active is a small accumulator; process_pending_stats is an
768 // inline gate check that early-outs unless now >= next_log_time_.
769 if (global_runtime_stats != nullptr) {
770 uint32_t loop_now_us = micros();
771 // Subtract scheduled-component time from the "before" bucket so it is
772 // not double-counted (it is already attributed to per-component stats).
773 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
774 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
775 ? loop_before_wall_us - static_cast<uint32_t>(loop_before_scheduled_us)
776 : 0;
777 // tail_us is only defined when Phase B ran; 0 on Phase A-only ticks so the
778 // stats bucket keeps its "component-phase trailing overhead" meaning.
779 uint32_t loop_tail_us = do_component_phase ? (loop_now_us - loop_tail_start_us) : 0;
780 global_runtime_stats->record_loop_active(loop_now_us - loop_active_start_us, loop_before_overhead_us, loop_tail_us);
782 }
783#endif
784
785 // Compute sleep: bounded by time-until-next-component-phase and the
786 // scheduler's next deadline. When a scheduler timer fires it re-enters
787 // loop(), Phase A services it, and the component phase stays gated by
788 // loop_interval_. When a background producer calls wake_loop_threadsafe()
789 // it sets the wake_request flag and wakes select() / the task notification;
790 // the gate above sees the flag and runs Phase B too so the producer's
791 // component can drain its queued work without waiting up to loop_interval_.
792 //
793 // Re-read HighFrequencyLoopRequester::is_high_frequency() here instead of
794 // reusing the cached `high_frequency` captured above: a component calling
795 // HighFrequencyLoopRequester::start() from within its loop() would
796 // otherwise sit under the stale value and sleep for up to loop_interval_
797 // before the request took effect. That was fine pre-decoupling (the old
798 // main loop also called the function fresh at the sleep point) but now
799 // matters much more — loop_interval_ is a power-saving knob documented
800 // to accept multi-second values, so the stale path could add seconds of
801 // latency on an HF request. The call is a trivial atomic read.
802 uint32_t delay_time = 0;
804 const uint32_t elapsed_since_phase = now - this->last_loop_;
805 const uint32_t until_phase =
806 (elapsed_since_phase >= this->loop_interval_) ? 0 : (this->loop_interval_ - elapsed_since_phase);
807 const uint32_t until_sched = this->scheduler.next_schedule_in(now).value_or(until_phase);
808 delay_time = std::min(until_phase, until_sched);
809 }
810 // All platforms route loop yields through the platform wake primitive.
811 // On host this drains the loopback wake socket via select(); on FreeRTOS
812 // targets it uses task notifications; on ESP8266/RP2040 it uses esp_delay/WFE.
814
815 if (this->dump_config_at_ < this->components_.size()) {
816 this->process_dump_config_();
817 }
818}
819
820} // namespace esphome
void original_setup()
void setup()
ComponentPhaseGuard & operator=(const ComponentPhaseGuard &)=delete
ESPHOME_ALWAYS_INLINE ComponentPhaseGuard(Application &app)
ESPHOME_ALWAYS_INLINE ~ComponentPhaseGuard()
ComponentPhaseGuard(const ComponentPhaseGuard &)=delete
void set_loop_component_start_time_(uint32_t now)
Freshen the cached loop component start time. Called by Scheduler before each dispatch.
uint32_t ESPHOME_ALWAYS_INLINE scheduler_tick_(uint32_t now)
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t get_loop_interval() const
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
const auto & get_areas()
const LogString * current_source_
uint16_t looping_components_active_end_
void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len)
Pre-setup with MAC suffix: overwrites placeholder in mutable static buffers with actual MAC.
Definition application.h:72
void set_current_execution_context_(Component *component, const LogString *source, uint32_t now)
void service_status_led_slow_(uint32_t time)
Slow path for the status_led dispatch rate limit.
Component * get_current_component()
void register_serial_proxy(serial_proxy::SerialProxy *proxy)
static void IRAM_ATTR wake_loop_any_context()
Wake from any context (ISR, thread, callback).
static void IRAM_ATTR ESPHOME_ALWAYS_INLINE wake_loop_isrsafe()
Wake from ISR (ESP8266). No task_woken arg — no FreeRTOS. Caller must be IRAM_ATTR.
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
void __attribute__((noinline)) process_dump_config_()
Process dump_config output one component per loop iteration.
StaticVector< Area *, ESPHOME_AREA_COUNT > areas_
std::string get_comment()
Get the comment of this Application as a string.
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
void register_component_(T *comp, uint8_t source_index=0)
Register a component, detecting loop() override at compile time.
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len)
Pre-setup without MAC suffix: StringRef points directly at const string literals in flash.
Definition application.h:92
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
bool any_component_has_status_flag_(uint8_t flag) const
Walk all registered components looking for any whose component_state_ has the given flag set.
void get_build_time_string(std::span< char, BUILD_TIME_STR_SIZE > buffer)
Copy the build time string into the provided buffer Buffer must be BUILD_TIME_STR_SIZE bytes (compile...
void register_area(Area *area)
Component * current_component_
static void wake_loop_isrsafe()
Wake from ISR (Zephyr). No task_woken arg — k_sem_give() handles ISR scheduling internally.
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void feed_wdt()
Feed the task watchdog.
void disable_component_loop_(Component *component)
const char * get_area() const
Get the area of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
void activate_looping_component_(uint16_t index)
const auto & get_devices()
uint32_t last_status_led_service_
ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0") std
Get the build time as a string (deprecated, use get_build_time_string() instead)
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
static constexpr size_t ESPHOME_COMMENT_SIZE_MAX
Maximum size of the comment buffer (including null terminator)
FixedVector< Component * > looping_components_
auto & get_serial_proxies() const
void add_looping_components_by_state_(bool match_loop_done)
const LogString * get_current_source()
volatile bool has_pending_enable_loop_requests_
void get_comment_string(std::span< char, ESPHOME_COMMENT_SIZE_MAX > buffer)
Copy the comment string into the provided buffer.
static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px)
Wake from ISR (ESP32 and LibreTiny).
void feed_wdt_slow_(uint32_t time)
Slow path for feed_wdt(): actually calls arch_feed_wdt() and updates last_wdt_feed_.
uint16_t current_loop_index_
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
void ESPHOME_ALWAYS_INLINE loop()
Make a loop iteration. Call this in your loop() function.
time_t get_build_time()
Get the build time as a Unix timestamp.
StaticVector< serial_proxy::SerialProxy *, SERIAL_PROXY_COUNT > serial_proxies_
bool is_setup_complete() const
True once Application::setup() has finished walking all components and finalized the initial status f...
void set_current_source(const LogString *source)
void register_device(Device *device)
static constexpr uint32_t STATUS_LED_DISPATCH_INTERVAL_MS
Dispatch interval for the status LED update.
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
StaticVector< Device *, ESPHOME_DEVICE_COUNT > devices_
void calculate_looping_components_()
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void register_component_impl_(Component *comp, bool has_loop)
uint8_t get_app_state() const
Return the public app state status bits (STATUS_LED_* only).
friend class Scheduler
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
static bool is_high_frequency()
Check whether the loop is running continuously.
Definition helpers.h:2001
LoopBlockingGuard(Component *component, const LogString *source, uint32_t now)
RAII guard that publishes a current source (e.g.
ScopedSourceGuard(const ScopedSourceGuard &)=delete
ScopedSourceGuard(const LogString *source)
ScopedSourceGuard & operator=(const ScopedSourceGuard &)=delete
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:217
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void ESPHOME_ALWAYS_INLINE process_pending_stats(uint32_t current_time)
void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us)
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
const Component * component
Definition component.cpp:34
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
constexpr uint8_t WARN_IF_BLOCKING_OVER_CS
Definition component.h:100
void wake_loop_threadsafe()
Non-ISR: always inline.
void arch_init()
Definition hal.cpp:47
constexpr uint8_t APP_STATE_SETUP_COMPLETE
Definition component.h:96
void ESPHOME_ALWAYS_INLINE wake_drain_notifications()
Definition wake_host.h:48
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:744
Application App
Global storage of Application pointer - only one Application can exist.
void ESPHOME_ALWAYS_INLINE wake_loop_isrsafe()
ISR-safe: no task_woken arg because ESP8266 has no FreeRTOS. Caller must be IRAM_ATTR.
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake_esp8266.cpp.
static void uint32_t
static uint64_t global_recorded_us
Definition component.h:124
SFINAE helper: detects whether T overrides Component::loop().
Definition application.h:56
Platform-specific main loop wake primitives.