12#ifdef USE_RUNTIME_STATS
18static const char *
const TAG =
"component";
36struct ComponentErrorMessage {
44#ifdef USE_SETUP_PRIORITY_OVERRIDE
45struct ComponentPriorityOverride {
53std::vector<ComponentPriorityOverride> *setup_priority_overrides =
nullptr;
60std::vector<ComponentErrorMessage> *component_error_messages =
nullptr;
66 if (!component_error_messages) {
67 component_error_messages =
new std::vector<ComponentErrorMessage>();
70 for (
auto &entry : *component_error_messages) {
85static constexpr uint16_t WARN_IF_BLOCKING_INCREMENT_MS =
88#ifdef USE_LOOP_PRIORITY
99#pragma GCC diagnostic push
100#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
101 App.scheduler.set_interval(
this, name, interval, std::move(
f));
102#pragma GCC diagnostic pop
106 App.scheduler.set_interval(
this, name, interval, std::move(
f));
110#pragma GCC diagnostic push
111#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
112 return App.scheduler.cancel_interval(
this, name);
113#pragma GCC diagnostic pop
117 return App.scheduler.cancel_interval(
this, name);
120void Component::set_retry(
const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
121 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
122#pragma GCC diagnostic push
123#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
125#pragma GCC diagnostic pop
128void Component::set_retry(
const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
129 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
130#pragma GCC diagnostic push
131#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
133#pragma GCC diagnostic pop
136bool Component::cancel_retry(
const std::string &name) {
137#pragma GCC diagnostic push
138#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
139 return App.scheduler.cancel_retry(
this, name);
140#pragma GCC diagnostic pop
143bool Component::cancel_retry(
const char *name) {
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146 return App.scheduler.cancel_retry(
this, name);
147#pragma GCC diagnostic pop
151#pragma GCC diagnostic push
152#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
153 App.scheduler.set_timeout(
this, name, timeout, std::move(
f));
154#pragma GCC diagnostic pop
158 App.scheduler.set_timeout(
this, name, timeout, std::move(
f));
162#pragma GCC diagnostic push
163#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
164 return App.scheduler.cancel_timeout(
this, name);
165#pragma GCC diagnostic pop
169 return App.scheduler.cancel_timeout(
this, name);
174 App.scheduler.set_timeout(
this,
id, timeout, std::move(
f));
180 App.scheduler.set_timeout(
this,
id, timeout, std::move(
f));
186 App.scheduler.set_interval(
this,
id, interval, std::move(
f));
192 App.scheduler.set_interval(
this,
id, interval, std::move(
f));
197void Component::set_retry(uint32_t
id, uint32_t initial_wait_time, uint8_t max_attempts,
198 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
199#pragma GCC diagnostic push
200#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
202#pragma GCC diagnostic pop
205bool Component::cancel_retry(uint32_t
id) {
206#pragma GCC diagnostic push
207#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
208 return App.scheduler.cancel_retry(
this,
id);
209#pragma GCC diagnostic pop
218 const char *error_msg =
nullptr;
220 if (component_error_messages) {
221 for (
const auto &entry : *component_error_messages) {
222 if (entry.component ==
this) {
223 error_msg = entry.message;
231 error_msg ? (
is_flash_ptr ? LOG_STR_ARG((
const LogString *) error_msg) : error_msg)
232 : LOG_STR_LITERAL(
"unspecified"));
244#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
245 uint32_t start_time =
millis();
248#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
249 uint32_t setup_time =
millis() - start_time;
253 ESP_LOGCONFIG(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
255 ESP_LOGV(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
283 if (blocking_time + WARN_IF_BLOCKING_INCREMENT_MS <
blocking_time ||
284 blocking_time + WARN_IF_BLOCKING_INCREMENT_MS > std::numeric_limits<uint16_t>::max()) {
326#if (defined(USE_LWIP_FAST_SELECT) && defined(USE_ESP32)) || (defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP))
336 ESP_LOGI(TAG,
"%s is being reset to construction state", LOG_STR_ARG(this->
get_component_log_str()));
346 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), 0, std::move(
f));
349#pragma GCC diagnostic push
350#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
351 return App.scheduler.cancel_timeout(
this, name);
352#pragma GCC diagnostic pop
355 return App.scheduler.cancel_timeout(
this, name);
358#pragma GCC diagnostic push
359#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
360 App.scheduler.set_timeout(
this, name, 0, std::move(
f));
361#pragma GCC diagnostic pop
364 App.scheduler.set_timeout(
this, name, 0, std::move(
f));
367 App.scheduler.set_timeout(
this,
id, 0, std::move(
f));
371 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), timeout, std::move(
f));
374 App.scheduler.set_interval(
this,
static_cast<const char *
>(
nullptr), interval, std::move(
f));
376void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> &&f,
377 float backoff_increase_factor) {
378#pragma GCC diagnostic push
379#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
381#pragma GCC diagnostic pop
405 message ?
message : LOG_STR_LITERAL(
"unspecified"));
411 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
418 message ?
message : LOG_STR_LITERAL(
"unspecified"));
420 store_component_error_message(
this,
message,
false);
427 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
430 store_component_error_message(
this, LOG_STR_ARG(
message),
true);
457 uint32_t update_interval =
component->get_update_interval();
459 ESP_LOGCONFIG(tag,
" Update Interval: never");
460 }
else if (update_interval < 100) {
461 ESP_LOGCONFIG(tag,
" Update Interval: %.3fs", update_interval / 1000.0f);
463 ESP_LOGCONFIG(tag,
" Update Interval: %.1fs", update_interval / 1000.0f);
467#ifdef USE_SETUP_PRIORITY_OVERRIDE
469 if (setup_priority_overrides) {
471 for (
const auto &entry : *setup_priority_overrides) {
472 if (entry.component ==
this) {
473 return entry.priority;
480#ifdef USE_SETUP_PRIORITY_OVERRIDE
483 if (!setup_priority_overrides) {
484 setup_priority_overrides =
new std::vector<ComponentPriorityOverride>();
488 for (
auto &entry : *setup_priority_overrides) {
489 if (entry.component ==
this) {
496 setup_priority_overrides->emplace_back(ComponentPriorityOverride{
this,
priority});
530 ESP_LOGW(TAG,
"%s took a long time for an operation (%" PRIu32
" ms), max is 30 ms",
537 uint32_t curr_time =
millis();
539#ifdef USE_RUNTIME_STATS
546 warn_blocking(this->
component_, blocking_time);
551#ifdef USE_SETUP_PRIORITY_OVERRIDE
554 delete setup_priority_overrides;
555 setup_priority_overrides =
nullptr;
static void IRAM_ATTR wake_loop_any_context()
Wake the main event loop from any context (ISR, thread, or main loop).
void enable_component_loop_(Component *component)
void disable_component_loop_(Component *component)
volatile bool has_pending_enable_loop_requests_
void mark_failed()
Mark this component as failed.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
virtual float get_setup_priority() const
priority of setup().
virtual void setup()
Where the component's initialization should happen.
float get_actual_setup_priority() const
const LogString * get_component_log_str() const
Get the integration where this component was declared as a LogString for logging.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
const LogString * component_source_
bool set_status_flag_(uint8_t flag)
Helper to set a status LED flag on both this component and the app.
uint8_t get_component_state() const
void status_set_warning(const char *message=nullptr)
bool should_warn_of_blocking(uint32_t blocking_time)
volatile bool pending_enable_loop_
ISR-safe flag for enable_loop_soon_any_context.
virtual bool can_proceed()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual float get_loop_priority() const
priority of loop().
void status_clear_error()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
bool is_in_loop_state() const
Check if this component has completed setup and is in the loop state.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_defer(const std boo cancel_defer)(const char *name)
Cancel a defer callback using the specified name, name must not be empty.
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
virtual void dump_config()
void enable_loop()
Enable this component's loop.
void set_component_state_(uint8_t state)
Helper to set component state (clears state bits and sets new state)
bool status_has_warning() const
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> float backoff_increase_factor
bool status_has_error() const
void disable_loop()
Disable this component's loop.
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t initial_wait_time
virtual void loop()
This method will be called repeatedly.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t max_attempts
void reset_to_construction_state()
Reset this component back to the construction state to allow setup to run again.
void set_setup_priority(float priority)
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_interval(const std boo cancel_interval)(const char *name)
Cancel an interval function.
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
void status_clear_warning()
virtual void call_setup()
bool is_idle() const
Check if this component is idle.
This class simplifies creating components that periodically check a state.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void call_setup() override
virtual void set_update_interval(uint32_t update_interval)
Manually set the update interval in ms for this polling object.
uint32_t update_interval_
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
struct @65::@66 __attribute__
const Component * component
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint8_t COMPONENT_STATE_FAILED
runtime_stats::RuntimeStatsCollector * global_runtime_stats
InternalSchedulerID
Type-safe scheduler IDs for core base classes.
constexpr uint8_t COMPONENT_STATE_LOOP
constexpr uint8_t STATUS_LED_WARNING
constexpr uint8_t COMPONENT_STATE_MASK
void log_update_interval(const char *tag, PollingComponent *component)
void clear_setup_priority_overrides()
static void uint32_t blocking_time
constexpr uint16_t WARN_IF_BLOCKING_OVER_MS
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t COMPONENT_STATE_SETUP
constexpr uint8_t COMPONENT_STATE_CONSTRUCTION
constexpr uint8_t STATUS_LED_ERROR
constexpr uint32_t SCHEDULER_DONT_RUN