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