ESPHome 2025.10.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 virtual void mark_failed();
151
152 void mark_failed(const char *message) {
153 this->status_set_error(message);
154 this->mark_failed();
155 }
156
165 void disable_loop();
166
175 void enable_loop();
176
197
198 bool is_failed() const;
199
200 bool is_ready() const;
201
202 virtual bool can_proceed();
203
204 bool status_has_warning() const;
205
206 bool status_has_error() const;
207
208 void status_set_warning(const char *message = nullptr);
209 void status_set_warning(const LogString *message);
210
211 void status_set_error(const char *message = nullptr);
212
214
215 void status_clear_error();
216
217 void status_momentary_warning(const std::string &name, uint32_t length = 5000);
218
219 void status_momentary_error(const std::string &name, uint32_t length = 5000);
220
221 bool has_overridden_loop() const;
222
227 void set_component_source(const LogString *source) { component_source_ = source; }
232 const LogString *get_component_log_str() const;
233
234 bool should_warn_of_blocking(uint32_t blocking_time);
235
236 protected:
237 friend class Application;
238
239 virtual void call_loop();
240 virtual void call_setup();
241 virtual void call_dump_config();
242
244 void set_component_state_(uint8_t state);
245
268 void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
269
284 void set_interval(const char *name, uint32_t interval, std::function<void()> &&f); // NOLINT
285
286 void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
287
293 bool cancel_interval(const std::string &name); // NOLINT
294 bool cancel_interval(const char *name); // NOLINT
295
326 void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
327 std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
328
329 void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
330 float backoff_increase_factor = 1.0f); // NOLINT
331
337 bool cancel_retry(const std::string &name); // NOLINT
338
353 void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
354
369 void set_timeout(const char *name, uint32_t timeout, std::function<void()> &&f); // NOLINT
370
371 void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
372
378 bool cancel_timeout(const std::string &name); // NOLINT
379 bool cancel_timeout(const char *name); // NOLINT
380
388 void defer(const std::string &name, std::function<void()> &&f); // NOLINT
389
403 void defer(const char *name, std::function<void()> &&f); // NOLINT
404
406 void defer(std::function<void()> &&f); // NOLINT
407
409 bool cancel_defer(const std::string &name); // NOLINT
410
411 // Ordered for optimal packing on 32-bit systems
412 const LogString *component_source_{nullptr};
419 uint8_t component_state_{0x00};
420 volatile bool pending_enable_loop_{false};
421};
422
430 public:
432
437 explicit PollingComponent(uint32_t update_interval);
438
445 virtual void set_update_interval(uint32_t update_interval);
446
447 // ========== OVERRIDE METHODS ==========
448 // (You'll only need this when creating your own custom sensor)
449 virtual void update() = 0;
450
451 // ========== INTERNAL METHODS ==========
452 // (In most use cases you won't need these)
453 void call_setup() override;
454
456 virtual uint32_t get_update_interval() const;
457
458 // Start the poller, used for component.suspend
459 void start_poller();
460
461 // Stop the poller, used for component.suspend
462 void stop_poller();
463
464 protected:
466};
467
469 public:
470 WarnIfComponentBlockingGuard(Component *component, uint32_t start_time);
471
472 // Finish the timing operation and return the current time
473 uint32_t finish();
474
476
477 protected:
478 uint32_t started_;
480};
481
482// Function to clear setup priority overrides after all components are set up
484
485} // 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:82
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
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:227
virtual void on_powerdown()
Called after teardown is complete to power down hardware.
Definition component.h:128
const LogString * component_source_
Definition component.h:412
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:88
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:420
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:80
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:413
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:419
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:96
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:86
void mark_failed(const char *message)
Definition component.h:152
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.
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:429
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:46
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:57
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:49
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:48
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:55
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:47
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:58
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:56
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:50
const float AFTER_BLUETOOTH
Definition component.cpp:52
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint8_t COMPONENT_STATE_SETUP
Definition component.cpp:65
const uint8_t COMPONENT_STATE_CONSTRUCTION
Definition component.cpp:64
const uint8_t STATUS_LED_MASK
Definition component.cpp:70
const uint8_t COMPONENT_STATE_FAILED
Definition component.cpp:67
const uint8_t COMPONENT_STATE_MASK
Definition component.cpp:63
void log_update_interval(const char *tag, PollingComponent *component)
const uint8_t COMPONENT_STATE_LOOP
Definition component.cpp:66
const uint16_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
Definition component.cpp:75
void clear_setup_priority_overrides()
const uint8_t STATUS_LED_OK
Definition component.cpp:71
const uint8_t STATUS_LED_WARNING
Definition component.cpp:72
const uint8_t COMPONENT_STATE_LOOP_DONE
Definition component.cpp:68
const uint8_t STATUS_LED_ERROR
Definition component.cpp:73
uint16_t length
Definition tt21100.cpp:0