ESPHome 2025.9.0-dev
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";
19static const char *const UNSPECIFIED_MESSAGE = "unspecified";
20
21// Global vectors for component data that doesn't belong in every instance.
22// Using vector instead of unordered_map for both because:
23// - Much lower memory overhead (8 bytes per entry vs 20+ for unordered_map)
24// - Linear search is fine for small n (typically < 5 entries)
25// - These are rarely accessed (setup only or error cases only)
26
27// Component error messages - only stores messages for failed components
28// Lazy allocated since most configs have zero failures
29// Note: We don't clear this vector because:
30// 1. Components are never destroyed in ESPHome
31// 2. Failed components remain failed (no recovery mechanism)
32// 3. Memory usage is minimal (only failures with custom messages are stored)
33
34// Using namespace-scope static to avoid guard variables (saves 16 bytes total)
35// This is safe because ESPHome is single-threaded during initialization
36namespace {
37// Error messages for failed components
38// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
39std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> component_error_messages;
40// Setup priority overrides - freed after setup completes
41// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
42std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides;
43} // namespace
44
45namespace setup_priority {
46
47const float BUS = 1000.0f;
48const float IO = 900.0f;
49const float HARDWARE = 800.0f;
50const float DATA = 600.0f;
51const float PROCESSOR = 400.0;
52const float BLUETOOTH = 350.0f;
53const float AFTER_BLUETOOTH = 300.0f;
54const float WIFI = 250.0f;
55const float ETHERNET = 250.0f;
56const float BEFORE_CONNECTION = 220.0f;
57const float AFTER_WIFI = 200.0f;
58const float AFTER_CONNECTION = 100.0f;
59const float LATE = -100.0f;
60
61} // namespace setup_priority
62
63// Component state uses bits 0-2 (8 states, 5 used)
64const uint8_t COMPONENT_STATE_MASK = 0x07;
65const uint8_t COMPONENT_STATE_CONSTRUCTION = 0x00;
66const uint8_t COMPONENT_STATE_SETUP = 0x01;
67const uint8_t COMPONENT_STATE_LOOP = 0x02;
68const uint8_t COMPONENT_STATE_FAILED = 0x03;
69const uint8_t COMPONENT_STATE_LOOP_DONE = 0x04;
70// Status LED uses bits 3-4
71const uint8_t STATUS_LED_MASK = 0x18;
72const uint8_t STATUS_LED_OK = 0x00;
73const uint8_t STATUS_LED_WARNING = 0x08; // Bit 3
74const uint8_t STATUS_LED_ERROR = 0x10; // Bit 4
75
76const uint16_t WARN_IF_BLOCKING_OVER_MS = 50U;
77const uint16_t WARN_IF_BLOCKING_INCREMENT_MS = 10U;
78
79uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
80
81float Component::get_loop_priority() const { return 0.0f; }
82
84
86
88
89void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
90 App.scheduler.set_interval(this, name, interval, std::move(f));
91}
92
93void Component::set_interval(const char *name, uint32_t interval, std::function<void()> &&f) { // NOLINT
94 App.scheduler.set_interval(this, name, interval, std::move(f));
95}
96
97bool Component::cancel_interval(const std::string &name) { // NOLINT
98 return App.scheduler.cancel_interval(this, name);
99}
100
101bool Component::cancel_interval(const char *name) { // NOLINT
102 return App.scheduler.cancel_interval(this, name);
103}
104
105void Component::set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
106 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor) { // NOLINT
107 App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
108}
109
110bool Component::cancel_retry(const std::string &name) { // NOLINT
111 return App.scheduler.cancel_retry(this, name);
112}
113
114void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
115 App.scheduler.set_timeout(this, name, timeout, std::move(f));
116}
117
118void Component::set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
119 App.scheduler.set_timeout(this, name, timeout, std::move(f));
120}
121
122bool Component::cancel_timeout(const std::string &name) { // NOLINT
123 return App.scheduler.cancel_timeout(this, name);
124}
125
126bool Component::cancel_timeout(const char *name) { // NOLINT
127 return App.scheduler.cancel_timeout(this, name);
128}
129
130void Component::call_loop() { this->loop(); }
131void Component::call_setup() { this->setup(); }
133 this->dump_config();
134 if (this->is_failed()) {
135 // Look up error message from global vector
136 const char *error_msg = nullptr;
137 if (component_error_messages) {
138 for (const auto &pair : *component_error_messages) {
139 if (pair.first == this) {
140 error_msg = pair.second;
141 break;
142 }
143 }
144 }
145 ESP_LOGE(TAG, " %s is marked FAILED: %s", this->get_component_source(),
146 error_msg ? error_msg : UNSPECIFIED_MESSAGE);
147 }
148}
149
150uint8_t Component::get_component_state() const { return this->component_state_; }
153 switch (state) {
155 // State Construction: Call setup and set state to setup
156 this->set_component_state_(COMPONENT_STATE_SETUP);
157 ESP_LOGV(TAG, "Setup %s", this->get_component_source());
158#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
159 uint32_t start_time = millis();
160#endif
161 this->call_setup();
162#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
163 uint32_t setup_time = millis() - start_time;
164 ESP_LOGCONFIG(TAG, "Setup %s took %ums", this->get_component_source(), (unsigned) setup_time);
165#endif
166 break;
167 }
169 // State setup: Call first loop and set state to loop
170 this->set_component_state_(COMPONENT_STATE_LOOP);
171 this->call_loop();
172 break;
174 // State loop: Call loop
175 this->call_loop();
176 break;
178 // State failed: Do nothing
180 // State loop done: Do nothing, component has finished its work
181 default:
182 break;
183 }
184}
186 if (this->component_source_ == nullptr)
187 return "<unknown>";
188 return this->component_source_;
189}
190bool Component::should_warn_of_blocking(uint32_t blocking_time) {
191 if (blocking_time > this->warn_if_blocking_over_) {
192 // Prevent overflow when adding increment - if we're about to overflow, just max out
193 if (blocking_time + WARN_IF_BLOCKING_INCREMENT_MS < blocking_time ||
194 blocking_time + WARN_IF_BLOCKING_INCREMENT_MS > std::numeric_limits<uint16_t>::max()) {
195 this->warn_if_blocking_over_ = std::numeric_limits<uint16_t>::max();
196 } else {
197 this->warn_if_blocking_over_ = static_cast<uint16_t>(blocking_time + WARN_IF_BLOCKING_INCREMENT_MS);
198 }
199 return true;
200 }
201 return false;
202}
204 ESP_LOGE(TAG, "%s was marked as failed", this->get_component_source());
205 this->set_component_state_(COMPONENT_STATE_FAILED);
206 this->status_set_error();
207 // Also remove from loop since failed components shouldn't loop
209}
211 this->component_state_ &= ~COMPONENT_STATE_MASK;
212 this->component_state_ |= state;
213}
215 if ((this->component_state_ & COMPONENT_STATE_MASK) != COMPONENT_STATE_LOOP_DONE) {
216 ESP_LOGVV(TAG, "%s loop disabled", this->get_component_source());
217 this->set_component_state_(COMPONENT_STATE_LOOP_DONE);
219 }
220}
222 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE) {
223 ESP_LOGVV(TAG, "%s loop enabled", this->get_component_source());
224 this->set_component_state_(COMPONENT_STATE_LOOP);
226 }
227}
229 // This method is thread and ISR-safe because:
230 // 1. Only performs simple assignments to volatile variables (atomic on all platforms)
231 // 2. No read-modify-write operations that could be interrupted
232 // 3. No memory allocation, object construction, or function calls
233 // 4. IRAM_ATTR ensures code is in IRAM, not flash (required for ISR execution)
234 // 5. Components are never destroyed, so no use-after-free concerns
235 // 6. App is guaranteed to be initialized before any ISR could fire
236 // 7. Multiple ISR/thread calls are safe - just sets the same flags to true
237 // 8. Race condition with main loop is handled by clearing flag before processing
238 this->pending_enable_loop_ = true;
240}
242 if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED) {
243 ESP_LOGI(TAG, "%s is being reset to construction state", this->get_component_source());
244 this->set_component_state_(COMPONENT_STATE_CONSTRUCTION);
245 // Clear error status when resetting
246 this->status_clear_error();
247 }
248}
250 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP;
251}
252void Component::defer(std::function<void()> &&f) { // NOLINT
253 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), 0, std::move(f));
254}
255bool Component::cancel_defer(const std::string &name) { // NOLINT
256 return App.scheduler.cancel_timeout(this, name);
257}
258void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
259 App.scheduler.set_timeout(this, name, 0, std::move(f));
260}
261void Component::defer(const char *name, std::function<void()> &&f) { // NOLINT
262 App.scheduler.set_timeout(this, name, 0, std::move(f));
263}
264void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
265 App.scheduler.set_timeout(this, static_cast<const char *>(nullptr), timeout, std::move(f));
266}
267void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
268 App.scheduler.set_interval(this, static_cast<const char *>(nullptr), interval, std::move(f));
269}
270void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f,
271 float backoff_increase_factor) { // NOLINT
272 App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
273}
274bool Component::is_failed() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
276 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP ||
277 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE ||
278 (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_SETUP;
279}
280bool Component::can_proceed() { return true; }
283void Component::status_set_warning(const char *message) {
284 // Don't spam the log. This risks missing different warning messages though.
285 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
286 return;
289 ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE);
290}
291void Component::status_set_error(const char *message) {
292 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
293 return;
296 ESP_LOGE(TAG, "%s set Error flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE);
297 if (message != nullptr) {
298 // Lazy allocate the error messages vector if needed
299 if (!component_error_messages) {
300 component_error_messages = std::make_unique<std::vector<std::pair<const Component *, const char *>>>();
301 }
302 // Check if this component already has an error message
303 for (auto &pair : *component_error_messages) {
304 if (pair.first == this) {
305 pair.second = message;
306 return;
307 }
308 }
309 // Add new error message
310 component_error_messages->emplace_back(this, message);
311 }
312}
314 if ((this->component_state_ & STATUS_LED_WARNING) == 0)
315 return;
316 this->component_state_ &= ~STATUS_LED_WARNING;
317 ESP_LOGW(TAG, "%s cleared Warning flag", this->get_component_source());
318}
320 if ((this->component_state_ & STATUS_LED_ERROR) == 0)
321 return;
322 this->component_state_ &= ~STATUS_LED_ERROR;
323 ESP_LOGE(TAG, "%s cleared Error flag", this->get_component_source());
324}
325void Component::status_momentary_warning(const std::string &name, uint32_t length) {
326 this->status_set_warning();
327 this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
328}
329void Component::status_momentary_error(const std::string &name, uint32_t length) {
330 this->status_set_error();
331 this->set_timeout(name, length, [this]() { this->status_clear_error(); });
332}
335 // Check if there's an override in the global vector
336 if (setup_priority_overrides) {
337 // Linear search is fine for small n (typically < 5 overrides)
338 for (const auto &pair : *setup_priority_overrides) {
339 if (pair.first == this) {
340 return pair.second;
341 }
342 }
343 }
344 return this->get_setup_priority();
345}
347 // Lazy allocate the vector if needed
348 if (!setup_priority_overrides) {
349 setup_priority_overrides = std::make_unique<std::vector<std::pair<const Component *, float>>>();
350 // Reserve some space to avoid reallocations (most configs have < 10 overrides)
351 setup_priority_overrides->reserve(10);
352 }
353
354 // Check if this component already has an override
355 for (auto &pair : *setup_priority_overrides) {
356 if (pair.first == this) {
357 pair.second = priority;
358 return;
359 }
360 }
361
362 // Add new override
363 setup_priority_overrides->emplace_back(this, priority);
364}
365
367#if defined(USE_HOST) || defined(CLANG_TIDY)
368 bool loop_overridden = true;
369 bool call_loop_overridden = true;
370#else
371#pragma GCC diagnostic push
372#pragma GCC diagnostic ignored "-Wpmf-conversions"
373 bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
374 bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
375#pragma GCC diagnostic pop
376#endif
377 return loop_overridden || call_loop_overridden;
378}
379
380PollingComponent::PollingComponent(uint32_t update_interval) : update_interval_(update_interval) {}
381
383 // init the poller before calling setup, allowing setup to cancel it if desired
384 this->start_poller();
385 // Let the polling component subclass setup their HW.
386 this->setup();
387}
388
390 // Register interval.
391 this->set_interval("update", this->get_update_interval(), [this]() { this->update(); });
392}
393
395 // Clear the interval to suspend component
396 this->cancel_interval("update");
397}
398
400void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
401
403 : started_(start_time), component_(component) {}
405 uint32_t curr_time = millis();
406
407 uint32_t blocking_time = curr_time - this->started_;
408
409#ifdef USE_RUNTIME_STATS
410 // Record component runtime stats
411 if (global_runtime_stats != nullptr) {
412 global_runtime_stats->record_component_time(this->component_, blocking_time, curr_time);
413 }
414#endif
415 bool should_warn;
416 if (this->component_ != nullptr) {
417 should_warn = this->component_->should_warn_of_blocking(blocking_time);
418 } else {
419 should_warn = blocking_time > WARN_IF_BLOCKING_OVER_MS;
420 }
421 if (should_warn) {
422 const char *src = component_ == nullptr ? "<null>" : component_->get_component_source();
423 ESP_LOGW(TAG, "%s took a long time for an operation (%" PRIu32 " ms)", src, blocking_time);
424 ESP_LOGW(TAG, "Components should block for at most 30 ms");
425 }
426
427 return curr_time;
428}
429
431
433 // Free the setup priority map completely
434 setup_priority_overrides.reset();
435}
436
437} // 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()
virtual float get_setup_priority() const
priority of setup().
Definition component.cpp:83
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:85
float get_actual_setup_priority() const
bool has_overridden_loop() const
bool is_failed() const
uint8_t get_component_state() const
void status_set_warning(const char *message=nullptr)
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.cpp:89
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:416
virtual bool can_proceed()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
virtual float get_loop_priority() const
priority of loop().
Definition component.cpp:81
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.
void status_momentary_error(const std::string &name, uint32_t length=5000)
const char * get_component_source() const
Get the integration where this component was declared as a string.
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:409
bool cancel_retry(const std::string &name)
Cancel a retry function.
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:415
void status_momentary_warning(const std::string &name, uint32_t length=5000)
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
bool cancel_interval(const std::string &name)
Cancel an interval function.
Definition component.cpp:97
void disable_loop()
Disable this component's loop.
bool cancel_defer(const std::string &name)
Cancel a defer callback using the specified name, name must not be empty.
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:87
void reset_to_construction_state()
Reset this component back to the construction state to allow setup to run again.
const char * component_source_
Definition component.h:408
void set_setup_priority(float priority)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
virtual void call_loop()
void status_clear_warning()
virtual void call_setup()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_set_error(const char *message=nullptr)
void set_retry(const std::string &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.
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)
uint8_t priority
bool state
Definition fan.h:0
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:47
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:58
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:56
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:48
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:59
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:57
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:51
const float AFTER_BLUETOOTH
Definition component.cpp:53
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint8_t COMPONENT_STATE_SETUP
Definition component.cpp:66
const uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:65
const uint8_t STATUS_LED_MASK
Definition component.cpp:71
const uint16_t WARN_IF_BLOCKING_INCREMENT_MS
How long the blocking time must be larger to warn again.
Definition component.cpp:77
runtime_stats::RuntimeStatsCollector * global_runtime_stats
const uint8_t COMPONENT_STATE_FAILED
Definition component.cpp:68
const uint8_t COMPONENT_STATE_MASK
Definition component.cpp:64
const uint8_t COMPONENT_STATE_LOOP
Definition component.cpp:67
const uint16_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
Definition component.cpp:76
void clear_setup_priority_overrides()
uint32_t global_state
Definition component.cpp:79
const uint8_t STATUS_LED_OK
Definition component.cpp:72
const uint8_t STATUS_LED_WARNING
Definition component.cpp:73
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
const uint8_t COMPONENT_STATE_LOOP_DONE
Definition component.cpp:69
const uint8_t STATUS_LED_ERROR
Definition component.cpp:74
uint16_t length
Definition tt21100.cpp:0