ESPHome 2026.3.0-dev
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
54enum class InternalSchedulerID : uint32_t {
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;
146
152
157 bool is_in_loop_state() const;
158
165 bool is_idle() const;
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;
234
235 bool is_ready() const;
236
237 virtual bool can_proceed();
238
239 bool status_has_warning() const;
240
241 bool status_has_error() const;
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
256 void status_clear_error();
257
265 void status_momentary_warning(const char *name, uint32_t length = 5000);
266
274 void status_momentary_error(const char *name, uint32_t length = 5000);
275
276 bool has_overridden_loop() const { return (this->component_state_ & COMPONENT_HAS_LOOP) != 0; }
277
282 void set_component_source(const LogString *source) { component_source_ = source; }
287 const LogString *get_component_log_str() const;
288
290
291 protected:
292 friend class Application;
293
294 void call_loop_();
295 virtual void call_setup();
296 void call_dump_config_();
297
299 inline void set_component_state_(uint8_t state) {
300 this->component_state_ &= ~COMPONENT_STATE_MASK;
301 this->component_state_ |= state;
302 }
303
308 bool set_status_flag_(uint8_t flag);
309
332 // Remove before 2026.7.0
333 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
334 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
335
350 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
351
358 void set_interval(uint32_t id, uint32_t interval, std::function<void()> &&f); // NOLINT
359
360 void set_interval(InternalSchedulerID id, uint32_t interval, std::function<void()> &&f); // NOLINT
361
362 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
363
369 // Remove before 2026.7.0
370 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
371 bool cancel_interval(const std::string &name); // NOLINT
372 bool cancel_interval(const char *name); // NOLINT
373 bool cancel_interval(uint32_t id); // NOLINT
375
377 // Remove before 2026.8.0
378 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
379 "2026.2.0")
380 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
381 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
382
383 // Remove before 2026.8.0
384 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
385 "2026.2.0")
386 void set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
387 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
388
389 // Remove before 2026.8.0
390 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
391 "2026.2.0")
392 void set_retry(uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
393 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
394
395 // Remove before 2026.8.0
396 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
397 "2026.2.0")
398 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
399 float backoff_increase_factor = 1.0f); // NOLINT
400
401 // Remove before 2026.8.0
402 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
403 bool cancel_retry(const std::string &name); // NOLINT
404 // Remove before 2026.8.0
405 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
406 bool cancel_retry(const char *name); // NOLINT
407 // Remove before 2026.8.0
408 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
409 bool cancel_retry(uint32_t id); // NOLINT
410
425 // Remove before 2026.7.0
426 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
427 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
428
443 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
444
451 void set_timeout(uint32_t id, uint32_t timeout, std::function<void()> &&f); // NOLINT
452
453 void set_timeout(InternalSchedulerID id, uint32_t timeout, std::function<void()> &&f); // NOLINT
454
455 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
456
462 // Remove before 2026.7.0
463 ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
464 bool cancel_timeout(const std::string &name); // NOLINT
465 bool cancel_timeout(const char *name); // NOLINT
466 bool cancel_timeout(uint32_t id); // NOLINT
468
476 // Remove before 2026.7.0
477 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
478 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
479
493 void defer(const char *name, std::function<void()> &&f); // NOLINT
494
496 void defer(std::function<void()> &&f); // NOLINT
497
499 void defer(uint32_t id, std::function<void()> &&f); // NOLINT
500
502 // Remove before 2026.7.0
503 ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0")
504 bool cancel_defer(const std::string &name); // NOLINT
505 bool cancel_defer(const char *name); // NOLINT
506 bool cancel_defer(uint32_t id); // NOLINT
507
508 // Ordered for optimal packing on 32-bit systems
509 const LogString *component_source_{nullptr};
517 uint8_t component_state_{0x00};
518 volatile bool pending_enable_loop_{false};
519};
520
528 public:
530
535 explicit PollingComponent(uint32_t update_interval);
536
543 virtual void set_update_interval(uint32_t update_interval);
544
545 // ========== OVERRIDE METHODS ==========
546 // (You'll only need this when creating your own custom sensor)
547 virtual void update() = 0;
548
549 // ========== INTERNAL METHODS ==========
550 // (In most use cases you won't need these)
551 void call_setup() override;
552
554 virtual uint32_t get_update_interval() const;
555
556 // Start the poller, used for component.suspend
557 void start_poller();
558
559 // Stop the poller, used for component.suspend
560 void stop_poller();
561
562 protected:
564};
565
567 public:
569 : started_(start_time), component_(component) {}
570
571 // Finish the timing operation and return the current time
572 uint32_t finish();
573
575
576 protected:
577 uint32_t started_;
579};
580
581// Function to clear setup priority overrides after all components are set up
582// Only has an implementation when USE_SETUP_PRIORITY_OVERRIDE is defined
584
585} // namespace esphome
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:276
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:282
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:493
const LogString * component_source_
Definition component.h:509
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
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:518
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:443
virtual float get_loop_priority() const
priority of loop().
Definition component.cpp:89
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:350
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: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:505
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:510
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:517
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)
Definition component.h:299
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:387
bool status_has_error() const
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:386
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:465
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:386
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:372
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:387
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:527
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)
Definition component.h:568
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
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()
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
uint16_t length
Definition tt21100.cpp:0