ESPHome 2026.5.0-dev
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#include "esphome/core/hal.h"
19
20#ifdef USE_DEVICES
21#include "esphome/core/device.h"
22#endif
23#ifdef USE_AREAS
24#include "esphome/core/area.h"
25#endif
26
27#ifdef USE_LWIP_FAST_SELECT
29#endif
30#ifdef USE_HOST
31#include <sys/select.h>
32#include <sys/socket.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#endif
38#ifdef USE_RUNTIME_STATS
40#endif
41#include "esphome/core/wake.h"
43
44namespace esphome::socket {
45#ifdef USE_HOST
47bool socket_ready_fd(int fd, bool loop_monitored); // NOLINT(readability-redundant-declaration)
48#endif
49} // namespace esphome::socket
50
51#ifdef USE_RUNTIME_STATS
52namespace esphome::runtime_stats {
53class RuntimeStatsCollector;
54} // namespace esphome::runtime_stats
55#endif
56
57// Forward declarations for friend access from codegen-generated setup()
58void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
59void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
60
61namespace esphome {
62
66template<typename T, typename = void> struct HasLoopOverride : std::true_type {};
67template<typename T>
68struct HasLoopOverride<T, std::void_t<decltype(&T::loop)>>
69 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
70
71// Teardown timeout constant (in milliseconds)
72// For reboots, it's more important to shut down quickly than disconnect cleanly
73// since we're not entering deep sleep. The only consequence of not shutting down
74// cleanly is a warning in the log.
75static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000; // 1 second for quick reboot
76
78 public:
79#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
80 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
82 void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len) {
83 arch_init();
84 this->name_add_mac_suffix_ = true;
85 // MAC address length: 12 hex chars + null terminator
86 constexpr size_t mac_address_len = 13;
87 // MAC address suffix length (last 6 characters of 12-char MAC address string)
88 constexpr size_t mac_address_suffix_len = 6;
89 char mac_addr[mac_address_len];
91 // Overwrite the placeholder suffix in the mutable static buffers with actual MAC
92 // name is always non-empty (validated by validate_hostname in Python config)
93 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
94 if (friendly_name_len > 0) {
95 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
96 mac_address_suffix_len);
97 }
98 this->name_ = StringRef(name, name_len);
99 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
100 }
101#else
102 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
104 void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len) {
105 arch_init();
106 this->name_add_mac_suffix_ = false;
107 this->name_ = StringRef(name, name_len);
108 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
109 }
110#endif
111
112#ifdef USE_DEVICES
113 void register_device(Device *device) { this->devices_.push_back(device); }
114#endif
115#ifdef USE_AREAS
116 void register_area(Area *area) { this->areas_.push_back(area); }
117#endif
118
121
122// Entity register methods (generated from entity_types.h)
123// NOLINTBEGIN(bugprone-macro-parentheses)
124#define ENTITY_TYPE_(type, singular, plural, count, upper) \
125 void register_##singular(type *obj) { this->plural##_.push_back(obj); }
126#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
127 ENTITY_TYPE_(type, singular, plural, count, upper)
129#undef ENTITY_TYPE_
130#undef ENTITY_CONTROLLER_TYPE_
131 // NOLINTEND(bugprone-macro-parentheses)
132
133#ifdef USE_SERIAL_PROXY
135 proxy->set_instance_index(this->serial_proxies_.size());
136 this->serial_proxies_.push_back(proxy);
137 }
138#endif
139
141
143 void setup();
144
146 inline void ESPHOME_ALWAYS_INLINE loop();
147
149 const StringRef &get_name() const { return this->name_; }
150
152 const StringRef &get_friendly_name() const { return this->friendly_name_; }
153
155 const char *get_area() const {
156#ifdef USE_AREAS
157 // If we have areas registered, return the name of the first one (which is the top-level area)
158 if (!this->areas_.empty() && this->areas_[0] != nullptr) {
159 return this->areas_[0]->get_name();
160 }
161#endif
162 return "";
163 }
164
166 static constexpr size_t ESPHOME_COMMENT_SIZE_MAX = 256;
167
169 void get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer);
170
172 std::string get_comment() {
173 char buffer[ESPHOME_COMMENT_SIZE_MAX];
174 this->get_comment_string(buffer);
175 return std::string(buffer);
176 }
177
179
181 static constexpr size_t BUILD_TIME_STR_SIZE = 26;
182
185
188
190 time_t get_build_time();
191
194 void get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer);
195
197 // Remove before 2026.7.0
198 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0")
199 std::string get_compilation_time() {
200 char buf[BUILD_TIME_STR_SIZE];
201 this->get_build_time_string(buf);
202 return std::string(buf);
203 }
204
206 inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }
207
224 void set_loop_interval(uint32_t loop_interval) {
225 this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
226 }
227
228 uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
229
231
235 static constexpr uint32_t WDT_FEED_INTERVAL_MS = 3;
236
239 void feed_wdt();
240
245 void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time) {
246 if (static_cast<uint32_t>(time - this->last_wdt_feed_) > WDT_FEED_INTERVAL_MS) [[unlikely]] {
247 this->feed_wdt_slow_(time);
248 }
249 }
250
251 void reboot();
252
253 void safe_reboot();
254
256
257 void run_powerdown_hooks();
258
263 void teardown_components(uint32_t timeout_ms);
264
268 uint8_t get_app_state() const { return this->app_state_ & ~APP_STATE_SETUP_COMPLETE; }
269
276 bool is_setup_complete() const { return (this->app_state_ & APP_STATE_SETUP_COMPLETE) != 0; }
277
278// Helper macro for entity getter method declarations
279#ifdef USE_DEVICES
280#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
281 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
282 for (auto *obj : this->entities_member##_) { \
283 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
284 (include_internal || !obj->is_internal())) \
285 return obj; \
286 } \
287 return nullptr; \
288 }
289 const auto &get_devices() { return this->devices_; }
290#else
291#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
292 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
293 for (auto *obj : this->entities_member##_) { \
294 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
295 return obj; \
296 } \
297 return nullptr; \
298 }
299#endif // USE_DEVICES
300#ifdef USE_AREAS
301 const auto &get_areas() { return this->areas_; }
302#endif
303// Entity getter methods (generated from entity_types.h)
304// NOLINTBEGIN(bugprone-macro-parentheses)
305#define ENTITY_TYPE_(type, singular, plural, count, upper) \
306 auto &get_##plural() const { return this->plural##_; } \
307 GET_ENTITY_METHOD(type, singular, plural)
308#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
309 ENTITY_TYPE_(type, singular, plural, count, upper)
311#undef ENTITY_TYPE_
312#undef ENTITY_CONTROLLER_TYPE_
313 // NOLINTEND(bugprone-macro-parentheses)
314
315#ifdef USE_SERIAL_PROXY
316 auto &get_serial_proxies() const { return this->serial_proxies_; }
317#endif
318
319 Scheduler scheduler;
320
323#ifdef USE_LWIP_FAST_SELECT
326 bool register_socket(struct lwip_sock *sock);
327 void unregister_socket(struct lwip_sock *sock);
328#elif defined(USE_HOST)
332 bool register_socket_fd(int fd);
333 void unregister_socket_fd(int fd);
334#endif
335
339
340#if defined(USE_ESP32) || defined(USE_LIBRETINY)
342 static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px) { esphome::wake_loop_isrsafe(px); }
343#endif
344
347
348 protected:
349 friend Component;
350#ifdef USE_HOST
351 friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
352#endif
353#ifdef USE_RUNTIME_STATS
355#endif
356 friend void ::setup();
357 friend void ::original_setup();
358#ifdef USE_HOST
359 friend void wake_loop_threadsafe(); // Host platform accesses wake_socket_fd_
360#endif
361
362#ifdef USE_HOST
363 bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); }
364#endif
365
370 bool any_component_has_status_flag_(uint8_t flag) const;
371
374 template<typename T> void register_component_(T *comp) {
376 }
377
378 void register_component_impl_(Component *comp, bool has_loop);
379
381 // FixedVector capacity was pre-initialized by codegen with the exact count
382 // of components that override loop(), computed at C++ compile time.
383
384 // Add all components with loop override that aren't already LOOP_DONE
385 // Some components (like logger) may call disable_loop() during initialization
386 // before setup runs, so we need to respect their LOOP_DONE state
389 // Then add any components that are already LOOP_DONE to the inactive section
390 // This handles components that called disable_loop() during initialization
392 }
393 void add_looping_components_by_state_(bool match_loop_done);
394
395 // These methods are called by Component::disable_loop() and Component::enable_loop()
396 // Components should not call these directly - use this->disable_loop() or this->enable_loop()
397 // to ensure component state is properly updated along with the loop partition
401 void activate_looping_component_(uint16_t index);
402 inline void ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time);
403 inline void ESPHOME_ALWAYS_INLINE after_loop_tasks_() { this->in_loop_ = false; }
404
408 void __attribute__((noinline)) process_dump_config_();
409
413 void feed_wdt_slow_(uint32_t time);
414
416#ifdef USE_HOST
417 // select() fallback path is too complex to inline (host platform)
418 void yield_with_select_(uint32_t delay_ms);
419#else
420 inline void ESPHOME_ALWAYS_INLINE yield_with_select_(uint32_t delay_ms);
421#endif
422
423#ifdef USE_HOST
424 void setup_wake_loop_threadsafe_(); // Create wake notification socket
425 inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
426#endif
427
428 // === Member variables ordered by size to minimize padding ===
429
430 // Pointer-sized members first
432
433 // std::vector (3 pointers each: begin, end, capacity)
434 // Partitioned vector design for looping components
435 // =================================================
436 // Components are partitioned into [active | inactive] sections:
437 //
438 // looping_components_: [A, B, C, D | E, F]
439 // ^
440 // looping_components_active_end_ (4)
441 //
442 // - Components A,B,C,D are active and will be called in loop()
443 // - Components E,F are inactive (disabled/failed) and won't be called
444 // - No flag checking needed during iteration - just loop 0 to active_end_
445 // - When a component is disabled, it's swapped with the last active component
446 // and active_end_ is decremented
447 // - When a component is enabled, it's swapped with the first inactive component
448 // and active_end_ is incremented
449 // - This eliminates branch mispredictions from flag checking in the hot loop
451#ifdef USE_LWIP_FAST_SELECT
452 std::vector<struct lwip_sock *> monitored_sockets_; // Cached lwip_sock pointers for direct rcvevent read
453#elif defined(USE_HOST)
454 std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
455#endif
456#ifdef USE_HOST
457 int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
458#endif
459
460 // StringRef members (8 bytes each: pointer + size)
463
464 // 4-byte members
467 uint32_t last_wdt_feed_{0}; // millis() of most recent arch_feed_wdt(); rate-limits feed_wdt() hot path
468
469#ifdef USE_HOST
470 int max_fd_{-1}; // Highest file descriptor number for select()
471#endif
472
473 // 2-byte members (grouped together for alignment)
474 uint16_t dump_config_at_{std::numeric_limits<uint16_t>::max()}; // Index into components_ for dump_config progress
475 uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
476 uint16_t looping_components_active_end_{0}; // Index marking end of active components in looping_components_
477 uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
478
479 // 1-byte members (grouped together to minimize padding)
480 uint8_t app_state_{0};
482 bool in_loop_{false};
484
485#ifdef USE_HOST
486 bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
487#endif
488
489#ifdef USE_HOST
490 // Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly)
491 fd_set read_fds_{}; // Working fd_set: populated by select()
492 fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
493#endif
494
495 // StaticVectors (largest members - contain actual array data inline)
497
498#ifdef USE_DEVICES
500#endif
501#ifdef USE_AREAS
503#endif
504// Entity StaticVector fields (generated from entity_types.h)
505// NOLINTBEGIN(bugprone-macro-parentheses)
506#define ENTITY_TYPE_(type, singular, plural, count, upper) StaticVector<type *, count> plural##_{};
507#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
508 ENTITY_TYPE_(type, singular, plural, count, upper)
510#undef ENTITY_TYPE_
511#undef ENTITY_CONTROLLER_TYPE_
512 // NOLINTEND(bugprone-macro-parentheses)
513
514#ifdef USE_SERIAL_PROXY
516#endif
517};
518
520extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
521
522#ifdef USE_HOST
523// Inline implementations for hot-path functions
524// drain_wake_notifications_() is called on every loop iteration
525
526// Small buffer for draining wake notification bytes (1 byte sent per wake)
527// Size allows draining multiple notifications per recvfrom() without wasting stack
528static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
529
531 // Called from main loop to drain any pending wake notifications
532 // Must check is_socket_ready_() to avoid blocking on empty socket
533 if (this->wake_socket_fd_ >= 0 && this->is_socket_ready_(this->wake_socket_fd_)) {
534 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
535 // Drain all pending notifications with non-blocking reads
536 // Multiple wake events may have triggered multiple writes, so drain until EWOULDBLOCK
537 // We control both ends of this loopback socket (always write 1 byte per wake),
538 // so no error checking needed - any errors indicate catastrophic system failure
539 while (::recvfrom(this->wake_socket_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
540 // Just draining, no action needed - wake has already occurred
541 }
542 }
543}
544#endif // USE_HOST
545
546inline void ESPHOME_ALWAYS_INLINE Application::before_loop_tasks_(uint32_t loop_start_time) {
547#ifdef USE_HOST
548 // Drain wake notifications first to clear socket for next wake
550#endif
551
552 // Process scheduled tasks. Scheduler::call now feeds the watchdog itself
553 // after each scheduled item that actually runs, so we no longer need an
554 // unconditional feed here — when Scheduler::call has no work to do, the
555 // only elapsed time is a sleep wake + a few instructions, and when it does
556 // have work, it fed the wdt as it went.
557 this->scheduler.call(loop_start_time);
558
559 // Process any pending enable_loop requests from ISRs
560 // This must be done before marking in_loop_ = true to avoid race conditions
562 // Clear flag BEFORE processing to avoid race condition
563 // If ISR sets it during processing, we'll catch it next loop iteration
564 // This is safe because:
565 // 1. Each component has its own pending_enable_loop_ flag that we check
566 // 2. If we can't process a component (wrong state), enable_pending_loops_()
567 // will set this flag back to true
568 // 3. Any new ISR requests during processing will set the flag again
570 this->enable_pending_loops_();
571 }
572
573 // Mark that we're in the loop for safe reentrant modifications
574 this->in_loop_ = true;
575}
576
577inline void ESPHOME_ALWAYS_INLINE Application::loop() {
578#ifdef USE_RUNTIME_STATS
579 // Capture the start of the active (non-sleeping) portion of this iteration.
580 // Used to derive main-loop overhead = active time − Σ(component time) −
581 // before/tail splits recorded below.
582 uint32_t loop_active_start_us = micros();
583 // Snapshot the cumulative component-recorded time so we can subtract the
584 // slice that the scheduler spends inside its own WarnIfComponentBlockingGuard
585 // (scheduler.cpp) — that time is already counted in per-component stats,
586 // so charging it again to "before" would double-count.
587 uint64_t loop_recorded_snap = ComponentRuntimeStats::global_recorded_us;
588#endif
589 // Get the initial loop time at the start
590 uint32_t last_op_end_time = millis();
591
592 this->before_loop_tasks_(last_op_end_time);
593#ifdef USE_RUNTIME_STATS
594 uint32_t loop_before_end_us = micros();
595 uint64_t loop_before_scheduled_us = ComponentRuntimeStats::global_recorded_us - loop_recorded_snap;
596#endif
597
599 this->current_loop_index_++) {
601
602 // Update the cached time before each component runs
603 this->loop_component_start_time_ = last_op_end_time;
604
605 {
606 this->set_current_component(component);
607 WarnIfComponentBlockingGuard guard{component, last_op_end_time};
608 component->loop();
609 // Use the finish method to get the current time as the end time
610 last_op_end_time = guard.finish();
611 }
612 this->feed_wdt_with_time(last_op_end_time);
613 }
614
615#ifdef USE_RUNTIME_STATS
616 uint32_t loop_tail_start_us = micros();
617#endif
618 this->after_loop_tasks_();
619
620#ifdef USE_RUNTIME_STATS
621 // Process any pending runtime stats printing after all components have run
622 // This ensures stats printing doesn't affect component timing measurements
623 if (global_runtime_stats != nullptr) {
624 uint32_t loop_now_us = micros();
625 // Subtract scheduled-component time from the "before" bucket so it is
626 // not double-counted (it is already attributed to per-component stats).
627 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
628 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
629 ? loop_before_wall_us - static_cast<uint32_t>(loop_before_scheduled_us)
630 : 0;
631 global_runtime_stats->record_loop_active(loop_now_us - loop_active_start_us, loop_before_overhead_us,
632 loop_now_us - loop_tail_start_us);
634 }
635#endif
636
637 // Use the last component's end time instead of calling millis() again
638 uint32_t delay_time = 0;
639 auto elapsed = last_op_end_time - this->last_loop_;
641 delay_time = this->loop_interval_ - elapsed;
642 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
643 // next_schedule is max 0.5*delay_time
644 // otherwise interval=0 schedules result in constant looping with almost no sleep
645 next_schedule = std::max(next_schedule, delay_time / 2);
646 delay_time = std::min(next_schedule, delay_time);
647 }
648 this->yield_with_select_(delay_time);
649 this->last_loop_ = last_op_end_time;
650
651 if (this->dump_config_at_ < this->components_.size()) {
652 this->process_dump_config_();
653 }
654}
655
656// Inline yield_with_select_ for all paths except the select() fallback
657#ifndef USE_HOST
658inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay_ms) {
659#ifdef USE_LWIP_FAST_SELECT
660 // Fast path (ESP32/LibreTiny): reads rcvevent directly from cached lwip_sock pointers.
661 // Safe because this runs on the main loop which owns socket lifetime (create, read, close).
662 if (delay_ms == 0) [[unlikely]] {
663 yield();
664 return;
665 }
666
667 // Check if any socket already has pending data before sleeping.
668 // If a socket still has unread data (rcvevent > 0) but the task notification was already
669 // consumed, ulTaskNotifyTake would block until timeout — adding up to delay_ms latency.
670 // This scan preserves select() semantics: return immediately when any fd is ready.
671 for (struct lwip_sock *sock : this->monitored_sockets_) {
672 if (esphome_lwip_socket_has_data(sock)) {
673 yield();
674 return;
675 }
676 }
677
678 // Sleep with instant wake via FreeRTOS task notification.
679 // Woken by: callback wrapper (socket data), wake_loop_threadsafe() (background tasks), or timeout.
680#endif
682}
683#endif // !USE_HOST
684
685} // namespace esphome
void original_setup()
void setup()
std::vector< struct lwip_sock * > monitored_sockets_
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()
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:82
void set_current_component(Component *component)
Component * get_current_component()
void yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
void register_serial_proxy(serial_proxy::SerialProxy *proxy)
bool register_socket(struct lwip_sock *sock)
Register/unregister a socket to be monitored for read events.
static void IRAM_ATTR wake_loop_any_context()
Wake from any context (ISR, thread, callback).
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.
std::vector< int > socket_fds_
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.
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_
void drain_wake_notifications_()
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 ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time)
void activate_looping_component_(uint16_t index)
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
const auto & get_devices()
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 ESPHOME_ALWAYS_INLINE after_loop_tasks_()
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)
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(), updates last_wdt_feed_, and re-dispatches t...
uint16_t current_loop_index_
bool is_socket_ready_(int fd) const
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 register_component_(T *comp)
Register a component, detecting loop() override at compile time.
void register_device(Device *device)
void unregister_socket_fd(int fd)
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
bool register_socket_fd(int fd)
Fallback select() path: monitors file descriptors.
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).
void unregister_socket(struct lwip_sock *sock)
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:522
static bool is_high_frequency()
Check whether the loop is running continuously.
Definition helpers.h:2153
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:210
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void 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)
const Component * component
Definition component.cpp:34
void wakeable_delay(uint32_t ms)
Definition wake.cpp:47
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Definition socket.cpp:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
runtime_stats::RuntimeStatsCollector * global_runtime_stats
void wake_loop_threadsafe()
Non-ISR: always inline.
Definition wake.cpp:74
void arch_init()
Definition core.cpp:39
constexpr uint8_t APP_STATE_SETUP_COMPLETE
Definition component.h:96
void HOT yield()
Definition core.cpp:25
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
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:867
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake.cpp.
Definition wake.cpp:20
void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken)
IRAM_ATTR entry points — defined in wake.cpp.
Definition wake.cpp:17
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:66
Platform-specific main loop wake primitives.