27#ifdef USE_LWIP_FAST_SELECT
31#include <sys/select.h>
32#include <sys/socket.h>
35#include <netinet/in.h>
38#ifdef USE_RUNTIME_STATS
42#ifdef USE_BINARY_SENSOR
72#ifdef USE_DATETIME_DATE
75#ifdef USE_DATETIME_TIME
78#ifdef USE_DATETIME_DATETIME
93#ifdef USE_MEDIA_PLAYER
96#ifdef USE_ALARM_CONTROL_PANEL
99#ifdef USE_WATER_HEATER
105#ifdef USE_SERIAL_PROXY
122#ifdef USE_RUNTIME_STATS
124class RuntimeStatsCollector;
140 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
146static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000;
150#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
153 void pre_setup(
char *name,
size_t name_len,
char *friendly_name,
size_t friendly_name_len) {
157 constexpr size_t mac_address_len = 13;
159 constexpr size_t mac_address_suffix_len = 6;
160 char mac_addr[mac_address_len];
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);
175 void pre_setup(
const char *name,
size_t name_len,
const char *friendly_name,
size_t friendly_name_len) {
193#ifdef USE_BINARY_SENSOR
211#ifdef USE_TEXT_SENSOR
235#ifdef USE_DATETIME_DATE
239#ifdef USE_DATETIME_TIME
243#ifdef USE_DATETIME_DATETIME
263#ifdef USE_MEDIA_PLAYER
267#ifdef USE_ALARM_CONTROL_PANEL
273#ifdef USE_WATER_HEATER
281#ifdef USE_SERIAL_PROXY
302 inline void ESPHOME_ALWAYS_INLINE
loop();
314 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
315 return this->
areas_[0]->get_name();
331 return std::string(buffer);
354 ESPDEPRECATED(
"Use get_build_time_string() instead. Removed in 2026.7.0",
"2026.1.0")
355 std::
string get_compilation_time() {
358 return std::string(buf);
381 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
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())) \
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())) \
459#ifdef USE_BINARY_SENSOR
464 auto &get_switches()
const {
return this->
switches_; }
468 auto &get_buttons()
const {
return this->
buttons_; }
472 auto &get_sensors()
const {
return this->
sensors_; }
475#ifdef USE_TEXT_SENSOR
476 auto &get_text_sensors()
const {
return this->
text_sensors_; }
480 auto &get_fans()
const {
return this->
fans_; }
484 auto &get_covers()
const {
return this->
covers_; }
488 auto &get_lights()
const {
return this->
lights_; }
492 auto &get_climates()
const {
return this->
climates_; }
496 auto &get_numbers()
const {
return this->
numbers_; }
499#ifdef USE_DATETIME_DATE
500 auto &get_dates()
const {
return this->
dates_; }
503#ifdef USE_DATETIME_TIME
504 auto &get_times()
const {
return this->
times_; }
507#ifdef USE_DATETIME_DATETIME
508 auto &get_datetimes()
const {
return this->
datetimes_; }
512 auto &get_texts()
const {
return this->
texts_; }
516 auto &get_selects()
const {
return this->
selects_; }
520 auto &get_locks()
const {
return this->
locks_; }
524 auto &get_valves()
const {
return this->
valves_; }
527#ifdef USE_MEDIA_PLAYER
532#ifdef USE_ALARM_CONTROL_PANEL
537#ifdef USE_WATER_HEATER
543 auto &get_infrareds()
const {
return this->
infrareds_; }
547#ifdef USE_SERIAL_PROXY
557 auto &get_updates()
const {
return this->
updates_; }
565#ifdef USE_LWIP_FAST_SELECT
570#elif defined(USE_HOST)
595#ifdef USE_RUNTIME_STATS
598 friend void ::setup();
599 friend void ::original_setup();
693#ifdef USE_LWIP_FAST_SELECT
695#elif defined(USE_HOST)
746#ifdef USE_BINARY_SENSOR
761#ifdef USE_TEXT_SENSOR
779#ifdef USE_DATETIME_DATE
782#ifdef USE_DATETIME_TIME
785#ifdef USE_DATETIME_DATETIME
800#ifdef USE_MEDIA_PLAYER
803#ifdef USE_ALARM_CONTROL_PANEL
807#ifdef USE_WATER_HEATER
813#ifdef USE_SERIAL_PROXY
822extern Application
App;
830static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
836 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
841 while (::recvfrom(this->
wake_socket_fd_, buffer,
sizeof(buffer), 0,
nullptr,
nullptr) > 0) {
856 uint32_t last_op_end_time = this->scheduler.call(loop_start_time);
874 return last_op_end_time;
878#ifdef USE_RUNTIME_STATS
899#ifdef USE_RUNTIME_STATS
916 last_op_end_time = guard.finish();
921#ifdef USE_RUNTIME_STATS
926#ifdef USE_RUNTIME_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)
938 loop_now_us - loop_tail_start_us);
945 auto elapsed = last_op_end_time - this->
last_loop_;
948 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
951 next_schedule = std::max(next_schedule, delay_time / 2);
952 delay_time = std::min(next_schedule, delay_time);
955 this->last_loop_ = last_op_end_time;
958 this->process_dump_config_();
965#ifdef USE_LWIP_FAST_SELECT
968 if (delay_ms == 0) [[unlikely]] {
978 if (esphome_lwip_socket_has_data(sock)) {
StaticVector< switch_::Switch *, ESPHOME_ENTITY_SWITCH_COUNT > switches_
void enable_pending_loops_()
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)
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)
void setup_wake_loop_threadsafe_()
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)
bool name_add_mac_suffix_
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
void schedule_dump_config()
GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors) auto &get_switches() const
void register_text_sensor(text_sensor::TextSensor *sensor)
void run_safe_shutdown_hooks()
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 run_powerdown_hooks()
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...
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.
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Base class for all cover devices.
Infrared - Base class for infrared remote control implementations.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Base class for all locks.
Base-class for all numbers.
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.
Base-class for all sensors.
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
Base class for all switches.
Base-class for all text inputs.
Base class for all valve devices.
const Component * component
void wakeable_delay(uint32_t ms)
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Providing packet encoding functions for exchanging data with a remote host.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
void wake_loop_threadsafe()
Non-ISR: always inline.
constexpr uint8_t APP_STATE_SETUP_COMPLETE
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.
uint32_t IRAM_ATTR HOT millis()
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.
void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken)
IRAM_ATTR entry point — defined in wake.cpp.
static uint64_t global_recorded_us
SFINAE helper: detects whether T overrides Component::loop().
Platform-specific main loop wake primitives.