15static const char *
const TAG =
"component";
33struct ComponentErrorMessage {
38#ifdef USE_SETUP_PRIORITY_OVERRIDE
39struct ComponentPriorityOverride {
47std::vector<ComponentPriorityOverride> *setup_priority_overrides =
nullptr;
54std::vector<ComponentErrorMessage> *component_error_messages =
nullptr;
57void store_component_error_message(
const Component *
component,
const LogString *
message) {
59 if (!component_error_messages) {
60 component_error_messages =
new std::vector<ComponentErrorMessage>();
63 for (
auto &entry : *component_error_messages) {
70 component_error_messages->emplace_back(ComponentErrorMessage{
component,
message});
77static constexpr uint16_t WARN_IF_BLOCKING_INCREMENT_MS =
89#pragma GCC diagnostic push
90#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
91 App.
scheduler.set_interval(
this, name, interval, std::move(
f));
92#pragma GCC diagnostic pop
96 App.
scheduler.set_interval(
this, name, interval, std::move(
f));
100#pragma GCC diagnostic push
101#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
103#pragma GCC diagnostic pop
110void Component::set_retry(
const std::string &name,
uint32_t initial_wait_time, uint8_t max_attempts,
111 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
112#pragma GCC diagnostic push
113#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
115#pragma GCC diagnostic pop
118void Component::set_retry(
const char *name,
uint32_t initial_wait_time, uint8_t max_attempts,
119 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
120#pragma GCC diagnostic push
121#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123#pragma GCC diagnostic pop
126bool Component::cancel_retry(
const std::string &name) {
127#pragma GCC diagnostic push
128#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
130#pragma GCC diagnostic pop
133bool Component::cancel_retry(
const char *name) {
134#pragma GCC diagnostic push
135#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
137#pragma GCC diagnostic pop
141#pragma GCC diagnostic push
142#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
143 App.
scheduler.set_timeout(
this, name, timeout, std::move(
f));
144#pragma GCC diagnostic pop
148 App.
scheduler.set_timeout(
this, name, timeout, std::move(
f));
152#pragma GCC diagnostic push
153#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
155#pragma GCC diagnostic pop
176 App.
scheduler.set_interval(
this,
id, interval, std::move(
f));
182 App.
scheduler.set_interval(
this,
id, interval, std::move(
f));
187void Component::set_retry(
uint32_t id,
uint32_t initial_wait_time, uint8_t max_attempts,
188 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
189#pragma GCC diagnostic push
190#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
192#pragma GCC diagnostic pop
195bool Component::cancel_retry(
uint32_t id) {
196#pragma GCC diagnostic push
197#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
199#pragma GCC diagnostic pop
207 const LogString *error_msg =
nullptr;
208 if (component_error_messages) {
209 for (
const auto &entry : *component_error_messages) {
210 if (entry.component ==
this) {
211 error_msg = entry.message;
217 error_msg ? LOG_STR_ARG(error_msg) : LOG_STR_LITERAL(
"unspecified"));
228#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
232#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
236 if (setup_time >= WARN_IF_BLOCKING_OVER_MS) {
237 ESP_LOGCONFIG(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
239 ESP_LOGV(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
264 if (blocking_time > threshold_ms) {
266 uint32_t new_threshold_ms = blocking_time + WARN_IF_BLOCKING_INCREMENT_MS;
267 uint32_t new_cs = new_threshold_ms / 10U;
311 ESP_LOGI(TAG,
"%s is being reset to construction state", LOG_STR_ARG(this->
get_component_log_str()));
318 App.
scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), 0, std::move(
f));
321#pragma GCC diagnostic push
322#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
324#pragma GCC diagnostic pop
330#pragma GCC diagnostic push
331#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
333#pragma GCC diagnostic pop
343 App.
scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), timeout, std::move(
f));
346 App.
scheduler.set_interval(
this,
static_cast<const char *
>(
nullptr), interval, std::move(
f));
348void Component::set_retry(
uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> &&f,
349 float backoff_increase_factor) {
350#pragma GCC diagnostic push
351#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
353#pragma GCC diagnostic pop
375 message ?
message : LOG_STR_LITERAL(
"unspecified"));
381 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
388 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
390 store_component_error_message(
this,
message);
428 ESP_LOGCONFIG(
tag,
" Update Interval: never");
429 }
else if (update_interval < 100) {
430 ESP_LOGCONFIG(
tag,
" Update Interval: %.3fs", update_interval / 1000.0f);
432 ESP_LOGCONFIG(
tag,
" Update Interval: %.1fs", update_interval / 1000.0f);
436#ifdef USE_SETUP_PRIORITY_OVERRIDE
438 if (setup_priority_overrides) {
440 for (
const auto &entry : *setup_priority_overrides) {
441 if (entry.component ==
this) {
442 return entry.priority;
449#ifdef USE_SETUP_PRIORITY_OVERRIDE
452 if (!setup_priority_overrides) {
453 setup_priority_overrides =
new std::vector<ComponentPriorityOverride>();
457 for (
auto &entry : *setup_priority_overrides) {
458 if (entry.component ==
this) {
465 setup_priority_overrides->emplace_back(ComponentPriorityOverride{
this,
priority});
490#ifdef USE_RUNTIME_STATS
498 should_warn =
component->should_warn_of_blocking(blocking_time);
503 ESP_LOGW(TAG,
"%s took a long time for an operation (%" PRIu32
" ms), max is 30 ms",
504 component ==
nullptr ? LOG_STR_LITERAL(
"<null>") : LOG_STR_ARG(
component->get_component_log_str()),
509#ifdef USE_SETUP_PRIORITY_OVERRIDE
512 delete setup_priority_overrides;
513 setup_priority_overrides =
nullptr;
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 enable_component_loop_(Component *component)
void disable_component_loop_(Component *component)
volatile bool has_pending_enable_loop_requests_
bool is_setup_complete() const
True once Application::setup() has finished walking all components and finalized the initial status f...
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
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.
bool set_status_flag_(uint8_t flag)
Helper to set a status LED flag on both this component and the app.
void status_set_warning()
void enable_loop_slow_path_()
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.
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.
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.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
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()
const LogString * get_component_log_str() const ESPHOME_ALWAYS_INLINE
Get the integration where this component was declared as a LogString for logging.
void status_clear_warning_slow_path_()
void set_component_state_(uint8_t state)
Helper to set component state (clears state bits and sets new state)
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
void status_clear_error_slow_path_()
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.
uint8_t warn_if_blocking_over_
Warn threshold in centiseconds (max 2550ms)
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()
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
uint32_t update_interval_
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
const Component * component
const LogString * message
constexpr float DATA
For components that import data from directly connected sensors like DHT.
constexpr uint8_t COMPONENT_STATE_FAILED
constexpr uint8_t WARN_IF_BLOCKING_OVER_CS
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()
const LogString * component_source_lookup(uint8_t index)
Lookup component source name by index (1-based).
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
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