ESPHome 2026.1.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// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
51std::unique_ptr<std::vector<ComponentErrorMessage>> component_error_messages;
52// Setup priority overrides - freed after setup completes
53// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
54std::unique_ptr<std::vector<ComponentPriorityOverride>> setup_priority_overrides;
55
56// Helper to store error messages - reduces duplication between deprecated and new API
57// Remove before 2026.6.0 when deprecated const char* API is removed
58void store_component_error_message(const Component *component, const char *message, bool is_flash_ptr) {
59 // Lazy allocate the error messages vector if needed
60 if (!component_error_messages) {
61 component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
62 }
63 // Check if this component already has an error message
64 for (auto &entry : *component_error_messages) {
65 if (entry.component == component) {
66 entry.message = message;
67 entry.is_flash_ptr = is_flash_ptr;
68 return;
69 }
70 }
71 // Add new error message
72 component_error_messages->emplace_back(ComponentErrorMessage{component, message, is_flash_ptr});
73}
74} // namespace
75
76namespace setup_priority {
77
78const float BUS = 1000.0f;
79const float IO = 900.0f;
80const float HARDWARE = 800.0f;
81const float DATA = 600.0f;
82const float PROCESSOR = 400.0;
83const float BLUETOOTH = 350.0f;
84const float AFTER_BLUETOOTH = 300.0f;
85const float WIFI = 250.0f;
86const float ETHERNET = 250.0f;
87const float BEFORE_CONNECTION = 220.0f;
88const float AFTER_WIFI = 200.0f;
89const float AFTER_CONNECTION = 100.0f;
90const float LATE = -100.0f;
91
92} // namespace setup_priority
93
94// Component state uses bits 0-2 (8 states, 5 used)
95const uint8_t COMPONENT_STATE_MASK = 0x07;
96const uint8_t COMPONENT_STATE_CONSTRUCTION = 0x00;
97const uint8_t COMPONENT_STATE_SETUP = 0x01;
98const uint8_t COMPONENT_STATE_LOOP = 0x02;
99const uint8_t COMPONENT_STATE_FAILED = 0x03;
100const uint8_t COMPONENT_STATE_LOOP_DONE = 0x04;
101// Status LED uses bits 3-4
102const uint8_t STATUS_LED_MASK = 0x18;
103const uint8_t STATUS_LED_OK = 0x00;
104const uint8_t STATUS_LED_WARNING = 0x08; // Bit 3
105const uint8_t STATUS_LED_ERROR = 0x10; // Bit 4
106
107const uint16_t WARN_IF_BLOCKING_OVER_MS = 50U;
108const uint16_t WARN_IF_BLOCKING_INCREMENT_MS = 10U;
109
110uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
111
112float Component::get_loop_priority() const { return 0.0f; }
113
115
117
119
120void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
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
125}
126
127void Component::set_interval(const char *name, uint32_t interval, std::function<void()> &&f) { // NOLINT
128 App.scheduler.set_interval(this, name, interval, std::move(f));
129}
130
131bool Component::cancel_interval(const std::string &name) { // NOLINT
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
136}
137
138bool Component::cancel_interval(const char *name) { // NOLINT
139 return App.scheduler.cancel_interval(this, name);
140}
141
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) { // NOLINT
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
148}
149
150void Component::set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
151 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
152 App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
153}
154
155bool Component::cancel_retry(const std::string &name) { // NOLINT
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
160}
161
162bool Component::cancel_retry(const char *name) { // NOLINT
163 return App.scheduler.cancel_retry(this, name);
164}
165
166void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
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
171}
172
173void Component::set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
174 App.scheduler.set_timeout(this, name, timeout, std::move(f));
175}
176
177bool Component::cancel_timeout(const std::string &name) { // NOLINT
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
182}
183
184bool Component::cancel_timeout(const char *name) { // NOLINT
185 return App.scheduler.cancel_timeout(this, name);
186}
187
188// uint32_t (numeric ID) overloads - zero heap allocation
189void Component::set_timeout(uint32_t id, uint32_t timeout, std::function<void()> &&f) { // NOLINT
190 App.scheduler.set_timeout(this, id, timeout, std::move(f));
191}
192
193bool Component::cancel_timeout(uint32_t id) { return App.scheduler.cancel_timeout(this, id); }
194
195void Component::set_interval(uint32_t id, uint32_t interval, std::function<void()> &&f) { // NOLINT
196 App.scheduler.set_interval(this, id, interval, std::move(f));
197}
198
199bool Component::cancel_interval(uint32_t id) { return App.scheduler.cancel_interval(this, id); }
200
201void Component::set_retry(uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
202 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
203 App.scheduler.set_retry(this, id, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
204}
205
206bool Component::cancel_retry(uint32_t id) { return App.scheduler.cancel_retry(this, id); }
207
208void Component::call_loop() { this->loop(); }
209void Component::call_setup() { this->setup(); }
211 this->dump_config();
212 if (this->is_failed()) {
213 // Look up error message from global vector
214 const char *error_msg = nullptr;
215 bool is_flash_ptr = false;
216 if (component_error_messages) {
217 for (const auto &entry : *component_error_messages) {
218 if (entry.component == this) {
219 error_msg = entry.message;
220 is_flash_ptr = entry.is_flash_ptr;
221 break;
222 }
223 }
224 }
225 // Log with appropriate format based on pointer type
226 ESP_LOGE(TAG, " %s is marked FAILED: %s", LOG_STR_ARG(this->get_component_log_str()),
227 error_msg ? (is_flash_ptr ? LOG_STR_ARG((const LogString *) error_msg) : error_msg)
228 : LOG_STR_LITERAL("unspecified"));
229 }
230}
231
232uint8_t Component::get_component_state() const { return this->component_state_; }
235 switch (state) {
237 // State Construction: Call setup and set state to setup
238 this->set_component_state_(COMPONENT_STATE_SETUP);
239 ESP_LOGV(TAG, "Setup %s", LOG_STR_ARG(this->get_component_log_str()));
240#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
241 uint32_t start_time = millis();
242#endif
243 this->call_setup();
244#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
245 uint32_t setup_time = millis() - start_time;
246 // Only log at CONFIG level if setup took longer than the blocking threshold
247 // to avoid spamming the log and blocking the event loop
248 if (setup_time >= WARN_IF_BLOCKING_OVER_MS) {
249 ESP_LOGCONFIG(TAG, "Setup %s took %ums", LOG_STR_ARG(this->get_component_log_str()), (unsigned) setup_time);
250 } else {
251 ESP_LOGV(TAG, "Setup %s took %ums", LOG_STR_ARG(this->get_component_log_str()), (unsigned) setup_time);
252 }
253#endif
254 break;
255 }
257 // State setup: Call first loop and set state to loop
258 this->set_component_state_(COMPONENT_STATE_LOOP);
259 this->call_loop();
260 break;
262 // State loop: Call loop
263 this->call_loop();
264 break;
266 // State failed: Do nothing
268 // State loop done: Do nothing, component has finished its work
269 default:
270 break;
271 }
272}
273const LogString *Component::get_component_log_str() const {
274 return this->component_source_ == nullptr ? LOG_STR("<unknown>") : this->component_source_;
275}
276bool Component::should_warn_of_blocking(uint32_t blocking_time) {
277 if (blocking_time > this->warn_if_blocking_over_) {
278 // Prevent overflow when adding increment - if we're about to overflow, just max out
279 if (blocking_time + WARN_IF_BLOCKING_INCREMENT_MS < blocking_time ||
280 blocking_time + WARN_IF_BLOCKING_INCREMENT_MS > std::numeric_limits<uint16_t>::max()) {
281 this->warn_if_blocking_over_ = std::numeric_limits<uint16_t>::max();
282 } else {
283 this->warn_if_blocking_over_ = static_cast<uint16_t>(blocking_time + WARN_IF_BLOCKING_INCREMENT_MS);
284 }
285 return true;
286 }
287 return false;
288}
290 ESP_LOGE(TAG, "%s was marked as failed", LOG_STR_ARG(this->get_component_log_str()));
291 this->set_component_state_(COMPONENT_STATE_FAILED);
292 this->status_set_error();
293 // Also remove from loop since failed components shouldn't loop
295}
297 this->component_state_ &= ~COMPONENT_STATE_MASK;
298 this->component_state_ |= state;
299}
301 if ((this->component_state_ & COMPONENT_STATE_MASK) != COMPONENT_STATE_LOOP_DONE) {
302 ESP_LOGVV(TAG, "%s loop disabled", LOG_STR_ARG(this->get_component_log_str()));
303 this->set_component_state_(COMPONENT_STATE_LOOP_DONE);
305 }
306}
308 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE) {
309 ESP_LOGVV(TAG, "%s loop enabled", LOG_STR_ARG(this->get_component_log_str()));
310 this->set_component_state_(COMPONENT_STATE_LOOP);
312 }
313}
315 // This method is thread and ISR-safe because:
316 // 1. Only performs simple assignments to volatile variables (atomic on all platforms)
317 // 2. No read-modify-write operations that could be interrupted
318 // 3. No memory allocation, object construction, or function calls
319 // 4. IRAM_ATTR ensures code is in IRAM, not flash (required for ISR execution)
320 // 5. Components are never destroyed, so no use-after-free concerns
321 // 6. App is guaranteed to be initialized before any ISR could fire
322 // 7. Multiple ISR/thread calls are safe - just sets the same flags to true
323 // 8. Race condition with main loop is handled by clearing flag before processing
324 this->pending_enable_loop_ = true;
326}
328 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED) {
329 ESP_LOGI(TAG, "%s is being reset to construction state", LOG_STR_ARG(this->get_component_log_str()));
330 this->set_component_state_(COMPONENT_STATE_CONSTRUCTION);
331 // Clear error status when resetting
332 this->status_clear_error();
333 }
334}
336 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP;
337}
338void Component::defer(std::function<void()> &&f) { // NOLINT
339 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), 0, std::move(f));
340}
341bool Component::cancel_defer(const std::string &name) { // NOLINT
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
346}
347bool Component::cancel_defer(const char *name) { // NOLINT
348 return App.scheduler.cancel_timeout(this, name);
349}
350void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
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
355}
356void Component::defer(const char *name, std::function<void()> &&f) { // NOLINT
357 App.scheduler.set_timeout(this, name, 0, std::move(f));
358}
359void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
360 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), timeout, std::move(f));
361}
362void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
363 App.scheduler.set_interval(this, static_cast<const char *>(nullptr), interval, std::move(f));
364}
365void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f,
366 float backoff_increase_factor) { // NOLINT
367 App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
368}
369bool Component::is_failed() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
371 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP ||
372 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE ||
373 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_SETUP;
374}
375bool Component::is_idle() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE; }
376bool Component::can_proceed() { return true; }
379
380void Component::status_set_warning(const char *message) {
381 // Don't spam the log. This risks missing different warning messages though.
382 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
383 return;
386 ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
387 message ? message : LOG_STR_LITERAL("unspecified"));
388}
390 // Don't spam the log. This risks missing different warning messages though.
391 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
392 return;
395 ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
396 message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
397}
398void Component::status_set_error() { this->status_set_error((const LogString *) nullptr); }
399void Component::status_set_error(const char *message) {
400 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
401 return;
404 ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
405 message ? message : LOG_STR_LITERAL("unspecified"));
406 if (message != nullptr) {
407 store_component_error_message(this, message, false);
408 }
409}
410void Component::status_set_error(const LogString *message) {
411 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
412 return;
415 ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
416 message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
417 if (message != nullptr) {
418 // Store the LogString pointer directly (safe because LogString is always in flash/static memory)
419 store_component_error_message(this, LOG_STR_ARG(message), true);
420 }
421}
423 if ((this->component_state_ & STATUS_LED_WARNING) == 0)
424 return;
425 this->component_state_ &= ~STATUS_LED_WARNING;
426 ESP_LOGW(TAG, "%s cleared Warning flag", LOG_STR_ARG(this->get_component_log_str()));
427}
429 if ((this->component_state_ & STATUS_LED_ERROR) == 0)
430 return;
431 this->component_state_ &= ~STATUS_LED_ERROR;
432 ESP_LOGE(TAG, "%s cleared Error flag", LOG_STR_ARG(this->get_component_log_str()));
433}
434void Component::status_momentary_warning(const char *name, uint32_t length) {
435 this->status_set_warning();
436 this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
437}
438void Component::status_momentary_error(const char *name, uint32_t length) {
439 this->status_set_error();
440 this->set_timeout(name, length, [this]() { this->status_clear_error(); });
441}
443
444// Function implementation of LOG_UPDATE_INTERVAL macro to reduce code size
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);
451 } else {
452 ESP_LOGCONFIG(tag, " Update Interval: %.1fs", update_interval / 1000.0f);
453 }
454}
456 // Check if there's an override in the global vector
457 if (setup_priority_overrides) {
458 // Linear search is fine for small n (typically < 5 overrides)
459 for (const auto &entry : *setup_priority_overrides) {
460 if (entry.component == this) {
461 return entry.priority;
462 }
463 }
464 }
465 return this->get_setup_priority();
466}
468 // Lazy allocate the vector if needed
469 if (!setup_priority_overrides) {
470 setup_priority_overrides = std::make_unique<std::vector<ComponentPriorityOverride>>();
471 // Reserve some space to avoid reallocations (most configs have < 10 overrides)
472 setup_priority_overrides->reserve(10);
473 }
474
475 // Check if this component already has an override
476 for (auto &entry : *setup_priority_overrides) {
477 if (entry.component == this) {
478 entry.priority = priority;
479 return;
480 }
481 }
482
483 // Add new override
484 setup_priority_overrides->emplace_back(ComponentPriorityOverride{this, priority});
485}
486
488#if defined(USE_HOST) || defined(CLANG_TIDY)
489 bool loop_overridden = true;
490 bool call_loop_overridden = true;
491#else
492#pragma GCC diagnostic push
493#pragma GCC diagnostic ignored "-Wpmf-conversions"
494 bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
495 bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
496#pragma GCC diagnostic pop
497#endif
498 return loop_overridden || call_loop_overridden;
499}
500
501PollingComponent::PollingComponent(uint32_t update_interval) : update_interval_(update_interval) {}
502
504 // init the poller before calling setup, allowing setup to cancel it if desired
505 this->start_poller();
506 // Let the polling component subclass setup their HW.
507 this->setup();
508}
509
511 // Register interval.
512 this->set_interval("update", this->get_update_interval(), [this]() { this->update(); });
513}
514
516 // Clear the interval to suspend component
517 this->cancel_interval("update");
518}
519
521void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
522
524 : started_(start_time), component_(component) {}
526 uint32_t curr_time = millis();
527
528 uint32_t blocking_time = curr_time - this->started_;
529
530#ifdef USE_RUNTIME_STATS
531 // Record component runtime stats
532 if (global_runtime_stats != nullptr) {
533 global_runtime_stats->record_component_time(this->component_, blocking_time, curr_time);
534 }
535#endif
536 bool should_warn;
537 if (this->component_ != nullptr) {
538 should_warn = this->component_->should_warn_of_blocking(blocking_time);
539 } else {
540 should_warn = blocking_time > WARN_IF_BLOCKING_OVER_MS;
541 }
542 if (should_warn) {
543 ESP_LOGW(TAG, "%s took a long time for an operation (%" PRIu32 " ms)",
544 component_ == nullptr ? LOG_STR_LITERAL("<null>") : LOG_STR_ARG(component_->get_component_log_str()),
545 blocking_time);
546 ESP_LOGW(TAG, "Components should block for at most 30 ms");
547 }
548
549 return curr_time;
550}
551
553
555 // Free the setup priority map completely
556 setup_priority_overrides.reset();
557}
558
559} // 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
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.
Definition component.h:385
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:492
const LogString * component_source_
Definition component.h:504
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:512
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:445
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.
Definition component.h:410
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:327
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:501
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:505
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:511
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
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.
Definition component.h:465
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:347
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:521
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:0
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:78
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:89
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:81
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:87
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:79
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:90
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:88
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:82
const float AFTER_BLUETOOTH
Definition component.cpp:84
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
Definition component.cpp:97
const uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:96
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
Definition component.cpp:99
const uint8_t COMPONENT_STATE_MASK
Definition component.cpp:95
void log_update_interval(const char *tag, PollingComponent *component)
const uint8_t COMPONENT_STATE_LOOP
Definition component.cpp:98
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