ESPHome  2023.11.6
component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <functional>
5 #include <cmath>
6 
8 
9 namespace esphome {
10 
15 namespace setup_priority {
16 
18 extern const float BUS;
20 extern const float IO;
22 extern const float HARDWARE;
24 extern const float DATA;
26 extern const float HARDWARE_LATE;
28 extern const float PROCESSOR;
29 extern const float BLUETOOTH;
30 extern const float AFTER_BLUETOOTH;
31 extern const float WIFI;
32 extern const float ETHERNET;
34 extern const float BEFORE_CONNECTION;
36 extern const float AFTER_WIFI;
38 extern const float AFTER_CONNECTION;
40 extern const float LATE;
41 
42 } // namespace setup_priority
43 
44 static const uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
45 
46 #define LOG_UPDATE_INTERVAL(this) \
47  if (this->get_update_interval() == SCHEDULER_DONT_RUN) { \
48  ESP_LOGCONFIG(TAG, " Update Interval: never"); \
49  } else if (this->get_update_interval() < 100) { \
50  ESP_LOGCONFIG(TAG, " Update Interval: %.3fs", this->get_update_interval() / 1000.0f); \
51  } else { \
52  ESP_LOGCONFIG(TAG, " Update Interval: %.1fs", this->get_update_interval() / 1000.0f); \
53  }
54 
55 extern const uint32_t COMPONENT_STATE_MASK;
56 extern const uint32_t COMPONENT_STATE_CONSTRUCTION;
57 extern const uint32_t COMPONENT_STATE_SETUP;
58 extern const uint32_t COMPONENT_STATE_LOOP;
59 extern const uint32_t COMPONENT_STATE_FAILED;
60 extern const uint32_t STATUS_LED_MASK;
61 extern const uint32_t STATUS_LED_OK;
62 extern const uint32_t STATUS_LED_WARNING;
63 extern const uint32_t STATUS_LED_ERROR;
64 
65 enum class RetryResult { DONE, RETRY };
66 
67 class Component {
68  public:
74  virtual void setup();
75 
81  virtual void loop();
82 
83  virtual void dump_config();
84 
91  virtual float get_setup_priority() const;
92 
93  float get_actual_setup_priority() const;
94 
95  void set_setup_priority(float priority);
96 
103  virtual float get_loop_priority() const;
104 
105  void call();
106 
107  virtual void on_shutdown() {}
108  virtual void on_safe_shutdown() {}
109 
110  uint32_t get_component_state() const;
111 
118  virtual void mark_failed();
119 
120  bool is_failed();
121 
122  bool is_ready();
123 
124  virtual bool can_proceed();
125 
126  bool status_has_warning();
127 
128  bool status_has_error();
129 
130  void status_set_warning();
131 
132  void status_set_error();
133 
134  void status_clear_warning();
135 
136  void status_clear_error();
137 
138  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
139 
140  void status_momentary_error(const std::string &name, uint32_t length = 5000);
141 
142  bool has_overridden_loop() const;
143 
148  void set_component_source(const char *source) { component_source_ = source; }
153  const char *get_component_source() const;
154 
155  protected:
156  friend class Application;
157 
158  virtual void call_loop();
159  virtual void call_setup();
160  virtual void call_dump_config();
161 
177  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
178 
179  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
180 
186  bool cancel_interval(const std::string &name); // NOLINT
187 
218  void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
219  std::function<RetryResult(uint8_t)> &&f, float backoff_increase_factor = 1.0f); // NOLINT
220 
221  void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult(uint8_t)> &&f, // NOLINT
222  float backoff_increase_factor = 1.0f); // NOLINT
223 
229  bool cancel_retry(const std::string &name); // NOLINT
230 
245  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
246 
247  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
248 
254  bool cancel_timeout(const std::string &name); // NOLINT
255 
263  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
264 
266  void defer(std::function<void()> &&f); // NOLINT
267 
269  bool cancel_defer(const std::string &name); // NOLINT
270 
271  uint32_t component_state_{0x0000};
272  float setup_priority_override_{NAN};
273  const char *component_source_{nullptr};
274 };
275 
282 class PollingComponent : public Component {
283  public:
285 
290  explicit PollingComponent(uint32_t update_interval);
291 
298  virtual void set_update_interval(uint32_t update_interval);
299 
300  // ========== OVERRIDE METHODS ==========
301  // (You'll only need this when creating your own custom sensor)
302  virtual void update() = 0;
303 
304  // ========== INTERNAL METHODS ==========
305  // (In most use cases you won't need these)
306  void call_setup() override;
307 
309  virtual uint32_t get_update_interval() const;
310 
311  // Start the poller, used for component.suspend
312  void start_poller();
313 
314  // Stop the poller, used for component.suspend
315  void stop_poller();
316 
317  protected:
319 };
320 
322  public:
325 
326  protected:
327  uint32_t started_;
329 };
330 
331 } // namespace esphome
void setup()
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:34
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:35
const char * name
Definition: stm32flash.h:78
void loop()
RetryResult
Definition: component.h:65
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
Definition: component.cpp:24
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:26
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:25
const uint32_t STATUS_LED_OK
Definition: component.cpp:37
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:27
This class simplifies creating components that periodically check a state.
Definition: component.h:282
const float AFTER_BLUETOOTH
Definition: component.cpp:21
void set_component_source(const char *source)
Set where this component was loaded from for some debug messages.
Definition: component.h:148
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:15
virtual void on_shutdown()
Definition: component.h:107
const float HARDWARE_LATE
Alias for DATA (here for compatibility reasons)
const uint32_t COMPONENT_STATE_SETUP
Definition: component.cpp:33
virtual void on_safe_shutdown()
Definition: component.h:108
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:32
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:19
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:31
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:38
uint8_t priority
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:16
const uint32_t STATUS_LED_ERROR
Definition: component.cpp:39
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:36