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