ESPHome  2021.11.3
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 
64 class Component {
65  public:
71  virtual void setup();
72 
78  virtual void loop();
79 
80  virtual void dump_config();
81 
88  virtual float get_setup_priority() const;
89 
90  float get_actual_setup_priority() const;
91 
92  void set_setup_priority(float priority);
93 
100  virtual float get_loop_priority() const;
101 
102  void call();
103 
104  virtual void on_shutdown() {}
105  virtual void on_safe_shutdown() {}
106 
107  uint32_t get_component_state() const;
108 
115  virtual void mark_failed();
116 
117  bool is_failed();
118 
119  virtual bool can_proceed();
120 
121  bool status_has_warning();
122 
123  bool status_has_error();
124 
125  void status_set_warning();
126 
127  void status_set_error();
128 
129  void status_clear_warning();
130 
131  void status_clear_error();
132 
133  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
134 
135  void status_momentary_error(const std::string &name, uint32_t length = 5000);
136 
137  bool has_overridden_loop() const;
138 
143  void set_component_source(const char *source) { component_source_ = source; }
148  const char *get_component_source() const;
149 
150  protected:
151  friend class Application;
152 
153  virtual void call_loop();
154  virtual void call_setup();
155  virtual void call_dump_config();
156 
172  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
173 
174  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
175 
181  bool cancel_interval(const std::string &name); // NOLINT
182 
183  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
184 
199  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
200 
206  bool cancel_timeout(const std::string &name); // NOLINT
207 
215  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
216 
218  void defer(std::function<void()> &&f); // NOLINT
219 
221  bool cancel_defer(const std::string &name); // NOLINT
222 
223  uint32_t component_state_{0x0000};
224  float setup_priority_override_{NAN};
225  const char *component_source_ = nullptr;
226 };
227 
234 class PollingComponent : public Component {
235  public:
237 
242  explicit PollingComponent(uint32_t update_interval);
243 
250  virtual void set_update_interval(uint32_t update_interval);
251 
252  // ========== OVERRIDE METHODS ==========
253  // (You'll only need this when creating your own custom sensor)
254  virtual void update() = 0;
255 
256  // ========== INTERNAL METHODS ==========
257  // (In most use cases you won't need these)
258  void call_setup() override;
259 
261  virtual uint32_t get_update_interval() const;
262 
263  protected:
265 };
266 
268  public:
271 
272  protected:
273  uint32_t started_;
275 };
276 
277 } // namespace esphome
void setup()
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:33
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:34
void loop()
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:234
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:143
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:15
virtual void on_shutdown()
Definition: component.h:104
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:105
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
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
const uint32_t STATUS_LED_MASK
Definition: component.cpp:35