ESPHome  2023.3.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_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 std::string &friendly_name, const std::string &comment,
57  const char *compilation_time, bool name_add_mac_suffix) {
58  arch_init();
59  this->name_add_mac_suffix_ = name_add_mac_suffix;
60  if (name_add_mac_suffix) {
61  this->name_ = name + "-" + get_mac_address().substr(6);
62  if (friendly_name.empty()) {
63  this->friendly_name_ = "";
64  } else {
65  this->friendly_name_ = friendly_name + " " + get_mac_address().substr(6);
66  }
67  } else {
68  this->name_ = name;
69  this->friendly_name_ = friendly_name;
70  }
71  this->comment_ = comment;
72  this->compilation_time_ = compilation_time;
73  }
74 
75 #ifdef USE_BINARY_SENSOR
77  this->binary_sensors_.push_back(binary_sensor);
78  }
79 #endif
80 
81 #ifdef USE_SENSOR
82  void register_sensor(sensor::Sensor *sensor) { this->sensors_.push_back(sensor); }
83 #endif
84 
85 #ifdef USE_SWITCH
86  void register_switch(switch_::Switch *a_switch) { this->switches_.push_back(a_switch); }
87 #endif
88 
89 #ifdef USE_BUTTON
90  void register_button(button::Button *button) { this->buttons_.push_back(button); }
91 #endif
92 
93 #ifdef USE_TEXT_SENSOR
94  void register_text_sensor(text_sensor::TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
95 #endif
96 
97 #ifdef USE_FAN
98  void register_fan(fan::Fan *state) { this->fans_.push_back(state); }
99 #endif
100 
101 #ifdef USE_COVER
102  void register_cover(cover::Cover *cover) { this->covers_.push_back(cover); }
103 #endif
104 
105 #ifdef USE_CLIMATE
106  void register_climate(climate::Climate *climate) { this->climates_.push_back(climate); }
107 #endif
108 
109 #ifdef USE_LIGHT
110  void register_light(light::LightState *light) { this->lights_.push_back(light); }
111 #endif
112 
113 #ifdef USE_NUMBER
114  void register_number(number::Number *number) { this->numbers_.push_back(number); }
115 #endif
116 
117 #ifdef USE_SELECT
118  void register_select(select::Select *select) { this->selects_.push_back(select); }
119 #endif
120 
121 #ifdef USE_LOCK
122  void register_lock(lock::Lock *a_lock) { this->locks_.push_back(a_lock); }
123 #endif
124 
125 #ifdef USE_MEDIA_PLAYER
126  void register_media_player(media_player::MediaPlayer *media_player) { this->media_players_.push_back(media_player); }
127 #endif
128 
130  template<class C> C *register_component(C *c) {
131  static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
132  this->register_component_((Component *) c);
133  return c;
134  }
135 
137  void setup();
138 
140  void loop();
141 
143  const std::string &get_name() const { return this->name_; }
144 
146  const std::string &get_friendly_name() const { return this->friendly_name_; }
148  const std::string &get_comment() const { return this->comment_; }
149 
151 
152  const std::string &get_compilation_time() const { return this->compilation_time_; }
153 
167  void set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; }
168 
169  void schedule_dump_config() { this->dump_config_at_ = 0; }
170 
171  void feed_wdt();
172 
173  void reboot();
174 
175  void safe_reboot();
176 
178 
179  uint32_t get_app_state() const { return this->app_state_; }
180 
181 #ifdef USE_BINARY_SENSOR
182  const std::vector<binary_sensor::BinarySensor *> &get_binary_sensors() { return this->binary_sensors_; }
183  binary_sensor::BinarySensor *get_binary_sensor_by_key(uint32_t key, bool include_internal = false) {
184  for (auto *obj : this->binary_sensors_)
185  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
186  return obj;
187  return nullptr;
188  }
189 #endif
190 #ifdef USE_SWITCH
191  const std::vector<switch_::Switch *> &get_switches() { return this->switches_; }
192  switch_::Switch *get_switch_by_key(uint32_t key, bool include_internal = false) {
193  for (auto *obj : this->switches_)
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_BUTTON
200  const std::vector<button::Button *> &get_buttons() { return this->buttons_; }
201  button::Button *get_button_by_key(uint32_t key, bool include_internal = false) {
202  for (auto *obj : this->buttons_)
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_SENSOR
209  const std::vector<sensor::Sensor *> &get_sensors() { return this->sensors_; }
210  sensor::Sensor *get_sensor_by_key(uint32_t key, bool include_internal = false) {
211  for (auto *obj : this->sensors_)
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_TEXT_SENSOR
218  const std::vector<text_sensor::TextSensor *> &get_text_sensors() { return this->text_sensors_; }
219  text_sensor::TextSensor *get_text_sensor_by_key(uint32_t key, bool include_internal = false) {
220  for (auto *obj : this->text_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_FAN
227  const std::vector<fan::Fan *> &get_fans() { return this->fans_; }
228  fan::Fan *get_fan_by_key(uint32_t key, bool include_internal = false) {
229  for (auto *obj : this->fans_)
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_COVER
236  const std::vector<cover::Cover *> &get_covers() { return this->covers_; }
237  cover::Cover *get_cover_by_key(uint32_t key, bool include_internal = false) {
238  for (auto *obj : this->covers_)
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_LIGHT
245  const std::vector<light::LightState *> &get_lights() { return this->lights_; }
246  light::LightState *get_light_by_key(uint32_t key, bool include_internal = false) {
247  for (auto *obj : this->lights_)
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_CLIMATE
254  const std::vector<climate::Climate *> &get_climates() { return this->climates_; }
255  climate::Climate *get_climate_by_key(uint32_t key, bool include_internal = false) {
256  for (auto *obj : this->climates_)
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_NUMBER
263  const std::vector<number::Number *> &get_numbers() { return this->numbers_; }
264  number::Number *get_number_by_key(uint32_t key, bool include_internal = false) {
265  for (auto *obj : this->numbers_)
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_SELECT
272  const std::vector<select::Select *> &get_selects() { return this->selects_; }
273  select::Select *get_select_by_key(uint32_t key, bool include_internal = false) {
274  for (auto *obj : this->selects_)
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_LOCK
281  const std::vector<lock::Lock *> &get_locks() { return this->locks_; }
282  lock::Lock *get_lock_by_key(uint32_t key, bool include_internal = false) {
283  for (auto *obj : this->locks_)
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_MEDIA_PLAYER
290  const std::vector<media_player::MediaPlayer *> &get_media_players() { return this->media_players_; }
291  media_player::MediaPlayer *get_media_player_by_key(uint32_t key, bool include_internal = false) {
292  for (auto *obj : this->media_players_)
293  if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
294  return obj;
295  return nullptr;
296  }
297 #endif
298 
300 
301  protected:
302  friend Component;
303 
304  void register_component_(Component *comp);
305 
307 
308  void feed_wdt_arch_();
309 
310  std::vector<Component *> components_{};
311  std::vector<Component *> looping_components_{};
312 
313 #ifdef USE_BINARY_SENSOR
314  std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
315 #endif
316 #ifdef USE_SWITCH
317  std::vector<switch_::Switch *> switches_{};
318 #endif
319 #ifdef USE_BUTTON
320  std::vector<button::Button *> buttons_{};
321 #endif
322 #ifdef USE_SENSOR
323  std::vector<sensor::Sensor *> sensors_{};
324 #endif
325 #ifdef USE_TEXT_SENSOR
326  std::vector<text_sensor::TextSensor *> text_sensors_{};
327 #endif
328 #ifdef USE_FAN
329  std::vector<fan::Fan *> fans_{};
330 #endif
331 #ifdef USE_COVER
332  std::vector<cover::Cover *> covers_{};
333 #endif
334 #ifdef USE_CLIMATE
335  std::vector<climate::Climate *> climates_{};
336 #endif
337 #ifdef USE_LIGHT
338  std::vector<light::LightState *> lights_{};
339 #endif
340 #ifdef USE_NUMBER
341  std::vector<number::Number *> numbers_{};
342 #endif
343 #ifdef USE_SELECT
344  std::vector<select::Select *> selects_{};
345 #endif
346 #ifdef USE_LOCK
347  std::vector<lock::Lock *> locks_{};
348 #endif
349 #ifdef USE_MEDIA_PLAYER
350  std::vector<media_player::MediaPlayer *> media_players_{};
351 #endif
352 
353  std::string name_;
354  std::string friendly_name_;
355  std::string comment_;
356  std::string compilation_time_;
358  uint32_t last_loop_{0};
359  uint32_t loop_interval_{16};
360  size_t dump_config_at_{SIZE_MAX};
361  uint32_t app_state_{0};
362 };
363 
365 extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
366 
367 } // namespace esphome
Base class for all switches.
Definition: switch.h:32
std::vector< light::LightState * > lights_
Definition: application.h:338
void register_fan(fan::Fan *state)
Definition: application.h:98
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:291
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:219
void register_button(button::Button *button)
Definition: application.h:90
void register_light(light::LightState *light)
Definition: application.h:110
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:254
void register_climate(climate::Climate *climate)
Definition: application.h:106
climate::Climate * get_climate_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:255
std::string compilation_time_
Definition: application.h:356
void register_media_player(media_player::MediaPlayer *media_player)
Definition: application.h:126
std::vector< binary_sensor::BinarySensor * > binary_sensors_
Definition: application.h:314
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:273
void register_text_sensor(text_sensor::TextSensor *sensor)
Definition: application.h:94
void register_sensor(sensor::Sensor *sensor)
Definition: application.h:82
binary_sensor::BinarySensor * get_binary_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:183
Base class for all buttons.
Definition: button.h:29
C * register_component(C *c)
Register the component in this Application instance.
Definition: application.h:130
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:282
std::vector< number::Number * > numbers_
Definition: application.h:341
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:201
std::vector< lock::Lock * > locks_
Definition: application.h:347
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:146
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:227
void setup()
Set up all the registered components. Call this at the end of your setup() function.
Definition: application.cpp:28
const std::string & get_comment() const
Get the comment of this Application set by pre_setup().
Definition: application.h:148
void register_cover(cover::Cover *cover)
Definition: application.h:102
void register_number(number::Number *number)
Definition: application.h:114
std::vector< cover::Cover * > covers_
Definition: application.h:332
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:237
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:281
std::string friendly_name_
Definition: application.h:354
std::vector< media_player::MediaPlayer * > media_players_
Definition: application.h:350
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:228
void loop()
Make a loop iteration. Call this in your loop() function.
Definition: application.cpp:66
std::vector< sensor::Sensor * > sensors_
Definition: application.h:323
std::vector< button::Button * > buttons_
Definition: application.h:320
const std::vector< button::Button * > & get_buttons()
Definition: application.h:200
std::vector< climate::Climate * > climates_
Definition: application.h:335
std::vector< text_sensor::TextSensor * > text_sensors_
Definition: application.h:326
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:191
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:454
void register_select(select::Select *select)
Definition: application.h:118
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:218
void calculate_looping_components_()
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:209
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:182
std::vector< Component * > components_
Definition: application.h:310
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
Definition: application.h:76
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:246
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:143
switch_::Switch * get_switch_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:192
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:150
std::vector< Component * > looping_components_
Definition: application.h:311
std::vector< fan::Fan * > fans_
Definition: application.h:329
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:236
const std::string & get_compilation_time() const
Definition: application.h:152
std::string comment_
Definition: application.h:355
void register_switch(switch_::Switch *a_switch)
Definition: application.h:86
std::vector< select::Select * > selects_
Definition: application.h:344
const std::vector< light::LightState * > & get_lights()
Definition: application.h:245
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:264
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:167
sensor::Sensor * get_sensor_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:210
uint32_t get_app_state() const
Definition: application.h:179
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:290
Base-class for all selects.
Definition: select.h:24
Definition: a4988.cpp:4
void pre_setup(const std::string &name, const std::string &friendly_name, const std::string &comment, const char *compilation_time, bool name_add_mac_suffix)
Definition: application.h:56
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void arch_init()
Definition: core.cpp:38
const std::vector< select::Select * > & get_selects()
Definition: application.h:272
Base-class for all sensors.
Definition: sensor.h:57
std::vector< switch_::Switch * > switches_
Definition: application.h:317
const std::vector< number::Number * > & get_numbers()
Definition: application.h:263
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:122