ESPHome  2023.11.6
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_TEXT
44 #endif
45 #ifdef USE_SELECT
47 #endif
48 #ifdef USE_LOCK
50 #endif
51 #ifdef USE_MEDIA_PLAYER
53 #endif
54 #ifdef USE_ALARM_CONTROL_PANEL
56 #endif
57 
58 namespace esphome {
59 
60 class Application {
61  public:
62  void pre_setup(const std::string &name, const std::string &friendly_name, const std::string &area,
63  const char *comment, const char *compilation_time, bool name_add_mac_suffix) {
64  arch_init();
65  this->name_add_mac_suffix_ = name_add_mac_suffix;
66  if (name_add_mac_suffix) {
67  this->name_ = name + "-" + get_mac_address().substr(6);
68  if (friendly_name.empty()) {
69  this->friendly_name_ = "";
70  } else {
71  this->friendly_name_ = friendly_name + " " + get_mac_address().substr(6);
72  }
73  } else {
74  this->name_ = name;
75  this->friendly_name_ = friendly_name;
76  }
77  this->area_ = area;
78  this->comment_ = comment;
79  this->compilation_time_ = compilation_time;
80  }
81 
82 #ifdef USE_BINARY_SENSOR
84  this->binary_sensors_.push_back(binary_sensor);
85  }
86 #endif
87 
88 #ifdef USE_SENSOR
89  void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
90 #endif
91 
92 #ifdef USE_SWITCH
93  void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
94 #endif
95 
96 #ifdef USE_BUTTON
97  void register_button(button::Button *button) { this->buttons_.push_back(button); }
98 #endif
99 
100 #ifdef USE_TEXT_SENSOR
101  void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
102 #endif
103 
104 #ifdef USE_FAN
105  void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
106 #endif
107 
108 #ifdef USE_COVER
109  void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
110 #endif
111 
112 #ifdef USE_CLIMATE
113  void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
114 #endif
115 
116 #ifdef USE_LIGHT
117  void register_light(light::LightState *light) { this->lights_.push_back(light); }
118 #endif
119 
120 #ifdef USE_NUMBER
121  void register_number(number::Number *number) { this->numbers_.push_back(number); }
122 #endif
123 
124 #ifdef USE_TEXT
125  void register_text(text::Text *text) { this->texts_.push_back(text); }
126 #endif
127 
128 #ifdef USE_SELECT
129  void register_select(select::Select *select) { this->selects_.push_back(select); }
130 #endif
131 
132 #ifdef USE_LOCK
133  void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
134 #endif
135 
136 #ifdef USE_MEDIA_PLAYER
137  void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
138 #endif
139 
140 #ifdef USE_ALARM_CONTROL_PANEL
142  this->alarm_control_panels_.push_back(a_alarm_control_panel);
143  }
144 #endif
145 
147  template<class C> C *register_component(C *c) {
148  static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
149  this->register_component_((Component *) c);
150  return c;
151  }
152 
154  void setup();
155 
157  void loop();
158 
160  const std::string &get_name() const { return this->name_; }
161 
163  const std::string &get_friendly_name() const { return this->friendly_name_; }
164 
166  const std::string &get_area() const { return this->area_; }
167 
169  std::string get_comment() const { return this->comment_; }
170 
172 
173  std::string get_compilation_time() const { return this->compilation_time_; }
174 
188  void set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; }
189 
190  void schedule_dump_config() { this->dump_config_at_ = 0; }
191 
192  void feed_wdt();
193 
194  void reboot();
195 
196  void safe_reboot();
197 
199 
200  uint32_t get_app_state() const { return this->app_state_; }
201 
202 #ifdef USE_BINARY_SENSOR
203  const std::vector<binary_sensor::BinarySensor *> &get_binary_sensors() { return this->binary_sensors_; }
204  binary_sensor::BinarySensor *get_binary_sensor_by_key(uint32_t key, bool include_internal = false) {
205  for (auto *obj : this->binary_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_SWITCH
212  const std::vector<switch_::Switch *> &get_switches() { return this->switches_; }
213  switch_::Switch *get_switch_by_key(uint32_t key, bool include_internal = false) {
214  for (auto *obj : this->switches_)
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_BUTTON
221  const std::vector<button::Button *> &get_buttons() { return this->buttons_; }
222  button::Button *get_button_by_key(uint32_t key, bool include_internal = false) {
223  for (auto *obj : this->buttons_)
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_SENSOR
230  const std::vector<sensor::Sensor *> &get_sensors() { return this->sensors_; }
231  sensor::Sensor *get_sensor_by_key(uint32_t key, bool include_internal = false) {
232  for (auto *obj : this->sensors_)
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_TEXT_SENSOR
239  const std::vector<text_sensor::TextSensor *> &get_text_sensors() { return this->text_sensors_; }
240  text_sensor::TextSensor *get_text_sensor_by_key(uint32_t key, bool include_internal = false) {
241  for (auto *obj : this->text_sensors_)
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_FAN
248  const std::vector<fan::Fan *> &get_fans() { return this->fans_; }
249  fan::Fan *get_fan_by_key(uint32_t key, bool include_internal = false) {
250  for (auto *obj : this->fans_)
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_COVER
257  const std::vector<cover::Cover *> &get_covers() { return this->covers_; }
258  cover::Cover *get_cover_by_key(uint32_t key, bool include_internal = false) {
259  for (auto *obj : this->covers_)
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_LIGHT
266  const std::vector<light::LightState *> &get_lights() { return this->lights_; }
267  light::LightState *get_light_by_key(uint32_t key, bool include_internal = false) {
268  for (auto *obj : this->lights_)
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_CLIMATE
275  const std::vector<climate::Climate *> &get_climates() { return this->climates_; }
276  climate::Climate *get_climate_by_key(uint32_t key, bool include_internal = false) {
277  for (auto *obj : this->climates_)
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_NUMBER
284  const std::vector<number::Number *> &get_numbers() { return this->numbers_; }
285  number::Number *get_number_by_key(uint32_t key, bool include_internal = false) {
286  for (auto *obj : this->numbers_)
287  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
288  return obj;
289  return nullptr;
290  }
291 #endif
292 #ifdef USE_TEXT
293  const std::vector<text::Text *> &get_texts() { return this->texts_; }
294  text::Text *get_text_by_key(uint32_t key, bool include_internal = false) {
295  for (auto *obj : this->texts_)
296  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
297  return obj;
298  return nullptr;
299  }
300 #endif
301 #ifdef USE_SELECT
302  const std::vector<select::Select *> &get_selects() { return this->selects_; }
303  select::Select *get_select_by_key(uint32_t key, bool include_internal = false) {
304  for (auto *obj : this->selects_)
305  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
306  return obj;
307  return nullptr;
308  }
309 #endif
310 #ifdef USE_LOCK
311  const std::vector<lock::Lock *> &get_locks() { return this->locks_; }
312  lock::Lock *get_lock_by_key(uint32_t key, bool include_internal = false) {
313  for (auto *obj : this->locks_)
314  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
315  return obj;
316  return nullptr;
317  }
318 #endif
319 #ifdef USE_MEDIA_PLAYER
320  const std::vector<media_player::MediaPlayer *> &get_media_players() { return this->media_players_; }
321  media_player::MediaPlayer *get_media_player_by_key(uint32_t key, bool include_internal = false) {
322  for (auto *obj : this->media_players_)
323  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
324  return obj;
325  return nullptr;
326  }
327 #endif
328 
329 #ifdef USE_ALARM_CONTROL_PANEL
330  const std::vector<alarm_control_panel::AlarmControlPanel *> &get_alarm_control_panels() {
331  return this->alarm_control_panels_;
332  }
333  alarm_control_panel::AlarmControlPanel *get_alarm_control_panel_by_key(uint32_t key, bool include_internal = false) {
334  for (auto *obj : this->alarm_control_panels_)
335  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
336  return obj;
337  return nullptr;
338  }
339 #endif
340 
342 
343  protected:
344  friend Component;
345 
346  void register_component_(Component *comp);
347 
349 
350  void feed_wdt_arch_();
351 
352  std::vector<Component *> components_{};
353  std::vector<Component *> looping_components_{};
354 
355 #ifdef USE_BINARY_SENSOR
356  std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
357 #endif
358 #ifdef USE_SWITCH
359  std::vector<switch_::Switch *> switches_{};
360 #endif
361 #ifdef USE_BUTTON
362  std::vector<button::Button *> buttons_{};
363 #endif
364 #ifdef USE_SENSOR
365  std::vector<sensor::Sensor *> sensors_{};
366 #endif
367 #ifdef USE_TEXT_SENSOR
368  std::vector<text_sensor::TextSensor *> text_sensors_{};
369 #endif
370 #ifdef USE_FAN
371  std::vector<fan::Fan *> fans_{};
372 #endif
373 #ifdef USE_COVER
374  std::vector<cover::Cover *> covers_{};
375 #endif
376 #ifdef USE_CLIMATE
377  std::vector<climate::Climate *> climates_{};
378 #endif
379 #ifdef USE_LIGHT
380  std::vector<light::LightState *> lights_{};
381 #endif
382 #ifdef USE_NUMBER
383  std::vector<number::Number *> numbers_{};
384 #endif
385 #ifdef USE_SELECT
386  std::vector<select::Select *> selects_{};
387 #endif
388 #ifdef USE_TEXT
389  std::vector<text::Text *> texts_{};
390 #endif
391 #ifdef USE_LOCK
392  std::vector<lock::Lock *> locks_{};
393 #endif
394 #ifdef USE_MEDIA_PLAYER
395  std::vector<media_player::MediaPlayer *> media_players_{};
396 #endif
397 #ifdef USE_ALARM_CONTROL_PANEL
398  std::vector<alarm_control_panel::AlarmControlPanel *> alarm_control_panels_{};
399 #endif
400 
401  std::string name_;
402  std::string friendly_name_;
403  std::string area_;
404  const char *comment_{nullptr};
405  const char *compilation_time_{nullptr};
407  uint32_t last_loop_{0};
408  uint32_t loop_interval_{16};
409  size_t dump_config_at_{SIZE_MAX};
410  uint32_t app_state_{0};
411 };
412 
414 extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
415 
416 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
std::vector< light::LightState * > lights_
Definition: application.h:380
void register_fan(fan::Fan *state)
Definition: application.h:105
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:321
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:240
void register_button(button::Button *button)
Definition: application.h:97
void register_light(light::LightState *light)
Definition: application.h:117
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:275
void register_climate(climate::Climate *climate)
Definition: application.h:113
climate::Climate * get_climate_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:276
void register_media_player(media_player::MediaPlayer *media_player)
Definition: application.h:137
const std::vector< alarm_control_panel::AlarmControlPanel * > & get_alarm_control_panels()
Definition: application.h:330
std::vector< binary_sensor::BinarySensor * > binary_sensors_
Definition: application.h:356
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:303
void register_text_sensor(text_sensor::TextSensor *sensor)
Definition: application.h:101
std::vector< alarm_control_panel::AlarmControlPanel * > alarm_control_panels_
Definition: application.h:398
void register_sensor(sensor::Sensor *sensor)
Definition: application.h:89
binary_sensor::BinarySensor * get_binary_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:204
Base class for all buttons.
Definition: button.h:29
C * register_component(C *c)
Register the component in this Application instance.
Definition: application.h:147
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:312
std::vector< number::Number * > numbers_
Definition: application.h:383
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:222
std::vector< lock::Lock * > locks_
Definition: application.h:392
const std::string & get_area() const
Get the area of this Application set by pre_setup().
Definition: application.h:166
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:163
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:248
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:141
alarm_control_panel::AlarmControlPanel * get_alarm_control_panel_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:333
void register_cover(cover::Cover *cover)
Definition: application.h:109
void register_number(number::Number *number)
Definition: application.h:121
std::vector< cover::Cover * > covers_
Definition: application.h:374
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:258
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:311
std::string friendly_name_
Definition: application.h:402
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:62
std::vector< media_player::MediaPlayer * > media_players_
Definition: application.h:395
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:249
void loop()
Make a loop iteration. Call this in your loop() function.
Definition: application.cpp:66
std::vector< sensor::Sensor * > sensors_
Definition: application.h:365
std::vector< button::Button * > buttons_
Definition: application.h:362
const std::vector< button::Button * > & get_buttons()
Definition: application.h:221
std::vector< climate::Climate * > climates_
Definition: application.h:377
std::vector< text_sensor::TextSensor * > text_sensors_
Definition: application.h:368
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:212
Base-class for all numbers.
Definition: number.h:39
std::vector< text::Text * > texts_
Definition: application.h:389
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:571
void register_text(text::Text *text)
Definition: application.h:125
void register_select(select::Select *select)
Definition: application.h:129
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:239
void calculate_looping_components_()
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:230
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:203
std::vector< Component * > components_
Definition: application.h:352
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
Definition: application.h:83
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:267
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:160
const std::vector< text::Text * > & get_texts()
Definition: application.h:293
switch_::Switch * get_switch_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:213
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:171
std::vector< Component * > looping_components_
Definition: application.h:353
std::vector< fan::Fan * > fans_
Definition: application.h:371
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:257
void register_switch(switch_::Switch *a_switch)
Definition: application.h:93
std::vector< select::Select * > selects_
Definition: application.h:386
const std::vector< light::LightState * > & get_lights()
Definition: application.h:266
std::string get_comment() const
Get the comment of this Application set by pre_setup().
Definition: application.h:169
const char * compilation_time_
Definition: application.h:405
text::Text * get_text_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:294
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:285
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:188
sensor::Sensor * get_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:231
uint32_t get_app_state() const
Definition: application.h:200
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:320
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
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:404
const std::vector< select::Select * > & get_selects()
Definition: application.h:302
Base-class for all sensors.
Definition: sensor.h:57
std::vector< switch_::Switch * > switches_
Definition: application.h:359
const std::vector< number::Number * > & get_numbers()
Definition: application.h:284
std::string get_compilation_time() const
Definition: application.h:173
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:161
bool state
Definition: fan.h:34
void register_lock(lock::Lock *a_lock)
Definition: application.h:133