ESPHome 2025.5.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
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 uint32_t COMPONENT_STATE_MASK;
57extern const uint32_t COMPONENT_STATE_CONSTRUCTION;
58extern const uint32_t COMPONENT_STATE_SETUP;
59extern const uint32_t COMPONENT_STATE_LOOP;
60extern const uint32_t COMPONENT_STATE_FAILED;
61extern const uint32_t STATUS_LED_MASK;
62extern const uint32_t STATUS_LED_OK;
63extern const uint32_t STATUS_LED_WARNING;
64extern const uint32_t STATUS_LED_ERROR;
65
66enum class RetryResult { DONE, RETRY };
67
68extern const uint32_t WARN_IF_BLOCKING_OVER_MS;
69
70class Component {
71 public:
77 virtual void setup();
78
84 virtual void loop();
85
86 virtual void dump_config();
87
94 virtual float get_setup_priority() const;
95
96 float get_actual_setup_priority() const;
97
98 void set_setup_priority(float priority);
99
106 virtual float get_loop_priority() const;
107
108 void call();
109
110 virtual void on_shutdown() {}
111 virtual void on_safe_shutdown() {}
112
113 uint32_t get_component_state() const;
114
121 virtual void mark_failed();
122
123 void mark_failed(const char *message) {
124 this->status_set_error(message);
125 this->mark_failed();
126 }
127
128 bool is_failed() const;
129
130 bool is_ready() const;
131
132 virtual bool can_proceed();
133
134 bool status_has_warning() const;
135
136 bool status_has_error() const;
137
138 void status_set_warning(const char *message = "unspecified");
139
140 void status_set_error(const char *message = "unspecified");
141
143
144 void status_clear_error();
145
146 void status_momentary_warning(const std::string &name, uint32_t length = 5000);
147
148 void status_momentary_error(const std::string &name, uint32_t length = 5000);
149
150 bool has_overridden_loop() const;
151
156 void set_component_source(const char *source) { component_source_ = source; }
161 const char *get_component_source() const;
162
163 bool should_warn_of_blocking(uint32_t blocking_time);
164
165 protected:
166 friend class Application;
167
168 virtual void call_loop();
169 virtual void call_setup();
170 virtual void call_dump_config();
171
194 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
195
196 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
197
203 bool cancel_interval(const std::string &name); // NOLINT
204
235 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
236 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
237
238 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
239 float backoff_increase_factor = 1.0f); // NOLINT
240
246 bool cancel_retry(const std::string &name); // NOLINT
247
262 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
263
264 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
265
271 bool cancel_timeout(const std::string &name); // NOLINT
272
280 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
281
283 void defer(std::function<void()> &&f); // NOLINT
284
286 bool cancel_defer(const std::string &name); // NOLINT
287
288 uint32_t component_state_{0x0000};
290 const char *component_source_{nullptr};
292 std::string error_message_{};
293};
294
302 public:
304
309 explicit PollingComponent(uint32_t update_interval);
310
317 virtual void set_update_interval(uint32_t update_interval);
318
319 // ========== OVERRIDE METHODS ==========
320 // (You'll only need this when creating your own custom sensor)
321 virtual void update() = 0;
322
323 // ========== INTERNAL METHODS ==========
324 // (In most use cases you won't need these)
325 void call_setup() override;
326
328 virtual uint32_t get_update_interval() const;
329
330 // Start the poller, used for component.suspend
331 void start_poller();
332
333 // Stop the poller, used for component.suspend
334 void stop_poller();
335
336 protected:
338};
339
341 public:
342 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
343
344 // Finish the timing operation and return the current time
345 uint32_t finish();
346
348
349 protected:
350 uint32_t started_;
352};
353
354} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
virtual void call_dump_config()
Definition component.cpp:82
virtual float get_setup_priority() const
priority of setup().
Definition component.cpp:49
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:51
float get_actual_setup_priority() const
bool has_overridden_loop() const
uint32_t warn_if_blocking_over_
Definition component.h:291
bool is_failed() const
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:55
void set_component_source(const char *source)
Set where this component was loaded from for some debug messages.
Definition component.h:156
bool should_warn_of_blocking(uint32_t blocking_time)
virtual bool can_proceed()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition component.cpp:76
virtual void on_safe_shutdown()
Definition component.h:111
virtual float get_loop_priority() const
priority of loop().
Definition component.cpp:47
void status_momentary_error(const std::string &name, uint32_t length=5000)
const char * get_component_source() const
Get the integration where this component was declared as a string.
bool cancel_retry(const std::string &name)
Cancel a retry function.
Definition component.cpp:68
void status_momentary_warning(const std::string &name, uint32_t length=5000)
bool is_ready() const
virtual void dump_config()
bool status_has_warning() const
bool status_has_error() const
bool cancel_interval(const std::string &name)
Cancel an interval function.
Definition component.cpp:59
void status_set_warning(const char *message="unspecified")
virtual void on_shutdown()
Definition component.h:110
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:53
void mark_failed(const char *message)
Definition component.h:123
void status_set_error(const char *message="unspecified")
uint32_t get_component_state() const
Definition component.cpp:89
uint32_t component_state_
State of this component.
Definition component.h:288
const char * component_source_
Definition component.h:290
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()
Definition component.cpp:80
float setup_priority_override_
Definition component.h:289
void status_clear_warning()
virtual void call_setup()
Definition component.cpp:81
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:72
std::string error_message_
Definition component.h:292
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.
Definition component.cpp:63
This class simplifies creating components that periodically check a state.
Definition component.h:301
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
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:16
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:27
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:18
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:25
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:17
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:28
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:26
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:20
const float AFTER_BLUETOOTH
Definition component.cpp:22
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint32_t STATUS_LED_OK
Definition component.cpp:38
const uint32_t COMPONENT_STATE_FAILED
Definition component.cpp:36
const uint32_t COMPONENT_STATE_MASK
Definition component.cpp:32
const uint32_t COMPONENT_STATE_LOOP
Definition component.cpp:35
const uint32_t COMPONENT_STATE_SETUP
Definition component.cpp:34
const uint32_t STATUS_LED_ERROR
Definition component.cpp:40
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:33
const uint32_t STATUS_LED_MASK
Definition component.cpp:37
const uint32_t STATUS_LED_WARNING
Definition component.cpp:39
const uint32_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
Definition component.cpp:42
uint16_t length
Definition tt21100.cpp:0