ESPHome  2024.11.1
component_iterator.cpp
Go to the documentation of this file.
1 #include "component_iterator.h"
2 
4 
5 #ifdef USE_API
8 #endif
9 
10 namespace esphome {
11 
12 void ComponentIterator::begin(bool include_internal) {
13  this->state_ = IteratorState::BEGIN;
14  this->at_ = 0;
15  this->include_internal_ = include_internal;
16 }
18  bool advance_platform = false;
19  bool success = true;
20  switch (this->state_) {
22  // not started
23  return;
25  if (this->on_begin()) {
26  advance_platform = true;
27  } else {
28  return;
29  }
30  break;
31 #ifdef USE_BINARY_SENSOR
33  if (this->at_ >= App.get_binary_sensors().size()) {
34  advance_platform = true;
35  } else {
36  auto *binary_sensor = App.get_binary_sensors()[this->at_];
37  if (binary_sensor->is_internal() && !this->include_internal_) {
38  success = true;
39  break;
40  } else {
41  success = this->on_binary_sensor(binary_sensor);
42  }
43  }
44  break;
45 #endif
46 #ifdef USE_COVER
48  if (this->at_ >= App.get_covers().size()) {
49  advance_platform = true;
50  } else {
51  auto *cover = App.get_covers()[this->at_];
52  if (cover->is_internal() && !this->include_internal_) {
53  success = true;
54  break;
55  } else {
56  success = this->on_cover(cover);
57  }
58  }
59  break;
60 #endif
61 #ifdef USE_FAN
62  case IteratorState::FAN:
63  if (this->at_ >= App.get_fans().size()) {
64  advance_platform = true;
65  } else {
66  auto *fan = App.get_fans()[this->at_];
67  if (fan->is_internal() && !this->include_internal_) {
68  success = true;
69  break;
70  } else {
71  success = this->on_fan(fan);
72  }
73  }
74  break;
75 #endif
76 #ifdef USE_LIGHT
78  if (this->at_ >= App.get_lights().size()) {
79  advance_platform = true;
80  } else {
81  auto *light = App.get_lights()[this->at_];
82  if (light->is_internal() && !this->include_internal_) {
83  success = true;
84  break;
85  } else {
86  success = this->on_light(light);
87  }
88  }
89  break;
90 #endif
91 #ifdef USE_SENSOR
93  if (this->at_ >= App.get_sensors().size()) {
94  advance_platform = true;
95  } else {
96  auto *sensor = App.get_sensors()[this->at_];
97  if (sensor->is_internal() && !this->include_internal_) {
98  success = true;
99  break;
100  } else {
101  success = this->on_sensor(sensor);
102  }
103  }
104  break;
105 #endif
106 #ifdef USE_SWITCH
108  if (this->at_ >= App.get_switches().size()) {
109  advance_platform = true;
110  } else {
111  auto *a_switch = App.get_switches()[this->at_];
112  if (a_switch->is_internal() && !this->include_internal_) {
113  success = true;
114  break;
115  } else {
116  success = this->on_switch(a_switch);
117  }
118  }
119  break;
120 #endif
121 #ifdef USE_BUTTON
123  if (this->at_ >= App.get_buttons().size()) {
124  advance_platform = true;
125  } else {
126  auto *button = App.get_buttons()[this->at_];
127  if (button->is_internal() && !this->include_internal_) {
128  success = true;
129  break;
130  } else {
131  success = this->on_button(button);
132  }
133  }
134  break;
135 #endif
136 #ifdef USE_TEXT_SENSOR
138  if (this->at_ >= App.get_text_sensors().size()) {
139  advance_platform = true;
140  } else {
141  auto *text_sensor = App.get_text_sensors()[this->at_];
142  if (text_sensor->is_internal() && !this->include_internal_) {
143  success = true;
144  break;
145  } else {
146  success = this->on_text_sensor(text_sensor);
147  }
148  }
149  break;
150 #endif
151 #ifdef USE_API
153  if (this->at_ >= api::global_api_server->get_user_services().size()) {
154  advance_platform = true;
155  } else {
156  auto *service = api::global_api_server->get_user_services()[this->at_];
157  success = this->on_service(service);
158  }
159  break;
160 #endif
161 #ifdef USE_ESP32_CAMERA
163  if (esp32_camera::global_esp32_camera == nullptr) {
164  advance_platform = true;
165  } else {
166  if (esp32_camera::global_esp32_camera->is_internal() && !this->include_internal_) {
167  advance_platform = success = true;
168  break;
169  } else {
170  advance_platform = success = this->on_camera(esp32_camera::global_esp32_camera);
171  }
172  }
173  break;
174 #endif
175 #ifdef USE_CLIMATE
177  if (this->at_ >= App.get_climates().size()) {
178  advance_platform = true;
179  } else {
180  auto *climate = App.get_climates()[this->at_];
181  if (climate->is_internal() && !this->include_internal_) {
182  success = true;
183  break;
184  } else {
185  success = this->on_climate(climate);
186  }
187  }
188  break;
189 #endif
190 #ifdef USE_NUMBER
192  if (this->at_ >= App.get_numbers().size()) {
193  advance_platform = true;
194  } else {
195  auto *number = App.get_numbers()[this->at_];
196  if (number->is_internal() && !this->include_internal_) {
197  success = true;
198  break;
199  } else {
200  success = this->on_number(number);
201  }
202  }
203  break;
204 #endif
205 #ifdef USE_DATETIME_DATE
207  if (this->at_ >= App.get_dates().size()) {
208  advance_platform = true;
209  } else {
210  auto *date = App.get_dates()[this->at_];
211  if (date->is_internal() && !this->include_internal_) {
212  success = true;
213  break;
214  } else {
215  success = this->on_date(date);
216  }
217  }
218  break;
219 #endif
220 #ifdef USE_DATETIME_TIME
222  if (this->at_ >= App.get_times().size()) {
223  advance_platform = true;
224  } else {
225  auto *time = App.get_times()[this->at_];
226  if (time->is_internal() && !this->include_internal_) {
227  success = true;
228  break;
229  } else {
230  success = this->on_time(time);
231  }
232  }
233  break;
234 #endif
235 #ifdef USE_DATETIME_DATETIME
237  if (this->at_ >= App.get_datetimes().size()) {
238  advance_platform = true;
239  } else {
240  auto *datetime = App.get_datetimes()[this->at_];
241  if (datetime->is_internal() && !this->include_internal_) {
242  success = true;
243  break;
244  } else {
245  success = this->on_datetime(datetime);
246  }
247  }
248  break;
249 #endif
250 #ifdef USE_TEXT
251  case IteratorState::TEXT:
252  if (this->at_ >= App.get_texts().size()) {
253  advance_platform = true;
254  } else {
255  auto *text = App.get_texts()[this->at_];
256  if (text->is_internal() && !this->include_internal_) {
257  success = true;
258  break;
259  } else {
260  success = this->on_text(text);
261  }
262  }
263  break;
264 #endif
265 #ifdef USE_SELECT
267  if (this->at_ >= App.get_selects().size()) {
268  advance_platform = true;
269  } else {
270  auto *select = App.get_selects()[this->at_];
271  if (select->is_internal() && !this->include_internal_) {
272  success = true;
273  break;
274  } else {
275  success = this->on_select(select);
276  }
277  }
278  break;
279 #endif
280 #ifdef USE_LOCK
281  case IteratorState::LOCK:
282  if (this->at_ >= App.get_locks().size()) {
283  advance_platform = true;
284  } else {
285  auto *a_lock = App.get_locks()[this->at_];
286  if (a_lock->is_internal() && !this->include_internal_) {
287  success = true;
288  break;
289  } else {
290  success = this->on_lock(a_lock);
291  }
292  }
293  break;
294 #endif
295 #ifdef USE_VALVE
297  if (this->at_ >= App.get_valves().size()) {
298  advance_platform = true;
299  } else {
300  auto *valve = App.get_valves()[this->at_];
301  if (valve->is_internal() && !this->include_internal_) {
302  success = true;
303  break;
304  } else {
305  success = this->on_valve(valve);
306  }
307  }
308  break;
309 #endif
310 #ifdef USE_MEDIA_PLAYER
312  if (this->at_ >= App.get_media_players().size()) {
313  advance_platform = true;
314  } else {
315  auto *media_player = App.get_media_players()[this->at_];
316  if (media_player->is_internal() && !this->include_internal_) {
317  success = true;
318  break;
319  } else {
320  success = this->on_media_player(media_player);
321  }
322  }
323  break;
324 #endif
325 #ifdef USE_ALARM_CONTROL_PANEL
327  if (this->at_ >= App.get_alarm_control_panels().size()) {
328  advance_platform = true;
329  } else {
330  auto *a_alarm_control_panel = App.get_alarm_control_panels()[this->at_];
331  if (a_alarm_control_panel->is_internal() && !this->include_internal_) {
332  success = true;
333  break;
334  } else {
335  success = this->on_alarm_control_panel(a_alarm_control_panel);
336  }
337  }
338  break;
339 #endif
340 #ifdef USE_EVENT
342  if (this->at_ >= App.get_events().size()) {
343  advance_platform = true;
344  } else {
345  auto *event = App.get_events()[this->at_];
346  if (event->is_internal() && !this->include_internal_) {
347  success = true;
348  break;
349  } else {
350  success = this->on_event(event);
351  }
352  }
353  break;
354 #endif
355 #ifdef USE_UPDATE
357  if (this->at_ >= App.get_updates().size()) {
358  advance_platform = true;
359  } else {
360  auto *update = App.get_updates()[this->at_];
361  if (update->is_internal() && !this->include_internal_) {
362  success = true;
363  break;
364  } else {
365  success = this->on_update(update);
366  }
367  }
368  break;
369 #endif
370  case IteratorState::MAX:
371  if (this->on_end()) {
372  this->state_ = IteratorState::NONE;
373  }
374  return;
375  }
376 
377  if (advance_platform) {
378  this->state_ = static_cast<IteratorState>(static_cast<uint32_t>(this->state_) + 1);
379  this->at_ = 0;
380  } else if (success) {
381  this->at_++;
382  }
383 }
384 bool ComponentIterator::on_end() { return true; }
385 bool ComponentIterator::on_begin() { return true; }
386 #ifdef USE_API
388 #endif
389 #ifdef USE_ESP32_CAMERA
391 #endif
392 #ifdef USE_MEDIA_PLAYER
394 #endif
395 } // namespace esphome
virtual bool on_time(datetime::TimeEntity *time)=0
virtual bool on_update(update::UpdateEntity *update)=0
virtual bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)=0
const std::vector< datetime::DateTimeEntity * > & get_datetimes()
Definition: application.h:367
virtual bool on_lock(lock::Lock *a_lock)=0
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:124
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:327
const std::vector< update::UpdateEntity * > & get_updates()
Definition: application.h:452
const std::vector< alarm_control_panel::AlarmControlPanel * > & get_alarm_control_panels()
Definition: application.h:428
const std::vector< valve::Valve * > & get_valves()
Definition: application.h:407
virtual bool on_button(button::Button *button)=0
const std::vector< event::Event * > & get_events()
Definition: application.h:441
virtual bool on_text(text::Text *text)=0
virtual bool on_select(select::Select *select)=0
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:297
virtual bool on_date(datetime::DateEntity *date)=0
virtual bool on_valve(valve::Valve *valve)=0
virtual bool on_climate(climate::Climate *climate)=0
virtual bool on_light(light::LightState *light)=0
const std::vector< datetime::TimeEntity * > & get_times()
Definition: application.h:357
virtual bool on_event(event::Event *event)=0
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:397
virtual bool on_media_player(media_player::MediaPlayer *media_player)
const std::vector< button::Button * > & get_buttons()
Definition: application.h:267
ESP32Camera * global_esp32_camera
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:257
virtual bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor)=0
virtual bool on_fan(fan::Fan *fan)=0
virtual bool on_camera(esp32_camera::ESP32Camera *camera)
bool is_internal() const
Definition: entity_base.cpp:22
virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor)=0
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:287
const std::vector< sensor::Sensor * > & get_sensors()
Definition: application.h:277
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
Definition: application.h:247
virtual bool on_sensor(sensor::Sensor *sensor)=0
virtual bool on_switch(switch_::Switch *a_switch)=0
void begin(bool include_internal=false)
const std::vector< text::Text * > & get_texts()
Definition: application.h:377
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:41
const std::vector< cover::Cover * > & get_covers()
Definition: application.h:307
virtual bool on_cover(cover::Cover *cover)=0
const std::vector< light::LightState * > & get_lights()
Definition: application.h:317
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:417
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool on_service(api::UserServiceDescriptor *service)
virtual bool on_number(number::Number *number)=0
const std::vector< datetime::DateEntity * > & get_dates()
Definition: application.h:347
const std::vector< select::Select * > & get_selects()
Definition: application.h:387
const std::vector< number::Number * > & get_numbers()
Definition: application.h:337
virtual bool on_datetime(datetime::DateTimeEntity *datetime)=0
APIServer * global_api_server
Definition: api_server.cpp:347
esphome::sensor::Sensor * sensor
Definition: statsd.h:38