ESPHome  2024.12.0
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 
10 namespace esphome {
11 
16 namespace setup_priority {
17 
19 extern const float BUS;
21 extern const float IO;
23 extern const float HARDWARE;
25 extern const float DATA;
27 extern const float HARDWARE_LATE;
29 extern const float PROCESSOR;
30 extern const float BLUETOOTH;
31 extern const float AFTER_BLUETOOTH;
32 extern const float WIFI;
33 extern const float ETHERNET;
35 extern const float BEFORE_CONNECTION;
37 extern const float AFTER_WIFI;
39 extern const float AFTER_CONNECTION;
41 extern const float LATE;
42 
43 } // namespace setup_priority
44 
45 static 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 
56 extern const uint32_t COMPONENT_STATE_MASK;
57 extern const uint32_t COMPONENT_STATE_CONSTRUCTION;
58 extern const uint32_t COMPONENT_STATE_SETUP;
59 extern const uint32_t COMPONENT_STATE_LOOP;
60 extern const uint32_t COMPONENT_STATE_FAILED;
61 extern const uint32_t STATUS_LED_MASK;
62 extern const uint32_t STATUS_LED_OK;
63 extern const uint32_t STATUS_LED_WARNING;
64 extern const uint32_t STATUS_LED_ERROR;
65 
66 enum class RetryResult { DONE, RETRY };
67 
68 class Component {
69  public:
75  virtual void setup();
76 
82  virtual void loop();
83 
84  virtual void dump_config();
85 
92  virtual float get_setup_priority() const;
93 
94  float get_actual_setup_priority() const;
95 
96  void set_setup_priority(float priority);
97 
104  virtual float get_loop_priority() const;
105 
106  void call();
107 
108  virtual void on_shutdown() {}
109  virtual void on_safe_shutdown() {}
110 
111  uint32_t get_component_state() const;
112 
119  virtual void mark_failed();
120 
121  bool is_failed() const;
122 
123  bool is_ready() const;
124 
125  virtual bool can_proceed();
126 
127  bool status_has_warning() const;
128 
129  bool status_has_error() const;
130 
131  void status_set_warning(const char *message = "unspecified");
132 
133  void status_set_error(const char *message = "unspecified");
134 
135  void status_clear_warning();
136 
137  void status_clear_error();
138 
139  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
140 
141  void status_momentary_error(const std::string &name, uint32_t length = 5000);
142 
143  bool has_overridden_loop() const;
144 
149  void set_component_source(const char *source) { component_source_ = source; }
154  const char *get_component_source() const;
155 
156  protected:
157  friend class Application;
158 
159  virtual void call_loop();
160  virtual void call_setup();
161  virtual void call_dump_config();
162 
178  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
179 
180  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
181 
187  bool cancel_interval(const std::string &name); // NOLINT
188 
219  void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
220  std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
221 
222  void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
223  float backoff_increase_factor = 1.0f); // NOLINT
224 
230  bool cancel_retry(const std::string &name); // NOLINT
231 
246  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
247 
248  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
249 
255  bool cancel_timeout(const std::string &name); // NOLINT
256 
264  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
265 
267  void defer(std::function<void()> &&f); // NOLINT
268 
270  bool cancel_defer(const std::string &name); // NOLINT
271 
272  uint32_t component_state_{0x0000};
273  float setup_priority_override_{NAN};
274  const char *component_source_{nullptr};
275 };
276 
283 class PollingComponent : public Component {
284  public:
286 
291  explicit PollingComponent(uint32_t update_interval);
292 
299  virtual void set_update_interval(uint32_t update_interval);
300 
301  // ========== OVERRIDE METHODS ==========
302  // (You'll only need this when creating your own custom sensor)
303  virtual void update() = 0;
304 
305  // ========== INTERNAL METHODS ==========
306  // (In most use cases you won't need these)
307  void call_setup() override;
308 
310  virtual uint32_t get_update_interval() const;
311 
312  // Start the poller, used for component.suspend
313  void start_poller();
314 
315  // Stop the poller, used for component.suspend
316  void stop_poller();
317 
318  protected:
320 };
321 
323  public:
326 
327  protected:
328  uint32_t started_;
330 };
331 
332 } // namespace esphome
void setup()
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:35
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:36
const char * name
Definition: stm32flash.h:78
void loop()
RetryResult
Definition: component.h:66
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
Definition: component.cpp:25
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:27
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
const uint32_t STATUS_LED_OK
Definition: component.cpp:38
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
This class simplifies creating components that periodically check a state.
Definition: component.h:283
const float AFTER_BLUETOOTH
Definition: component.cpp:22
void set_component_source(const char *source)
Set where this component was loaded from for some debug messages.
Definition: component.h:149
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:16
virtual void on_shutdown()
Definition: component.h:108
const float HARDWARE_LATE
Alias for DATA (here for compatibility reasons)
const uint32_t COMPONENT_STATE_SETUP
Definition: component.cpp:34
virtual void on_safe_shutdown()
Definition: component.h:109
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:33
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:32
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:39
uint8_t priority
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:17
const uint32_t STATUS_LED_ERROR
Definition: component.cpp:40
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint32_t STATUS_LED_MASK
Definition: component.cpp:37