ESPHome 2026.2.3
Loading...
Searching...
No Matches
component.cpp
Go to the documentation of this file.
2
3#include <cinttypes>
4#include <limits>
5#include <memory>
6#include <utility>
7#include <vector>
9#include "esphome/core/hal.h"
11#include "esphome/core/log.h"
12#ifdef USE_RUNTIME_STATS
14#endif
15
16namespace esphome {
17
18static const char *const TAG = "component";
19
20// Global vectors for component data that doesn't belong in every instance.
21// Using vector instead of unordered_map for both because:
22// - Much lower memory overhead (8 bytes per entry vs 20+ for unordered_map)
23// - Linear search is fine for small n (typically < 5 entries)
24// - These are rarely accessed (setup only or error cases only)
25
26// Component error messages - only stores messages for failed components
27// Lazy allocated since most configs have zero failures
28// Note: We don't clear this vector because:
29// 1. Components are never destroyed in ESPHome
30// 2. Failed components remain failed (no recovery mechanism)
31// 3. Memory usage is minimal (only failures with custom messages are stored)
32
33// Using namespace-scope static to avoid guard variables (saves 16 bytes total)
34// This is safe because ESPHome is single-threaded during initialization
35namespace {
36struct ComponentErrorMessage {
37 const Component *component;
38 const char *message;
39 // Track if message is flash pointer (needs LOG_STR_ARG) or RAM pointer
40 // Remove before 2026.6.0 when deprecated const char* API is removed
42};
43
44struct ComponentPriorityOverride {
45 const Component *component;
46 float priority;
47};
48
49// Error messages for failed components
50// Using raw pointer instead of unique_ptr to avoid global constructor/destructor overhead
51// This is never freed as error messages persist for the lifetime of the device
52// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
53std::vector<ComponentErrorMessage> *component_error_messages = nullptr;
54// Setup priority overrides - freed after setup completes
55// Using raw pointer instead of unique_ptr to avoid global constructor/destructor overhead
56// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
57std::vector<ComponentPriorityOverride> *setup_priority_overrides = nullptr;
58
59// Helper to store error messages - reduces duplication between deprecated and new API
60// Remove before 2026.6.0 when deprecated const char* API is removed
61void store_component_error_message(const Component *component, const char *message, bool is_flash_ptr) {
62 // Lazy allocate the error messages vector if needed
63 if (!component_error_messages) {
64 component_error_messages = new std::vector<ComponentErrorMessage>();
65 }
66 // Check if this component already has an error message
67 for (auto &entry : *component_error_messages) {
68 if (entry.component == component) {
69 entry.message = message;
70 entry.is_flash_ptr = is_flash_ptr;
71 return;
72 }
73 }
74 // Add new error message
75 component_error_messages->emplace_back(ComponentErrorMessage{component, message, is_flash_ptr});
76}
77} // namespace
78
79namespace setup_priority {
80
81const float BUS = 1000.0f;
82const float IO = 900.0f;
83const float HARDWARE = 800.0f;
84const float DATA = 600.0f;
85const float PROCESSOR = 400.0;
86const float BLUETOOTH = 350.0f;
87const float AFTER_BLUETOOTH = 300.0f;
88const float WIFI = 250.0f;
89const float ETHERNET = 250.0f;
90const float BEFORE_CONNECTION = 220.0f;
91const float AFTER_WIFI = 200.0f;
92const float AFTER_CONNECTION = 100.0f;
93const float LATE = -100.0f;
94
95} // namespace setup_priority
96
97// Component state uses bits 0-2 (8 states, 5 used)
98const uint8_t COMPONENT_STATE_MASK = 0x07;
99const uint8_t COMPONENT_STATE_CONSTRUCTION = 0x00;
100const uint8_t COMPONENT_STATE_SETUP = 0x01;
101const uint8_t COMPONENT_STATE_LOOP = 0x02;
102const uint8_t COMPONENT_STATE_FAILED = 0x03;
103const uint8_t COMPONENT_STATE_LOOP_DONE = 0x04;
104// Status LED uses bits 3-4
105const uint8_t STATUS_LED_MASK = 0x18;
106const uint8_t STATUS_LED_OK = 0x00;
107const uint8_t STATUS_LED_WARNING = 0x08; // Bit 3
108const uint8_t STATUS_LED_ERROR = 0x10; // Bit 4
109
110const uint16_t WARN_IF_BLOCKING_OVER_MS = 50U;
111const uint16_t WARN_IF_BLOCKING_INCREMENT_MS = 10U;
112
113uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
114
115float Component::get_loop_priority() const { return 0.0f; }
116
118
120
122
123void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
124#pragma GCC diagnostic push
125#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
126 App.scheduler.set_interval(this, name, interval, std::move(f));
127#pragma GCC diagnostic pop
128}
129
130void Component::set_interval(const char *name, uint32_t interval, std::function<void()> &&f) { // NOLINT
131 App.scheduler.set_interval(this, name, interval, std::move(f));
132}
133
134bool Component::cancel_interval(const std::string &name) { // NOLINT
135#pragma GCC diagnostic push
136#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
137 return App.scheduler.cancel_interval(this, name);
138#pragma GCC diagnostic pop
139}
140
141bool Component::cancel_interval(const char *name) { // NOLINT
142 return App.scheduler.cancel_interval(this, name);
143}
144
145void Component::set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
146 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
147#pragma GCC diagnostic push
148#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
149 App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
150#pragma GCC diagnostic pop
151}
152
153void Component::set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
154 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
155#pragma GCC diagnostic push
156#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
157 App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
158#pragma GCC diagnostic pop
159}
160
161bool Component::cancel_retry(const std::string &name) { // NOLINT
162#pragma GCC diagnostic push
163#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
164 return App.scheduler.cancel_retry(this, name);
165#pragma GCC diagnostic pop
166}
167
168bool Component::cancel_retry(const char *name) { // NOLINT
169#pragma GCC diagnostic push
170#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
171 return App.scheduler.cancel_retry(this, name);
172#pragma GCC diagnostic pop
173}
174
175void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
178 App.scheduler.set_timeout(this, name, timeout, std::move(f));
179#pragma GCC diagnostic pop
180}
181
182void Component::set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
183 App.scheduler.set_timeout(this, name, timeout, std::move(f));
184}
185
186bool Component::cancel_timeout(const std::string &name) { // NOLINT
187#pragma GCC diagnostic push
188#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
189 return App.scheduler.cancel_timeout(this, name);
190#pragma GCC diagnostic pop
191}
192
193bool Component::cancel_timeout(const char *name) { // NOLINT
194 return App.scheduler.cancel_timeout(this, name);
195}
196
197// uint32_t (numeric ID) overloads - zero heap allocation
198void Component::set_timeout(uint32_t id, uint32_t timeout, std::function<void()> &&f) { // NOLINT
199 App.scheduler.set_timeout(this, id, timeout, std::move(f));
200}
201
202bool Component::cancel_timeout(uint32_t id) { return App.scheduler.cancel_timeout(this, id); }
203
204void Component::set_timeout(InternalSchedulerID id, uint32_t timeout, std::function<void()> &&f) { // NOLINT
205 App.scheduler.set_timeout(this, id, timeout, std::move(f));
206}
207
208bool Component::cancel_timeout(InternalSchedulerID id) { return App.scheduler.cancel_timeout(this, id); }
209
210void Component::set_interval(uint32_t id, uint32_t interval, std::function<void()> &&f) { // NOLINT
211 App.scheduler.set_interval(this, id, interval, std::move(f));
212}
213
214bool Component::cancel_interval(uint32_t id) { return App.scheduler.cancel_interval(this, id); }
215
216void Component::set_interval(InternalSchedulerID id, uint32_t interval, std::function<void()> &&f) { // NOLINT
217 App.scheduler.set_interval(this, id, interval, std::move(f));
218}
219
220bool Component::cancel_interval(InternalSchedulerID id) { return App.scheduler.cancel_interval(this, id); }
221
222void Component::set_retry(uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
223 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
224#pragma GCC diagnostic push
225#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
226 App.scheduler.set_retry(this, id, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
227#pragma GCC diagnostic pop
228}
229
230bool Component::cancel_retry(uint32_t id) {
231#pragma GCC diagnostic push
232#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
233 return App.scheduler.cancel_retry(this, id);
234#pragma GCC diagnostic pop
235}
236
237void Component::call_loop() { this->loop(); }
238void Component::call_setup() { this->setup(); }
240 this->dump_config();
241 if (this->is_failed()) {
242 // Look up error message from global vector
243 const char *error_msg = nullptr;
244 bool is_flash_ptr = false;
245 if (component_error_messages) {
246 for (const auto &entry : *component_error_messages) {
247 if (entry.component == this) {
248 error_msg = entry.message;
249 is_flash_ptr = entry.is_flash_ptr;
250 break;
251 }
252 }
253 }
254 // Log with appropriate format based on pointer type
255 ESP_LOGE(TAG, " %s is marked FAILED: %s", LOG_STR_ARG(this->get_component_log_str()),
256 error_msg ? (is_flash_ptr ? LOG_STR_ARG((const LogString *) error_msg) : error_msg)
257 : LOG_STR_LITERAL("unspecified"));
258 }
259}
260
261uint8_t Component::get_component_state() const { return this->component_state_; }
264 switch (state) {
266 // State Construction: Call setup and set state to setup
267 this->set_component_state_(COMPONENT_STATE_SETUP);
268 ESP_LOGV(TAG, "Setup %s", LOG_STR_ARG(this->get_component_log_str()));
269#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
270 uint32_t start_time = millis();
271#endif
272 this->call_setup();
273#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
274 uint32_t setup_time = millis() - start_time;
275 // Only log at CONFIG level if setup took longer than the blocking threshold
276 // to avoid spamming the log and blocking the event loop
277 if (setup_time >= WARN_IF_BLOCKING_OVER_MS) {
278 ESP_LOGCONFIG(TAG, "Setup %s took %ums", LOG_STR_ARG(this->get_component_log_str()), (unsigned) setup_time);
279 } else {
280 ESP_LOGV(TAG, "Setup %s took %ums", LOG_STR_ARG(this->get_component_log_str()), (unsigned) setup_time);
281 }
282#endif
283 break;
284 }
286 // State setup: Call first loop and set state to loop
287 this->set_component_state_(COMPONENT_STATE_LOOP);
288 this->call_loop();
289 break;
291 // State loop: Call loop
292 this->call_loop();
293 break;
295 // State failed: Do nothing
297 // State loop done: Do nothing, component has finished its work
298 default:
299 break;
300 }
301}
302const LogString *Component::get_component_log_str() const {
303 return this->component_source_ == nullptr ? LOG_STR("<unknown>") : this->component_source_;
304}
305bool Component::should_warn_of_blocking(uint32_t blocking_time) {
306 if (blocking_time > this->warn_if_blocking_over_) {
307 // Prevent overflow when adding increment - if we're about to overflow, just max out
308 if (blocking_time + WARN_IF_BLOCKING_INCREMENT_MS < blocking_time ||
309 blocking_time + WARN_IF_BLOCKING_INCREMENT_MS > std::numeric_limits<uint16_t>::max()) {
310 this->warn_if_blocking_over_ = std::numeric_limits<uint16_t>::max();
311 } else {
312 this->warn_if_blocking_over_ = static_cast<uint16_t>(blocking_time + WARN_IF_BLOCKING_INCREMENT_MS);
313 }
314 return true;
315 }
316 return false;
317}
319 ESP_LOGE(TAG, "%s was marked as failed", LOG_STR_ARG(this->get_component_log_str()));
320 this->set_component_state_(COMPONENT_STATE_FAILED);
321 this->status_set_error();
322 // Also remove from loop since failed components shouldn't loop
324}
326 this->component_state_ &= ~COMPONENT_STATE_MASK;
327 this->component_state_ |= state;
328}
330 if ((this->component_state_ & COMPONENT_STATE_MASK) != COMPONENT_STATE_LOOP_DONE) {
331 ESP_LOGVV(TAG, "%s loop disabled", LOG_STR_ARG(this->get_component_log_str()));
332 this->set_component_state_(COMPONENT_STATE_LOOP_DONE);
334 }
335}
337 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE) {
338 ESP_LOGVV(TAG, "%s loop enabled", LOG_STR_ARG(this->get_component_log_str()));
339 this->set_component_state_(COMPONENT_STATE_LOOP);
341 }
342}
344 // This method is thread and ISR-safe because:
345 // 1. Only performs simple assignments to volatile variables (atomic on all platforms)
346 // 2. No read-modify-write operations that could be interrupted
347 // 3. No memory allocation, object construction, or function calls
348 // 4. IRAM_ATTR ensures code is in IRAM, not flash (required for ISR execution)
349 // 5. Components are never destroyed, so no use-after-free concerns
350 // 6. App is guaranteed to be initialized before any ISR could fire
351 // 7. Multiple ISR/thread calls are safe - just sets the same flags to true
352 // 8. Race condition with main loop is handled by clearing flag before processing
353 this->pending_enable_loop_ = true;
355}
357 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED) {
358 ESP_LOGI(TAG, "%s is being reset to construction state", LOG_STR_ARG(this->get_component_log_str()));
359 this->set_component_state_(COMPONENT_STATE_CONSTRUCTION);
360 // Clear error status when resetting
361 this->status_clear_error();
362 }
363}
365 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP;
366}
367void Component::defer(std::function<void()> &&f) { // NOLINT
368 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), 0, std::move(f));
369}
370bool Component::cancel_defer(const std::string &name) { // NOLINT
371#pragma GCC diagnostic push
372#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
373 return App.scheduler.cancel_timeout(this, name);
374#pragma GCC diagnostic pop
375}
376bool Component::cancel_defer(const char *name) { // NOLINT
377 return App.scheduler.cancel_timeout(this, name);
378}
379void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
380#pragma GCC diagnostic push
381#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
382 App.scheduler.set_timeout(this, name, 0, std::move(f));
383#pragma GCC diagnostic pop
384}
385void Component::defer(const char *name, std::function<void()> &&f) { // NOLINT
386 App.scheduler.set_timeout(this, name, 0, std::move(f));
387}
388void Component::defer(uint32_t id, std::function<void()> &&f) { // NOLINT
389 App.scheduler.set_timeout(this, id, 0, std::move(f));
390}
391bool Component::cancel_defer(uint32_t id) { return App.scheduler.cancel_timeout(this, id); }
392void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
393 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), timeout, std::move(f));
394}
395void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
396 App.scheduler.set_interval(this, static_cast<const char *>(nullptr), interval, std::move(f));
397}
398void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f,
399 float backoff_increase_factor) { // NOLINT
400#pragma GCC diagnostic push
401#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
402 App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
403#pragma GCC diagnostic pop
404}
405bool Component::is_failed() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
407 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP ||
408 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE ||
409 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_SETUP;
410}
411bool Component::is_idle() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE; }
412bool Component::can_proceed() { return true; }
415
416void Component::status_set_warning(const char *message) {
417 // Don't spam the log. This risks missing different warning messages though.
418 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
419 return;
422 ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
423 message ? message : LOG_STR_LITERAL("unspecified"));
424}
426 // Don't spam the log. This risks missing different warning messages though.
427 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
428 return;
431 ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
432 message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
433}
434void Component::status_set_error() { this->status_set_error((const LogString *) nullptr); }
435void Component::status_set_error(const char *message) {
436 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
437 return;
440 ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
441 message ? message : LOG_STR_LITERAL("unspecified"));
442 if (message != nullptr) {
443 store_component_error_message(this, message, false);
444 }
445}
446void Component::status_set_error(const LogString *message) {
447 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
448 return;
451 ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
452 message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
453 if (message != nullptr) {
454 // Store the LogString pointer directly (safe because LogString is always in flash/static memory)
455 store_component_error_message(this, LOG_STR_ARG(message), true);
456 }
457}
459 if ((this->component_state_ & STATUS_LED_WARNING) == 0)
460 return;
461 this->component_state_ &= ~STATUS_LED_WARNING;
462 ESP_LOGW(TAG, "%s cleared Warning flag", LOG_STR_ARG(this->get_component_log_str()));
463}
465 if ((this->component_state_ & STATUS_LED_ERROR) == 0)
466 return;
467 this->component_state_ &= ~STATUS_LED_ERROR;
468 ESP_LOGE(TAG, "%s cleared Error flag", LOG_STR_ARG(this->get_component_log_str()));
469}
470void Component::status_momentary_warning(const char *name, uint32_t length) {
471 this->status_set_warning();
472 this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
473}
474void Component::status_momentary_error(const char *name, uint32_t length) {
475 this->status_set_error();
476 this->set_timeout(name, length, [this]() { this->status_clear_error(); });
477}
479
480// Function implementation of LOG_UPDATE_INTERVAL macro to reduce code size
482 uint32_t update_interval = component->get_update_interval();
483 if (update_interval == SCHEDULER_DONT_RUN) {
484 ESP_LOGCONFIG(tag, " Update Interval: never");
485 } else if (update_interval < 100) {
486 ESP_LOGCONFIG(tag, " Update Interval: %.3fs", update_interval / 1000.0f);
487 } else {
488 ESP_LOGCONFIG(tag, " Update Interval: %.1fs", update_interval / 1000.0f);
489 }
490}
492 // Check if there's an override in the global vector
493 if (setup_priority_overrides) {
494 // Linear search is fine for small n (typically < 5 overrides)
495 for (const auto &entry : *setup_priority_overrides) {
496 if (entry.component == this) {
497 return entry.priority;
498 }
499 }
500 }
501 return this->get_setup_priority();
502}
504 // Lazy allocate the vector if needed
505 if (!setup_priority_overrides) {
506 setup_priority_overrides = new std::vector<ComponentPriorityOverride>();
507 // Reserve some space to avoid reallocations (most configs have < 10 overrides)
508 setup_priority_overrides->reserve(10);
509 }
510
511 // Check if this component already has an override
512 for (auto &entry : *setup_priority_overrides) {
513 if (entry.component == this) {
514 entry.priority = priority;
515 return;
516 }
517 }
518
519 // Add new override
520 setup_priority_overrides->emplace_back(ComponentPriorityOverride{this, priority});
521}
522
524#if defined(USE_HOST) || defined(CLANG_TIDY)
525 bool loop_overridden = true;
526 bool call_loop_overridden = true;
527#else
528#pragma GCC diagnostic push
529#pragma GCC diagnostic ignored "-Wpmf-conversions"
530 bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
531 bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
532#pragma GCC diagnostic pop
533#endif
534 return loop_overridden || call_loop_overridden;
535}
536
537PollingComponent::PollingComponent(uint32_t update_interval) : update_interval_(update_interval) {}
538
540 // init the poller before calling setup, allowing setup to cancel it if desired
541 this->start_poller();
542 // Let the polling component subclass setup their HW.
543 this->setup();
544}
545
547 // Register interval.
548 this->set_interval(InternalSchedulerID::POLLING_UPDATE, this->get_update_interval(), [this]() { this->update(); });
549}
550
552 // Clear the interval to suspend component
554}
555
557void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
558
560 : started_(start_time), component_(component) {}
562 uint32_t curr_time = millis();
563
564 uint32_t blocking_time = curr_time - this->started_;
565
566#ifdef USE_RUNTIME_STATS
567 // Record component runtime stats
568 if (global_runtime_stats != nullptr) {
569 global_runtime_stats->record_component_time(this->component_, blocking_time, curr_time);
570 }
571#endif
572 bool should_warn;
573 if (this->component_ != nullptr) {
574 should_warn = this->component_->should_warn_of_blocking(blocking_time);
575 } else {
576 should_warn = blocking_time > WARN_IF_BLOCKING_OVER_MS;
577 }
578 if (should_warn) {
579 ESP_LOGW(TAG, "%s took a long time for an operation (%" PRIu32 " ms)",
580 component_ == nullptr ? LOG_STR_LITERAL("<null>") : LOG_STR_ARG(component_->get_component_log_str()),
581 blocking_time);
582 ESP_LOGW(TAG, "Components should block for at most 30 ms");
583 }
584
585 return curr_time;
586}
587
589
591 // Free the setup priority map completely
592 delete setup_priority_overrides;
593 setup_priority_overrides = nullptr;
594}
595
596} // namespace esphome
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
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.
Definition component.h:479
const LogString * component_source_
Definition component.h:495
bool is_failed() const
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.
Definition component.h:503
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.
Definition component.h:429
virtual float get_loop_priority() const
priority of loop().
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.
Definition component.h:336
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.
Definition component.h:491
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:496
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:502
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
bool is_ready() const
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
Definition component.h:373
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
Definition component.h:372
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.
Definition component.h:451
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
Definition component.h:372
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.
Definition component.h:358
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
Definition component.h:373
virtual void call_loop()
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.
Definition component.h:512
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.
virtual void update()=0
WarnIfComponentBlockingGuard(Component *component, uint32_t start_time)
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
const Component * component
Definition component.cpp:37
const char * message
Definition component.cpp:38
bool is_flash_ptr
Definition component.cpp:41
uint8_t priority
bool state
Definition fan.h:2
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:81
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:92
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:84
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:83
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:90
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:82
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:93
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:91
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:85
const float AFTER_BLUETOOTH
Definition component.cpp:87
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint8_t COMPONENT_STATE_SETUP
const uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:99
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
InternalSchedulerID
Type-safe scheduler IDs for core base classes.
Definition component.h:55
const uint8_t COMPONENT_STATE_FAILED
const uint8_t COMPONENT_STATE_MASK
Definition component.cpp:98
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()
uint32_t global_state
const uint8_t STATUS_LED_OK
const uint8_t STATUS_LED_WARNING
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
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
uint16_t length
Definition tt21100.cpp:0