ESPHome  2022.6.2
application.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include "esphome/core/defines.h"
8 #include "esphome/core/hal.h"
9 #include "esphome/core/helpers.h"
10 #include "esphome/core/scheduler.h"
11 
12 #ifdef USE_BINARY_SENSOR
14 #endif
15 #ifdef USE_SENSOR
17 #endif
18 #ifdef USE_SWITCH
20 #endif
21 #ifdef USE_BUTTON
23 #endif
24 #ifdef USE_TEXT_SENSOR
26 #endif
27 #ifdef USE_FAN
29 #endif
30 #ifdef USE_CLIMATE
32 #endif
33 #ifdef USE_LIGHT
35 #endif
36 #ifdef USE_COVER
38 #endif
39 #ifdef USE_NUMBER
41 #endif
42 #ifdef USE_SELECT
44 #endif
45 #ifdef USE_LOCK
47 #endif
48 #ifdef USE_MEDIA_PLAYER
50 #endif
51 
52 namespace esphome {
53 
54 class Application {
55  public:
56  void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix) {
57  arch_init();
58  this->name_add_mac_suffix_ = name_add_mac_suffix;
59  if (name_add_mac_suffix) {
60  this->name_ = name + "-" + get_mac_address().substr(6);
61  } else {
62  this->name_ = name;
63  }
64  this->compilation_time_ = compilation_time;
65  }
66 
67 #ifdef USE_BINARY_SENSOR
69  this->binary_sensors_.push_back(binary_sensor);
70  }
71 #endif
72 
73 #ifdef USE_SENSOR
74  void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
75 #endif
76 
77 #ifdef USE_SWITCH
78  void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
79 #endif
80 
81 #ifdef USE_BUTTON
82  void register_button(button::Button *button) { this->buttons_.push_back(button); }
83 #endif
84 
85 #ifdef USE_TEXT_SENSOR
86  void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
87 #endif
88 
89 #ifdef USE_FAN
90  void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
91 #endif
92 
93 #ifdef USE_COVER
94  void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
95 #endif
96 
97 #ifdef USE_CLIMATE
98  void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
99 #endif
100 
101 #ifdef USE_LIGHT
102  void register_light(light::LightState *light) { this->lights_.push_back(light); }
103 #endif
104 
105 #ifdef USE_NUMBER
106  void register_number(number::Number *number) { this->numbers_.push_back(number); }
107 #endif
108 
109 #ifdef USE_SELECT
110  void register_select(select::Select *select) { this->selects_.push_back(select); }
111 #endif
112 
113 #ifdef USE_LOCK
114  void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
115 #endif
116 
117 #ifdef USE_MEDIA_PLAYER
118  void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
119 #endif
120 
122  template<class C> C *register_component(C *c) {
123  static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
124  this->register_component_((Component *) c);
125  return c;
126  }
127 
129  void setup();
130 
132  void loop();
133 
135  const std::string &get_name() const { return this->name_; }
136 
138 
139  const std::string &get_compilation_time() const { return this->compilation_time_; }
140 
154  void set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; }
155 
156  void schedule_dump_config() { this->dump_config_at_ = 0; }
157 
158  void feed_wdt();
159 
160  void reboot();
161 
162  void safe_reboot();
163 
165  for (auto *comp : this->components_) {
166  comp->on_safe_shutdown();
167  }
168  for (auto *comp : this->components_) {
169  comp->on_shutdown();
170  }
171  }
172 
173  uint32_t get_app_state() const { return this->app_state_; }
174 
175 #ifdef USE_BINARY_SENSOR
176  const std::vector<binary_sensor::BinarySensor *> &get_binary_sensors() { return this->binary_sensors_; }
177  binary_sensor::BinarySensor *get_binary_sensor_by_key(uint32_t key, bool include_internal = false) {
178  for (auto *obj : this->binary_sensors_)
179  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
180  return obj;
181  return nullptr;
182  }
183 #endif
184 #ifdef USE_SWITCH
185  const std::vector<switch_::Switch *> &get_switches() { return this->switches_; }
186  switch_::Switch *get_switch_by_key(uint32_t key, bool include_internal = false) {
187  for (auto *obj : this->switches_)
188  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
189  return obj;
190  return nullptr;
191  }
192 #endif
193 #ifdef USE_BUTTON
194  const std::vector<button::Button *> &get_buttons() { return this->buttons_; }
195  button::Button *get_button_by_key(uint32_t key, bool include_internal = false) {
196  for (auto *obj : this->buttons_)
197  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
198  return obj;
199  return nullptr;
200  }
201 #endif
202 #ifdef USE_SENSOR
203  const std::vector<sensor::Sensor *> &get_sensors() { return this->sensors_; }
204  sensor::Sensor *get_sensor_by_key(uint32_t key, bool include_internal = false) {
205  for (auto *obj : this->sensors_)
206  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
207  return obj;
208  return nullptr;
209  }
210 #endif
211 #ifdef USE_TEXT_SENSOR
212  const std::vector<text_sensor::TextSensor *> &get_text_sensors() { return this->text_sensors_; }
213  text_sensor::TextSensor *get_text_sensor_by_key(uint32_t key, bool include_internal = false) {
214  for (auto *obj : this->text_sensors_)
215  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
216  return obj;
217  return nullptr;
218  }
219 #endif
220 #ifdef USE_FAN
221  const std::vector<fan::Fan *> &get_fans() { return this->fans_; }
222  fan::Fan *get_fan_by_key(uint32_t key, bool include_internal = false) {
223  for (auto *obj : this->fans_)
224  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
225  return obj;
226  return nullptr;
227  }
228 #endif
229 #ifdef USE_COVER
230  const std::vector<cover::Cover *> &get_covers() { return this->covers_; }
231  cover::Cover *get_cover_by_key(uint32_t key, bool include_internal = false) {
232  for (auto *obj : this->covers_)
233  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
234  return obj;
235  return nullptr;
236  }
237 #endif
238 #ifdef USE_LIGHT
239  const std::vector<light::LightState *> &get_lights() { return this->lights_; }
240  light::LightState *get_light_by_key(uint32_t key, bool include_internal = false) {
241  for (auto *obj : this->lights_)
242  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
243  return obj;
244  return nullptr;
245  }
246 #endif
247 #ifdef USE_CLIMATE
248  const std::vector<climate::Climate *> &get_climates() { return this->climates_; }
249  climate::Climate *get_climate_by_key(uint32_t key, bool include_internal = false) {
250  for (auto *obj : this->climates_)
251  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
252  return obj;
253  return nullptr;
254  }
255 #endif
256 #ifdef USE_NUMBER
257  const std::vector<number::Number *> &get_numbers() { return this->numbers_; }
258  number::Number *get_number_by_key(uint32_t key, bool include_internal = false) {
259  for (auto *obj : this->numbers_)
260  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
261  return obj;
262  return nullptr;
263  }
264 #endif
265 #ifdef USE_SELECT
266  const std::vector<select::Select *> &get_selects() { return this->selects_; }
267  select::Select *get_select_by_key(uint32_t key, bool include_internal = false) {
268  for (auto *obj : this->selects_)
269  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
270  return obj;
271  return nullptr;
272  }
273 #endif
274 #ifdef USE_LOCK
275  const std::vector<lock::Lock *> &get_locks() { return this->locks_; }
276  lock::Lock *get_lock_by_key(uint32_t key, bool include_internal = false) {
277  for (auto *obj : this->locks_)
278  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
279  return obj;
280  return nullptr;
281  }
282 #endif
283 #ifdef USE_MEDIA_PLAYER
284  const std::vector<media_player::MediaPlayer *> &get_media_players() { return this->media_players_; }
285  media_player::MediaPlayer *get_media_player_by_key(uint32_t key, bool include_internal = false) {
286  for (auto *obj : this->media_players_)
287  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
288  return obj;
289  return nullptr;
290  }
291 #endif
292 
294 
295  protected:
296  friend Component;
297 
298  void register_component_(Component *comp);
299 
301 
302  void feed_wdt_arch_();
303 
304  std::vector<Component *> components_{};
305  std::vector<Component *> looping_components_{};
306 
307 #ifdef USE_BINARY_SENSOR
308  std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
309 #endif
310 #ifdef USE_SWITCH
311  std::vector<switch_::Switch *> switches_{};
312 #endif
313 #ifdef USE_BUTTON
314  std::vector<button::Button *> buttons_{};
315 #endif
316 #ifdef USE_SENSOR
317  std::vector<sensor::Sensor *> sensors_{};
318 #endif
319 #ifdef USE_TEXT_SENSOR
320  std::vector<text_sensor::TextSensor *> text_sensors_{};
321 #endif
322 #ifdef USE_FAN
323  std::vector<fan::Fan *> fans_{};
324 #endif
325 #ifdef USE_COVER
326  std::vector<cover::Cover *> covers_{};
327 #endif
328 #ifdef USE_CLIMATE
329  std::vector<climate::Climate *> climates_{};
330 #endif
331 #ifdef USE_LIGHT
332  std::vector<light::LightState *> lights_{};
333 #endif
334 #ifdef USE_NUMBER
335  std::vector<number::Number *> numbers_{};
336 #endif
337 #ifdef USE_SELECT
338  std::vector<select::Select *> selects_{};
339 #endif
340 #ifdef USE_LOCK
341  std::vector<lock::Lock *> locks_{};
342 #endif
343 #ifdef USE_MEDIA_PLAYER
344  std::vector<media_player::MediaPlayer *> media_players_{};
345 #endif
346 
347  std::string name_;
348  std::string compilation_time_;
350  uint32_t last_loop_{0};
351  uint32_t loop_interval_{16};
352  size_t dump_config_at_{SIZE_MAX};
353  uint32_t app_state_{0};
354 };
355 
357 extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
358 
359 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
std::vector< light::LightState * > lights_
Definition: application.h:332
void register_fan(fan::Fan *state)
Definition: application.h:90
const char * name
Definition: stm32flash.h:78
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:285
Base class for all cover devices.
Definition: cover.h:111
text_sensor::TextSensor * get_text_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:213
void register_button(button::Button *button)
Definition: application.h:82
void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix)
Definition: application.h:56
void register_light(light::LightState *light)
Definition: application.h:102
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:248
void register_climate(climate::Climate *climate)
Definition: application.h:98
climate::Climate * get_climate_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:249
std::string compilation_time_
Definition: application.h:348
void register_media_player(media_player::MediaPlayer *media_player)
Definition: application.h:118
std::vector< binary_sensor::BinarySensor * > binary_sensors_
Definition: application.h:308
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:267
void register_text_sensor(text_sensor::TextSensor *sensor)
Definition: application.h:86
void register_sensor(sensor::Sensor *sensor)
Definition: application.h:74
binary_sensor::BinarySensor * get_binary_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:177
Base class for all buttons.
Definition: button.h:22
C * register_component(C *c)
Register the component in this Application instance.
Definition: application.h:122
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:276
std::vector< number::Number * > numbers_
Definition: application.h:335
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:195
std::vector< lock::Lock * > locks_
Definition: application.h:341
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:221
void setup()
Set up all the registered components. Call this at the end of your setup() function.
Definition: application.cpp:28
void register_cover(cover::Cover *cover)
Definition: application.h:94
void run_safe_shutdown_hooks()
Definition: application.h:164
void register_number(number::Number *number)
Definition: application.h:106
std::vector< cover::Cover * > covers_
Definition: application.h:326
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:231
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:275
std::vector< media_player::MediaPlayer * > media_players_
Definition: application.h:344
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:222
void loop()
Make a loop iteration. Call this in your loop() function.
Definition: application.cpp:66
std::vector< sensor::Sensor * > sensors_
Definition: application.h:317
std::vector< button::Button * > buttons_
Definition: application.h:314
const std::vector< button::Button * > & get_buttons()
Definition: application.h:194
std::vector< climate::Climate * > climates_
Definition: application.h:329
std::vector< text_sensor::TextSensor * > text_sensors_
Definition: application.h:320
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:185
Base-class for all numbers.
Definition: number.h:29
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:386
void register_select(select::Select *select)
Definition: application.h:110
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:212
void calculate_looping_components_()
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:203
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:176
std::vector< Component * > components_
Definition: application.h:304
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
Definition: application.h:68
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:240
const std::string & get_name() const
Get the name of this Application set by set_name().
Definition: application.h:135
switch_::Switch * get_switch_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:186
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:137
std::vector< Component * > looping_components_
Definition: application.h:305
std::vector< fan::Fan * > fans_
Definition: application.h:323
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:230
const std::string & get_compilation_time() const
Definition: application.h:139
void register_switch(switch_::Switch *a_switch)
Definition: application.h:78
std::vector< select::Select * > selects_
Definition: application.h:338
const std::vector< light::LightState * > & get_lights()
Definition: application.h:239
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:258
void register_component_(Component *comp)
Definition: application.cpp:14
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
Definition: application.h:154
sensor::Sensor * get_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:204
uint32_t get_app_state() const
Definition: application.h:173
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:284
Base-class for all selects.
Definition: select.h:24
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
void arch_init()
Definition: core.cpp:38
const std::vector< select::Select * > & get_selects()
Definition: application.h:266
Base-class for all sensors.
Definition: sensor.h:47
std::vector< switch_::Switch * > switches_
Definition: application.h:311
const std::vector< number::Number * > & get_numbers()
Definition: application.h:257
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
bool state
Definition: fan.h:34
void register_lock(lock::Lock *a_lock)
Definition: application.h:114