ESPHome  2022.4.0
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;
33 extern const float BEFORE_CONNECTION;
35 extern const float AFTER_WIFI;
37 extern const float AFTER_CONNECTION;
39 extern const float LATE;
40 
41 } // namespace setup_priority
42 
43 static const uint32_t SCHEDULER_DONT_RUN = 4294967295UL;
44 
45 #define LOG_UPDATE_INTERVAL(this) \
46  if (this->get_update_interval() == SCHEDULER_DONT_RUN) { \
47  ESP_LOGCONFIG(TAG, " Update Interval: never"); \
48  } else if (this->get_update_interval() < 100) { \
49  ESP_LOGCONFIG(TAG, " Update Interval: %.3fs", this->get_update_interval() / 1000.0f); \
50  } else { \
51  ESP_LOGCONFIG(TAG, " Update Interval: %.1fs", this->get_update_interval() / 1000.0f); \
52  }
53 
54 extern const uint32_t COMPONENT_STATE_MASK;
55 extern const uint32_t COMPONENT_STATE_CONSTRUCTION;
56 extern const uint32_t COMPONENT_STATE_SETUP;
57 extern const uint32_t COMPONENT_STATE_LOOP;
58 extern const uint32_t COMPONENT_STATE_FAILED;
59 extern const uint32_t STATUS_LED_MASK;
60 extern const uint32_t STATUS_LED_OK;
61 extern const uint32_t STATUS_LED_WARNING;
62 extern const uint32_t STATUS_LED_ERROR;
63 
65 
66 class Component {
67  public:
73  virtual void setup();
74 
80  virtual void loop();
81 
82  virtual void dump_config();
83 
90  virtual float get_setup_priority() const;
91 
92  float get_actual_setup_priority() const;
93 
94  void set_setup_priority(float priority);
95 
102  virtual float get_loop_priority() const;
103 
104  void call();
105 
106  virtual void on_shutdown() {}
107  virtual void on_safe_shutdown() {}
108 
109  uint32_t get_component_state() const;
110 
117  virtual void mark_failed();
118 
119  bool is_failed();
120 
121  virtual bool can_proceed();
122 
123  bool status_has_warning();
124 
125  bool status_has_error();
126 
127  void status_set_warning();
128 
129  void status_set_error();
130 
131  void status_clear_warning();
132 
133  void status_clear_error();
134 
135  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
136 
137  void status_momentary_error(const std::string &name, uint32_t length = 5000);
138 
139  bool has_overridden_loop() const;
140 
145  void set_component_source(const char *source) { component_source_ = source; }
150  const char *get_component_source() const;
151 
152  protected:
153  friend class Application;
154 
155  virtual void call_loop();
156  virtual void call_setup();
157  virtual void call_dump_config();
158 
174  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
175 
176  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
177 
183  bool cancel_interval(const std::string &name); // NOLINT
184 
202  void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, // NOLINT
203  std::function<RetryResult()> &&f, float backoff_increase_factor = 1.0f); // NOLINT
204 
205  void set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult()> &&f, // NOLINT
206  float backoff_increase_factor = 1.0f); // NOLINT
207 
213  bool cancel_retry(const std::string &name); // NOLINT
214 
229  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
230 
231  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
232 
238  bool cancel_timeout(const std::string &name); // NOLINT
239 
247  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
248 
250  void defer(std::function<void()> &&f); // NOLINT
251 
253  bool cancel_defer(const std::string &name); // NOLINT
254 
255  uint32_t component_state_{0x0000};
256  float setup_priority_override_{NAN};
257  const char *component_source_ = nullptr;
258 };
259 
266 class PollingComponent : public Component {
267  public:
269 
274  explicit PollingComponent(uint32_t update_interval);
275 
282  virtual void set_update_interval(uint32_t update_interval);
283 
284  // ========== OVERRIDE METHODS ==========
285  // (You'll only need this when creating your own custom sensor)
286  virtual void update() = 0;
287 
288  // ========== INTERNAL METHODS ==========
289  // (In most use cases you won't need these)
290  void call_setup() override;
291 
293  virtual uint32_t get_update_interval() const;
294 
295  protected:
297 };
298 
300  public:
303 
304  protected:
305  uint32_t started_;
307 };
308 
309 } // namespace esphome
void setup()
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:33
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:34
const char * name
Definition: stm32flash.h:77
void loop()
RetryResult
Definition: component.h:64
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:23
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:25
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:24
const uint32_t STATUS_LED_OK
Definition: component.cpp:36
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:26
This class simplifies creating components that periodically check a state.
Definition: component.h:266
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:145
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:15
virtual void on_shutdown()
Definition: component.h:106
const float HARDWARE_LATE
Alias for DATA (here for compatibility reasons)
const uint32_t COMPONENT_STATE_SETUP
Definition: component.cpp:32
virtual void on_safe_shutdown()
Definition: component.h:107
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:31
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:30
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:37
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:38
Definition: a4988.cpp:4
const uint32_t STATUS_LED_MASK
Definition: component.cpp:35