ESPHome  2022.11.3
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 
166  uint32_t get_app_state() const { return this->app_state_; }
167 
168 #ifdef USE_BINARY_SENSOR
169  const std::vector<binary_sensor::BinarySensor *> &get_binary_sensors() { return this->binary_sensors_; }
170  binary_sensor::BinarySensor *get_binary_sensor_by_key(uint32_t key, bool include_internal = false) {
171  for (auto *obj : this->binary_sensors_)
172  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
173  return obj;
174  return nullptr;
175  }
176 #endif
177 #ifdef USE_SWITCH
178  const std::vector<switch_::Switch *> &get_switches() { return this->switches_; }
179  switch_::Switch *get_switch_by_key(uint32_t key, bool include_internal = false) {
180  for (auto *obj : this->switches_)
181  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
182  return obj;
183  return nullptr;
184  }
185 #endif
186 #ifdef USE_BUTTON
187  const std::vector<button::Button *> &get_buttons() { return this->buttons_; }
188  button::Button *get_button_by_key(uint32_t key, bool include_internal = false) {
189  for (auto *obj : this->buttons_)
190  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
191  return obj;
192  return nullptr;
193  }
194 #endif
195 #ifdef USE_SENSOR
196  const std::vector<sensor::Sensor *> &get_sensors() { return this->sensors_; }
197  sensor::Sensor *get_sensor_by_key(uint32_t key, bool include_internal = false) {
198  for (auto *obj : this->sensors_)
199  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
200  return obj;
201  return nullptr;
202  }
203 #endif
204 #ifdef USE_TEXT_SENSOR
205  const std::vector<text_sensor::TextSensor *> &get_text_sensors() { return this->text_sensors_; }
206  text_sensor::TextSensor *get_text_sensor_by_key(uint32_t key, bool include_internal = false) {
207  for (auto *obj : this->text_sensors_)
208  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
209  return obj;
210  return nullptr;
211  }
212 #endif
213 #ifdef USE_FAN
214  const std::vector<fan::Fan *> &get_fans() { return this->fans_; }
215  fan::Fan *get_fan_by_key(uint32_t key, bool include_internal = false) {
216  for (auto *obj : this->fans_)
217  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
218  return obj;
219  return nullptr;
220  }
221 #endif
222 #ifdef USE_COVER
223  const std::vector<cover::Cover *> &get_covers() { return this->covers_; }
224  cover::Cover *get_cover_by_key(uint32_t key, bool include_internal = false) {
225  for (auto *obj : this->covers_)
226  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
227  return obj;
228  return nullptr;
229  }
230 #endif
231 #ifdef USE_LIGHT
232  const std::vector<light::LightState *> &get_lights() { return this->lights_; }
233  light::LightState *get_light_by_key(uint32_t key, bool include_internal = false) {
234  for (auto *obj : this->lights_)
235  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
236  return obj;
237  return nullptr;
238  }
239 #endif
240 #ifdef USE_CLIMATE
241  const std::vector<climate::Climate *> &get_climates() { return this->climates_; }
242  climate::Climate *get_climate_by_key(uint32_t key, bool include_internal = false) {
243  for (auto *obj : this->climates_)
244  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
245  return obj;
246  return nullptr;
247  }
248 #endif
249 #ifdef USE_NUMBER
250  const std::vector<number::Number *> &get_numbers() { return this->numbers_; }
251  number::Number *get_number_by_key(uint32_t key, bool include_internal = false) {
252  for (auto *obj : this->numbers_)
253  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
254  return obj;
255  return nullptr;
256  }
257 #endif
258 #ifdef USE_SELECT
259  const std::vector<select::Select *> &get_selects() { return this->selects_; }
260  select::Select *get_select_by_key(uint32_t key, bool include_internal = false) {
261  for (auto *obj : this->selects_)
262  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
263  return obj;
264  return nullptr;
265  }
266 #endif
267 #ifdef USE_LOCK
268  const std::vector<lock::Lock *> &get_locks() { return this->locks_; }
269  lock::Lock *get_lock_by_key(uint32_t key, bool include_internal = false) {
270  for (auto *obj : this->locks_)
271  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
272  return obj;
273  return nullptr;
274  }
275 #endif
276 #ifdef USE_MEDIA_PLAYER
277  const std::vector<media_player::MediaPlayer *> &get_media_players() { return this->media_players_; }
278  media_player::MediaPlayer *get_media_player_by_key(uint32_t key, bool include_internal = false) {
279  for (auto *obj : this->media_players_)
280  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
281  return obj;
282  return nullptr;
283  }
284 #endif
285 
287 
288  protected:
289  friend Component;
290 
291  void register_component_(Component *comp);
292 
294 
295  void feed_wdt_arch_();
296 
297  std::vector<Component *> components_{};
298  std::vector<Component *> looping_components_{};
299 
300 #ifdef USE_BINARY_SENSOR
301  std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
302 #endif
303 #ifdef USE_SWITCH
304  std::vector<switch_::Switch *> switches_{};
305 #endif
306 #ifdef USE_BUTTON
307  std::vector<button::Button *> buttons_{};
308 #endif
309 #ifdef USE_SENSOR
310  std::vector<sensor::Sensor *> sensors_{};
311 #endif
312 #ifdef USE_TEXT_SENSOR
313  std::vector<text_sensor::TextSensor *> text_sensors_{};
314 #endif
315 #ifdef USE_FAN
316  std::vector<fan::Fan *> fans_{};
317 #endif
318 #ifdef USE_COVER
319  std::vector<cover::Cover *> covers_{};
320 #endif
321 #ifdef USE_CLIMATE
322  std::vector<climate::Climate *> climates_{};
323 #endif
324 #ifdef USE_LIGHT
325  std::vector<light::LightState *> lights_{};
326 #endif
327 #ifdef USE_NUMBER
328  std::vector<number::Number *> numbers_{};
329 #endif
330 #ifdef USE_SELECT
331  std::vector<select::Select *> selects_{};
332 #endif
333 #ifdef USE_LOCK
334  std::vector<lock::Lock *> locks_{};
335 #endif
336 #ifdef USE_MEDIA_PLAYER
337  std::vector<media_player::MediaPlayer *> media_players_{};
338 #endif
339 
340  std::string name_;
341  std::string compilation_time_;
343  uint32_t last_loop_{0};
344  uint32_t loop_interval_{16};
345  size_t dump_config_at_{SIZE_MAX};
346  uint32_t app_state_{0};
347 };
348 
350 extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
351 
352 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
std::vector< light::LightState * > lights_
Definition: application.h:325
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:278
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:206
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:241
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:242
std::string compilation_time_
Definition: application.h:341
void register_media_player(media_player::MediaPlayer *media_player)
Definition: application.h:118
std::vector< binary_sensor::BinarySensor * > binary_sensors_
Definition: application.h:301
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:260
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:170
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:269
std::vector< number::Number * > numbers_
Definition: application.h:328
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:188
std::vector< lock::Lock * > locks_
Definition: application.h:334
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:214
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 register_number(number::Number *number)
Definition: application.h:106
std::vector< cover::Cover * > covers_
Definition: application.h:319
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:224
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:268
std::vector< media_player::MediaPlayer * > media_players_
Definition: application.h:337
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:215
void loop()
Make a loop iteration. Call this in your loop() function.
Definition: application.cpp:66
std::vector< sensor::Sensor * > sensors_
Definition: application.h:310
std::vector< button::Button * > buttons_
Definition: application.h:307
const std::vector< button::Button * > & get_buttons()
Definition: application.h:187
std::vector< climate::Climate * > climates_
Definition: application.h:322
std::vector< text_sensor::TextSensor * > text_sensors_
Definition: application.h:313
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:178
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:442
void register_select(select::Select *select)
Definition: application.h:110
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:205
void calculate_looping_components_()
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:196
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:169
std::vector< Component * > components_
Definition: application.h:297
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:233
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:179
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:137
std::vector< Component * > looping_components_
Definition: application.h:298
std::vector< fan::Fan * > fans_
Definition: application.h:316
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:223
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:331
const std::vector< light::LightState * > & get_lights()
Definition: application.h:232
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:251
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:197
uint32_t get_app_state() const
Definition: application.h:166
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:277
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:259
Base-class for all sensors.
Definition: sensor.h:48
std::vector< switch_::Switch * > switches_
Definition: application.h:304
const std::vector< number::Number * > & get_numbers()
Definition: application.h:250
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