ESPHome 2026.1.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
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
52// Forward declaration
53class PollingComponent;
54
55// Function declaration for LOG_UPDATE_INTERVAL
56void log_update_interval(const char *tag, PollingComponent *component);
57
58#define LOG_UPDATE_INTERVAL(this) log_update_interval(TAG, this)
59
60extern const uint8_t COMPONENT_STATE_MASK;
61extern const uint8_t COMPONENT_STATE_CONSTRUCTION;
62extern const uint8_t COMPONENT_STATE_SETUP;
63extern const uint8_t COMPONENT_STATE_LOOP;
64extern const uint8_t COMPONENT_STATE_FAILED;
65extern const uint8_t COMPONENT_STATE_LOOP_DONE;
66extern const uint8_t STATUS_LED_MASK;
67extern const uint8_t STATUS_LED_OK;
68extern const uint8_t STATUS_LED_WARNING;
69extern const uint8_t STATUS_LED_ERROR;
70
71enum class RetryResult { DONE, RETRY };
72
73extern const uint16_t WARN_IF_BLOCKING_OVER_MS;
74
75class Component {
76 public:
82 virtual void setup();
83
89 virtual void loop();
90
91 virtual void dump_config();
92
99 virtual float get_setup_priority() const;
100
101 float get_actual_setup_priority() const;
102
103 void set_setup_priority(float priority);
104
111 virtual float get_loop_priority() const;
112
113 void call();
114
115 virtual void on_shutdown() {}
116 virtual void on_safe_shutdown() {}
117
122 virtual bool teardown() { return true; }
123
129 virtual void on_powerdown() {}
130
131 uint8_t get_component_state() const;
132
138
143 bool is_in_loop_state() const;
144
151 bool is_idle() const;
152
159 virtual void mark_failed();
160
161 // Remove before 2026.6.0
162 ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
163 "strings. Will stop working in 2026.6.0",
164 "2025.12.0")
165 void mark_failed(const char *message) {
166#pragma GCC diagnostic push
167#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
168 this->status_set_error(message);
169#pragma GCC diagnostic pop
170 this->mark_failed();
171 }
172
173 void mark_failed(const LogString *message) {
174 this->status_set_error(message);
175 this->mark_failed();
176 }
177
186 void disable_loop();
187
196 void enable_loop();
197
218
219 bool is_failed() const;
220
221 bool is_ready() const;
222
223 virtual bool can_proceed();
224
225 bool status_has_warning() const;
226
227 bool status_has_error() const;
228
229 void status_set_warning(const char *message = nullptr);
230 void status_set_warning(const LogString *message);
231
232 void status_set_error(); // Set error flag without message
233 // Remove before 2026.6.0
234 ESPDEPRECATED("Use status_set_error(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary "
235 "strings. Will stop working in 2026.6.0",
236 "2025.12.0")
237 void status_set_error(const char *message);
238 void status_set_error(const LogString *message);
239
241
242 void status_clear_error();
243
251 void status_momentary_warning(const char *name, uint32_t length = 5000);
252
260 void status_momentary_error(const char *name, uint32_t length = 5000);
261
262 bool has_overridden_loop() const;
263
268 void set_component_source(const LogString *source) { component_source_ = source; }
273 const LogString *get_component_log_str() const;
274
275 bool should_warn_of_blocking(uint32_t blocking_time);
276
277 protected:
278 friend class Application;
279
280 virtual void call_loop();
281 virtual void call_setup();
282 virtual void call_dump_config();
283
285 void set_component_state_(uint8_t state);
286
309 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
310
325 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
326
327 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
328
334 bool cancel_interval(const std::string &name); // NOLINT
335 bool cancel_interval(const char *name); // NOLINT
336
367 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
368 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
369
370 void set_retry(const char *name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
371 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
372
373 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
374 float backoff_increase_factor = 1.0f); // NOLINT
375
381 bool cancel_retry(const std::string &name); // NOLINT
382 bool cancel_retry(const char *name); // NOLINT
383
398 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
399
414 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
415
416 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
417
423 bool cancel_timeout(const std::string &name); // NOLINT
424 bool cancel_timeout(const char *name); // NOLINT
425
433 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
434
448 void defer(const char *name, std::function<void()> &&f); // NOLINT
449
451 void defer(std::function<void()> &&f); // NOLINT
452
454 bool cancel_defer(const std::string &name); // NOLINT
455
456 // Ordered for optimal packing on 32-bit systems
457 const LogString *component_source_{nullptr};
464 uint8_t component_state_{0x00};
465 volatile bool pending_enable_loop_{false};
466};
467
475 public:
477
482 explicit PollingComponent(uint32_t update_interval);
483
490 virtual void set_update_interval(uint32_t update_interval);
491
492 // ========== OVERRIDE METHODS ==========
493 // (You'll only need this when creating your own custom sensor)
494 virtual void update() = 0;
495
496 // ========== INTERNAL METHODS ==========
497 // (In most use cases you won't need these)
498 void call_setup() override;
499
501 virtual uint32_t get_update_interval() const;
502
503 // Start the poller, used for component.suspend
504 void start_poller();
505
506 // Stop the poller, used for component.suspend
507 void stop_poller();
508
509 protected:
511};
512
514 public:
515 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
516
517 // Finish the timing operation and return the current time
518 uint32_t finish();
519
521
522 protected:
523 uint32_t started_;
525};
526
527// Function to clear setup priority overrides after all components are set up
529
530} // 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:173
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:268
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:129
const LogString * component_source_
Definition component.h:457
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.
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:465
virtual bool can_proceed()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
virtual void on_safe_shutdown()
Definition component.h:116
virtual float get_loop_priority() const
priority of loop().
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:122
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:458
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:464
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
bool cancel_interval(const std::string &name)
Cancel an interval function.
void disable_loop()
Disable this component's loop.
virtual void on_shutdown()
Definition component.h:115
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.
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:162
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.
bool is_idle() const
Check if this component is idle.
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.
This class simplifies creating components that periodically check a state.
Definition component.h:474
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:0
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:78
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: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
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 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()
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