ESPHome 2026.4.1
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"
42#ifdef USE_BINARY_SENSOR
44#endif
45#ifdef USE_SENSOR
47#endif
48#ifdef USE_SWITCH
50#endif
51#ifdef USE_BUTTON
53#endif
54#ifdef USE_TEXT_SENSOR
56#endif
57#ifdef USE_FAN
59#endif
60#ifdef USE_CLIMATE
62#endif
63#ifdef USE_LIGHT
65#endif
66#ifdef USE_COVER
68#endif
69#ifdef USE_NUMBER
71#endif
72#ifdef USE_DATETIME_DATE
74#endif
75#ifdef USE_DATETIME_TIME
77#endif
78#ifdef USE_DATETIME_DATETIME
80#endif
81#ifdef USE_TEXT
83#endif
84#ifdef USE_SELECT
86#endif
87#ifdef USE_LOCK
89#endif
90#ifdef USE_VALVE
92#endif
93#ifdef USE_MEDIA_PLAYER
95#endif
96#ifdef USE_ALARM_CONTROL_PANEL
98#endif
99#ifdef USE_WATER_HEATER
101#endif
102#ifdef USE_INFRARED
104#endif
105#ifdef USE_SERIAL_PROXY
107#endif
108#ifdef USE_EVENT
110#endif
111#ifdef USE_UPDATE
113#endif
114
115namespace esphome::socket {
116#ifdef USE_HOST
118bool socket_ready_fd(int fd, bool loop_monitored); // NOLINT(readability-redundant-declaration)
119#endif
120} // namespace esphome::socket
121
122#ifdef USE_RUNTIME_STATS
123namespace esphome::runtime_stats {
124class RuntimeStatsCollector;
125} // namespace esphome::runtime_stats
126#endif
127
128// Forward declarations for friend access from codegen-generated setup()
129void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
130void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
131
132namespace esphome {
133
137template<typename T, typename = void> struct HasLoopOverride : std::true_type {};
138template<typename T>
139struct HasLoopOverride<T, std::void_t<decltype(&T::loop)>>
140 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
141
142// Teardown timeout constant (in milliseconds)
143// For reboots, it's more important to shut down quickly than disconnect cleanly
144// since we're not entering deep sleep. The only consequence of not shutting down
145// cleanly is a warning in the log.
146static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000; // 1 second for quick reboot
147
149 public:
150#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
151 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
153 void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len) {
154 arch_init();
155 this->name_add_mac_suffix_ = true;
156 // MAC address length: 12 hex chars + null terminator
157 constexpr size_t mac_address_len = 13;
158 // MAC address suffix length (last 6 characters of 12-char MAC address string)
159 constexpr size_t mac_address_suffix_len = 6;
160 char mac_addr[mac_address_len];
162 // Overwrite the placeholder suffix in the mutable static buffers with actual MAC
163 // name is always non-empty (validated by validate_hostname in Python config)
164 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
165 if (friendly_name_len > 0) {
166 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
167 mac_address_suffix_len);
168 }
169 this->name_ = StringRef(name, name_len);
170 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
171 }
172#else
173 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
175 void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len) {
176 arch_init();
177 this->name_add_mac_suffix_ = false;
178 this->name_ = StringRef(name, name_len);
179 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
180 }
181#endif
182
183#ifdef USE_DEVICES
184 void register_device(Device *device) { this->devices_.push_back(device); }
185#endif
186#ifdef USE_AREAS
187 void register_area(Area *area) { this->areas_.push_back(area); }
188#endif
189
192
193#ifdef USE_BINARY_SENSOR
195 this->binary_sensors_.push_back(binary_sensor);
196 }
197#endif
198
199#ifdef USE_SENSOR
200 void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
201#endif
202
203#ifdef USE_SWITCH
204 void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
205#endif
206
207#ifdef USE_BUTTON
208 void register_button(button::Button *button) { this->buttons_.push_back(button); }
209#endif
210
211#ifdef USE_TEXT_SENSOR
212 void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
213#endif
214
215#ifdef USE_FAN
216 void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
217#endif
218
219#ifdef USE_COVER
220 void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
221#endif
222
223#ifdef USE_CLIMATE
224 void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
225#endif
226
227#ifdef USE_LIGHT
228 void register_light(light::LightState *light) { this->lights_.push_back(light); }
229#endif
230
231#ifdef USE_NUMBER
232 void register_number(number::Number *number) { this->numbers_.push_back(number); }
233#endif
234
235#ifdef USE_DATETIME_DATE
236 void register_date(datetime::DateEntity *date) { this->dates_.push_back(date); }
237#endif
238
239#ifdef USE_DATETIME_TIME
240 void register_time(datetime::TimeEntity *time) { this->times_.push_back(time); }
241#endif
242
243#ifdef USE_DATETIME_DATETIME
244 void register_datetime(datetime::DateTimeEntity *datetime) { this->datetimes_.push_back(datetime); }
245#endif
246
247#ifdef USE_TEXT
248 void register_text(text::Text *text) { this->texts_.push_back(text); }
249#endif
250
251#ifdef USE_SELECT
252 void register_select(select::Select *select) { this->selects_.push_back(select); }
253#endif
254
255#ifdef USE_LOCK
256 void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
257#endif
258
259#ifdef USE_VALVE
260 void register_valve(valve::Valve *valve) { this->valves_.push_back(valve); }
261#endif
262
263#ifdef USE_MEDIA_PLAYER
264 void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
265#endif
266
267#ifdef USE_ALARM_CONTROL_PANEL
269 this->alarm_control_panels_.push_back(a_alarm_control_panel);
270 }
271#endif
272
273#ifdef USE_WATER_HEATER
274 void register_water_heater(water_heater::WaterHeater *water_heater) { this->water_heaters_.push_back(water_heater); }
275#endif
276
277#ifdef USE_INFRARED
278 void register_infrared(infrared::Infrared *infrared) { this->infrareds_.push_back(infrared); }
279#endif
280
281#ifdef USE_SERIAL_PROXY
283 proxy->set_instance_index(this->serial_proxies_.size());
284 this->serial_proxies_.push_back(proxy);
285 }
286#endif
287
288#ifdef USE_EVENT
289 void register_event(event::Event *event) { this->events_.push_back(event); }
290#endif
291
292#ifdef USE_UPDATE
293 void register_update(update::UpdateEntity *update) { this->updates_.push_back(update); }
294#endif
295
297
299 void setup();
300
302 inline void ESPHOME_ALWAYS_INLINE loop();
303
305 const StringRef &get_name() const { return this->name_; }
306
308 const StringRef &get_friendly_name() const { return this->friendly_name_; }
309
311 const char *get_area() const {
312#ifdef USE_AREAS
313 // If we have areas registered, return the name of the first one (which is the top-level area)
314 if (!this->areas_.empty() && this->areas_[0] != nullptr) {
315 return this->areas_[0]->get_name();
316 }
317#endif
318 return "";
319 }
320
322 static constexpr size_t ESPHOME_COMMENT_SIZE_MAX = 256;
323
325 void get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer);
326
328 std::string get_comment() {
329 char buffer[ESPHOME_COMMENT_SIZE_MAX];
330 this->get_comment_string(buffer);
331 return std::string(buffer);
332 }
333
335
337 static constexpr size_t BUILD_TIME_STR_SIZE = 26;
338
341
344
346 time_t get_build_time();
347
350 void get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer);
351
353 // Remove before 2026.7.0
354 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0")
355 std::string get_compilation_time() {
356 char buf[BUILD_TIME_STR_SIZE];
357 this->get_build_time_string(buf);
358 return std::string(buf);
359 }
360
362 inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }
363
380 void set_loop_interval(uint32_t loop_interval) {
381 this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
382 }
383
384 uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
385
387
391 static constexpr uint32_t WDT_FEED_INTERVAL_MS = 3;
392
395 void feed_wdt();
396
401 void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time) {
402 if (static_cast<uint32_t>(time - this->last_wdt_feed_) > WDT_FEED_INTERVAL_MS) [[unlikely]] {
403 this->feed_wdt_slow_(time);
404 }
405 }
406
407 void reboot();
408
409 void safe_reboot();
410
412
413 void run_powerdown_hooks();
414
419 void teardown_components(uint32_t timeout_ms);
420
424 uint8_t get_app_state() const { return this->app_state_ & ~APP_STATE_SETUP_COMPLETE; }
425
432 bool is_setup_complete() const { return (this->app_state_ & APP_STATE_SETUP_COMPLETE) != 0; }
433
434// Helper macro for entity getter method declarations
435#ifdef USE_DEVICES
436#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
437 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
438 for (auto *obj : this->entities_member##_) { \
439 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
440 (include_internal || !obj->is_internal())) \
441 return obj; \
442 } \
443 return nullptr; \
444 }
445 const auto &get_devices() { return this->devices_; }
446#else
447#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
448 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
449 for (auto *obj : this->entities_member##_) { \
450 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
451 return obj; \
452 } \
453 return nullptr; \
454 }
455#endif // USE_DEVICES
456#ifdef USE_AREAS
457 const auto &get_areas() { return this->areas_; }
458#endif
459#ifdef USE_BINARY_SENSOR
460 auto &get_binary_sensors() const { return this->binary_sensors_; }
461 GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors)
462#endif
463#ifdef USE_SWITCH
464 auto &get_switches() const { return this->switches_; }
466#endif
467#ifdef USE_BUTTON
468 auto &get_buttons() const { return this->buttons_; }
470#endif
471#ifdef USE_SENSOR
472 auto &get_sensors() const { return this->sensors_; }
474#endif
475#ifdef USE_TEXT_SENSOR
476 auto &get_text_sensors() const { return this->text_sensors_; }
477 GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors)
478#endif
479#ifdef USE_FAN
480 auto &get_fans() const { return this->fans_; }
482#endif
483#ifdef USE_COVER
484 auto &get_covers() const { return this->covers_; }
486#endif
487#ifdef USE_LIGHT
488 auto &get_lights() const { return this->lights_; }
490#endif
491#ifdef USE_CLIMATE
492 auto &get_climates() const { return this->climates_; }
494#endif
495#ifdef USE_NUMBER
496 auto &get_numbers() const { return this->numbers_; }
498#endif
499#ifdef USE_DATETIME_DATE
500 auto &get_dates() const { return this->dates_; }
502#endif
503#ifdef USE_DATETIME_TIME
504 auto &get_times() const { return this->times_; }
506#endif
507#ifdef USE_DATETIME_DATETIME
508 auto &get_datetimes() const { return this->datetimes_; }
510#endif
511#ifdef USE_TEXT
512 auto &get_texts() const { return this->texts_; }
514#endif
515#ifdef USE_SELECT
516 auto &get_selects() const { return this->selects_; }
518#endif
519#ifdef USE_LOCK
520 auto &get_locks() const { return this->locks_; }
522#endif
523#ifdef USE_VALVE
524 auto &get_valves() const { return this->valves_; }
526#endif
527#ifdef USE_MEDIA_PLAYER
528 auto &get_media_players() const { return this->media_players_; }
529 GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players)
530#endif
531
532#ifdef USE_ALARM_CONTROL_PANEL
533 auto &get_alarm_control_panels() const { return this->alarm_control_panels_; }
534 GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels)
535#endif
536
537#ifdef USE_WATER_HEATER
538 auto &get_water_heaters() const { return this->water_heaters_; }
539 GET_ENTITY_METHOD(water_heater::WaterHeater, water_heater, water_heaters)
540#endif
541
542#ifdef USE_INFRARED
543 auto &get_infrareds() const { return this->infrareds_; }
545#endif
546
547#ifdef USE_SERIAL_PROXY
548 auto &get_serial_proxies() const { return this->serial_proxies_; }
549#endif
550
551#ifdef USE_EVENT
552 auto &get_events() const { return this->events_; }
554#endif
555
556#ifdef USE_UPDATE
557 auto &get_updates() const { return this->updates_; }
559#endif
560
561 Scheduler scheduler;
562
565#ifdef USE_LWIP_FAST_SELECT
568 bool register_socket(struct lwip_sock *sock);
569 void unregister_socket(struct lwip_sock *sock);
570#elif defined(USE_HOST)
574 bool register_socket_fd(int fd);
575 void unregister_socket_fd(int fd);
576#endif
577
581
582#ifdef USE_ESP32
584 static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px) { esphome::wake_loop_isrsafe(px); }
585#endif
586
589
590 protected:
591 friend Component;
592#ifdef USE_HOST
593 friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
594#endif
595#ifdef USE_RUNTIME_STATS
597#endif
598 friend void ::setup();
599 friend void ::original_setup();
600#ifdef USE_HOST
601 friend void wake_loop_threadsafe(); // Host platform accesses wake_socket_fd_
602#endif
603
604#ifdef USE_HOST
605 bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); }
606#endif
607
612 bool any_component_has_status_flag_(uint8_t flag) const;
613
616 template<typename T> void register_component_(T *comp) {
618 }
619
620 void register_component_impl_(Component *comp, bool has_loop);
621
623 // FixedVector capacity was pre-initialized by codegen with the exact count
624 // of components that override loop(), computed at C++ compile time.
625
626 // Add all components with loop override that aren't already LOOP_DONE
627 // Some components (like logger) may call disable_loop() during initialization
628 // before setup runs, so we need to respect their LOOP_DONE state
631 // Then add any components that are already LOOP_DONE to the inactive section
632 // This handles components that called disable_loop() during initialization
634 }
635 void add_looping_components_by_state_(bool match_loop_done);
636
637 // These methods are called by Component::disable_loop() and Component::enable_loop()
638 // Components should not call these directly - use this->disable_loop() or this->enable_loop()
639 // to ensure component state is properly updated along with the loop partition
643 void activate_looping_component_(uint16_t index);
644 inline uint32_t ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time);
645 inline void ESPHOME_ALWAYS_INLINE after_loop_tasks_() { this->in_loop_ = false; }
646
650 void __attribute__((noinline)) process_dump_config_();
651
655 void feed_wdt_slow_(uint32_t time);
656
658#ifdef USE_HOST
659 // select() fallback path is too complex to inline (host platform)
660 void yield_with_select_(uint32_t delay_ms);
661#else
662 inline void ESPHOME_ALWAYS_INLINE yield_with_select_(uint32_t delay_ms);
663#endif
664
665#ifdef USE_HOST
666 void setup_wake_loop_threadsafe_(); // Create wake notification socket
667 inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
668#endif
669
670 // === Member variables ordered by size to minimize padding ===
671
672 // Pointer-sized members first
674
675 // std::vector (3 pointers each: begin, end, capacity)
676 // Partitioned vector design for looping components
677 // =================================================
678 // Components are partitioned into [active | inactive] sections:
679 //
680 // looping_components_: [A, B, C, D | E, F]
681 // ^
682 // looping_components_active_end_ (4)
683 //
684 // - Components A,B,C,D are active and will be called in loop()
685 // - Components E,F are inactive (disabled/failed) and won't be called
686 // - No flag checking needed during iteration - just loop 0 to active_end_
687 // - When a component is disabled, it's swapped with the last active component
688 // and active_end_ is decremented
689 // - When a component is enabled, it's swapped with the first inactive component
690 // and active_end_ is incremented
691 // - This eliminates branch mispredictions from flag checking in the hot loop
693#ifdef USE_LWIP_FAST_SELECT
694 std::vector<struct lwip_sock *> monitored_sockets_; // Cached lwip_sock pointers for direct rcvevent read
695#elif defined(USE_HOST)
696 std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
697#endif
698#ifdef USE_HOST
699 int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
700#endif
701
702 // StringRef members (8 bytes each: pointer + size)
705
706 // 4-byte members
709 uint32_t last_wdt_feed_{0}; // millis() of most recent arch_feed_wdt(); rate-limits feed_wdt() hot path
710
711#ifdef USE_HOST
712 int max_fd_{-1}; // Highest file descriptor number for select()
713#endif
714
715 // 2-byte members (grouped together for alignment)
716 uint16_t dump_config_at_{std::numeric_limits<uint16_t>::max()}; // Index into components_ for dump_config progress
717 uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
718 uint16_t looping_components_active_end_{0}; // Index marking end of active components in looping_components_
719 uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
720
721 // 1-byte members (grouped together to minimize padding)
722 uint8_t app_state_{0};
724 bool in_loop_{false};
726
727#ifdef USE_HOST
728 bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
729#endif
730
731#ifdef USE_HOST
732 // Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly)
733 fd_set read_fds_{}; // Working fd_set: populated by select()
734 fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
735#endif
736
737 // StaticVectors (largest members - contain actual array data inline)
739
740#ifdef USE_DEVICES
742#endif
743#ifdef USE_AREAS
745#endif
746#ifdef USE_BINARY_SENSOR
748#endif
749#ifdef USE_SWITCH
751#endif
752#ifdef USE_BUTTON
754#endif
755#ifdef USE_EVENT
757#endif
758#ifdef USE_SENSOR
760#endif
761#ifdef USE_TEXT_SENSOR
763#endif
764#ifdef USE_FAN
766#endif
767#ifdef USE_COVER
769#endif
770#ifdef USE_CLIMATE
772#endif
773#ifdef USE_LIGHT
775#endif
776#ifdef USE_NUMBER
778#endif
779#ifdef USE_DATETIME_DATE
781#endif
782#ifdef USE_DATETIME_TIME
784#endif
785#ifdef USE_DATETIME_DATETIME
787#endif
788#ifdef USE_SELECT
790#endif
791#ifdef USE_TEXT
793#endif
794#ifdef USE_LOCK
796#endif
797#ifdef USE_VALVE
799#endif
800#ifdef USE_MEDIA_PLAYER
802#endif
803#ifdef USE_ALARM_CONTROL_PANEL
806#endif
807#ifdef USE_WATER_HEATER
809#endif
810#ifdef USE_INFRARED
812#endif
813#ifdef USE_SERIAL_PROXY
815#endif
816#ifdef USE_UPDATE
818#endif
819};
820
822extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
823
824#ifdef USE_HOST
825// Inline implementations for hot-path functions
826// drain_wake_notifications_() is called on every loop iteration
827
828// Small buffer for draining wake notification bytes (1 byte sent per wake)
829// Size allows draining multiple notifications per recvfrom() without wasting stack
830static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
831
833 // Called from main loop to drain any pending wake notifications
834 // Must check is_socket_ready_() to avoid blocking on empty socket
835 if (this->wake_socket_fd_ >= 0 && this->is_socket_ready_(this->wake_socket_fd_)) {
836 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
837 // Drain all pending notifications with non-blocking reads
838 // Multiple wake events may have triggered multiple writes, so drain until EWOULDBLOCK
839 // We control both ends of this loopback socket (always write 1 byte per wake),
840 // so no error checking needed - any errors indicate catastrophic system failure
841 while (::recvfrom(this->wake_socket_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
842 // Just draining, no action needed - wake has already occurred
843 }
844 }
845}
846#endif // USE_HOST
847
848inline uint32_t ESPHOME_ALWAYS_INLINE Application::before_loop_tasks_(uint32_t loop_start_time) {
849#ifdef USE_HOST
850 // Drain wake notifications first to clear socket for next wake
852#endif
853
854 // Scheduler::call feeds the WDT per item and returns the timestamp of the
855 // last fired item, or the input unchanged when nothing ran.
856 uint32_t last_op_end_time = this->scheduler.call(loop_start_time);
857
858 // Process any pending enable_loop requests from ISRs
859 // This must be done before marking in_loop_ = true to avoid race conditions
861 // Clear flag BEFORE processing to avoid race condition
862 // If ISR sets it during processing, we'll catch it next loop iteration
863 // This is safe because:
864 // 1. Each component has its own pending_enable_loop_ flag that we check
865 // 2. If we can't process a component (wrong state), enable_pending_loops_()
866 // will set this flag back to true
867 // 3. Any new ISR requests during processing will set the flag again
869 this->enable_pending_loops_();
870 }
871
872 // Mark that we're in the loop for safe reentrant modifications
873 this->in_loop_ = true;
874 return last_op_end_time;
875}
876
877inline void ESPHOME_ALWAYS_INLINE Application::loop() {
878#ifdef USE_RUNTIME_STATS
879 // Capture the start of the active (non-sleeping) portion of this iteration.
880 // Used to derive main-loop overhead = active time − Σ(component time) −
881 // before/tail splits recorded below.
882 uint32_t loop_active_start_us = micros();
883 // Snapshot the cumulative component-recorded time so we can subtract the
884 // slice that the scheduler spends inside its own WarnIfComponentBlockingGuard
885 // (scheduler.cpp) — that time is already counted in per-component stats,
886 // so charging it again to "before" would double-count.
887 uint64_t loop_recorded_snap = ComponentRuntimeStats::global_recorded_us;
888#endif
889 // Get the initial loop time at the start
890 uint32_t last_op_end_time = millis();
891
892 // Returned timestamp keeps us monotonic with last_wdt_feed_ (advanced by
893 // the scheduler's per-item feeds) without an extra millis() call.
894 last_op_end_time = this->before_loop_tasks_(last_op_end_time);
895 // Guarantee a WDT touch every tick — covers configs with no looping
896 // components and no scheduler work, where the per-item / per-component
897 // feeds never fire. Rate-limited inline fast path, ~free when unneeded.
898 this->feed_wdt_with_time(last_op_end_time);
899#ifdef USE_RUNTIME_STATS
900 uint32_t loop_before_end_us = micros();
901 uint64_t loop_before_scheduled_us = ComponentRuntimeStats::global_recorded_us - loop_recorded_snap;
902#endif
903
905 this->current_loop_index_++) {
907
908 // Update the cached time before each component runs
909 this->loop_component_start_time_ = last_op_end_time;
910
911 {
912 this->set_current_component(component);
913 WarnIfComponentBlockingGuard guard{component, last_op_end_time};
914 component->loop();
915 // Use the finish method to get the current time as the end time
916 last_op_end_time = guard.finish();
917 }
918 this->feed_wdt_with_time(last_op_end_time);
919 }
920
921#ifdef USE_RUNTIME_STATS
922 uint32_t loop_tail_start_us = micros();
923#endif
924 this->after_loop_tasks_();
925
926#ifdef USE_RUNTIME_STATS
927 // Process any pending runtime stats printing after all components have run
928 // This ensures stats printing doesn't affect component timing measurements
929 if (global_runtime_stats != nullptr) {
930 uint32_t loop_now_us = micros();
931 // Subtract scheduled-component time from the "before" bucket so it is
932 // not double-counted (it is already attributed to per-component stats).
933 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
934 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
935 ? loop_before_wall_us - static_cast<uint32_t>(loop_before_scheduled_us)
936 : 0;
937 global_runtime_stats->record_loop_active(loop_now_us - loop_active_start_us, loop_before_overhead_us,
938 loop_now_us - loop_tail_start_us);
940 }
941#endif
942
943 // Use the last component's end time instead of calling millis() again
944 uint32_t delay_time = 0;
945 auto elapsed = last_op_end_time - this->last_loop_;
947 delay_time = this->loop_interval_ - elapsed;
948 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
949 // next_schedule is max 0.5*delay_time
950 // otherwise interval=0 schedules result in constant looping with almost no sleep
951 next_schedule = std::max(next_schedule, delay_time / 2);
952 delay_time = std::min(next_schedule, delay_time);
953 }
954 this->yield_with_select_(delay_time);
955 this->last_loop_ = last_op_end_time;
956
957 if (this->dump_config_at_ < this->components_.size()) {
958 this->process_dump_config_();
959 }
960}
961
962// Inline yield_with_select_ for all paths except the select() fallback
963#ifndef USE_HOST
964inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay_ms) {
965#ifdef USE_LWIP_FAST_SELECT
966 // Fast path (ESP32/LibreTiny): reads rcvevent directly from cached lwip_sock pointers.
967 // Safe because this runs on the main loop which owns socket lifetime (create, read, close).
968 if (delay_ms == 0) [[unlikely]] {
969 yield();
970 return;
971 }
972
973 // Check if any socket already has pending data before sleeping.
974 // If a socket still has unread data (rcvevent > 0) but the task notification was already
975 // consumed, ulTaskNotifyTake would block until timeout — adding up to delay_ms latency.
976 // This scan preserves select() semantics: return immediately when any fd is ready.
977 for (struct lwip_sock *sock : this->monitored_sockets_) {
978 if (esphome_lwip_socket_has_data(sock)) {
979 yield();
980 return;
981 }
982 }
983
984 // Sleep with instant wake via FreeRTOS task notification.
985 // Woken by: callback wrapper (socket data), wake_loop_threadsafe() (background tasks), or timeout.
986#endif
988}
989#endif // !USE_HOST
990
991} // namespace esphome
void original_setup()
void setup()
StaticVector< switch_::Switch *, ESPHOME_ENTITY_SWITCH_COUNT > switches_
StaticVector< light::LightState *, ESPHOME_ENTITY_LIGHT_COUNT > lights_
std::vector< struct lwip_sock * > monitored_sockets_
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time)
uint32_t get_loop_interval() const
StaticVector< valve::Valve *, ESPHOME_ENTITY_VALVE_COUNT > valves_
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void register_fan(fan::Fan *state)
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void register_button(button::Button *button)
GET_ENTITY_METHOD(select::Select, select, selects) auto &get_locks() const
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
StaticVector< datetime::TimeEntity *, ESPHOME_ENTITY_TIME_COUNT > times_
GET_ENTITY_METHOD(fan::Fan, fan, fans) auto &get_covers() const
void register_light(light::LightState *light)
const auto & get_areas()
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
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.
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.
StaticVector< binary_sensor::BinarySensor *, ESPHOME_ENTITY_BINARY_SENSOR_COUNT > binary_sensors_
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).
void register_infrared(infrared::Infrared *infrared)
void register_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
GET_ENTITY_METHOD(datetime::TimeEntity, time, times) auto &get_datetimes() const
StaticVector< select::Select *, ESPHOME_ENTITY_SELECT_COUNT > selects_
GET_ENTITY_METHOD(climate::Climate, climate, climates) auto &get_numbers() const
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.
void register_update(update::UpdateEntity *update)
StaticVector< Area *, ESPHOME_AREA_COUNT > areas_
void register_media_player(media_player::MediaPlayer *media_player)
std::string get_comment()
Get the comment of this Application as a string.
GET_ENTITY_METHOD(valve::Valve, valve, valves) auto &get_media_players() const
StaticVector< fan::Fan *, ESPHOME_ENTITY_FAN_COUNT > fans_
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
std::vector< int > socket_fds_
void register_number(number::Number *number)
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
GET_ENTITY_METHOD(update::UpdateEntity, update, updates) Scheduler scheduler
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_
GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels) auto &get_water_heaters() const
void register_climate(climate::Climate *climate)
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.
StaticVector< datetime::DateEntity *, ESPHOME_ENTITY_DATE_COUNT > dates_
void register_cover(cover::Cover *cover)
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...
StaticVector< media_player::MediaPlayer *, ESPHOME_ENTITY_MEDIA_PLAYER_COUNT > media_players_
GET_ENTITY_METHOD(text::Text, text, texts) auto &get_selects() const
GET_ENTITY_METHOD(water_heater::WaterHeater, water_heater, water_heaters) auto &get_infrareds() const
StaticVector< update::UpdateEntity *, ESPHOME_ENTITY_UPDATE_COUNT > updates_
void register_water_heater(water_heater::WaterHeater *water_heater)
void register_area(Area *area)
Component * current_component_
GET_ENTITY_METHOD(datetime::DateEntity, date, dates) auto &get_times() const
void register_datetime(datetime::DateTimeEntity *datetime)
GET_ENTITY_METHOD(light::LightState, light, lights) auto &get_climates() const
void drain_wake_notifications_()
StaticVector< infrared::Infrared *, ESPHOME_ENTITY_INFRARED_COUNT > infrareds_
void register_time(datetime::TimeEntity *time)
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players) auto &get_alarm_control_panels() const
GET_ENTITY_METHOD(infrared::Infrared, infrared, infrareds) auto &get_serial_proxies() const
StaticVector< climate::Climate *, ESPHOME_ENTITY_CLIMATE_COUNT > climates_
void feed_wdt()
Feed the task watchdog.
GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors) auto &get_fans() const
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)
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
const auto & get_devices()
StaticVector< cover::Cover *, ESPHOME_ENTITY_COVER_COUNT > covers_
StaticVector< lock::Lock *, ESPHOME_ENTITY_LOCK_COUNT > locks_
void register_switch(switch_::Switch *a_switch)
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 register_lock(lock::Lock *a_lock)
StaticVector< event::Event *, ESPHOME_ENTITY_EVENT_COUNT > events_
void ESPHOME_ALWAYS_INLINE after_loop_tasks_()
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
void register_event(event::Event *event)
void register_valve(valve::Valve *valve)
static constexpr size_t ESPHOME_COMMENT_SIZE_MAX
Maximum size of the comment buffer (including null terminator)
FixedVector< Component * > looping_components_
void add_looping_components_by_state_(bool match_loop_done)
void register_sensor(sensor::Sensor *sensor)
StaticVector< alarm_control_panel::AlarmControlPanel *, ESPHOME_ENTITY_ALARM_CONTROL_PANEL_COUNT > alarm_control_panels_
volatile bool has_pending_enable_loop_requests_
GET_ENTITY_METHOD(datetime::DateTimeEntity, datetime, datetimes) auto &get_texts() const
GET_ENTITY_METHOD(sensor::Sensor, sensor, sensors) auto &get_text_sensors() const
StaticVector< text_sensor::TextSensor *, ESPHOME_ENTITY_TEXT_SENSOR_COUNT > text_sensors_
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 only).
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...
StaticVector< datetime::DateTimeEntity *, ESPHOME_ENTITY_DATETIME_COUNT > datetimes_
GET_ENTITY_METHOD(switch_::Switch, switch, switches) auto &get_buttons() const
GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors) auto &get_switches() const
void register_text_sensor(text_sensor::TextSensor *sensor)
uint16_t current_loop_index_
StaticVector< button::Button *, ESPHOME_ENTITY_BUTTON_COUNT > buttons_
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< text::Text *, ESPHOME_ENTITY_TEXT_COUNT > texts_
StaticVector< serial_proxy::SerialProxy *, SERIAL_PROXY_COUNT > serial_proxies_
void register_select(select::Select *select)
bool is_setup_complete() const
True once Application::setup() has finished walking all components and finalized the initial status f...
void register_text(text::Text *text)
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)
GET_ENTITY_METHOD(event::Event, event, events) auto &get_updates() const
StaticVector< water_heater::WaterHeater *, ESPHOME_ENTITY_WATER_HEATER_COUNT > water_heaters_
GET_ENTITY_METHOD(cover::Cover, cover, covers) auto &get_lights() const
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_
StaticVector< number::Number *, ESPHOME_ENTITY_NUMBER_COUNT > numbers_
void calculate_looping_components_()
auto & get_binary_sensors() const
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).
GET_ENTITY_METHOD(number::Number, number, numbers) auto &get_dates() const
GET_ENTITY_METHOD(lock::Lock, lock, locks) auto &get_valves() const
void unregister_socket(struct lwip_sock *sock)
StaticVector< sensor::Sensor *, ESPHOME_ENTITY_SENSOR_COUNT > sensors_
GET_ENTITY_METHOD(button::Button, button, buttons) auto &get_sensors() const
auto & get_events() const
void register_date(datetime::DateEntity *date)
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:2138
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
Base class for all binary_sensor-type classes.
Base class for all buttons.
Definition button.h:25
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:187
Base class for all cover devices.
Definition cover.h:110
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:114
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
Base class for all locks.
Definition lock.h:110
Base-class for all numbers.
Definition number.h:29
void process_pending_stats(uint32_t current_time)
void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us)
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:47
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
Base class for all switches.
Definition switch.h:38
Base-class for all text inputs.
Definition text.h:21
Base class for all valve devices.
Definition valve.h:104
const Component * component
Definition component.cpp:34
bool state
Definition fan.h:2
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:850
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 — defined in wake.cpp.
Definition wake.cpp:20
void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken)
IRAM_ATTR entry point — 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().
Platform-specific main loop wake primitives.