ESPHome  2024.10.2
list_entities.cpp
Go to the documentation of this file.
1 #include "list_entities.h"
2 #ifdef USE_WEBSERVER
4 #include "esphome/core/log.h"
5 #include "esphome/core/util.h"
6 
7 #include "web_server.h"
8 
9 namespace esphome {
10 namespace web_server {
11 
12 ListEntitiesIterator::ListEntitiesIterator(WebServer *web_server) : web_server_(web_server) {}
13 
14 #ifdef USE_BINARY_SENSOR
16  if (this->web_server_->events_.count() == 0)
17  return true;
18  this->web_server_->events_.send(
19  this->web_server_->binary_sensor_json(binary_sensor, binary_sensor->state, DETAIL_ALL).c_str(), "state");
20  return true;
21 }
22 #endif
23 #ifdef USE_COVER
25  if (this->web_server_->events_.count() == 0)
26  return true;
27  this->web_server_->events_.send(this->web_server_->cover_json(cover, DETAIL_ALL).c_str(), "state");
28  return true;
29 }
30 #endif
31 #ifdef USE_FAN
33  if (this->web_server_->events_.count() == 0)
34  return true;
35  this->web_server_->events_.send(this->web_server_->fan_json(fan, DETAIL_ALL).c_str(), "state");
36  return true;
37 }
38 #endif
39 #ifdef USE_LIGHT
41  if (this->web_server_->events_.count() == 0)
42  return true;
43  this->web_server_->events_.send(this->web_server_->light_json(light, DETAIL_ALL).c_str(), "state");
44  return true;
45 }
46 #endif
47 #ifdef USE_SENSOR
49  if (this->web_server_->events_.count() == 0)
50  return true;
51  this->web_server_->events_.send(this->web_server_->sensor_json(sensor, sensor->state, DETAIL_ALL).c_str(), "state");
52  return true;
53 }
54 #endif
55 #ifdef USE_SWITCH
57  if (this->web_server_->events_.count() == 0)
58  return true;
59  this->web_server_->events_.send(this->web_server_->switch_json(a_switch, a_switch->state, DETAIL_ALL).c_str(),
60  "state");
61  return true;
62 }
63 #endif
64 #ifdef USE_BUTTON
66  if (this->web_server_->events_.count() == 0)
67  return true;
68  this->web_server_->events_.send(this->web_server_->button_json(button, DETAIL_ALL).c_str(), "state");
69  return true;
70 }
71 #endif
72 #ifdef USE_TEXT_SENSOR
74  if (this->web_server_->events_.count() == 0)
75  return true;
76  this->web_server_->events_.send(
77  this->web_server_->text_sensor_json(text_sensor, text_sensor->state, DETAIL_ALL).c_str(), "state");
78  return true;
79 }
80 #endif
81 #ifdef USE_LOCK
83  if (this->web_server_->events_.count() == 0)
84  return true;
85  this->web_server_->events_.send(this->web_server_->lock_json(a_lock, a_lock->state, DETAIL_ALL).c_str(), "state");
86  return true;
87 }
88 #endif
89 
90 #ifdef USE_VALVE
92  if (this->web_server_->events_.count() == 0)
93  return true;
94  this->web_server_->events_.send(this->web_server_->valve_json(valve, DETAIL_ALL).c_str(), "state");
95  return true;
96 }
97 #endif
98 
99 #ifdef USE_CLIMATE
101  if (this->web_server_->events_.count() == 0)
102  return true;
103  this->web_server_->events_.send(this->web_server_->climate_json(climate, DETAIL_ALL).c_str(), "state");
104  return true;
105 }
106 #endif
107 
108 #ifdef USE_NUMBER
110  if (this->web_server_->events_.count() == 0)
111  return true;
112  this->web_server_->events_.send(this->web_server_->number_json(number, number->state, DETAIL_ALL).c_str(), "state");
113  return true;
114 }
115 #endif
116 
117 #ifdef USE_DATETIME_DATE
119  if (this->web_server_->events_.count() == 0)
120  return true;
121  this->web_server_->events_.send(this->web_server_->date_json(date, DETAIL_ALL).c_str(), "state");
122  return true;
123 }
124 #endif
125 
126 #ifdef USE_DATETIME_TIME
128  this->web_server_->events_.send(this->web_server_->time_json(time, DETAIL_ALL).c_str(), "state");
129  return true;
130 }
131 #endif
132 
133 #ifdef USE_DATETIME_DATETIME
135  if (this->web_server_->events_.count() == 0)
136  return true;
137  this->web_server_->events_.send(this->web_server_->datetime_json(datetime, DETAIL_ALL).c_str(), "state");
138  return true;
139 }
140 #endif
141 
142 #ifdef USE_TEXT
144  if (this->web_server_->events_.count() == 0)
145  return true;
146  this->web_server_->events_.send(this->web_server_->text_json(text, text->state, DETAIL_ALL).c_str(), "state");
147  return true;
148 }
149 #endif
150 
151 #ifdef USE_SELECT
153  if (this->web_server_->events_.count() == 0)
154  return true;
155  this->web_server_->events_.send(this->web_server_->select_json(select, select->state, DETAIL_ALL).c_str(), "state");
156  return true;
157 }
158 #endif
159 
160 #ifdef USE_ALARM_CONTROL_PANEL
162  if (this->web_server_->events_.count() == 0)
163  return true;
164  this->web_server_->events_.send(
165  this->web_server_->alarm_control_panel_json(a_alarm_control_panel, a_alarm_control_panel->get_state(), DETAIL_ALL)
166  .c_str(),
167  "state");
168  return true;
169 }
170 #endif
171 
172 #ifdef USE_EVENT
174  // Null event type, since we are just iterating over entities
175  const std::string null_event_type = "";
176  this->web_server_->events_.send(this->web_server_->event_json(event, null_event_type, DETAIL_ALL).c_str(), "state");
177  return true;
178 }
179 #endif
180 
181 #ifdef USE_UPDATE
183  if (this->web_server_->events_.count() == 0)
184  return true;
185  this->web_server_->events_.send(this->web_server_->update_json(update, DETAIL_ALL).c_str(), "state");
186  return true;
187 }
188 #endif
189 
190 } // namespace web_server
191 } // namespace esphome
192 #endif
Base class for all switches.
Definition: switch.h:39
bool on_sensor(sensor::Sensor *sensor) override
AlarmControlPanelState get_state() const
Get the state.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
bool on_event(event::Event *event) override
bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) override
std::string number_json(number::Number *obj, float value, JsonDetail start_config)
Dump the number state with its value as a JSON string.
Definition: web_server.cpp:765
std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config)
Dump the sensor state with its value as a JSON string.
Definition: web_server.cpp:243
Base class for all cover devices.
Definition: cover.h:111
std::string state
Definition: text.h:26
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:66
std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config)
Dump the select state with its value as a JSON string.
LockState state
The current reported state of the lock.
Definition: lock.h:122
bool on_button(button::Button *button) override
Base class for all buttons.
Definition: button.h:29
bool on_lock(lock::Lock *a_lock) override
bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) override
bool on_climate(climate::Climate *climate) override
bool on_text(text::Text *text) override
Base-class for all text inputs.
Definition: text.h:24
bool on_time(datetime::TimeEntity *time) override
std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config)
Dump the event details with its value as a JSON string.
bool on_switch(switch_::Switch *a_switch) override
std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config)
Dump the text sensor state with its value as a JSON string.
Definition: web_server.cpp:291
std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config)
Dump the text state with its value as a JSON string.
std::string update_json(update::UpdateEntity *obj, JsonDetail start_config)
Dump the update state with its value as a JSON string.
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
Base-class for all numbers.
Definition: number.h:39
bool on_select(select::Select *select) override
std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config)
Dump the time state with its value as a JSON string.
Definition: web_server.cpp:904
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config)
Dump the switch state with its value as a JSON string.
Definition: web_server.cpp:342
bool on_valve(valve::Valve *valve) override
std::string light_json(light::LightState *obj, JsonDetail start_config)
Dump the light state as a JSON string.
Definition: web_server.cpp:618
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:40
std::string valve_json(valve::Valve *obj, JsonDetail start_config)
Dump the valve state as a JSON string.
std::string button_json(button::Button *obj, JsonDetail start_config)
Dump the button details with its value as a JSON string.
Definition: web_server.cpp:382
bool on_cover(cover::Cover *cover) override
std::string state
Definition: select.h:33
bool on_fan(fan::Fan *fan) override
std::string date_json(datetime::DateEntity *obj, JsonDetail start_config)
Dump the date state with its value as a JSON string.
Definition: web_server.cpp:843
std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config)
Dump the datetime state with its value as a JSON string.
Definition: web_server.cpp:965
bool on_update(update::UpdateEntity *update) override
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
std::string fan_json(fan::Fan *obj, JsonDetail start_config)
Dump the fan state as a JSON string.
Definition: web_server.cpp:499
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool on_text_sensor(text_sensor::TextSensor *text_sensor) override
Base-class for all sensors.
Definition: sensor.h:57
std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj, alarm_control_panel::AlarmControlPanelState value, JsonDetail start_config)
Dump the alarm_control_panel state with its value as a JSON string.
bool on_datetime(datetime::DateTimeEntity *datetime) override
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config)
Dump the lock state with its value as a JSON string.
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
bool on_light(light::LightState *light) override
std::string climate_json(climate::Climate *obj, JsonDetail start_config)
Dump the climate details.
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config)
Dump the binary sensor state with its value as a JSON string.
Definition: web_server.cpp:420
bool on_number(number::Number *number) override
bool on_date(datetime::DateEntity *date) override
std::string cover_json(cover::Cover *obj, JsonDetail start_config)
Dump the cover state as a JSON string.
Definition: web_server.cpp:703