13#if defined(USE_LWIP_FAST_SELECT) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
35#ifdef USE_RUNTIME_STATS
41#ifdef USE_RUNTIME_STATS
43class RuntimeStatsCollector;
59 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
65static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000;
69#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
72 void pre_setup(
char *name,
size_t name_len,
char *friendly_name,
size_t friendly_name_len) {
76 constexpr size_t mac_address_suffix_len = 6;
77 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
81 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
82 if (friendly_name_len > 0) {
83 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
84 mac_address_suffix_len);
92 void pre_setup(
const char *name,
size_t name_len,
const char *friendly_name,
size_t friendly_name_len) {
117#define ENTITY_TYPE_(type, singular, plural, count, upper) \
118 void register_##singular(type *obj) { this->plural##_.push_back(obj); } \
119 void register_##singular(type *obj, const char *name, uint32_t object_id_hash, uint32_t entity_fields) { \
120 obj->configure_entity_(name, object_id_hash, entity_fields); \
121 this->plural##_.push_back(obj); \
123#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
124 ENTITY_TYPE_(type, singular, plural, count, upper)
127#undef ENTITY_CONTROLLER_TYPE_
130#ifdef USE_SERIAL_PROXY
143 inline void ESPHOME_ALWAYS_INLINE
loop();
155 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
156 return this->
areas_[0]->get_name();
172 return std::string(buffer);
195 ESPDEPRECATED(
"Use get_build_time_string() instead. Removed in 2026.7.0",
"2026.1.0")
196 std::
string get_compilation_time() {
199 return std::string(buf);
222 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
245#elif defined(USE_ESP32)
256 static_assert(CONFIG_ESP_TASK_WDT_TIMEOUT_S >= 5,
257 "CONFIG_ESP_TASK_WDT_TIMEOUT_S must be at least 5s for a safe WDT feed interval");
259#elif defined(USE_ESP8266)
334#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
335 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
336 for (auto *obj : this->entities_member##_) { \
337 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
338 (include_internal || !obj->is_internal())) \
345#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
346 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
347 for (auto *obj : this->entities_member##_) { \
348 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
359#define ENTITY_TYPE_(type, singular, plural, count, upper) \
360 auto &get_##plural() const { return this->plural##_; } \
361 GET_ENTITY_METHOD(type, singular, plural)
362#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
363 ENTITY_TYPE_(type, singular, plural, count, upper)
366#undef ENTITY_CONTROLLER_TYPE_
369#ifdef USE_SERIAL_PROXY
379#if defined(USE_ESP32) || defined(USE_LIBRETINY)
382#elif defined(USE_ESP8266)
385#elif defined(USE_ZEPHYR)
396#ifdef USE_RUNTIME_STATS
399 friend void ::setup();
400 friend void ::original_setup();
416 if (source_index != 0)
417 comp->set_component_source_(source_index);
541#define ENTITY_TYPE_(type, singular, plural, count, upper) StaticVector<type *, count> plural##_{};
542#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
543 ENTITY_TYPE_(type, singular, plural, count, upper)
546#undef ENTITY_CONTROLLER_TYPE_
549#ifdef USE_SERIAL_PROXY
555extern Application
App;
569 return this->scheduler.call(now);
586 this->app_.has_pending_enable_loop_requests_ = false;
587 this->app_.enable_pending_loops_();
591 this->app_.in_loop_ =
true;
595#if defined(USE_LWIP_FAST_SELECT) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
602 std::atomic_thread_fence(std::memory_order_acquire);
604#ifdef USE_RUNTIME_STATS
629#ifdef USE_RUNTIME_STATS
655 if (do_component_phase) {
671 last_op_end_time = guard.finish();
676#ifdef USE_RUNTIME_STATS
677 loop_tail_start_us =
micros();
679 this->last_loop_ = last_op_end_time;
680 now = last_op_end_time;
684#ifdef USE_RUNTIME_STATS
692 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
693 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
694 ? loop_before_wall_us -
static_cast<uint32_t>(loop_before_scheduled_us)
698 uint32_t loop_tail_us = do_component_phase ? (loop_now_us - loop_tail_start_us) : 0;
726 const uint32_t until_sched = this->scheduler.next_schedule_in(now).value_or(until_phase);
727 delay_time = std::min(until_phase, until_sched);
735 this->process_dump_config_();
ComponentPhaseGuard & operator=(const ComponentPhaseGuard &)=delete
ESPHOME_ALWAYS_INLINE ComponentPhaseGuard(Application &app)
ESPHOME_ALWAYS_INLINE ~ComponentPhaseGuard()
ComponentPhaseGuard(const ComponentPhaseGuard &)=delete
void enable_pending_loops_()
void set_loop_component_start_time_(uint32_t now)
Freshen the cached loop component start time. Called by Scheduler before each dispatch.
uint32_t ESPHOME_ALWAYS_INLINE scheduler_tick_(uint32_t now)
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t get_loop_interval() const
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
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 service_status_led_slow_(uint32_t time)
Slow path for the status_led dispatch rate limit.
void set_current_component(Component *component)
Component * get_current_component()
void register_serial_proxy(serial_proxy::SerialProxy *proxy)
static void IRAM_ATTR wake_loop_any_context()
Wake from any context (ISR, thread, callback).
static void IRAM_ATTR ESPHOME_ALWAYS_INLINE wake_loop_isrsafe()
Wake from ISR (ESP8266). No task_woken arg — no FreeRTOS. Caller must be IRAM_ATTR.
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
void __attribute__((noinline)) process_dump_config_()
Process dump_config output one component per loop iteration.
StaticVector< Area *, ESPHOME_AREA_COUNT > areas_
std::string get_comment()
Get the comment of this Application as a string.
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
void register_component_(T *comp, uint8_t source_index=0)
Register a component, detecting loop() override at compile time.
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len)
Pre-setup without MAC suffix: StringRef points directly at const string literals in flash.
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
bool any_component_has_status_flag_(uint8_t flag) const
Walk all registered components looking for any whose component_state_ has the given flag set.
void get_build_time_string(std::span< char, BUILD_TIME_STR_SIZE > buffer)
Copy the build time string into the provided buffer Buffer must be BUILD_TIME_STR_SIZE bytes (compile...
void register_area(Area *area)
Component * current_component_
static void wake_loop_isrsafe()
Wake from ISR (Zephyr). No task_woken arg — k_sem_give() handles ISR scheduling internally.
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void feed_wdt()
Feed the task watchdog.
void disable_component_loop_(Component *component)
const char * get_area() const
Get the area of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
void activate_looping_component_(uint16_t index)
const auto & get_devices()
uint32_t last_status_led_service_
ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0") std
Get the build time as a string (deprecated, use get_build_time_string() instead)
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
static constexpr size_t ESPHOME_COMMENT_SIZE_MAX
Maximum size of the comment buffer (including null terminator)
FixedVector< Component * > looping_components_
auto & get_serial_proxies() const
void add_looping_components_by_state_(bool match_loop_done)
bool name_add_mac_suffix_
volatile bool has_pending_enable_loop_requests_
void get_comment_string(std::span< char, ESPHOME_COMMENT_SIZE_MAX > buffer)
Copy the comment string into the provided buffer.
static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px)
Wake from ISR (ESP32 and LibreTiny).
void feed_wdt_slow_(uint32_t time)
Slow path for feed_wdt(): actually calls arch_feed_wdt() and updates last_wdt_feed_.
void schedule_dump_config()
void run_safe_shutdown_hooks()
uint16_t current_loop_index_
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
void ESPHOME_ALWAYS_INLINE loop()
Make a loop iteration. Call this in your loop() function.
time_t get_build_time()
Get the build time as a Unix timestamp.
StaticVector< serial_proxy::SerialProxy *, SERIAL_PROXY_COUNT > serial_proxies_
bool is_setup_complete() const
True once Application::setup() has finished walking all components and finalized the initial status f...
void register_device(Device *device)
static constexpr uint32_t STATUS_LED_DISPATCH_INTERVAL_MS
Dispatch interval for the status LED update.
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
StaticVector< Device *, ESPHOME_DEVICE_COUNT > devices_
void calculate_looping_components_()
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void register_component_impl_(Component *comp, bool has_loop)
uint8_t get_app_state() const
Return the public app state status bits (STATUS_LED_* only).
void run_powerdown_hooks()
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
static bool is_high_frequency()
Check whether the loop is running continuously.
Minimal static vector - saves memory by avoiding std::vector overhead.
StringRef is a reference to a string owned by something else.
void ESPHOME_ALWAYS_INLINE process_pending_stats(uint32_t current_time)
void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us)
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
const Component * component
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
void wake_loop_threadsafe()
Non-ISR: always inline.
constexpr uint8_t APP_STATE_SETUP_COMPLETE
void ESPHOME_ALWAYS_INLINE wake_drain_notifications()
uint32_t IRAM_ATTR HOT micros()
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.
Application App
Global storage of Application pointer - only one Application can exist.
void ESPHOME_ALWAYS_INLINE wake_loop_isrsafe()
ISR-safe: no task_woken arg because ESP8266 has no FreeRTOS. Caller must be IRAM_ATTR.
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake_esp8266.cpp.
static uint64_t global_recorded_us
SFINAE helper: detects whether T overrides Component::loop().
Platform-specific main loop wake primitives.