ESPHome 2026.8.2
Loading...
Searching...
No Matches
component_iterator.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_CAMERA
9#endif
10
11namespace esphome {
12
13#ifdef USE_API_USER_DEFINED_ACTIONS
14namespace api {
15class UserServiceDescriptor;
16} // namespace api
17#endif
18
19#ifdef USE_INFRARED
20namespace infrared {
21class Infrared;
22} // namespace infrared
23#endif
24#ifdef USE_RADIO_FREQUENCY
25namespace radio_frequency {
26class RadioFrequency;
27} // namespace radio_frequency
28#endif
29
31 public:
32 void begin(bool include_internal = false);
36 ESPHOME_ALWAYS_INLINE void try_advance(size_t max_steps) {
37 size_t steps = 0;
38 while (steps < max_steps && !this->completed()) {
39 this->yield_requested_ = false;
40 if (!this->advance_step_())
41 break;
42 steps++;
43 if (this->yield_requested_)
44 break;
45 }
46 }
47 // Remove before 2027.3.0
48 ESPDEPRECATED("Use try_advance() instead. Removed in 2027.3.0", "2026.8.1")
49 void advance() { this->try_advance(1); }
50 bool completed() const { return this->state_ == IteratorState::NONE; }
51 virtual bool on_begin();
52// Pure virtual entity callbacks (generated from entity_types.h)
53// NOLINTBEGIN(bugprone-macro-parentheses)
54#define ENTITY_TYPE_(type, singular, plural, count, upper) virtual bool on_##singular(type *obj) = 0;
55#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
56 ENTITY_TYPE_(type, singular, plural, count, upper)
58#undef ENTITY_TYPE_
59#undef ENTITY_CONTROLLER_TYPE_
60// NOLINTEND(bugprone-macro-parentheses)
61// Non-entity and non-pure-virtual callbacks (have default implementations)
62#ifdef USE_API_USER_DEFINED_ACTIONS
63 virtual bool on_service(api::UserServiceDescriptor *service);
64#endif
65#ifdef USE_CAMERA
66 virtual bool on_camera(camera::Camera *camera);
67#endif
68 virtual bool on_end();
69
70 protected:
71 // Iterates over all ESPHome entities (sensors, switches, lights, etc.)
72 // Supports up to 256 entity types and up to 65,535 entities of each type
73 enum class IteratorState : uint8_t {
74 NONE = 0,
75 BEGIN,
76// Entity iterator states (generated from entity_types.h)
77// NOLINTBEGIN(bugprone-macro-parentheses)
78#define ENTITY_TYPE_(type, singular, plural, count, upper) upper,
79#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) upper,
81#undef ENTITY_TYPE_
82#undef ENTITY_CONTROLLER_TYPE_
83// NOLINTEND(bugprone-macro-parentheses)
84#ifdef USE_API_USER_DEFINED_ACTIONS
85 SERVICE,
86#endif
87#ifdef USE_CAMERA
88 CAMERA,
89#endif
90 MAX,
91 };
94 void yield_after_step_() { this->yield_requested_ = true; }
95
96 uint16_t at_{0}; // Supports up to 65,535 entities per type
98 bool yield_requested_ : 1 {false};
99 bool include_internal_ : 1 {false};
100
101 template<typename Container>
102 bool process_platform_item_(const Container &items,
103 bool (ComponentIterator::*on_item)(typename Container::value_type)) {
104 if (this->at_ >= items.size()) {
105 this->advance_platform_();
106 return true;
107 }
108 typename Container::value_type item = items[this->at_];
109 if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) {
110 this->at_++;
111 return true;
112 }
113 return false;
114 }
115
118 bool advance_step_();
119
120 void advance_platform_();
121};
122
123} // namespace esphome
bool advance_step_()
One iteration step; false if no progress was made (callback refused or iterator not running).
void begin(bool include_internal=false)
ESPHOME_ALWAYS_INLINE void try_advance(size_t max_steps)
Run up to max_steps iteration steps; stops early when iteration completes or a callback refuses (that...
virtual bool on_service(api::UserServiceDescriptor *service)
void yield_after_step_()
End the current try_advance() pass after this step; lets callbacks that write directly to the socket ...
virtual bool on_camera(camera::Camera *camera)
ESPDEPRECATED("Use try_advance() instead. Removed in 2027.3.0", "2026.8.1") void advance()
bool process_platform_item_(const Container &items, bool(ComponentIterator::*on_item)(typename Container::value_type))
Abstract camera base class.
Definition camera.h:114