ESPHome 2026.3.2
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
10#include "esphome/core/log.h"
12
13namespace esphome {
14
15// Forward declaration for LogString
16struct LogString;
17
22namespace setup_priority {
23
25inline constexpr float BUS = 1000.0f;
27inline constexpr float IO = 900.0f;
29inline constexpr float HARDWARE = 800.0f;
31inline constexpr float DATA = 600.0f;
33inline constexpr float PROCESSOR = 400.0f;
34inline constexpr float BLUETOOTH = 350.0f;
35inline constexpr float AFTER_BLUETOOTH = 300.0f;
36inline constexpr float WIFI = 250.0f;
37inline constexpr float ETHERNET = 250.0f;
39inline constexpr float BEFORE_CONNECTION = 220.0f;
41inline constexpr float AFTER_WIFI = 200.0f;
43inline constexpr float AFTER_CONNECTION = 100.0f;
45inline constexpr float LATE = -100.0f;
46
47} // namespace setup_priority
48
49inline constexpr uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
50
55 POLLING_UPDATE = 0, // PollingComponent interval
56 DELAY_ACTION = 1, // DelayAction timeout
57};
58
59// Forward declaration
60class PollingComponent;
61
62// Function declaration for LOG_UPDATE_INTERVAL
63void log_update_interval(const char *tag, PollingComponent *component);
64
65#define LOG_UPDATE_INTERVAL(this) log_update_interval(TAG, this)
66
67// Component state uses bits 0-2 (8 states, 5 used)
68inline constexpr uint8_t COMPONENT_STATE_MASK = 0x07;
69inline constexpr uint8_t COMPONENT_STATE_CONSTRUCTION = 0x00;
70inline constexpr uint8_t COMPONENT_STATE_SETUP = 0x01;
71inline constexpr uint8_t COMPONENT_STATE_LOOP = 0x02;
72inline constexpr uint8_t COMPONENT_STATE_FAILED = 0x03;
73inline constexpr uint8_t COMPONENT_STATE_LOOP_DONE = 0x04;
74// Status LED uses bits 3-4
75inline constexpr uint8_t STATUS_LED_MASK = 0x18;
76inline constexpr uint8_t STATUS_LED_OK = 0x00;
77inline constexpr uint8_t STATUS_LED_WARNING = 0x08;
78inline constexpr uint8_t STATUS_LED_ERROR = 0x10;
79// Component loop override flag uses bit 5 (set at registration time)
80inline constexpr uint8_t COMPONENT_HAS_LOOP = 0x20;
81
82// Remove before 2026.8.0
83enum class RetryResult { DONE, RETRY };
84
85inline constexpr uint16_t WARN_IF_BLOCKING_OVER_MS = 50U;
86
87class Component {
88 public:
94 virtual void setup();
95
101 virtual void loop();
102
103 virtual void dump_config();
104
111 virtual float get_setup_priority() const;
112
113 float get_actual_setup_priority() const;
114
115 void set_setup_priority(float priority);
116
123#ifdef USE_LOOP_PRIORITY
124 virtual float get_loop_priority() const;
125#endif
126
127 void call();
128
129 virtual void on_shutdown() {}
130 virtual void on_safe_shutdown() {}
131
136 virtual bool teardown() { return true; }
137
143 virtual void on_powerdown() {}
144
145 uint8_t get_component_state() const { return this->component_state_; }
146
152
157 bool is_in_loop_state() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP; }
158
165 bool is_idle() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE; }
166
173 void mark_failed();
174
175 // Remove before 2026.6.0
176 ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
177 "strings. Will stop working in 2026.6.0",
178 "2025.12.0")
179 void mark_failed(const char *message) {
180#pragma GCC diagnostic push
181#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
182 this->status_set_error(message);
183#pragma GCC diagnostic pop
184 this->mark_failed();
185 }
186
187 void mark_failed(const LogString *message) {
188 this->status_set_error(message);
189 this->mark_failed();
190 }
191
200 void disable_loop();
201
210 void enable_loop();
211
232
233 bool is_failed() const { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
234
235 bool is_ready() const;
236
237 virtual bool can_proceed();
238
240
241 bool status_has_error() const { return this->component_state_ & STATUS_LED_ERROR; }
242
243 void status_set_warning(const char *message = nullptr);
244 void status_set_warning(const LogString *message);
245
246 void status_set_error(); // Set error flag without message
247 // Remove before 2026.6.0
248 ESPDEPRECATED("Use status_set_error(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
249 "strings. Will stop working in 2026.6.0",
250 "2025.12.0")
251 void status_set_error(const char *message);
252 void status_set_error(const LogString *message);
253
255 if ((this->component_state_ & STATUS_LED_WARNING) == 0)
256 return;
258 }
259
261 if ((this->component_state_ & STATUS_LED_ERROR) == 0)
262 return;
264 }
265
273 void status_momentary_warning(const char *name, uint32_t length = 5000);
274
282 void status_momentary_error(const char *name, uint32_t length = 5000);
283
284 bool has_overridden_loop() const { return (this->component_state_ & COMPONENT_HAS_LOOP) != 0; }
285
295 const LogString *get_component_log_str() const;
296
298
299 protected:
300 friend class Application;
301
302 void call_loop_();
303 virtual void call_setup();
304 void call_dump_config_();
305
307 inline void set_component_state_(uint8_t state) {
308 this->component_state_ &= ~COMPONENT_STATE_MASK;
309 this->component_state_ |= state;
310 }
311
316 bool set_status_flag_(uint8_t flag);
317
340 // Remove before 2026.7.0
341 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
342 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
343
358 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
359
366 void set_interval(uint32_t id, uint32_t interval, std::function<void()> &&f); // NOLINT
367
368 void set_interval(InternalSchedulerID id, uint32_t interval, std::function<void()> &&f); // NOLINT
369
370 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
371
377 // Remove before 2026.7.0
378 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
379 bool cancel_interval(const std::string &name); // NOLINT
380 bool cancel_interval(const char *name); // NOLINT
381 bool cancel_interval(uint32_t id); // NOLINT
383
385 // Remove before 2026.8.0
386 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
387 "2026.2.0")
388 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
389 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
390
391 // Remove before 2026.8.0
392 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
393 "2026.2.0")
394 void set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
395 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
396
397 // Remove before 2026.8.0
398 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
399 "2026.2.0")
400 void set_retry(uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
401 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
402
403 // Remove before 2026.8.0
404 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
405 "2026.2.0")
406 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
407 float backoff_increase_factor = 1.0f); // NOLINT
408
409 // Remove before 2026.8.0
410 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
411 bool cancel_retry(const std::string &name); // NOLINT
412 // Remove before 2026.8.0
413 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
414 bool cancel_retry(const char *name); // NOLINT
415 // Remove before 2026.8.0
416 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
417 bool cancel_retry(uint32_t id); // NOLINT
418
433 // Remove before 2026.7.0
434 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
435 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
436
451 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
452
459 void set_timeout(uint32_t id, uint32_t timeout, std::function<void()> &&f); // NOLINT
460
461 void set_timeout(InternalSchedulerID id, uint32_t timeout, std::function<void()> &&f); // NOLINT
462
463 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
464
470 // Remove before 2026.7.0
471 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
472 bool cancel_timeout(const std::string &name); // NOLINT
473 bool cancel_timeout(const char *name); // NOLINT
474 bool cancel_timeout(uint32_t id); // NOLINT
476
484 // Remove before 2026.7.0
485 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
486 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
487
501 void defer(const char *name, std::function<void()> &&f); // NOLINT
502
504 void defer(std::function<void()> &&f); // NOLINT
505
507 void defer(uint32_t id, std::function<void()> &&f); // NOLINT
508
510 // Remove before 2026.7.0
511 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
512 bool cancel_defer(const std::string &name); // NOLINT
513 bool cancel_defer(const char *name); // NOLINT
514 bool cancel_defer(uint32_t id); // NOLINT
515
518
519 // Ordered for optimal packing on 32-bit systems
520 const LogString *component_source_{nullptr};
528 uint8_t component_state_{0x00};
529 volatile bool pending_enable_loop_{false};
530};
531
539 public:
541
546 explicit PollingComponent(uint32_t update_interval);
547
554 virtual void set_update_interval(uint32_t update_interval);
555
556 // ========== OVERRIDE METHODS ==========
557 // (You'll only need this when creating your own custom sensor)
558 virtual void update() = 0;
559
560 // ========== INTERNAL METHODS ==========
561 // (In most use cases you won't need these)
562 void call_setup() override;
563
565 virtual uint32_t get_update_interval() const;
566
567 // Start the poller, used for component.suspend
568 void start_poller();
569
570 // Stop the poller, used for component.suspend
571 void stop_poller();
572
573 protected:
575};
576
577#ifdef USE_RUNTIME_STATS
578uint32_t micros(); // Forward declare for inline constructor
579#endif
580
582 public:
584 : started_(start_time),
586#ifdef USE_RUNTIME_STATS
587 ,
589#endif
590 {
591 }
592
593 // Finish the timing operation and return the current time
594 uint32_t finish();
595
597
598 protected:
601#ifdef USE_RUNTIME_STATS
603#endif
604};
605
606// Function to clear setup priority overrides after all components are set up
607// Only has an implementation when USE_SETUP_PRIORITY_OVERRIDE is defined
609
610} // namespace esphome
media_source::MediaSource * source
void mark_failed()
Mark this component as failed.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
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().
Definition component.cpp:92
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:94
float get_actual_setup_priority() const
bool has_overridden_loop() const
Definition component.h:284
void mark_failed(const LogString *message)
Definition component.h:187
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:290
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:143
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:501
const LogString * component_source_
Definition component.h:520
bool set_status_flag_(uint8_t flag)
Helper to set a status LED flag on both this component and the app.
bool is_failed() const
Definition component.h:233
uint8_t get_component_state() const
Definition component.h:145
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:529
virtual bool can_proceed()
virtual void on_safe_shutdown()
Definition component.h:130
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:451
virtual float get_loop_priority() const
priority of loop().
Definition component.cpp:89
void status_clear_error()
Definition component.h:260
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:358
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.
Definition component.h:157
virtual bool teardown()
Called during teardown to allow component to gracefully finish operations.
Definition component.h:136
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:513
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:521
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:528
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 status_clear_warning_slow_path_()
void set_component_state_(uint8_t state)
Helper to set component state (clears state bits and sets new state)
Definition component.h:307
bool status_has_warning() const
Definition component.h:239
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:395
bool status_has_error() const
Definition component.h:241
void status_clear_error_slow_path_()
void disable_loop()
Disable this component's loop.
virtual void on_shutdown()
Definition component.h:129
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:394
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:96
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:473
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:394
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:176
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:380
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:395
void status_clear_warning()
Definition component.h:254
virtual void call_setup()
bool is_idle() const
Check if this component is idle.
Definition component.h:165
This class simplifies creating components that periodically check a state.
Definition component.h:538
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 ~WarnIfComponentBlockingGuard)()=default
Definition component.h:596
const Component * component
Definition component.cpp:37
const char * message
Definition component.cpp:38
uint8_t priority
bool state
Definition fan.h:2
constexpr float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.h:39
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:29
constexpr float WIFI
Definition component.h:36
constexpr float ETHERNET
Definition component.h:37
constexpr float AFTER_BLUETOOTH
Definition component.h:35
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:41
constexpr float LATE
For components that should be initialized at the very end of the setup process.
Definition component.h:45
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Definition component.h:31
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:33
constexpr float BLUETOOTH
Definition component.h:34
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:25
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:27
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:43
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char * tag
Definition log.h:74
constexpr uint8_t COMPONENT_STATE_FAILED
Definition component.h:72
constexpr uint8_t COMPONENT_HAS_LOOP
Definition component.h:80
InternalSchedulerID
Type-safe scheduler IDs for core base classes.
Definition component.h:54
constexpr uint8_t STATUS_LED_MASK
Definition component.h:75
constexpr uint8_t COMPONENT_STATE_LOOP
Definition component.h:71
constexpr uint8_t STATUS_LED_WARNING
Definition component.h:77
constexpr uint8_t COMPONENT_STATE_MASK
Definition component.h:68
void log_update_interval(const char *tag, PollingComponent *component)
void clear_setup_priority_overrides()
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
static void uint32_t blocking_time
constexpr uint16_t WARN_IF_BLOCKING_OVER_MS
Definition component.h:85
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
Definition component.h:73
constexpr uint8_t COMPONENT_STATE_SETUP
Definition component.h:70
constexpr uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.h:69
constexpr uint8_t STATUS_LED_OK
Definition component.h:76
constexpr uint8_t STATUS_LED_ERROR
Definition component.h:78
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:49
static void uint32_t
uint16_t length
Definition tt21100.cpp:0