ESPHome 2025.12.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
8#include "esphome/core/log.h"
10
11namespace esphome {
12
13// Forward declaration for LogString
14struct LogString;
15
20namespace setup_priority {
21
23extern const float BUS;
25extern const float IO;
27extern const float HARDWARE;
29extern const float DATA;
31extern const float HARDWARE_LATE;
33extern const float PROCESSOR;
34extern const float BLUETOOTH;
35extern const float AFTER_BLUETOOTH;
36extern const float WIFI;
37extern const float ETHERNET;
39extern const float BEFORE_CONNECTION;
41extern const float AFTER_WIFI;
43extern const float AFTER_CONNECTION;
45extern const float LATE;
46
47} // namespace setup_priority
48
49static const uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
50
51// Forward declaration
52class PollingComponent;
53
54// Function declaration for LOG_UPDATE_INTERVAL
55void log_update_interval(const char *tag, PollingComponent *component);
56
57#define LOG_UPDATE_INTERVAL(this) log_update_interval(TAG, this)
58
59extern const uint8_t COMPONENT_STATE_MASK;
60extern const uint8_t COMPONENT_STATE_CONSTRUCTION;
61extern const uint8_t COMPONENT_STATE_SETUP;
62extern const uint8_t COMPONENT_STATE_LOOP;
63extern const uint8_t COMPONENT_STATE_FAILED;
64extern const uint8_t COMPONENT_STATE_LOOP_DONE;
65extern const uint8_t STATUS_LED_MASK;
66extern const uint8_t STATUS_LED_OK;
67extern const uint8_t STATUS_LED_WARNING;
68extern const uint8_t STATUS_LED_ERROR;
69
70enum class RetryResult { DONE, RETRY };
71
72extern const uint16_t WARN_IF_BLOCKING_OVER_MS;
73
74class Component {
75 public:
81 virtual void setup();
82
88 virtual void loop();
89
90 virtual void dump_config();
91
98 virtual float get_setup_priority() const;
99
100 float get_actual_setup_priority() const;
101
102 void set_setup_priority(float priority);
103
110 virtual float get_loop_priority() const;
111
112 void call();
113
114 virtual void on_shutdown() {}
115 virtual void on_safe_shutdown() {}
116
121 virtual bool teardown() { return true; }
122
128 virtual void on_powerdown() {}
129
130 uint8_t get_component_state() const;
131
137
142 bool is_in_loop_state() const;
143
150 bool is_idle() const;
151
158 virtual void mark_failed();
159
160 void mark_failed(const char *message) {
161 this->status_set_error(message);
162 this->mark_failed();
163 }
164
173 void disable_loop();
174
183 void enable_loop();
184
205
206 bool is_failed() const;
207
208 bool is_ready() const;
209
210 virtual bool can_proceed();
211
212 bool status_has_warning() const;
213
214 bool status_has_error() const;
215
216 void status_set_warning(const char *message = nullptr);
217 void status_set_warning(const LogString *message);
218
219 void status_set_error(const char *message = nullptr);
220
222
223 void status_clear_error();
224
225 void status_momentary_warning(const std::string &name, uint32_t length = 5000);
226
227 void status_momentary_error(const std::string &name, uint32_t length = 5000);
228
229 bool has_overridden_loop() const;
230
235 void set_component_source(const LogString *source) { component_source_ = source; }
240 const LogString *get_component_log_str() const;
241
242 bool should_warn_of_blocking(uint32_t blocking_time);
243
244 protected:
245 friend class Application;
246
247 virtual void call_loop();
248 virtual void call_setup();
249 virtual void call_dump_config();
250
252 void set_component_state_(uint8_t state);
253
276 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
277
292 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
293
294 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
295
301 bool cancel_interval(const std::string &name); // NOLINT
302 bool cancel_interval(const char *name); // NOLINT
303
334 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
335 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
336
337 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
338 float backoff_increase_factor = 1.0f); // NOLINT
339
345 bool cancel_retry(const std::string &name); // NOLINT
346
361 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
362
377 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
378
379 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
380
386 bool cancel_timeout(const std::string &name); // NOLINT
387 bool cancel_timeout(const char *name); // NOLINT
388
396 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
397
411 void defer(const char *name, std::function<void()> &&f); // NOLINT
412
414 void defer(std::function<void()> &&f); // NOLINT
415
417 bool cancel_defer(const std::string &name); // NOLINT
418
419 // Ordered for optimal packing on 32-bit systems
420 const LogString *component_source_{nullptr};
427 uint8_t component_state_{0x00};
428 volatile bool pending_enable_loop_{false};
429};
430
438 public:
440
445 explicit PollingComponent(uint32_t update_interval);
446
453 virtual void set_update_interval(uint32_t update_interval);
454
455 // ========== OVERRIDE METHODS ==========
456 // (You'll only need this when creating your own custom sensor)
457 virtual void update() = 0;
458
459 // ========== INTERNAL METHODS ==========
460 // (In most use cases you won't need these)
461 void call_setup() override;
462
464 virtual uint32_t get_update_interval() const;
465
466 // Start the poller, used for component.suspend
467 void start_poller();
468
469 // Stop the poller, used for component.suspend
470 void stop_poller();
471
472 protected:
474};
475
477 public:
478 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
479
480 // Finish the timing operation and return the current time
481 uint32_t finish();
482
484
485 protected:
486 uint32_t started_;
488};
489
490// Function to clear setup priority overrides after all components are set up
492
493} // 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: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
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:235
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:128
const LogString * component_source_
Definition component.h:420
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:98
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:428
virtual bool can_proceed()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
virtual void on_safe_shutdown()
Definition component.h:115
virtual float get_loop_priority() const
priority of loop().
Definition component.cpp:90
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:121
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
Definition component.h:421
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:427
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.
void disable_loop()
Disable this component's loop.
virtual void on_shutdown()
Definition component.h:114
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:96
void mark_failed(const char *message)
Definition component.h:160
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)
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 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:437
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:56
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:67
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:59
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:58
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:65
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:57
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:68
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:66
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:60
const float AFTER_BLUETOOTH
Definition component.cpp:62
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint8_t COMPONENT_STATE_SETUP
Definition component.cpp:75
const uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:74
const uint8_t STATUS_LED_MASK
Definition component.cpp:80
const uint8_t COMPONENT_STATE_FAILED
Definition component.cpp:77
const uint8_t COMPONENT_STATE_MASK
Definition component.cpp:73
void log_update_interval(const char *tag, PollingComponent *component)
const uint8_t COMPONENT_STATE_LOOP
Definition component.cpp:76
const uint16_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
Definition component.cpp:85
void clear_setup_priority_overrides()
const uint8_t STATUS_LED_OK
Definition component.cpp:81
const uint8_t STATUS_LED_WARNING
Definition component.cpp:82
const uint8_t COMPONENT_STATE_LOOP_DONE
Definition component.cpp:78
const uint8_t STATUS_LED_ERROR
Definition component.cpp:83
uint16_t length
Definition tt21100.cpp:0