ESPHome 2026.2.0
Loading...
Searching...
No Matches
component.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <functional>
6#include <string>
7
9#include "esphome/core/log.h"
11
12namespace esphome {
13
14// Forward declaration for LogString
15struct LogString;
16
21namespace setup_priority {
22
24extern const float BUS;
26extern const float IO;
28extern const float HARDWARE;
30extern const float DATA;
32extern const float HARDWARE_LATE;
34extern const float PROCESSOR;
35extern const float BLUETOOTH;
36extern const float AFTER_BLUETOOTH;
37extern const float WIFI;
38extern const float ETHERNET;
40extern const float BEFORE_CONNECTION;
42extern const float AFTER_WIFI;
44extern const float AFTER_CONNECTION;
46extern const float LATE;
47
48} // namespace setup_priority
49
50static const uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
51
55enum class InternalSchedulerID : uint32_t {
56 POLLING_UPDATE = 0, // PollingComponent interval
57 DELAY_ACTION = 1, // DelayAction timeout
58};
59
60// Forward declaration
61class PollingComponent;
62
63// Function declaration for LOG_UPDATE_INTERVAL
64void log_update_interval(const char *tag, PollingComponent *component);
65
66#define LOG_UPDATE_INTERVAL(this) log_update_interval(TAG, this)
67
68extern const uint8_t COMPONENT_STATE_MASK;
69extern const uint8_t COMPONENT_STATE_CONSTRUCTION;
70extern const uint8_t COMPONENT_STATE_SETUP;
71extern const uint8_t COMPONENT_STATE_LOOP;
72extern const uint8_t COMPONENT_STATE_FAILED;
73extern const uint8_t COMPONENT_STATE_LOOP_DONE;
74extern const uint8_t STATUS_LED_MASK;
75extern const uint8_t STATUS_LED_OK;
76extern const uint8_t STATUS_LED_WARNING;
77extern const uint8_t STATUS_LED_ERROR;
78
79// Remove before 2026.8.0
80enum class RetryResult { DONE, RETRY };
81
82extern const uint16_t WARN_IF_BLOCKING_OVER_MS;
83
84class Component {
85 public:
91 virtual void setup();
92
98 virtual void loop();
99
100 virtual void dump_config();
101
108 virtual float get_setup_priority() const;
109
110 float get_actual_setup_priority() const;
111
112 void set_setup_priority(float priority);
113
120 virtual float get_loop_priority() const;
121
122 void call();
123
124 virtual void on_shutdown() {}
125 virtual void on_safe_shutdown() {}
126
131 virtual bool teardown() { return true; }
132
138 virtual void on_powerdown() {}
139
140 uint8_t get_component_state() const;
141
147
152 bool is_in_loop_state() const;
153
160 bool is_idle() const;
161
168 virtual void mark_failed();
169
170 // Remove before 2026.6.0
171 ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
172 "strings. Will stop working in 2026.6.0",
173 "2025.12.0")
174 void mark_failed(const char *message) {
175#pragma GCC diagnostic push
176#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
177 this->status_set_error(message);
178#pragma GCC diagnostic pop
179 this->mark_failed();
180 }
181
182 void mark_failed(const LogString *message) {
183 this->status_set_error(message);
184 this->mark_failed();
185 }
186
195 void disable_loop();
196
205 void enable_loop();
206
227
228 bool is_failed() const;
229
230 bool is_ready() const;
231
232 virtual bool can_proceed();
233
234 bool status_has_warning() const;
235
236 bool status_has_error() const;
237
238 void status_set_warning(const char *message = nullptr);
239 void status_set_warning(const LogString *message);
240
241 void status_set_error(); // Set error flag without message
242 // Remove before 2026.6.0
243 ESPDEPRECATED("Use status_set_error(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
244 "strings. Will stop working in 2026.6.0",
245 "2025.12.0")
246 void status_set_error(const char *message);
247 void status_set_error(const LogString *message);
248
250
251 void status_clear_error();
252
260 void status_momentary_warning(const char *name, uint32_t length = 5000);
261
269 void status_momentary_error(const char *name, uint32_t length = 5000);
270
271 bool has_overridden_loop() const;
272
277 void set_component_source(const LogString *source) { component_source_ = source; }
282 const LogString *get_component_log_str() const;
283
284 bool should_warn_of_blocking(uint32_t blocking_time);
285
286 protected:
287 friend class Application;
288
289 virtual void call_loop();
290 virtual void call_setup();
291 virtual void call_dump_config();
292
294 void set_component_state_(uint8_t state);
295
318 // Remove before 2026.7.0
319 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
320 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
321
336 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
337
344 void set_interval(uint32_t id, uint32_t interval, std::function<void()> &&f); // NOLINT
345
346 void set_interval(InternalSchedulerID id, uint32_t interval, std::function<void()> &&f); // NOLINT
347
348 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
349
355 // Remove before 2026.7.0
356 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
357 bool cancel_interval(const std::string &name); // NOLINT
358 bool cancel_interval(const char *name); // NOLINT
359 bool cancel_interval(uint32_t id); // NOLINT
361
363 // Remove before 2026.8.0
364 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
365 "2026.2.0")
366 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
367 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
368
369 // Remove before 2026.8.0
370 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
371 "2026.2.0")
372 void set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
373 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
374
375 // Remove before 2026.8.0
376 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
377 "2026.2.0")
378 void set_retry(uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
379 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
380
381 // Remove before 2026.8.0
382 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
383 "2026.2.0")
384 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
385 float backoff_increase_factor = 1.0f); // NOLINT
386
387 // Remove before 2026.8.0
388 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
389 bool cancel_retry(const std::string &name); // NOLINT
390 // Remove before 2026.8.0
391 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
392 bool cancel_retry(const char *name); // NOLINT
393 // Remove before 2026.8.0
394 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
395 bool cancel_retry(uint32_t id); // NOLINT
396
411 // Remove before 2026.7.0
412 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
413 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
414
429 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
430
437 void set_timeout(uint32_t id, uint32_t timeout, std::function<void()> &&f); // NOLINT
438
439 void set_timeout(InternalSchedulerID id, uint32_t timeout, std::function<void()> &&f); // NOLINT
440
441 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
442
448 // Remove before 2026.7.0
449 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
450 bool cancel_timeout(const std::string &name); // NOLINT
451 bool cancel_timeout(const char *name); // NOLINT
452 bool cancel_timeout(uint32_t id); // NOLINT
454
462 // Remove before 2026.7.0
463 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
464 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
465
479 void defer(const char *name, std::function<void()> &&f); // NOLINT
480
482 void defer(std::function<void()> &&f); // NOLINT
483
485 void defer(uint32_t id, std::function<void()> &&f); // NOLINT
486
488 // Remove before 2026.7.0
489 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
490 bool cancel_defer(const std::string &name); // NOLINT
491 bool cancel_defer(const char *name); // NOLINT
492 bool cancel_defer(uint32_t id); // NOLINT
493
494 // Ordered for optimal packing on 32-bit systems
495 const LogString *component_source_{nullptr};
502 uint8_t component_state_{0x00};
503 volatile bool pending_enable_loop_{false};
504};
505
513 public:
515
520 explicit PollingComponent(uint32_t update_interval);
521
528 virtual void set_update_interval(uint32_t update_interval);
529
530 // ========== OVERRIDE METHODS ==========
531 // (You'll only need this when creating your own custom sensor)
532 virtual void update() = 0;
533
534 // ========== INTERNAL METHODS ==========
535 // (In most use cases you won't need these)
536 void call_setup() override;
537
539 virtual uint32_t get_update_interval() const;
540
541 // Start the poller, used for component.suspend
542 void start_poller();
543
544 // Stop the poller, used for component.suspend
545 void stop_poller();
546
547 protected:
549};
550
552 public:
553 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
554
555 // Finish the timing operation and return the current time
556 uint32_t finish();
557
559
560 protected:
561 uint32_t started_;
563};
564
565// Function to clear setup priority overrides after all components are set up
567
568} // namespace esphome
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.
ESPDEPRECATED("Use status_set_error(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void status_set_error(const char *message)
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
void mark_failed(const LogString *message)
Definition component.h:182
const LogString * get_component_log_str() const
Get the integration where this component was declared as a LogString for logging.
void set_component_source(const LogString *source)
Set where this component was loaded from for some debug messages.
Definition component.h:277
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:138
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()
virtual void on_safe_shutdown()
Definition component.h:125
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.
virtual bool teardown()
Called during teardown to allow component to gracefully finish operations.
Definition component.h:131
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.
virtual void on_shutdown()
Definition component.h:124
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 mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void mark_failed(const char *message)
Definition component.h:171
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)
const Component * component
Definition component.cpp:37
const char * message
Definition component.cpp:38
uint8_t priority
bool state
Definition fan.h:2
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:81
const float HARDWARE_LATE
Alias for DATA (here for compatibility reasons)
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
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
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()
const uint8_t STATUS_LED_OK
const uint8_t STATUS_LED_WARNING
const uint8_t COMPONENT_STATE_LOOP_DONE
const uint8_t STATUS_LED_ERROR
uint16_t length
Definition tt21100.cpp:0