ESPHome 2026.3.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_SOCKET_SELECT_SUPPORT
28#ifdef USE_LWIP_FAST_SELECT
30#else
31#include <sys/select.h>
32#ifdef USE_WAKE_LOOP_THREADSAFE
33#include <lwip/sockets.h>
34#endif
35#endif
36#endif // USE_SOCKET_SELECT_SUPPORT
37#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
38namespace esphome::socket {
39void socket_wake(); // NOLINT(readability-redundant-declaration)
40} // namespace esphome::socket
41#endif
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_EVENT
107#endif
108#ifdef USE_UPDATE
110#endif
111
112namespace esphome::socket {
113#ifdef USE_SOCKET_SELECT_SUPPORT
115bool socket_ready_fd(int fd, bool loop_monitored); // NOLINT(readability-redundant-declaration)
116#endif
117} // namespace esphome::socket
118
119// Forward declarations for friend access from codegen-generated setup()
120void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
121void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
122
123namespace esphome {
124
128template<typename T, typename = void> struct HasLoopOverride : std::true_type {};
129template<typename T>
130struct HasLoopOverride<T, std::void_t<decltype(&T::loop)>>
131 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
132
133// Teardown timeout constant (in milliseconds)
134// For reboots, it's more important to shut down quickly than disconnect cleanly
135// since we're not entering deep sleep. The only consequence of not shutting down
136// cleanly is a warning in the log.
137static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000; // 1 second for quick reboot
138
140 public:
141 void pre_setup(const std::string &name, const std::string &friendly_name, bool name_add_mac_suffix) {
142 arch_init();
143 this->name_add_mac_suffix_ = name_add_mac_suffix;
144 if (name_add_mac_suffix) {
145 // MAC address length: 12 hex chars + null terminator
146 constexpr size_t mac_address_len = 13;
147 // MAC address suffix length (last 6 characters of 12-char MAC address string)
148 constexpr size_t mac_address_suffix_len = 6;
149 char mac_addr[mac_address_len];
151 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
152 this->name_ = make_name_with_suffix(name, '-', mac_suffix_ptr, mac_address_suffix_len);
153 if (!friendly_name.empty()) {
154 this->friendly_name_ = make_name_with_suffix(friendly_name, ' ', mac_suffix_ptr, mac_address_suffix_len);
155 }
156 } else {
157 this->name_ = name;
158 this->friendly_name_ = friendly_name;
159 }
160 }
161
162#ifdef USE_DEVICES
163 void register_device(Device *device) { this->devices_.push_back(device); }
164#endif
165#ifdef USE_AREAS
166 void register_area(Area *area) { this->areas_.push_back(area); }
167#endif
168
171
172#ifdef USE_BINARY_SENSOR
174 this->binary_sensors_.push_back(binary_sensor);
175 }
176#endif
177
178#ifdef USE_SENSOR
179 void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
180#endif
181
182#ifdef USE_SWITCH
183 void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
184#endif
185
186#ifdef USE_BUTTON
187 void register_button(button::Button *button) { this->buttons_.push_back(button); }
188#endif
189
190#ifdef USE_TEXT_SENSOR
191 void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
192#endif
193
194#ifdef USE_FAN
195 void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
196#endif
197
198#ifdef USE_COVER
199 void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
200#endif
201
202#ifdef USE_CLIMATE
203 void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
204#endif
205
206#ifdef USE_LIGHT
207 void register_light(light::LightState *light) { this->lights_.push_back(light); }
208#endif
209
210#ifdef USE_NUMBER
211 void register_number(number::Number *number) { this->numbers_.push_back(number); }
212#endif
213
214#ifdef USE_DATETIME_DATE
215 void register_date(datetime::DateEntity *date) { this->dates_.push_back(date); }
216#endif
217
218#ifdef USE_DATETIME_TIME
219 void register_time(datetime::TimeEntity *time) { this->times_.push_back(time); }
220#endif
221
222#ifdef USE_DATETIME_DATETIME
223 void register_datetime(datetime::DateTimeEntity *datetime) { this->datetimes_.push_back(datetime); }
224#endif
225
226#ifdef USE_TEXT
227 void register_text(text::Text *text) { this->texts_.push_back(text); }
228#endif
229
230#ifdef USE_SELECT
231 void register_select(select::Select *select) { this->selects_.push_back(select); }
232#endif
233
234#ifdef USE_LOCK
235 void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
236#endif
237
238#ifdef USE_VALVE
239 void register_valve(valve::Valve *valve) { this->valves_.push_back(valve); }
240#endif
241
242#ifdef USE_MEDIA_PLAYER
243 void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
244#endif
245
246#ifdef USE_ALARM_CONTROL_PANEL
248 this->alarm_control_panels_.push_back(a_alarm_control_panel);
249 }
250#endif
251
252#ifdef USE_WATER_HEATER
253 void register_water_heater(water_heater::WaterHeater *water_heater) { this->water_heaters_.push_back(water_heater); }
254#endif
255
256#ifdef USE_INFRARED
257 void register_infrared(infrared::Infrared *infrared) { this->infrareds_.push_back(infrared); }
258#endif
259
260#ifdef USE_EVENT
261 void register_event(event::Event *event) { this->events_.push_back(event); }
262#endif
263
264#ifdef USE_UPDATE
265 void register_update(update::UpdateEntity *update) { this->updates_.push_back(update); }
266#endif
267
269
271 void setup();
272
274 void loop();
275
277 const std::string &get_name() const { return this->name_; }
278
280 const std::string &get_friendly_name() const { return this->friendly_name_; }
281
283 const char *get_area() const {
284#ifdef USE_AREAS
285 // If we have areas registered, return the name of the first one (which is the top-level area)
286 if (!this->areas_.empty() && this->areas_[0] != nullptr) {
287 return this->areas_[0]->get_name();
288 }
289#endif
290 return "";
291 }
292
294 static constexpr size_t ESPHOME_COMMENT_SIZE_MAX = 256;
295
297 void get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer);
298
300 std::string get_comment() {
301 char buffer[ESPHOME_COMMENT_SIZE_MAX];
302 this->get_comment_string(buffer);
303 return std::string(buffer);
304 }
305
307
309 static constexpr size_t BUILD_TIME_STR_SIZE = 26;
310
312 uint32_t get_config_hash();
313
315 uint32_t get_config_version_hash();
316
318 time_t get_build_time();
319
322 void get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer);
323
325 // Remove before 2026.7.0
326 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0")
327 std::string get_compilation_time() {
328 char buf[BUILD_TIME_STR_SIZE];
329 this->get_build_time_string(buf);
330 return std::string(buf);
331 }
332
334 inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }
335
352 void set_loop_interval(uint32_t loop_interval) {
353 this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
354 }
355
356 uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
357
359
360 void feed_wdt(uint32_t time = 0);
361
362 void reboot();
363
364 void safe_reboot();
365
367
368 void run_powerdown_hooks();
369
374 void teardown_components(uint32_t timeout_ms);
375
376 uint8_t get_app_state() const { return this->app_state_; }
377
378// Helper macro for entity getter method declarations
379#ifdef USE_DEVICES
380#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
381 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
382 for (auto *obj : this->entities_member##_) { \
383 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
384 (include_internal || !obj->is_internal())) \
385 return obj; \
386 } \
387 return nullptr; \
388 }
389 const auto &get_devices() { return this->devices_; }
390#else
391#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
392 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
393 for (auto *obj : this->entities_member##_) { \
394 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
395 return obj; \
396 } \
397 return nullptr; \
398 }
399#endif // USE_DEVICES
400#ifdef USE_AREAS
401 const auto &get_areas() { return this->areas_; }
402#endif
403#ifdef USE_BINARY_SENSOR
404 auto &get_binary_sensors() const { return this->binary_sensors_; }
405 GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors)
406#endif
407#ifdef USE_SWITCH
408 auto &get_switches() const { return this->switches_; }
410#endif
411#ifdef USE_BUTTON
412 auto &get_buttons() const { return this->buttons_; }
414#endif
415#ifdef USE_SENSOR
416 auto &get_sensors() const { return this->sensors_; }
418#endif
419#ifdef USE_TEXT_SENSOR
420 auto &get_text_sensors() const { return this->text_sensors_; }
421 GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors)
422#endif
423#ifdef USE_FAN
424 auto &get_fans() const { return this->fans_; }
426#endif
427#ifdef USE_COVER
428 auto &get_covers() const { return this->covers_; }
430#endif
431#ifdef USE_LIGHT
432 auto &get_lights() const { return this->lights_; }
434#endif
435#ifdef USE_CLIMATE
436 auto &get_climates() const { return this->climates_; }
438#endif
439#ifdef USE_NUMBER
440 auto &get_numbers() const { return this->numbers_; }
442#endif
443#ifdef USE_DATETIME_DATE
444 auto &get_dates() const { return this->dates_; }
446#endif
447#ifdef USE_DATETIME_TIME
448 auto &get_times() const { return this->times_; }
450#endif
451#ifdef USE_DATETIME_DATETIME
452 auto &get_datetimes() const { return this->datetimes_; }
454#endif
455#ifdef USE_TEXT
456 auto &get_texts() const { return this->texts_; }
458#endif
459#ifdef USE_SELECT
460 auto &get_selects() const { return this->selects_; }
462#endif
463#ifdef USE_LOCK
464 auto &get_locks() const { return this->locks_; }
466#endif
467#ifdef USE_VALVE
468 auto &get_valves() const { return this->valves_; }
470#endif
471#ifdef USE_MEDIA_PLAYER
472 auto &get_media_players() const { return this->media_players_; }
473 GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players)
474#endif
475
476#ifdef USE_ALARM_CONTROL_PANEL
477 auto &get_alarm_control_panels() const { return this->alarm_control_panels_; }
478 GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels)
479#endif
480
481#ifdef USE_WATER_HEATER
482 auto &get_water_heaters() const { return this->water_heaters_; }
483 GET_ENTITY_METHOD(water_heater::WaterHeater, water_heater, water_heaters)
484#endif
485
486#ifdef USE_INFRARED
487 auto &get_infrareds() const { return this->infrareds_; }
489#endif
490
491#ifdef USE_EVENT
492 auto &get_events() const { return this->events_; }
494#endif
495
496#ifdef USE_UPDATE
497 auto &get_updates() const { return this->updates_; }
499#endif
500
501 Scheduler scheduler;
502
504#ifdef USE_SOCKET_SELECT_SUPPORT
509 bool register_socket_fd(int fd);
510 void unregister_socket_fd(int fd);
511
512#ifdef USE_WAKE_LOOP_THREADSAFE
518#endif
519
520#ifdef USE_LWIP_FAST_SELECT
525 static void IRAM_ATTR wake_loop_isrsafe(int *px_higher_priority_task_woken) {
526 esphome_lwip_wake_main_loop_from_isr(px_higher_priority_task_woken);
527 }
528
529#ifdef USE_ESP32
533#endif
534#endif
535#endif
536
537#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
540 static void IRAM_ATTR wake_loop_any_context() { socket::socket_wake(); }
541#endif
542
543 protected:
544 friend Component;
545#ifdef USE_SOCKET_SELECT_SUPPORT
546 friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
547#endif
548 friend void ::setup();
549 friend void ::original_setup();
550
551#ifdef USE_SOCKET_SELECT_SUPPORT
557#ifdef USE_LWIP_FAST_SELECT
558 bool is_socket_ready_(int fd) const { return esphome_lwip_socket_has_data(fd); }
559#else
560 bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); }
561#endif
562#endif
563
566 template<typename T> void register_component_(T *comp) {
568 }
569
570 void register_component_impl_(Component *comp, bool has_loop);
571
573 void add_looping_components_by_state_(bool match_loop_done);
574
575 // These methods are called by Component::disable_loop() and Component::enable_loop()
576 // Components should not call these directly - use this->disable_loop() or this->enable_loop()
577 // to ensure component state is properly updated along with the loop partition
581 void activate_looping_component_(uint16_t index);
582 void before_loop_tasks_(uint32_t loop_start_time);
583 void after_loop_tasks_();
584
588 void __attribute__((noinline)) process_dump_config_();
589
591
593 void yield_with_select_(uint32_t delay_ms);
594
595#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
596 void setup_wake_loop_threadsafe_(); // Create wake notification socket
597 inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
598#endif
599
600 // === Member variables ordered by size to minimize padding ===
601
602 // Pointer-sized members first
604
605 // std::vector (3 pointers each: begin, end, capacity)
606 // Partitioned vector design for looping components
607 // =================================================
608 // Components are partitioned into [active | inactive] sections:
609 //
610 // looping_components_: [A, B, C, D | E, F]
611 // ^
612 // looping_components_active_end_ (4)
613 //
614 // - Components A,B,C,D are active and will be called in loop()
615 // - Components E,F are inactive (disabled/failed) and won't be called
616 // - No flag checking needed during iteration - just loop 0 to active_end_
617 // - When a component is disabled, it's swapped with the last active component
618 // and active_end_ is decremented
619 // - When a component is enabled, it's swapped with the first inactive component
620 // and active_end_ is incremented
621 // - This eliminates branch mispredictions from flag checking in the hot loop
623#ifdef USE_SOCKET_SELECT_SUPPORT
624 std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
625#if defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
626 int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
627#endif
628#endif
629
630 // std::string members (typically 24-32 bytes each)
631 std::string name_;
632 std::string friendly_name_;
633
634 // 4-byte members
635 uint32_t last_loop_{0};
637
638#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
639 int max_fd_{-1}; // Highest file descriptor number for select()
640#endif
641
642 // 2-byte members (grouped together for alignment)
643 uint16_t dump_config_at_{std::numeric_limits<uint16_t>::max()}; // Index into components_ for dump_config progress
644 uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
645 uint16_t looping_components_active_end_{0}; // Index marking end of active components in looping_components_
646 uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
647
648 // 1-byte members (grouped together to minimize padding)
649 uint8_t app_state_{0};
651 bool in_loop_{false};
653
654#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
655 bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
656#endif
657
658#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
659 // Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly)
660 fd_set read_fds_{}; // Working fd_set: populated by select()
661 fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
662#endif
663
664 // StaticVectors (largest members - contain actual array data inline)
666
667#ifdef USE_DEVICES
669#endif
670#ifdef USE_AREAS
672#endif
673#ifdef USE_BINARY_SENSOR
675#endif
676#ifdef USE_SWITCH
678#endif
679#ifdef USE_BUTTON
681#endif
682#ifdef USE_EVENT
684#endif
685#ifdef USE_SENSOR
687#endif
688#ifdef USE_TEXT_SENSOR
690#endif
691#ifdef USE_FAN
693#endif
694#ifdef USE_COVER
696#endif
697#ifdef USE_CLIMATE
699#endif
700#ifdef USE_LIGHT
702#endif
703#ifdef USE_NUMBER
705#endif
706#ifdef USE_DATETIME_DATE
708#endif
709#ifdef USE_DATETIME_TIME
711#endif
712#ifdef USE_DATETIME_DATETIME
714#endif
715#ifdef USE_SELECT
717#endif
718#ifdef USE_TEXT
720#endif
721#ifdef USE_LOCK
723#endif
724#ifdef USE_VALVE
726#endif
727#ifdef USE_MEDIA_PLAYER
729#endif
730#ifdef USE_ALARM_CONTROL_PANEL
733#endif
734#ifdef USE_WATER_HEATER
736#endif
737#ifdef USE_INFRARED
739#endif
740#ifdef USE_UPDATE
742#endif
743};
744
746extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
747
748#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
749// Inline implementations for hot-path functions
750// drain_wake_notifications_() is called on every loop iteration
751
752// Small buffer for draining wake notification bytes (1 byte sent per wake)
753// Size allows draining multiple notifications per recvfrom() without wasting stack
754static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
755
757 // Called from main loop to drain any pending wake notifications
758 // Must check is_socket_ready_() to avoid blocking on empty socket
759 if (this->wake_socket_fd_ >= 0 && this->is_socket_ready_(this->wake_socket_fd_)) {
760 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
761 // Drain all pending notifications with non-blocking reads
762 // Multiple wake events may have triggered multiple writes, so drain until EWOULDBLOCK
763 // We control both ends of this loopback socket (always write 1 byte per wake),
764 // so no error checking needed - any errors indicate catastrophic system failure
765 while (lwip_recvfrom(this->wake_socket_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
766 // Just draining, no action needed - wake has already occurred
767 }
768 }
769}
770#endif // defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
771
772} // namespace esphome
void original_setup()
void setup()
StaticVector< switch_::Switch *, ESPHOME_ENTITY_SWITCH_COUNT > switches_
StaticVector< light::LightState *, ESPHOME_ENTITY_LIGHT_COUNT > lights_
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t get_loop_interval() const
StaticVector< valve::Valve *, ESPHOME_ENTITY_VALVE_COUNT > valves_
void register_fan(fan::Fan *state)
void wake_loop_threadsafe()
Wake the main event loop from another FreeRTOS task.
void register_button(button::Button *button)
GET_ENTITY_METHOD(select::Select, select, selects) auto &get_locks() const
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
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_
GET_ENTITY_METHOD(infrared::Infrared, infrared, infrareds) auto &get_events() const
void set_current_component(Component *component)
Component * get_current_component()
static void IRAM_ATTR wake_loop_isrsafe(int *px_higher_priority_task_woken)
Wake the main event loop from an ISR.
StaticVector< binary_sensor::BinarySensor *, ESPHOME_ENTITY_BINARY_SENSOR_COUNT > binary_sensors_
static void IRAM_ATTR wake_loop_any_context()
Wake the main event loop from any context (ISR, thread, or main loop).
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)
GET_ENTITY_METHOD(update::UpdateEntity, update, updates) Scheduler scheduler
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)
void pre_setup(const std::string &name, const std::string &friendly_name, bool name_add_mac_suffix)
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 feed_wdt(uint32_t time=0)
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
StaticVector< climate::Climate *, ESPHOME_ENTITY_CLIMATE_COUNT > climates_
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)
const auto & get_devices()
StaticVector< cover::Cover *, ESPHOME_ENTITY_COVER_COUNT > covers_
StaticVector< lock::Lock *, ESPHOME_ENTITY_LOCK_COUNT > locks_
std::string friendly_name_
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)
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void register_lock(lock::Lock *a_lock)
StaticVector< event::Event *, ESPHOME_ENTITY_EVENT_COUNT > events_
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.
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
Fast path for Socket::ready() via friendship - skips negative fd check.
time_t get_build_time()
Get the build time as a Unix timestamp.
StaticVector< text::Text *, ESPHOME_ENTITY_TEXT_COUNT > texts_
void register_select(select::Select *select)
void before_loop_tasks_(uint32_t loop_start_time)
void loop()
Make a loop iteration. Call this in your loop() function.
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)
Register/unregister a socket file descriptor to be monitored for read events.
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 yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
void register_component_impl_(Component *comp, bool has_loop)
uint8_t get_app_state() const
GET_ENTITY_METHOD(number::Number, number, numbers) auto &get_dates() const
GET_ENTITY_METHOD(lock::Lock, lock, locks) auto &get_valves() const
StaticVector< sensor::Sensor *, ESPHOME_ENTITY_SENSOR_COUNT > sensors_
GET_ENTITY_METHOD(button::Button, button, buttons) auto &get_sensors() const
void register_date(datetime::DateEntity *date)
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:209
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:186
Base class for all cover devices.
Definition cover.h:110
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:110
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
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:47
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:37
bool state
Definition fan.h:2
void IRAM_ATTR esphome_lwip_wake_main_loop_any_context(void)
Wake the main loop task from any context (ISR, thread, or main loop).
bool esphome_lwip_socket_has_data(int fd)
Check if a LwIP socket has data ready via direct rcvevent read (~215 ns per socket).
void IRAM_ATTR esphome_lwip_wake_main_loop_from_isr(int *px_higher_priority_task_woken)
Wake the main loop task from an ISR — costs <1 us.
void IRAM_ATTR socket_wake()
Signal socket/IO activity and wake the main loop from esp_delay() early.
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
void arch_init()
Definition core.cpp:38
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:811
Application App
Global storage of Application pointer - only one Application can exist.
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Optimized string concatenation: name + separator + suffix (const char* overload) Uses a fixed stack b...
Definition helpers.cpp:281
SFINAE helper: detects whether T overrides Component::loop().