12#ifdef USE_RUNTIME_STATS
18static const char *
const TAG =
"component";
36struct ComponentErrorMessage {
44struct ComponentPriorityOverride {
51std::unique_ptr<std::vector<ComponentErrorMessage>> component_error_messages;
54std::unique_ptr<std::vector<ComponentPriorityOverride>> setup_priority_overrides;
60 if (!component_error_messages) {
61 component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
64 for (
auto &entry : *component_error_messages) {
76namespace setup_priority {
78const float BUS = 1000.0f;
79const float IO = 900.0f;
121#pragma GCC diagnostic push
122#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123 App.scheduler.set_interval(
this, name, interval, std::move(f));
124#pragma GCC diagnostic pop
128 App.scheduler.set_interval(
this, name, interval, std::move(f));
132#pragma GCC diagnostic push
133#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
134 return App.scheduler.cancel_interval(
this, name);
135#pragma GCC diagnostic pop
139 return App.scheduler.cancel_interval(
this, name);
142void Component::set_retry(
const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
143 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146 App.scheduler.set_retry(
this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
147#pragma GCC diagnostic pop
151 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
152 App.scheduler.set_retry(
this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
156#pragma GCC diagnostic push
157#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
158 return App.scheduler.cancel_retry(
this, name);
159#pragma GCC diagnostic pop
163 return App.scheduler.cancel_retry(
this, name);
167#pragma GCC diagnostic push
168#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
169 App.scheduler.set_timeout(
this, name, timeout, std::move(f));
170#pragma GCC diagnostic pop
174 App.scheduler.set_timeout(
this, name, timeout, std::move(f));
178#pragma GCC diagnostic push
179#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
180 return App.scheduler.cancel_timeout(
this, name);
181#pragma GCC diagnostic pop
185 return App.scheduler.cancel_timeout(
this, name);
190 App.scheduler.set_timeout(
this,
id, timeout, std::move(f));
196 App.scheduler.set_interval(
this,
id, interval, std::move(f));
202 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
203 App.scheduler.set_retry(
this,
id, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
214 const char *error_msg =
nullptr;
216 if (component_error_messages) {
217 for (
const auto &entry : *component_error_messages) {
218 if (entry.component ==
this) {
219 error_msg = entry.message;
227 error_msg ? (
is_flash_ptr ? LOG_STR_ARG((
const LogString *) error_msg) : error_msg)
228 : LOG_STR_LITERAL(
"unspecified"));
240#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
241 uint32_t start_time =
millis();
244#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
245 uint32_t setup_time =
millis() - start_time;
249 ESP_LOGCONFIG(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
251 ESP_LOGV(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
329 ESP_LOGI(TAG,
"%s is being reset to construction state", LOG_STR_ARG(this->
get_component_log_str()));
339 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), 0, std::move(f));
342#pragma GCC diagnostic push
343#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
344 return App.scheduler.cancel_timeout(
this, name);
345#pragma GCC diagnostic pop
348 return App.scheduler.cancel_timeout(
this, name);
351#pragma GCC diagnostic push
352#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
353 App.scheduler.set_timeout(
this, name, 0, std::move(f));
354#pragma GCC diagnostic pop
357 App.scheduler.set_timeout(
this, name, 0, std::move(f));
360 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), timeout, std::move(f));
363 App.scheduler.set_interval(
this,
static_cast<const char *
>(
nullptr), interval, std::move(f));
366 float backoff_increase_factor) {
367 App.scheduler.set_retry(
this,
"", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
387 message ?
message : LOG_STR_LITERAL(
"unspecified"));
396 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
405 message ?
message : LOG_STR_LITERAL(
"unspecified"));
407 store_component_error_message(
this,
message,
false);
416 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
419 store_component_error_message(
this, LOG_STR_ARG(
message),
true);
446 uint32_t update_interval =
component->get_update_interval();
447 if (update_interval == SCHEDULER_DONT_RUN) {
448 ESP_LOGCONFIG(tag,
" Update Interval: never");
449 }
else if (update_interval < 100) {
450 ESP_LOGCONFIG(tag,
" Update Interval: %.3fs", update_interval / 1000.0f);
452 ESP_LOGCONFIG(tag,
" Update Interval: %.1fs", update_interval / 1000.0f);
457 if (setup_priority_overrides) {
459 for (
const auto &entry : *setup_priority_overrides) {
460 if (entry.component ==
this) {
461 return entry.priority;
469 if (!setup_priority_overrides) {
470 setup_priority_overrides = std::make_unique<std::vector<ComponentPriorityOverride>>();
472 setup_priority_overrides->reserve(10);
476 for (
auto &entry : *setup_priority_overrides) {
477 if (entry.component ==
this) {
484 setup_priority_overrides->emplace_back(ComponentPriorityOverride{
this,
priority});
488#if defined(USE_HOST) || defined(CLANG_TIDY)
489 bool loop_overridden =
true;
490 bool call_loop_overridden =
true;
492#pragma GCC diagnostic push
493#pragma GCC diagnostic ignored "-Wpmf-conversions"
496#pragma GCC diagnostic pop
498 return loop_overridden || call_loop_overridden;
524 : started_(start_time), component_(
component) {}
526 uint32_t curr_time =
millis();
528 uint32_t blocking_time = curr_time - this->
started_;
530#ifdef USE_RUNTIME_STATS
543 ESP_LOGW(TAG,
"%s took a long time for an operation (%" PRIu32
" ms)",
546 ESP_LOGW(TAG,
"Components should block for at most 30 ms");
556 setup_priority_overrides.reset();
void enable_component_loop_(Component *component)
void disable_component_loop_(Component *component)
volatile bool has_pending_enable_loop_requests_
virtual void mark_failed()
Mark this component as failed.
virtual void call_dump_config()
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
bool has_overridden_loop() const
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_retry(const std voi set_retry)(const char *name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
Set an retry function with a unique name.
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_
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.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_retry(const std boo cancel_retry)(const char *name)
Cancel a retry function.
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
bool status_has_error() const
void disable_loop()
Disable this component's loop.
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.
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.
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_
~WarnIfComponentBlockingGuard()
WarnIfComponentBlockingGuard(Component *component, uint32_t start_time)
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
const Component * component
const float BUS
For communication buses like i2c/spi.
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
const float DATA
For components that import data from directly connected sensors like DHT.
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
const float IO
For components that represent GPIO pins like PCF8573.
const float LATE
For components that should be initialized at the very end of the setup process.
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
const float PROCESSOR
For components that use data from sensors like displays.
const float AFTER_BLUETOOTH
Providing packet encoding functions for exchanging data with a remote host.
const uint8_t COMPONENT_STATE_SETUP
const uint8_t COMPONENT_STATE_CONSTRUCTION
const uint8_t STATUS_LED_MASK
const uint16_t WARN_IF_BLOCKING_INCREMENT_MS
How long the blocking time must be larger to warn again.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
const uint8_t COMPONENT_STATE_FAILED
const uint8_t COMPONENT_STATE_MASK
void log_update_interval(const char *tag, PollingComponent *component)
const uint8_t COMPONENT_STATE_LOOP
const uint16_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
void clear_setup_priority_overrides()
const uint8_t STATUS_LED_OK
const uint8_t STATUS_LED_WARNING
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
const uint8_t COMPONENT_STATE_LOOP_DONE
const uint8_t STATUS_LED_ERROR