ESPHome 2026.5.0-dev
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
26 public:
27 void begin(bool include_internal = false);
28 void advance();
29 bool completed() const { return this->state_ == IteratorState::NONE; }
30 virtual bool on_begin();
31// Pure virtual entity callbacks (generated from entity_types.h)
32// NOLINTBEGIN(bugprone-macro-parentheses)
33#define ENTITY_TYPE_(type, singular, plural, count, upper) virtual bool on_##singular(type *obj) = 0;
34#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
35 ENTITY_TYPE_(type, singular, plural, count, upper)
37#undef ENTITY_TYPE_
38#undef ENTITY_CONTROLLER_TYPE_
39// NOLINTEND(bugprone-macro-parentheses)
40// Non-entity and non-pure-virtual callbacks (have default implementations)
41#ifdef USE_API_USER_DEFINED_ACTIONS
42 virtual bool on_service(api::UserServiceDescriptor *service);
43#endif
44#ifdef USE_CAMERA
45 virtual bool on_camera(camera::Camera *camera);
46#endif
47 virtual bool on_end();
48
49 protected:
50 // Iterates over all ESPHome entities (sensors, switches, lights, etc.)
51 // Supports up to 256 entity types and up to 65,535 entities of each type
52 enum class IteratorState : uint8_t {
53 NONE = 0,
54 BEGIN,
55// Entity iterator states (generated from entity_types.h)
56// NOLINTBEGIN(bugprone-macro-parentheses)
57#define ENTITY_TYPE_(type, singular, plural, count, upper) upper,
58#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) upper,
60#undef ENTITY_TYPE_
61#undef ENTITY_CONTROLLER_TYPE_
62// NOLINTEND(bugprone-macro-parentheses)
63#ifdef USE_API_USER_DEFINED_ACTIONS
64 SERVICE,
65#endif
66#ifdef USE_CAMERA
67 CAMERA,
68#endif
69 MAX,
70 };
71 uint16_t at_{0}; // Supports up to 65,535 entities per type
73 bool include_internal_{false};
74
75 template<typename Container>
76 void process_platform_item_(const Container &items,
77 bool (ComponentIterator::*on_item)(typename Container::value_type)) {
78 if (this->at_ >= items.size()) {
79 this->advance_platform_();
80 } else {
81 typename Container::value_type item = items[this->at_];
82 if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) {
83 this->at_++;
84 }
85 }
86 }
87
88 void advance_platform_();
89};
90
91} // namespace esphome
void process_platform_item_(const Container &items, bool(ComponentIterator::*on_item)(typename Container::value_type))
void begin(bool include_internal=false)
virtual bool on_service(api::UserServiceDescriptor *service)
virtual bool on_camera(camera::Camera *camera)
Abstract camera base class.
Definition camera.h:115
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7