ESPHome  2024.4.0
application.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
6 #include "esphome/core/defines.h"
7 #include "esphome/core/hal.h"
8 #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_DATETIME_DATE
44 #endif
45 #ifdef USE_DATETIME_TIME
47 #endif
48 #ifdef USE_TEXT
50 #endif
51 #ifdef USE_SELECT
53 #endif
54 #ifdef USE_LOCK
56 #endif
57 #ifdef USE_MEDIA_PLAYER
59 #endif
60 #ifdef USE_ALARM_CONTROL_PANEL
62 #endif
63 
64 namespace esphome {
65 
66 class Application {
67  public:
68  void pre_setup(const std::string &name, const std::string &friendly_name, const std::string &area,
69  const char *comment, const char *compilation_time, bool name_add_mac_suffix) {
70  arch_init();
71  this->name_add_mac_suffix_ = name_add_mac_suffix;
72  if (name_add_mac_suffix) {
73  this->name_ = name + "-" + get_mac_address().substr(6);
74  if (friendly_name.empty()) {
75  this->friendly_name_ = "";
76  } else {
77  this->friendly_name_ = friendly_name + " " + get_mac_address().substr(6);
78  }
79  } else {
80  this->name_ = name;
81  this->friendly_name_ = friendly_name;
82  }
83  this->area_ = area;
84  this->comment_ = comment;
85  this->compilation_time_ = compilation_time;
86  }
87 
88 #ifdef USE_BINARY_SENSOR
90  this->binary_sensors_.push_back(binary_sensor);
91  }
92 #endif
93 
94 #ifdef USE_SENSOR
95  void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
96 #endif
97 
98 #ifdef USE_SWITCH
99  void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
100 #endif
101 
102 #ifdef USE_BUTTON
103  void register_button(button::Button *button) { this->buttons_.push_back(button); }
104 #endif
105 
106 #ifdef USE_TEXT_SENSOR
107  void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
108 #endif
109 
110 #ifdef USE_FAN
111  void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
112 #endif
113 
114 #ifdef USE_COVER
115  void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
116 #endif
117 
118 #ifdef USE_CLIMATE
119  void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
120 #endif
121 
122 #ifdef USE_LIGHT
123  void register_light(light::LightState *light) { this->lights_.push_back(light); }
124 #endif
125 
126 #ifdef USE_NUMBER
127  void register_number(number::Number *number) { this->numbers_.push_back(number); }
128 #endif
129 
130 #ifdef USE_DATETIME_DATE
131  void register_date(datetime::DateEntity *date) { this->dates_.push_back(date); }
132 #endif
133 
134 #ifdef USE_DATETIME_TIME
135  void register_time(datetime::TimeEntity *time) { this->times_.push_back(time); }
136 #endif
137 
138 #ifdef USE_TEXT
139  void register_text(text::Text *text) { this->texts_.push_back(text); }
140 #endif
141 
142 #ifdef USE_SELECT
143  void register_select(select::Select *select) { this->selects_.push_back(select); }
144 #endif
145 
146 #ifdef USE_LOCK
147  void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
148 #endif
149 
150 #ifdef USE_MEDIA_PLAYER
151  void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
152 #endif
153 
154 #ifdef USE_ALARM_CONTROL_PANEL
156  this->alarm_control_panels_.push_back(a_alarm_control_panel);
157  }
158 #endif
159 
161  template<class C> C *register_component(C *c) {
162  static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
163  this->register_component_((Component *) c);
164  return c;
165  }
166 
168  void setup();
169 
171  void loop();
172 
174  const std::string &get_name() const { return this->name_; }
175 
177  const std::string &get_friendly_name() const { return this->friendly_name_; }
178 
180  const std::string &get_area() const { return this->area_; }
181 
183  std::string get_comment() const { return this->comment_; }
184 
186 
187  std::string get_compilation_time() const { return this->compilation_time_; }
188 
202  void set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; }
203 
204  void schedule_dump_config() { this->dump_config_at_ = 0; }
205 
206  void feed_wdt();
207 
208  void reboot();
209 
210  void safe_reboot();
211 
213 
214  uint32_t get_app_state() const { return this->app_state_; }
215 
216 #ifdef USE_BINARY_SENSOR
217  const std::vector<binary_sensor::BinarySensor *> &get_binary_sensors() { return this->binary_sensors_; }
218  binary_sensor::BinarySensor *get_binary_sensor_by_key(uint32_t key, bool include_internal = false) {
219  for (auto *obj : this->binary_sensors_)
220  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
221  return obj;
222  return nullptr;
223  }
224 #endif
225 #ifdef USE_SWITCH
226  const std::vector<switch_::Switch *> &get_switches() { return this->switches_; }
227  switch_::Switch *get_switch_by_key(uint32_t key, bool include_internal = false) {
228  for (auto *obj : this->switches_)
229  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
230  return obj;
231  return nullptr;
232  }
233 #endif
234 #ifdef USE_BUTTON
235  const std::vector<button::Button *> &get_buttons() { return this->buttons_; }
236  button::Button *get_button_by_key(uint32_t key, bool include_internal = false) {
237  for (auto *obj : this->buttons_)
238  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
239  return obj;
240  return nullptr;
241  }
242 #endif
243 #ifdef USE_SENSOR
244  const std::vector<sensor::Sensor *> &get_sensors() { return this->sensors_; }
245  sensor::Sensor *get_sensor_by_key(uint32_t key, bool include_internal = false) {
246  for (auto *obj : this->sensors_)
247  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
248  return obj;
249  return nullptr;
250  }
251 #endif
252 #ifdef USE_TEXT_SENSOR
253  const std::vector<text_sensor::TextSensor *> &get_text_sensors() { return this->text_sensors_; }
254  text_sensor::TextSensor *get_text_sensor_by_key(uint32_t key, bool include_internal = false) {
255  for (auto *obj : this->text_sensors_)
256  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
257  return obj;
258  return nullptr;
259  }
260 #endif
261 #ifdef USE_FAN
262  const std::vector<fan::Fan *> &get_fans() { return this->fans_; }
263  fan::Fan *get_fan_by_key(uint32_t key, bool include_internal = false) {
264  for (auto *obj : this->fans_)
265  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
266  return obj;
267  return nullptr;
268  }
269 #endif
270 #ifdef USE_COVER
271  const std::vector<cover::Cover *> &get_covers() { return this->covers_; }
272  cover::Cover *get_cover_by_key(uint32_t key, bool include_internal = false) {
273  for (auto *obj : this->covers_)
274  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
275  return obj;
276  return nullptr;
277  }
278 #endif
279 #ifdef USE_LIGHT
280  const std::vector<light::LightState *> &get_lights() { return this->lights_; }
281  light::LightState *get_light_by_key(uint32_t key, bool include_internal = false) {
282  for (auto *obj : this->lights_)
283  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
284  return obj;
285  return nullptr;
286  }
287 #endif
288 #ifdef USE_CLIMATE
289  const std::vector<climate::Climate *> &get_climates() { return this->climates_; }
290  climate::Climate *get_climate_by_key(uint32_t key, bool include_internal = false) {
291  for (auto *obj : this->climates_)
292  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
293  return obj;
294  return nullptr;
295  }
296 #endif
297 #ifdef USE_NUMBER
298  const std::vector<number::Number *> &get_numbers() { return this->numbers_; }
299  number::Number *get_number_by_key(uint32_t key, bool include_internal = false) {
300  for (auto *obj : this->numbers_)
301  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
302  return obj;
303  return nullptr;
304  }
305 #endif
306 #ifdef USE_DATETIME_DATE
307  const std::vector<datetime::DateEntity *> &get_dates() { return this->dates_; }
308  datetime::DateEntity *get_date_by_key(uint32_t key, bool include_internal = false) {
309  for (auto *obj : this->dates_)
310  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
311  return obj;
312  return nullptr;
313  }
314 #endif
315 #ifdef USE_DATETIME_TIME
316  const std::vector<datetime::TimeEntity *> &get_times() { return this->times_; }
317  datetime::TimeEntity *get_time_by_key(uint32_t key, bool include_internal = false) {
318  for (auto *obj : this->times_)
319  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
320  return obj;
321  return nullptr;
322  }
323 #endif
324 #ifdef USE_TEXT
325  const std::vector<text::Text *> &get_texts() { return this->texts_; }
326  text::Text *get_text_by_key(uint32_t key, bool include_internal = false) {
327  for (auto *obj : this->texts_)
328  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
329  return obj;
330  return nullptr;
331  }
332 #endif
333 #ifdef USE_SELECT
334  const std::vector<select::Select *> &get_selects() { return this->selects_; }
335  select::Select *get_select_by_key(uint32_t key, bool include_internal = false) {
336  for (auto *obj : this->selects_)
337  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
338  return obj;
339  return nullptr;
340  }
341 #endif
342 #ifdef USE_LOCK
343  const std::vector<lock::Lock *> &get_locks() { return this->locks_; }
344  lock::Lock *get_lock_by_key(uint32_t key, bool include_internal = false) {
345  for (auto *obj : this->locks_)
346  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
347  return obj;
348  return nullptr;
349  }
350 #endif
351 #ifdef USE_MEDIA_PLAYER
352  const std::vector<media_player::MediaPlayer *> &get_media_players() { return this->media_players_; }
353  media_player::MediaPlayer *get_media_player_by_key(uint32_t key, bool include_internal = false) {
354  for (auto *obj : this->media_players_)
355  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
356  return obj;
357  return nullptr;
358  }
359 #endif
360 
361 #ifdef USE_ALARM_CONTROL_PANEL
362  const std::vector<alarm_control_panel::AlarmControlPanel *> &get_alarm_control_panels() {
363  return this->alarm_control_panels_;
364  }
365  alarm_control_panel::AlarmControlPanel *get_alarm_control_panel_by_key(uint32_t key, bool include_internal = false) {
366  for (auto *obj : this->alarm_control_panels_)
367  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
368  return obj;
369  return nullptr;
370  }
371 #endif
372 
374 
375  protected:
376  friend Component;
377 
378  void register_component_(Component *comp);
379 
381 
382  void feed_wdt_arch_();
383 
384  std::vector<Component *> components_{};
385  std::vector<Component *> looping_components_{};
386 
387 #ifdef USE_BINARY_SENSOR
388  std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
389 #endif
390 #ifdef USE_SWITCH
391  std::vector<switch_::Switch *> switches_{};
392 #endif
393 #ifdef USE_BUTTON
394  std::vector<button::Button *> buttons_{};
395 #endif
396 #ifdef USE_SENSOR
397  std::vector<sensor::Sensor *> sensors_{};
398 #endif
399 #ifdef USE_TEXT_SENSOR
400  std::vector<text_sensor::TextSensor *> text_sensors_{};
401 #endif
402 #ifdef USE_FAN
403  std::vector<fan::Fan *> fans_{};
404 #endif
405 #ifdef USE_COVER
406  std::vector<cover::Cover *> covers_{};
407 #endif
408 #ifdef USE_CLIMATE
409  std::vector<climate::Climate *> climates_{};
410 #endif
411 #ifdef USE_LIGHT
412  std::vector<light::LightState *> lights_{};
413 #endif
414 #ifdef USE_NUMBER
415  std::vector<number::Number *> numbers_{};
416 #endif
417 #ifdef USE_DATETIME_DATE
418  std::vector<datetime::DateEntity *> dates_{};
419 #endif
420 #ifdef USE_DATETIME_TIME
421  std::vector<datetime::TimeEntity *> times_{};
422 #endif
423 #ifdef USE_SELECT
424  std::vector<select::Select *> selects_{};
425 #endif
426 #ifdef USE_TEXT
427  std::vector<text::Text *> texts_{};
428 #endif
429 #ifdef USE_LOCK
430  std::vector<lock::Lock *> locks_{};
431 #endif
432 #ifdef USE_MEDIA_PLAYER
433  std::vector<media_player::MediaPlayer *> media_players_{};
434 #endif
435 #ifdef USE_ALARM_CONTROL_PANEL
436  std::vector<alarm_control_panel::AlarmControlPanel *> alarm_control_panels_{};
437 #endif
438 
439  std::string name_;
440  std::string friendly_name_;
441  std::string area_;
442  const char *comment_{nullptr};
443  const char *compilation_time_{nullptr};
445  uint32_t last_loop_{0};
446  uint32_t loop_interval_{16};
447  size_t dump_config_at_{SIZE_MAX};
448  uint32_t app_state_{0};
449 };
450 
452 extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
453 
454 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
std::vector< light::LightState * > lights_
Definition: application.h:412
void register_fan(fan::Fan *state)
Definition: application.h:111
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:34
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:353
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:254
void register_button(button::Button *button)
Definition: application.h:103
void register_light(light::LightState *light)
Definition: application.h:123
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:289
void register_climate(climate::Climate *climate)
Definition: application.h:119
climate::Climate * get_climate_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:290
void register_media_player(media_player::MediaPlayer *media_player)
Definition: application.h:151
const std::vector< alarm_control_panel::AlarmControlPanel * > & get_alarm_control_panels()
Definition: application.h:362
std::vector< binary_sensor::BinarySensor * > binary_sensors_
Definition: application.h:388
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:335
void register_text_sensor(text_sensor::TextSensor *sensor)
Definition: application.h:107
std::vector< alarm_control_panel::AlarmControlPanel * > alarm_control_panels_
Definition: application.h:436
void register_sensor(sensor::Sensor *sensor)
Definition: application.h:95
binary_sensor::BinarySensor * get_binary_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:218
Base class for all buttons.
Definition: button.h:29
void register_time(datetime::TimeEntity *time)
Definition: application.h:135
C * register_component(C *c)
Register the component in this Application instance.
Definition: application.h:161
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:344
std::vector< datetime::DateEntity * > dates_
Definition: application.h:418
std::vector< number::Number * > numbers_
Definition: application.h:415
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:236
std::vector< lock::Lock * > locks_
Definition: application.h:430
const std::string & get_area() const
Get the area of this Application set by pre_setup().
Definition: application.h:180
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:177
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:262
Base-class for all text inputs.
Definition: text.h:24
void setup()
Set up all the registered components. Call this at the end of your setup() function.
Definition: application.cpp:28
void register_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
Definition: application.h:155
const std::vector< datetime::TimeEntity * > & get_times()
Definition: application.h:316
alarm_control_panel::AlarmControlPanel * get_alarm_control_panel_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:365
void register_cover(cover::Cover *cover)
Definition: application.h:115
void register_number(number::Number *number)
Definition: application.h:127
std::vector< cover::Cover * > covers_
Definition: application.h:406
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:272
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:343
std::string friendly_name_
Definition: application.h:440
void pre_setup(const std::string &name, const std::string &friendly_name, const std::string &area, const char *comment, const char *compilation_time, bool name_add_mac_suffix)
Definition: application.h:68
std::vector< media_player::MediaPlayer * > media_players_
Definition: application.h:433
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:263
void register_date(datetime::DateEntity *date)
Definition: application.h:131
void loop()
Make a loop iteration. Call this in your loop() function.
Definition: application.cpp:66
std::vector< sensor::Sensor * > sensors_
Definition: application.h:397
std::vector< button::Button * > buttons_
Definition: application.h:394
const std::vector< button::Button * > & get_buttons()
Definition: application.h:235
std::vector< climate::Climate * > climates_
Definition: application.h:409
std::vector< text_sensor::TextSensor * > text_sensors_
Definition: application.h:400
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:226
Base-class for all numbers.
Definition: number.h:39
std::vector< text::Text * > texts_
Definition: application.h:427
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:587
void register_text(text::Text *text)
Definition: application.h:139
void register_select(select::Select *select)
Definition: application.h:143
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:253
void calculate_looping_components_()
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:244
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:217
std::vector< datetime::TimeEntity * > times_
Definition: application.h:421
std::vector< Component * > components_
Definition: application.h:384
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
Definition: application.h:89
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:281
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:174
const std::vector< text::Text * > & get_texts()
Definition: application.h:325
switch_::Switch * get_switch_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:227
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:185
std::vector< Component * > looping_components_
Definition: application.h:385
std::vector< fan::Fan * > fans_
Definition: application.h:403
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:271
void register_switch(switch_::Switch *a_switch)
Definition: application.h:99
std::vector< select::Select * > selects_
Definition: application.h:424
datetime::DateEntity * get_date_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:308
const std::vector< light::LightState * > & get_lights()
Definition: application.h:280
std::string get_comment() const
Get the comment of this Application set by pre_setup().
Definition: application.h:183
const char * compilation_time_
Definition: application.h:443
text::Text * get_text_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:326
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:299
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:202
sensor::Sensor * get_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:245
uint32_t get_app_state() const
Definition: application.h:214
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:352
Base-class for all selects.
Definition: select.h:31
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void arch_init()
Definition: core.cpp:37
const char * comment_
Definition: application.h:442
const std::vector< datetime::DateEntity * > & get_dates()
Definition: application.h:307
const std::vector< select::Select * > & get_selects()
Definition: application.h:334
Base-class for all sensors.
Definition: sensor.h:57
datetime::TimeEntity * get_time_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:317
std::vector< switch_::Switch * > switches_
Definition: application.h:391
const std::vector< number::Number * > & get_numbers()
Definition: application.h:298
std::string get_compilation_time() const
Definition: application.h:187
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
bool state
Definition: fan.h:34
void register_lock(lock::Lock *a_lock)
Definition: application.h:147