ESPHome  2022.12.8
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  virtual bool can_proceed();
123 
124  bool status_has_warning();
125 
126  bool status_has_error();
127 
128  void status_set_warning();
129 
130  void status_set_error();
131 
132  void status_clear_warning();
133 
134  void status_clear_error();
135 
136  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
137 
138  void status_momentary_error(const std::string &name, uint32_t length = 5000);
139 
140  bool has_overridden_loop() const;
141 
146  void set_component_source(const char *source) { component_source_ = source; }
151  const char *get_component_source() const;
152 
153  protected:
154  friend class Application;
155 
156  virtual void call_loop();
157  virtual void call_setup();
158  virtual void call_dump_config();
159 
175  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
176 
177  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
178 
184  bool cancel_interval(const std::string &name); // NOLINT
185 
203  void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
204  std::function<RetryResult()> &&f, float backoff_increase_factor = 1.0f); // NOLINT
205 
206  void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult()> &&f, // NOLINT
207  float backoff_increase_factor = 1.0f); // NOLINT
208 
214  bool cancel_retry(const std::string &name); // NOLINT
215 
230  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
231 
232  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
233 
239  bool cancel_timeout(const std::string &name); // NOLINT
240 
248  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
249 
251  void defer(std::function<void()> &&f); // NOLINT
252 
254  bool cancel_defer(const std::string &name); // NOLINT
255 
256  uint32_t component_state_{0x0000};
257  float setup_priority_override_{NAN};
258  const char *component_source_{nullptr};
259 };
260 
267 class PollingComponent : public Component {
268  public:
270 
275  explicit PollingComponent(uint32_t update_interval);
276 
283  virtual void set_update_interval(uint32_t update_interval);
284 
285  // ========== OVERRIDE METHODS ==========
286  // (You'll only need this when creating your own custom sensor)
287  virtual void update() = 0;
288 
289  // ========== INTERNAL METHODS ==========
290  // (In most use cases you won't need these)
291  void call_setup() override;
292 
294  virtual uint32_t get_update_interval() const;
295 
296  protected:
298 };
299 
301  public:
304 
305  protected:
306  uint32_t started_;
308 };
309 
310 } // 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:267
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:146
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
Definition: a4988.cpp:4
const uint32_t STATUS_LED_MASK
Definition: component.cpp:36