15static const char *
const TAG =
"component";
33struct ComponentErrorMessage {
41#ifdef USE_SETUP_PRIORITY_OVERRIDE
42struct ComponentPriorityOverride {
50std::vector<ComponentPriorityOverride> *setup_priority_overrides =
nullptr;
57std::vector<ComponentErrorMessage> *component_error_messages =
nullptr;
63 if (!component_error_messages) {
64 component_error_messages =
new std::vector<ComponentErrorMessage>();
67 for (
auto &entry : *component_error_messages) {
82static constexpr uint16_t WARN_IF_BLOCKING_INCREMENT_MS =
94#pragma GCC diagnostic push
95#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
96 App.scheduler.set_interval(
this, name, interval, std::move(
f));
97#pragma GCC diagnostic pop
101 App.scheduler.set_interval(
this, name, interval, std::move(
f));
105#pragma GCC diagnostic push
106#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
107 return App.scheduler.cancel_interval(
this, name);
108#pragma GCC diagnostic pop
112 return App.scheduler.cancel_interval(
this, name);
115void Component::set_retry(
const std::string &name,
uint32_t initial_wait_time, uint8_t max_attempts,
116 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
117#pragma GCC diagnostic push
118#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
120#pragma GCC diagnostic pop
123void Component::set_retry(
const char *name,
uint32_t initial_wait_time, uint8_t max_attempts,
124 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
125#pragma GCC diagnostic push
126#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
128#pragma GCC diagnostic pop
131bool Component::cancel_retry(
const std::string &name) {
132#pragma GCC diagnostic push
133#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
134 return App.scheduler.cancel_retry(
this, name);
135#pragma GCC diagnostic pop
138bool Component::cancel_retry(
const char *name) {
139#pragma GCC diagnostic push
140#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
141 return App.scheduler.cancel_retry(
this, name);
142#pragma GCC diagnostic pop
146#pragma GCC diagnostic push
147#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
148 App.scheduler.set_timeout(
this, name, timeout, std::move(
f));
149#pragma GCC diagnostic pop
153 App.scheduler.set_timeout(
this, name, timeout, std::move(
f));
157#pragma GCC diagnostic push
158#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
159 return App.scheduler.cancel_timeout(
this, name);
160#pragma GCC diagnostic pop
164 return App.scheduler.cancel_timeout(
this, name);
169 App.scheduler.set_timeout(
this,
id, timeout, std::move(
f));
175 App.scheduler.set_timeout(
this,
id, timeout, std::move(
f));
181 App.scheduler.set_interval(
this,
id, interval, std::move(
f));
187 App.scheduler.set_interval(
this,
id, interval, std::move(
f));
192void Component::set_retry(
uint32_t id,
uint32_t initial_wait_time, uint8_t max_attempts,
193 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
194#pragma GCC diagnostic push
195#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
197#pragma GCC diagnostic pop
200bool Component::cancel_retry(
uint32_t id) {
201#pragma GCC diagnostic push
202#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
203 return App.scheduler.cancel_retry(
this,
id);
204#pragma GCC diagnostic pop
212 const char *error_msg =
nullptr;
214 if (component_error_messages) {
215 for (
const auto &entry : *component_error_messages) {
216 if (entry.component ==
this) {
217 error_msg = entry.message;
225 error_msg ? (
is_flash_ptr ? LOG_STR_ARG((
const LogString *) error_msg) : error_msg)
226 : LOG_STR_LITERAL(
"unspecified"));
237#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
241#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
245 if (setup_time >= WARN_IF_BLOCKING_OVER_MS) {
246 ESP_LOGCONFIG(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
248 ESP_LOGV(TAG,
"Setup %s took %ums", LOG_STR_ARG(this->
get_component_log_str()), (
unsigned) setup_time);
273 if (blocking_time > threshold_ms) {
275 uint32_t new_threshold_ms = blocking_time + WARN_IF_BLOCKING_INCREMENT_MS;
276 uint32_t new_cs = new_threshold_ms / 10U;
320 ESP_LOGI(TAG,
"%s is being reset to construction state", LOG_STR_ARG(this->
get_component_log_str()));
327 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), 0, std::move(
f));
330#pragma GCC diagnostic push
331#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
332 return App.scheduler.cancel_timeout(
this, name);
333#pragma GCC diagnostic pop
336 return App.scheduler.cancel_timeout(
this, name);
339#pragma GCC diagnostic push
340#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
341 App.scheduler.set_timeout(
this, name, 0, std::move(
f));
342#pragma GCC diagnostic pop
345 App.scheduler.set_timeout(
this, name, 0, std::move(
f));
348 App.scheduler.set_timeout(
this,
id, 0, std::move(
f));
352 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), timeout, std::move(
f));
355 App.scheduler.set_interval(
this,
static_cast<const char *
>(
nullptr), interval, std::move(
f));
357void Component::set_retry(
uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> &&f,
358 float backoff_increase_factor) {
359#pragma GCC diagnostic push
360#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
362#pragma GCC diagnostic pop
384 message ?
message : LOG_STR_LITERAL(
"unspecified"));
390 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
397 message ?
message : LOG_STR_LITERAL(
"unspecified"));
399 store_component_error_message(
this,
message,
false);
406 message ? LOG_STR_ARG(
message) : LOG_STR_LITERAL(
"unspecified"));
409 store_component_error_message(
this, LOG_STR_ARG(
message),
true);
434 ESP_LOGCONFIG(
tag,
" Update Interval: never");
435 }
else if (update_interval < 100) {
436 ESP_LOGCONFIG(
tag,
" Update Interval: %.3fs", update_interval / 1000.0f);
438 ESP_LOGCONFIG(
tag,
" Update Interval: %.1fs", update_interval / 1000.0f);
442#ifdef USE_SETUP_PRIORITY_OVERRIDE
444 if (setup_priority_overrides) {
446 for (
const auto &entry : *setup_priority_overrides) {
447 if (entry.component ==
this) {
448 return entry.priority;
455#ifdef USE_SETUP_PRIORITY_OVERRIDE
458 if (!setup_priority_overrides) {
459 setup_priority_overrides =
new std::vector<ComponentPriorityOverride>();
463 for (
auto &entry : *setup_priority_overrides) {
464 if (entry.component ==
this) {
471 setup_priority_overrides->emplace_back(ComponentPriorityOverride{
this,
priority});
500 should_warn =
component->should_warn_of_blocking(blocking_time);
505 ESP_LOGW(TAG,
"%s took a long time for an operation (%" PRIu32
" ms), max is 30 ms",
506 component ==
nullptr ? LOG_STR_LITERAL(
"<null>") : LOG_STR_ARG(
component->get_component_log_str()),
511#ifdef USE_SETUP_PRIORITY_OVERRIDE
514 delete setup_priority_overrides;
515 setup_priority_overrides =
nullptr;
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
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__
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
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 — defined in wake.cpp.