ESPHome 2026.3.0
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_RP2040)) && 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_SERIAL_PROXY
107#endif
108#ifdef USE_EVENT
110#endif
111#ifdef USE_UPDATE
113#endif
114
115namespace esphome::socket {
116#ifdef USE_SOCKET_SELECT_SUPPORT
118bool socket_ready_fd(int fd, bool loop_monitored); // NOLINT(readability-redundant-declaration)
119#endif
120} // namespace esphome::socket
121
122// Forward declarations for friend access from codegen-generated setup()
123void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
124void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
125
126namespace esphome {
127
131template<typename T, typename = void> struct HasLoopOverride : std::true_type {};
132template<typename T>
133struct HasLoopOverride<T, std::void_t<decltype(&T::loop)>>
134 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
135
136// Teardown timeout constant (in milliseconds)
137// For reboots, it's more important to shut down quickly than disconnect cleanly
138// since we're not entering deep sleep. The only consequence of not shutting down
139// cleanly is a warning in the log.
140static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000; // 1 second for quick reboot
141
143 public:
144#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
145 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
147 void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len) {
148 arch_init();
149 this->name_add_mac_suffix_ = true;
150 // MAC address length: 12 hex chars + null terminator
151 constexpr size_t mac_address_len = 13;
152 // MAC address suffix length (last 6 characters of 12-char MAC address string)
153 constexpr size_t mac_address_suffix_len = 6;
154 char mac_addr[mac_address_len];
156 // Overwrite the placeholder suffix in the mutable static buffers with actual MAC
157 // name is always non-empty (validated by validate_hostname in Python config)
158 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
159 if (friendly_name_len > 0) {
160 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
161 mac_address_suffix_len);
162 }
163 this->name_ = StringRef(name, name_len);
164 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
165 }
166#else
167 // Called before Logger::pre_setup() — must not log (global_logger is not yet set).
169 void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len) {
170 arch_init();
171 this->name_add_mac_suffix_ = false;
172 this->name_ = StringRef(name, name_len);
173 this->friendly_name_ = StringRef(friendly_name, friendly_name_len);
174 }
175#endif
176
177#ifdef USE_DEVICES
178 void register_device(Device *device) { this->devices_.push_back(device); }
179#endif
180#ifdef USE_AREAS
181 void register_area(Area *area) { this->areas_.push_back(area); }
182#endif
183
186
187#ifdef USE_BINARY_SENSOR
189 this->binary_sensors_.push_back(binary_sensor);
190 }
191#endif
192
193#ifdef USE_SENSOR
194 void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
195#endif
196
197#ifdef USE_SWITCH
198 void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
199#endif
200
201#ifdef USE_BUTTON
202 void register_button(button::Button *button) { this->buttons_.push_back(button); }
203#endif
204
205#ifdef USE_TEXT_SENSOR
206 void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
207#endif
208
209#ifdef USE_FAN
210 void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
211#endif
212
213#ifdef USE_COVER
214 void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
215#endif
216
217#ifdef USE_CLIMATE
218 void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
219#endif
220
221#ifdef USE_LIGHT
222 void register_light(light::LightState *light) { this->lights_.push_back(light); }
223#endif
224
225#ifdef USE_NUMBER
226 void register_number(number::Number *number) { this->numbers_.push_back(number); }
227#endif
228
229#ifdef USE_DATETIME_DATE
230 void register_date(datetime::DateEntity *date) { this->dates_.push_back(date); }
231#endif
232
233#ifdef USE_DATETIME_TIME
234 void register_time(datetime::TimeEntity *time) { this->times_.push_back(time); }
235#endif
236
237#ifdef USE_DATETIME_DATETIME
238 void register_datetime(datetime::DateTimeEntity *datetime) { this->datetimes_.push_back(datetime); }
239#endif
240
241#ifdef USE_TEXT
242 void register_text(text::Text *text) { this->texts_.push_back(text); }
243#endif
244
245#ifdef USE_SELECT
246 void register_select(select::Select *select) { this->selects_.push_back(select); }
247#endif
248
249#ifdef USE_LOCK
250 void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
251#endif
252
253#ifdef USE_VALVE
254 void register_valve(valve::Valve *valve) { this->valves_.push_back(valve); }
255#endif
256
257#ifdef USE_MEDIA_PLAYER
258 void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
259#endif
260
261#ifdef USE_ALARM_CONTROL_PANEL
263 this->alarm_control_panels_.push_back(a_alarm_control_panel);
264 }
265#endif
266
267#ifdef USE_WATER_HEATER
268 void register_water_heater(water_heater::WaterHeater *water_heater) { this->water_heaters_.push_back(water_heater); }
269#endif
270
271#ifdef USE_INFRARED
272 void register_infrared(infrared::Infrared *infrared) { this->infrareds_.push_back(infrared); }
273#endif
274
275#ifdef USE_SERIAL_PROXY
277 proxy->set_instance_index(this->serial_proxies_.size());
278 this->serial_proxies_.push_back(proxy);
279 }
280#endif
281
282#ifdef USE_EVENT
283 void register_event(event::Event *event) { this->events_.push_back(event); }
284#endif
285
286#ifdef USE_UPDATE
287 void register_update(update::UpdateEntity *update) { this->updates_.push_back(update); }
288#endif
289
291
293 void setup();
294
296 void loop();
297
299 const StringRef &get_name() const { return this->name_; }
300
302 const StringRef &get_friendly_name() const { return this->friendly_name_; }
303
305 const char *get_area() const {
306#ifdef USE_AREAS
307 // If we have areas registered, return the name of the first one (which is the top-level area)
308 if (!this->areas_.empty() && this->areas_[0] != nullptr) {
309 return this->areas_[0]->get_name();
310 }
311#endif
312 return "";
313 }
314
316 static constexpr size_t ESPHOME_COMMENT_SIZE_MAX = 256;
317
319 void get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer);
320
322 std::string get_comment() {
323 char buffer[ESPHOME_COMMENT_SIZE_MAX];
324 this->get_comment_string(buffer);
325 return std::string(buffer);
326 }
327
329
331 static constexpr size_t BUILD_TIME_STR_SIZE = 26;
332
335
338
340 time_t get_build_time();
341
344 void get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer);
345
347 // Remove before 2026.7.0
348 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0")
349 std::string get_compilation_time() {
350 char buf[BUILD_TIME_STR_SIZE];
351 this->get_build_time_string(buf);
352 return std::string(buf);
353 }
354
356 inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }
357
374 void set_loop_interval(uint32_t loop_interval) {
375 this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
376 }
377
378 uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
379
381
382 void feed_wdt(uint32_t time = 0);
383
384 void reboot();
385
386 void safe_reboot();
387
389
390 void run_powerdown_hooks();
391
396 void teardown_components(uint32_t timeout_ms);
397
398 uint8_t get_app_state() const { return this->app_state_; }
399
400// Helper macro for entity getter method declarations
401#ifdef USE_DEVICES
402#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
403 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
404 for (auto *obj : this->entities_member##_) { \
405 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
406 (include_internal || !obj->is_internal())) \
407 return obj; \
408 } \
409 return nullptr; \
410 }
411 const auto &get_devices() { return this->devices_; }
412#else
413#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
414 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
415 for (auto *obj : this->entities_member##_) { \
416 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
417 return obj; \
418 } \
419 return nullptr; \
420 }
421#endif // USE_DEVICES
422#ifdef USE_AREAS
423 const auto &get_areas() { return this->areas_; }
424#endif
425#ifdef USE_BINARY_SENSOR
426 auto &get_binary_sensors() const { return this->binary_sensors_; }
427 GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors)
428#endif
429#ifdef USE_SWITCH
430 auto &get_switches() const { return this->switches_; }
432#endif
433#ifdef USE_BUTTON
434 auto &get_buttons() const { return this->buttons_; }
436#endif
437#ifdef USE_SENSOR
438 auto &get_sensors() const { return this->sensors_; }
440#endif
441#ifdef USE_TEXT_SENSOR
442 auto &get_text_sensors() const { return this->text_sensors_; }
443 GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors)
444#endif
445#ifdef USE_FAN
446 auto &get_fans() const { return this->fans_; }
448#endif
449#ifdef USE_COVER
450 auto &get_covers() const { return this->covers_; }
452#endif
453#ifdef USE_LIGHT
454 auto &get_lights() const { return this->lights_; }
456#endif
457#ifdef USE_CLIMATE
458 auto &get_climates() const { return this->climates_; }
460#endif
461#ifdef USE_NUMBER
462 auto &get_numbers() const { return this->numbers_; }
464#endif
465#ifdef USE_DATETIME_DATE
466 auto &get_dates() const { return this->dates_; }
468#endif
469#ifdef USE_DATETIME_TIME
470 auto &get_times() const { return this->times_; }
472#endif
473#ifdef USE_DATETIME_DATETIME
474 auto &get_datetimes() const { return this->datetimes_; }
476#endif
477#ifdef USE_TEXT
478 auto &get_texts() const { return this->texts_; }
480#endif
481#ifdef USE_SELECT
482 auto &get_selects() const { return this->selects_; }
484#endif
485#ifdef USE_LOCK
486 auto &get_locks() const { return this->locks_; }
488#endif
489#ifdef USE_VALVE
490 auto &get_valves() const { return this->valves_; }
492#endif
493#ifdef USE_MEDIA_PLAYER
494 auto &get_media_players() const { return this->media_players_; }
495 GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players)
496#endif
497
498#ifdef USE_ALARM_CONTROL_PANEL
499 auto &get_alarm_control_panels() const { return this->alarm_control_panels_; }
500 GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels)
501#endif
502
503#ifdef USE_WATER_HEATER
504 auto &get_water_heaters() const { return this->water_heaters_; }
505 GET_ENTITY_METHOD(water_heater::WaterHeater, water_heater, water_heaters)
506#endif
507
508#ifdef USE_INFRARED
509 auto &get_infrareds() const { return this->infrareds_; }
511#endif
512
513#ifdef USE_SERIAL_PROXY
514 auto &get_serial_proxies() const { return this->serial_proxies_; }
515#endif
516
517#ifdef USE_EVENT
518 auto &get_events() const { return this->events_; }
520#endif
521
522#ifdef USE_UPDATE
523 auto &get_updates() const { return this->updates_; }
525#endif
526
527 Scheduler scheduler;
528
531#ifdef USE_LWIP_FAST_SELECT
534 bool register_socket(struct lwip_sock *sock);
535 void unregister_socket(struct lwip_sock *sock);
536#elif defined(USE_SOCKET_SELECT_SUPPORT)
540 bool register_socket_fd(int fd);
541 void unregister_socket_fd(int fd);
542#endif
543
544#ifdef USE_WAKE_LOOP_THREADSAFE
550#endif
551
552#ifdef USE_LWIP_FAST_SELECT
557 static void IRAM_ATTR wake_loop_isrsafe(int *px_higher_priority_task_woken) {
558 esphome_lwip_wake_main_loop_from_isr(px_higher_priority_task_woken);
559 }
560
561#ifdef USE_ESP32
565#endif
566#endif
567
568#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
571 static void IRAM_ATTR wake_loop_any_context() { socket::socket_wake(); }
572#elif defined(USE_RP2040) && defined(USE_SOCKET_IMPL_LWIP_TCP)
576#endif
577
578 protected:
579 friend Component;
580#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
581 friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
582#endif
583 friend void ::setup();
584 friend void ::original_setup();
585
586#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
587 bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); }
588#endif
589
592 template<typename T> void register_component_(T *comp) {
594 }
595
596 void register_component_impl_(Component *comp, bool has_loop);
597
599 void add_looping_components_by_state_(bool match_loop_done);
600
601 // These methods are called by Component::disable_loop() and Component::enable_loop()
602 // Components should not call these directly - use this->disable_loop() or this->enable_loop()
603 // to ensure component state is properly updated along with the loop partition
607 void activate_looping_component_(uint16_t index);
608 void before_loop_tasks_(uint32_t loop_start_time);
609 void after_loop_tasks_();
610
614 void __attribute__((noinline)) process_dump_config_();
615
617
619 void yield_with_select_(uint32_t delay_ms);
620
621#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
622 void setup_wake_loop_threadsafe_(); // Create wake notification socket
623 inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
624#endif
625
626 // === Member variables ordered by size to minimize padding ===
627
628 // Pointer-sized members first
630
631 // std::vector (3 pointers each: begin, end, capacity)
632 // Partitioned vector design for looping components
633 // =================================================
634 // Components are partitioned into [active | inactive] sections:
635 //
636 // looping_components_: [A, B, C, D | E, F]
637 // ^
638 // looping_components_active_end_ (4)
639 //
640 // - Components A,B,C,D are active and will be called in loop()
641 // - Components E,F are inactive (disabled/failed) and won't be called
642 // - No flag checking needed during iteration - just loop 0 to active_end_
643 // - When a component is disabled, it's swapped with the last active component
644 // and active_end_ is decremented
645 // - When a component is enabled, it's swapped with the first inactive component
646 // and active_end_ is incremented
647 // - This eliminates branch mispredictions from flag checking in the hot loop
649#ifdef USE_LWIP_FAST_SELECT
650 std::vector<struct lwip_sock *> monitored_sockets_; // Cached lwip_sock pointers for direct rcvevent read
651#elif defined(USE_SOCKET_SELECT_SUPPORT)
652 std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
653#endif
654#ifdef USE_SOCKET_SELECT_SUPPORT
655#if defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
656 int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
657#endif
658#endif
659
660 // StringRef members (8 bytes each: pointer + size)
663
664 // 4-byte members
667
668#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
669 int max_fd_{-1}; // Highest file descriptor number for select()
670#endif
671
672 // 2-byte members (grouped together for alignment)
673 uint16_t dump_config_at_{std::numeric_limits<uint16_t>::max()}; // Index into components_ for dump_config progress
674 uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
675 uint16_t looping_components_active_end_{0}; // Index marking end of active components in looping_components_
676 uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
677
678 // 1-byte members (grouped together to minimize padding)
679 uint8_t app_state_{0};
681 bool in_loop_{false};
683
684#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
685 bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
686#endif
687
688#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
689 // Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly)
690 fd_set read_fds_{}; // Working fd_set: populated by select()
691 fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
692#endif
693
694 // StaticVectors (largest members - contain actual array data inline)
696
697#ifdef USE_DEVICES
699#endif
700#ifdef USE_AREAS
702#endif
703#ifdef USE_BINARY_SENSOR
705#endif
706#ifdef USE_SWITCH
708#endif
709#ifdef USE_BUTTON
711#endif
712#ifdef USE_EVENT
714#endif
715#ifdef USE_SENSOR
717#endif
718#ifdef USE_TEXT_SENSOR
720#endif
721#ifdef USE_FAN
723#endif
724#ifdef USE_COVER
726#endif
727#ifdef USE_CLIMATE
729#endif
730#ifdef USE_LIGHT
732#endif
733#ifdef USE_NUMBER
735#endif
736#ifdef USE_DATETIME_DATE
738#endif
739#ifdef USE_DATETIME_TIME
741#endif
742#ifdef USE_DATETIME_DATETIME
744#endif
745#ifdef USE_SELECT
747#endif
748#ifdef USE_TEXT
750#endif
751#ifdef USE_LOCK
753#endif
754#ifdef USE_VALVE
756#endif
757#ifdef USE_MEDIA_PLAYER
759#endif
760#ifdef USE_ALARM_CONTROL_PANEL
763#endif
764#ifdef USE_WATER_HEATER
766#endif
767#ifdef USE_INFRARED
769#endif
770#ifdef USE_SERIAL_PROXY
772#endif
773#ifdef USE_UPDATE
775#endif
776};
777
779extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
780
781#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
782// Inline implementations for hot-path functions
783// drain_wake_notifications_() is called on every loop iteration
784
785// Small buffer for draining wake notification bytes (1 byte sent per wake)
786// Size allows draining multiple notifications per recvfrom() without wasting stack
787static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
788
790 // Called from main loop to drain any pending wake notifications
791 // Must check is_socket_ready_() to avoid blocking on empty socket
792 if (this->wake_socket_fd_ >= 0 && this->is_socket_ready_(this->wake_socket_fd_)) {
793 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
794 // Drain all pending notifications with non-blocking reads
795 // Multiple wake events may have triggered multiple writes, so drain until EWOULDBLOCK
796 // We control both ends of this loopback socket (always write 1 byte per wake),
797 // so no error checking needed - any errors indicate catastrophic system failure
798 while (lwip_recvfrom(this->wake_socket_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
799 // Just draining, no action needed - wake has already occurred
800 }
801 }
802}
803#endif // defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
804
805} // 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 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 FreeRTOS task.
void register_button(button::Button *button)
GET_ENTITY_METHOD(select::Select, select, selects) auto &get_locks() const
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()
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_
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 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)
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)
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
GET_ENTITY_METHOD(infrared::Infrared, infrared, infrareds) auto &get_serial_proxies() 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_
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 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_
static void wake_loop_any_context()
Wake the main event loop from any context.
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
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)
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)
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 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
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:374
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:209
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: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
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: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).
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 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:39
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.
static void uint32_t
SFINAE helper: detects whether T overrides Component::loop().