ESPHome 2025.9.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
10namespace esphome {
11
16namespace setup_priority {
17
19extern const float BUS;
21extern const float IO;
23extern const float HARDWARE;
25extern const float DATA;
27extern const float HARDWARE_LATE;
29extern const float PROCESSOR;
30extern const float BLUETOOTH;
31extern const float AFTER_BLUETOOTH;
32extern const float WIFI;
33extern const float ETHERNET;
35extern const float BEFORE_CONNECTION;
37extern const float AFTER_WIFI;
39extern const float AFTER_CONNECTION;
41extern const float LATE;
42
43} // namespace setup_priority
44
45static const uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
46
47#define LOG_UPDATE_INTERVAL(this) \
48 if (this->get_update_interval() == SCHEDULER_DONT_RUN) { \
49 ESP_LOGCONFIG(TAG, " Update Interval: never"); \
50 } else if (this->get_update_interval() < 100) { \
51 ESP_LOGCONFIG(TAG, " Update Interval: %.3fs", this->get_update_interval() / 1000.0f); \
52 } else { \
53 ESP_LOGCONFIG(TAG, " Update Interval: %.1fs", this->get_update_interval() / 1000.0f); \
54 }
55
56extern const uint8_t COMPONENT_STATE_MASK;
57extern const uint8_t COMPONENT_STATE_CONSTRUCTION;
58extern const uint8_t COMPONENT_STATE_SETUP;
59extern const uint8_t COMPONENT_STATE_LOOP;
60extern const uint8_t COMPONENT_STATE_FAILED;
61extern const uint8_t COMPONENT_STATE_LOOP_DONE;
62extern const uint8_t STATUS_LED_MASK;
63extern const uint8_t STATUS_LED_OK;
64extern const uint8_t STATUS_LED_WARNING;
65extern const uint8_t STATUS_LED_ERROR;
66
67enum class RetryResult { DONE, RETRY };
68
69extern const uint16_t WARN_IF_BLOCKING_OVER_MS;
70
71class Component {
72 public:
78 virtual void setup();
79
85 virtual void loop();
86
87 virtual void dump_config();
88
95 virtual float get_setup_priority() const;
96
97 float get_actual_setup_priority() const;
98
99 void set_setup_priority(float priority);
100
107 virtual float get_loop_priority() const;
108
109 void call();
110
111 virtual void on_shutdown() {}
112 virtual void on_safe_shutdown() {}
113
118 virtual bool teardown() { return true; }
119
125 virtual void on_powerdown() {}
126
127 uint8_t get_component_state() const;
128
134
139 bool is_in_loop_state() const;
140
147 virtual void mark_failed();
148
149 void mark_failed(const char *message) {
150 this->status_set_error(message);
151 this->mark_failed();
152 }
153
162 void disable_loop();
163
172 void enable_loop();
173
194
195 bool is_failed() const;
196
197 bool is_ready() const;
198
199 virtual bool can_proceed();
200
201 bool status_has_warning() const;
202
203 bool status_has_error() const;
204
205 void status_set_warning(const char *message = nullptr);
206
207 void status_set_error(const char *message = nullptr);
208
210
211 void status_clear_error();
212
213 void status_momentary_warning(const std::string &name, uint32_t length = 5000);
214
215 void status_momentary_error(const std::string &name, uint32_t length = 5000);
216
217 bool has_overridden_loop() const;
218
223 void set_component_source(const char *source) { component_source_ = source; }
228 const char *get_component_source() const;
229
230 bool should_warn_of_blocking(uint32_t blocking_time);
231
232 protected:
233 friend class Application;
234
235 virtual void call_loop();
236 virtual void call_setup();
237 virtual void call_dump_config();
238
240 void set_component_state_(uint8_t state);
241
264 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
265
280 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
281
282 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
283
289 bool cancel_interval(const std::string &name); // NOLINT
290 bool cancel_interval(const char *name); // NOLINT
291
322 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
323 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
324
325 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
326 float backoff_increase_factor = 1.0f); // NOLINT
327
333 bool cancel_retry(const std::string &name); // NOLINT
334
349 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
350
365 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
366
367 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
368
374 bool cancel_timeout(const std::string &name); // NOLINT
375 bool cancel_timeout(const char *name); // NOLINT
376
384 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
385
399 void defer(const char *name, std::function<void()> &&f); // NOLINT
400
402 void defer(std::function<void()> &&f); // NOLINT
403
405 bool cancel_defer(const std::string &name); // NOLINT
406
407 // Ordered for optimal packing on 32-bit systems
408 const char *component_source_{nullptr};
415 uint8_t component_state_{0x00};
416 volatile bool pending_enable_loop_{false};
417};
418
426 public:
428
433 explicit PollingComponent(uint32_t update_interval);
434
441 virtual void set_update_interval(uint32_t update_interval);
442
443 // ========== OVERRIDE METHODS ==========
444 // (You'll only need this when creating your own custom sensor)
445 virtual void update() = 0;
446
447 // ========== INTERNAL METHODS ==========
448 // (In most use cases you won't need these)
449 void call_setup() override;
450
452 virtual uint32_t get_update_interval() const;
453
454 // Start the poller, used for component.suspend
455 void start_poller();
456
457 // Stop the poller, used for component.suspend
458 void stop_poller();
459
460 protected:
462};
463
465 public:
466 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
467
468 // Finish the timing operation and return the current time
469 uint32_t finish();
470
472
473 protected:
474 uint32_t started_;
476};
477
478// Function to clear setup priority overrides after all components are set up
480
481} // namespace esphome
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
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:125
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
void set_component_source(const char *source)
Set where this component was loaded from for some debug messages.
Definition component.h:223
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 void on_safe_shutdown()
Definition component.h:112
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)
virtual bool teardown()
Called during teardown to allow component to gracefully finish operations.
Definition component.h:118
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.
virtual void on_shutdown()
Definition component.h:111
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 mark_failed(const char *message)
Definition component.h:149
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.
This class simplifies creating components that periodically check a state.
Definition component.h:425
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)
uint8_t priority
bool state
Definition fan.h:0
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:47
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: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
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 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()
const uint8_t STATUS_LED_OK
Definition component.cpp:72
const uint8_t STATUS_LED_WARNING
Definition component.cpp:73
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