ESPHome  1.15.2
component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <functional>
5 #include "Arduino.h"
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 WIFI;
31 extern const float AFTER_WIFI;
33 extern const float AFTER_CONNECTION;
35 extern const float LATE;
36 
37 } // namespace setup_priority
38 
39 #define LOG_UPDATE_INTERVAL(this) \
40  if (this->get_update_interval() < 100) { \
41  ESP_LOGCONFIG(TAG, " Update Interval: %.3fs", this->get_update_interval() / 1000.0f); \
42  } else { \
43  ESP_LOGCONFIG(TAG, " Update Interval: %.1fs", this->get_update_interval() / 1000.0f); \
44  }
45 
46 extern const uint32_t COMPONENT_STATE_MASK;
47 extern const uint32_t COMPONENT_STATE_CONSTRUCTION;
48 extern const uint32_t COMPONENT_STATE_SETUP;
49 extern const uint32_t COMPONENT_STATE_LOOP;
50 extern const uint32_t COMPONENT_STATE_FAILED;
51 extern const uint32_t STATUS_LED_MASK;
52 extern const uint32_t STATUS_LED_OK;
53 extern const uint32_t STATUS_LED_WARNING;
54 extern const uint32_t STATUS_LED_ERROR;
55 
56 class Component {
57  public:
63  virtual void setup();
64 
70  virtual void loop();
71 
72  virtual void dump_config();
73 
80  virtual float get_setup_priority() const;
81 
82  float get_actual_setup_priority() const;
83 
84  void set_setup_priority(float priority);
85 
92  virtual float get_loop_priority() const;
93 
94  void call();
95 
96  virtual void on_shutdown() {}
97  virtual void on_safe_shutdown() {}
98 
99  uint32_t get_component_state() const;
100 
107  virtual void mark_failed();
108 
109  bool is_failed();
110 
111  virtual bool can_proceed();
112 
113  bool status_has_warning();
114 
115  bool status_has_error();
116 
117  void status_set_warning();
118 
119  void status_set_error();
120 
121  void status_clear_warning();
122 
123  void status_clear_error();
124 
125  void status_momentary_warning(const std::string &name, uint32_t length = 5000);
126 
127  void status_momentary_error(const std::string &name, uint32_t length = 5000);
128 
129  bool has_overridden_loop() const;
130 
131  protected:
132  virtual void call_loop();
133  virtual void call_setup();
149  void set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f); // NOLINT
150 
151  void set_interval(uint32_t interval, std::function<void()> &&f); // NOLINT
152 
158  bool cancel_interval(const std::string &name); // NOLINT
159 
160  void set_timeout(uint32_t timeout, std::function<void()> &&f); // NOLINT
161 
176  void set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f); // NOLINT
177 
183  bool cancel_timeout(const std::string &name); // NOLINT
184 
192  void defer(const std::string &name, std::function<void()> &&f); // NOLINT
193 
195  void defer(std::function<void()> &&f); // NOLINT
196 
198  bool cancel_defer(const std::string &name); // NOLINT
199 
200  uint32_t component_state_{0x0000};
201  float setup_priority_override_{NAN};
202 };
203 
210 class PollingComponent : public Component {
211  public:
213 
218  explicit PollingComponent(uint32_t update_interval);
219 
226  virtual void set_update_interval(uint32_t update_interval);
227 
228  // ========== OVERRIDE METHODS ==========
229  // (You'll only need this when creating your own custom sensor)
230  virtual void update() = 0;
231 
232  // ========== INTERNAL METHODS ==========
233  // (In most use cases you won't need these)
234  void call_setup() override;
235 
237  virtual uint32_t get_update_interval() const;
238 
239  protected:
241 };
242 
244 class Nameable {
245  public:
246  Nameable() : Nameable("") {}
247  explicit Nameable(const std::string &name);
248  const std::string &get_name() const;
249  void set_name(const std::string &name);
251  const std::string &get_object_id();
252  uint32_t get_object_id_hash();
253 
254  bool is_internal() const;
255  void set_internal(bool internal);
256 
257  protected:
258  virtual uint32_t hash_base() = 0;
259 
260  void calc_object_id_();
261 
262  std::string name_;
263  std::string object_id_;
264  uint32_t object_id_hash_;
265  bool internal_{false};
266 };
267 
268 } // namespace esphome
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:28
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:29
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:16
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:20
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:19
const uint32_t STATUS_LED_OK
Definition: component.cpp:31
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:21
This class simplifies creating components that periodically check a state.
Definition: component.h:210
uint32_t object_id_hash_
Definition: component.h:264
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:13
virtual void on_shutdown()
Definition: component.h:96
const float HARDWARE_LATE
Alias for DATA (here for compatability reasons)
const uint32_t COMPONENT_STATE_SETUP
Definition: component.cpp:27
virtual void on_safe_shutdown()
Definition: component.h:97
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:26
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:17
std::string name_
Definition: component.h:262
std::string object_id_
Definition: component.h:263
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:25
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:32
Helper class that enables naming of objects so that it doesn&#39;t have to be re-implement every time...
Definition: component.h:244
uint8_t priority
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:15
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:14
const uint32_t STATUS_LED_ERROR
Definition: component.cpp:33
Definition: a4988.cpp:4
const uint32_t STATUS_LED_MASK
Definition: component.cpp:30