ESPHome  2022.11.3
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_SELECT
207  if (this->at_ >= App.get_selects().size()) {
208  advance_platform = true;
209  } else {
210  auto *select = App.get_selects()[this->at_];
211  if (select->is_internal() && !this->include_internal_) {
212  success = true;
213  break;
214  } else {
215  success = this->on_select(select);
216  }
217  }
218  break;
219 #endif
220 #ifdef USE_LOCK
221  case IteratorState::LOCK:
222  if (this->at_ >= App.get_locks().size()) {
223  advance_platform = true;
224  } else {
225  auto *a_lock = App.get_locks()[this->at_];
226  if (a_lock->is_internal() && !this->include_internal_) {
227  success = true;
228  break;
229  } else {
230  success = this->on_lock(a_lock);
231  }
232  }
233  break;
234 #endif
235 #ifdef USE_MEDIA_PLAYER
237  if (this->at_ >= App.get_media_players().size()) {
238  advance_platform = true;
239  } else {
240  auto *media_player = App.get_media_players()[this->at_];
241  if (media_player->is_internal() && !this->include_internal_) {
242  success = true;
243  break;
244  } else {
245  success = this->on_media_player(media_player);
246  }
247  }
248  break;
249 #endif
250  case IteratorState::MAX:
251  if (this->on_end()) {
252  this->state_ = IteratorState::NONE;
253  }
254  return;
255  }
256 
257  if (advance_platform) {
258  this->state_ = static_cast<IteratorState>(static_cast<uint32_t>(this->state_) + 1);
259  this->at_ = 0;
260  } else if (success) {
261  this->at_++;
262  }
263 }
264 bool ComponentIterator::on_end() { return true; }
265 bool ComponentIterator::on_begin() { return true; }
266 #ifdef USE_API
268 #endif
269 #ifdef USE_ESP32_CAMERA
271 #endif
272 #ifdef USE_MEDIA_PLAYER
274 #endif
275 } // namespace esphome
virtual bool on_lock(lock::Lock *a_lock)=0
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:104
const std::vector< climate::Climate * > & get_climates()
Definition: application.h:241
virtual bool on_button(button::Button *button)=0
virtual bool on_select(select::Select *select)=0
const std::vector< fan::Fan * > & get_fans()
Definition: application.h:214
virtual bool on_climate(climate::Climate *climate)=0
virtual bool on_light(light::LightState *light)=0
const std::vector< lock::Lock * > & get_locks()
Definition: application.h:268
virtual bool on_media_player(media_player::MediaPlayer *media_player)
const std::vector< button::Button * > & get_buttons()
Definition: application.h:187
ESP32Camera * global_esp32_camera
const std::vector< switch_::Switch * > & get_switches()
Definition: application.h:178
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)
virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor)=0
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
Definition: application.h:205
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
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< cover::Cover * > & get_covers()
Definition: application.h:223
virtual bool on_cover(cover::Cover *cover)=0
const std::vector< light::LightState * > & get_lights()
Definition: application.h:232
const std::vector< media_player::MediaPlayer * > & get_media_players()
Definition: application.h:277
Definition: a4988.cpp:4
virtual bool on_service(api::UserServiceDescriptor *service)
virtual bool on_number(number::Number *number)=0
const std::vector< select::Select * > & get_selects()
Definition: application.h:259
const std::vector< number::Number * > & get_numbers()
Definition: application.h:250
APIServer * global_api_server
Definition: api_server.cpp:286