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) {
121#define ENTITY_TYPE_(type, singular, plural, count, upper) \
122 void register_##singular(type *obj) { this->plural##_.push_back(obj); } \
123 void register_##singular(type *obj, const char *name, uint32_t object_id_hash, uint32_t entity_fields) { \
124 obj->configure_entity_(name, object_id_hash, entity_fields); \
125 this->plural##_.push_back(obj); \
127#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
128 ENTITY_TYPE_(type, singular, plural, count, upper)
131#undef ENTITY_CONTROLLER_TYPE_
134#ifdef USE_SERIAL_PROXY
147 inline void ESPHOME_ALWAYS_INLINE
loop();
159 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
160 return this->
areas_[0]->get_name();
176 return std::string(buffer);
199 ESPDEPRECATED(
"Use get_build_time_string() instead. Removed in 2026.7.0",
"2026.1.0")
200 std::
string get_compilation_time() {
203 return std::string(buf);
226 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
249#elif defined(USE_ESP32)
260 static_assert(CONFIG_ESP_TASK_WDT_TIMEOUT_S >= 5,
261 "CONFIG_ESP_TASK_WDT_TIMEOUT_S must be at least 5s for a safe WDT feed interval");
263#elif defined(USE_ESP8266)
338#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
339 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
340 for (auto *obj : this->entities_member##_) { \
341 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
342 (include_internal || !obj->is_internal())) \
349#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
350 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
351 for (auto *obj : this->entities_member##_) { \
352 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
363#define ENTITY_TYPE_(type, singular, plural, count, upper) \
364 auto &get_##plural() const { return this->plural##_; } \
365 GET_ENTITY_METHOD(type, singular, plural)
366#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
367 ENTITY_TYPE_(type, singular, plural, count, upper)
370#undef ENTITY_CONTROLLER_TYPE_
373#ifdef USE_SERIAL_PROXY
383#if defined(USE_ESP32) || defined(USE_LIBRETINY)
386#elif defined(USE_ESP8266)
389#elif defined(USE_ZEPHYR)
401#ifdef USE_RUNTIME_STATS
404 friend void ::setup();
405 friend void ::original_setup();
429 if (source_index != 0)
430 comp->set_component_source_(source_index);
555#define ENTITY_TYPE_(type, singular, plural, count, upper) StaticVector<type *, count> plural##_{};
556#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
557 ENTITY_TYPE_(type, singular, plural, count, upper)
560#undef ENTITY_CONTROLLER_TYPE_
563#ifdef USE_SERIAL_PROXY
569extern Application
App;
583 const LogString *prev_;
599#ifdef USE_RUNTIME_STATS
607#ifdef USE_RUNTIME_STATS
612 component->runtime_stats_.record_time(elapsed_us);
617 uint32_t curr_time = MillisInternal::get();
622 if (blocking_time > WARN_IF_BLOCKING_OVER_MS) [[unlikely]] {
623 warn_blocking(blocking_time);
631#ifdef USE_RUNTIME_STATS
653 return this->scheduler.call(now);
670 this->app_.has_pending_enable_loop_requests_ = false;
671 this->app_.enable_pending_loops_();
675 this->app_.in_loop_ =
true;
679#if defined(USE_LWIP_FAST_SELECT) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
686 std::atomic_thread_fence(std::memory_order_acquire);
688#ifdef USE_RUNTIME_STATS
713#ifdef USE_RUNTIME_STATS
739 if (do_component_phase) {
752 last_op_end_time = guard.
finish();
757#ifdef USE_RUNTIME_STATS
758 loop_tail_start_us =
micros();
760 this->last_loop_ = last_op_end_time;
761 now = last_op_end_time;
765#ifdef USE_RUNTIME_STATS
773 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
774 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
775 ? loop_before_wall_us -
static_cast<uint32_t>(loop_before_scheduled_us)
779 uint32_t loop_tail_us = do_component_phase ? (loop_now_us - loop_tail_start_us) : 0;
807 const uint32_t until_sched = this->scheduler.next_schedule_in(now).value_or(until_phase);
808 delay_time = std::min(until_phase, until_sched);
816 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.
const LogString * current_source_
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_execution_context_(Component *component, const LogString *source, uint32_t now)
void service_status_led_slow_(uint32_t time)
Slow path for the status_led dispatch rate limit.
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_
const LogString * get_current_source()
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 set_current_source(const LogString *source)
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.
~LoopBlockingGuard()=default
LoopBlockingGuard(Component *component, const LogString *source, uint32_t now)
RAII guard that publishes a current source (e.g.
ScopedSourceGuard(const ScopedSourceGuard &)=delete
ScopedSourceGuard(const LogString *source)
ScopedSourceGuard & operator=(const ScopedSourceGuard &)=delete
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)
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
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
constexpr uint8_t WARN_IF_BLOCKING_OVER_CS
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.