ESPHome 2026.2.3
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#ifdef USE_BINARY_SENSOR
32 virtual bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) = 0;
33#endif
34#ifdef USE_COVER
35 virtual bool on_cover(cover::Cover *cover) = 0;
36#endif
37#ifdef USE_FAN
38 virtual bool on_fan(fan::Fan *fan) = 0;
39#endif
40#ifdef USE_LIGHT
41 virtual bool on_light(light::LightState *light) = 0;
42#endif
43#ifdef USE_SENSOR
44 virtual bool on_sensor(sensor::Sensor *sensor) = 0;
45#endif
46#ifdef USE_SWITCH
47 virtual bool on_switch(switch_::Switch *a_switch) = 0;
48#endif
49#ifdef USE_BUTTON
50 virtual bool on_button(button::Button *button) = 0;
51#endif
52#ifdef USE_TEXT_SENSOR
53 virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor) = 0;
54#endif
55#ifdef USE_API_USER_DEFINED_ACTIONS
56 virtual bool on_service(api::UserServiceDescriptor *service);
57#endif
58#ifdef USE_CAMERA
59 virtual bool on_camera(camera::Camera *camera);
60#endif
61#ifdef USE_CLIMATE
62 virtual bool on_climate(climate::Climate *climate) = 0;
63#endif
64#ifdef USE_NUMBER
65 virtual bool on_number(number::Number *number) = 0;
66#endif
67#ifdef USE_DATETIME_DATE
68 virtual bool on_date(datetime::DateEntity *date) = 0;
69#endif
70#ifdef USE_DATETIME_TIME
71 virtual bool on_time(datetime::TimeEntity *time) = 0;
72#endif
73#ifdef USE_DATETIME_DATETIME
74 virtual bool on_datetime(datetime::DateTimeEntity *datetime) = 0;
75#endif
76#ifdef USE_TEXT
77 virtual bool on_text(text::Text *text) = 0;
78#endif
79#ifdef USE_SELECT
80 virtual bool on_select(select::Select *select) = 0;
81#endif
82#ifdef USE_LOCK
83 virtual bool on_lock(lock::Lock *a_lock) = 0;
84#endif
85#ifdef USE_VALVE
86 virtual bool on_valve(valve::Valve *valve) = 0;
87#endif
88#ifdef USE_MEDIA_PLAYER
89 virtual bool on_media_player(media_player::MediaPlayer *media_player);
90#endif
91#ifdef USE_ALARM_CONTROL_PANEL
92 virtual bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) = 0;
93#endif
94#ifdef USE_WATER_HEATER
95 virtual bool on_water_heater(water_heater::WaterHeater *water_heater) = 0;
96#endif
97#ifdef USE_INFRARED
98 virtual bool on_infrared(infrared::Infrared *infrared) = 0;
99#endif
100#ifdef USE_EVENT
101 virtual bool on_event(event::Event *event) = 0;
102#endif
103#ifdef USE_UPDATE
104 virtual bool on_update(update::UpdateEntity *update) = 0;
105#endif
106 virtual bool on_end();
107
108 protected:
109 // Iterates over all ESPHome entities (sensors, switches, lights, etc.)
110 // Supports up to 256 entity types and up to 65,535 entities of each type
111 enum class IteratorState : uint8_t {
112 NONE = 0,
113 BEGIN,
114#ifdef USE_BINARY_SENSOR
116#endif
117#ifdef USE_COVER
118 COVER,
119#endif
120#ifdef USE_FAN
121 FAN,
122#endif
123#ifdef USE_LIGHT
124 LIGHT,
125#endif
126#ifdef USE_SENSOR
127 SENSOR,
128#endif
129#ifdef USE_SWITCH
130 SWITCH,
131#endif
132#ifdef USE_BUTTON
133 BUTTON,
134#endif
135#ifdef USE_TEXT_SENSOR
137#endif
138#ifdef USE_API_USER_DEFINED_ACTIONS
139 SERVICE,
140#endif
141#ifdef USE_CAMERA
142 CAMERA,
143#endif
144#ifdef USE_CLIMATE
145 CLIMATE,
146#endif
147#ifdef USE_NUMBER
148 NUMBER,
149#endif
150#ifdef USE_DATETIME_DATE
152#endif
153#ifdef USE_DATETIME_TIME
155#endif
156#ifdef USE_DATETIME_DATETIME
158#endif
159#ifdef USE_TEXT
160 TEXT,
161#endif
162#ifdef USE_SELECT
163 SELECT,
164#endif
165#ifdef USE_LOCK
166 LOCK,
167#endif
168#ifdef USE_VALVE
169 VALVE,
170#endif
171#ifdef USE_MEDIA_PLAYER
173#endif
174#ifdef USE_ALARM_CONTROL_PANEL
176#endif
177#ifdef USE_WATER_HEATER
179#endif
180#ifdef USE_INFRARED
181 INFRARED,
182#endif
183#ifdef USE_EVENT
184 EVENT,
185#endif
186#ifdef USE_UPDATE
187 UPDATE,
188#endif
189 MAX,
190 };
191 uint16_t at_{0}; // Supports up to 65,535 entities per type
193 bool include_internal_{false};
194
195 template<typename Container>
196 void process_platform_item_(const Container &items,
197 bool (ComponentIterator::*on_item)(typename Container::value_type)) {
198 if (this->at_ >= items.size()) {
199 this->advance_platform_();
200 } else {
201 typename Container::value_type item = items[this->at_];
202 if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) {
203 this->at_++;
204 }
205 }
206 }
207
208 void advance_platform_();
209};
210
211} // namespace esphome
virtual bool on_text(text::Text *text)=0
virtual bool on_media_player(media_player::MediaPlayer *media_player)
void process_platform_item_(const Container &items, bool(ComponentIterator::*on_item)(typename Container::value_type))
virtual bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)=0
virtual bool on_date(datetime::DateEntity *date)=0
virtual bool on_cover(cover::Cover *cover)=0
virtual bool on_datetime(datetime::DateTimeEntity *datetime)=0
virtual bool on_select(select::Select *select)=0
void begin(bool include_internal=false)
virtual bool on_button(button::Button *button)=0
virtual bool on_water_heater(water_heater::WaterHeater *water_heater)=0
virtual bool on_sensor(sensor::Sensor *sensor)=0
virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor)=0
virtual bool on_valve(valve::Valve *valve)=0
virtual bool on_service(api::UserServiceDescriptor *service)
virtual bool on_fan(fan::Fan *fan)=0
virtual bool on_number(number::Number *number)=0
virtual bool on_infrared(infrared::Infrared *infrared)=0
virtual bool on_switch(switch_::Switch *a_switch)=0
virtual bool on_camera(camera::Camera *camera)
virtual bool on_climate(climate::Climate *climate)=0
virtual bool on_time(datetime::TimeEntity *time)=0
virtual bool on_light(light::LightState *light)=0
virtual bool on_event(event::Event *event)=0
virtual bool on_lock(lock::Lock *a_lock)=0
virtual bool on_update(update::UpdateEntity *update)=0
virtual bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor)=0
Base class for all binary_sensor-type classes.
Base class for all buttons.
Definition button.h:25
Abstract camera base class.
Definition camera.h:115
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:182
Base class for all cover devices.
Definition cover.h:110
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:110
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
Base class for all locks.
Definition lock.h:110
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:43
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:22
Base class for all valve devices.
Definition valve.h:104
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7