ESPHome  2024.5.4
list_entities.cpp
Go to the documentation of this file.
1 #include "list_entities.h"
3 #include "esphome/core/log.h"
4 #include "esphome/core/util.h"
5 
6 #include "web_server.h"
7 
8 namespace esphome {
9 namespace web_server {
10 
11 ListEntitiesIterator::ListEntitiesIterator(WebServer *web_server) : web_server_(web_server) {}
12 
13 #ifdef USE_BINARY_SENSOR
15  if (this->web_server_->events_.count() == 0)
16  return true;
17  this->web_server_->events_.send(
18  this->web_server_->binary_sensor_json(binary_sensor, binary_sensor->state, DETAIL_ALL).c_str(), "state");
19  return true;
20 }
21 #endif
22 #ifdef USE_COVER
24  if (this->web_server_->events_.count() == 0)
25  return true;
26  this->web_server_->events_.send(this->web_server_->cover_json(cover, DETAIL_ALL).c_str(), "state");
27  return true;
28 }
29 #endif
30 #ifdef USE_FAN
32  if (this->web_server_->events_.count() == 0)
33  return true;
34  this->web_server_->events_.send(this->web_server_->fan_json(fan, DETAIL_ALL).c_str(), "state");
35  return true;
36 }
37 #endif
38 #ifdef USE_LIGHT
40  if (this->web_server_->events_.count() == 0)
41  return true;
42  this->web_server_->events_.send(this->web_server_->light_json(light, DETAIL_ALL).c_str(), "state");
43  return true;
44 }
45 #endif
46 #ifdef USE_SENSOR
48  if (this->web_server_->events_.count() == 0)
49  return true;
50  this->web_server_->events_.send(this->web_server_->sensor_json(sensor, sensor->state, DETAIL_ALL).c_str(), "state");
51  return true;
52 }
53 #endif
54 #ifdef USE_SWITCH
56  if (this->web_server_->events_.count() == 0)
57  return true;
58  this->web_server_->events_.send(this->web_server_->switch_json(a_switch, a_switch->state, DETAIL_ALL).c_str(),
59  "state");
60  return true;
61 }
62 #endif
63 #ifdef USE_BUTTON
65  if (this->web_server_->events_.count() == 0)
66  return true;
67  this->web_server_->events_.send(this->web_server_->button_json(button, DETAIL_ALL).c_str(), "state");
68  return true;
69 }
70 #endif
71 #ifdef USE_TEXT_SENSOR
73  if (this->web_server_->events_.count() == 0)
74  return true;
75  this->web_server_->events_.send(
76  this->web_server_->text_sensor_json(text_sensor, text_sensor->state, DETAIL_ALL).c_str(), "state");
77  return true;
78 }
79 #endif
80 #ifdef USE_LOCK
82  if (this->web_server_->events_.count() == 0)
83  return true;
84  this->web_server_->events_.send(this->web_server_->lock_json(a_lock, a_lock->state, DETAIL_ALL).c_str(), "state");
85  return true;
86 }
87 #endif
88 
89 #ifdef USE_VALVE
91  if (this->web_server_->events_.count() == 0)
92  return true;
93  this->web_server_->events_.send(this->web_server_->valve_json(valve, DETAIL_ALL).c_str(), "state");
94  return true;
95 }
96 #endif
97 
98 #ifdef USE_CLIMATE
100  if (this->web_server_->events_.count() == 0)
101  return true;
102  this->web_server_->events_.send(this->web_server_->climate_json(climate, DETAIL_ALL).c_str(), "state");
103  return true;
104 }
105 #endif
106 
107 #ifdef USE_NUMBER
109  if (this->web_server_->events_.count() == 0)
110  return true;
111  this->web_server_->events_.send(this->web_server_->number_json(number, number->state, DETAIL_ALL).c_str(), "state");
112  return true;
113 }
114 #endif
115 
116 #ifdef USE_DATETIME_DATE
118  if (this->web_server_->events_.count() == 0)
119  return true;
120  this->web_server_->events_.send(this->web_server_->date_json(date, DETAIL_ALL).c_str(), "state");
121  return true;
122 }
123 #endif
124 
125 #ifdef USE_DATETIME_TIME
127  this->web_server_->events_.send(this->web_server_->time_json(time, DETAIL_ALL).c_str(), "state");
128  return true;
129 }
130 #endif
131 
132 #ifdef USE_DATETIME_DATETIME
134  if (this->web_server_->events_.count() == 0)
135  return true;
136  this->web_server_->events_.send(this->web_server_->datetime_json(datetime, DETAIL_ALL).c_str(), "state");
137  return true;
138 }
139 #endif
140 
141 #ifdef USE_TEXT
143  if (this->web_server_->events_.count() == 0)
144  return true;
145  this->web_server_->events_.send(this->web_server_->text_json(text, text->state, DETAIL_ALL).c_str(), "state");
146  return true;
147 }
148 #endif
149 
150 #ifdef USE_SELECT
152  if (this->web_server_->events_.count() == 0)
153  return true;
154  this->web_server_->events_.send(this->web_server_->select_json(select, select->state, DETAIL_ALL).c_str(), "state");
155  return true;
156 }
157 #endif
158 
159 #ifdef USE_ALARM_CONTROL_PANEL
161  if (this->web_server_->events_.count() == 0)
162  return true;
163  this->web_server_->events_.send(
164  this->web_server_->alarm_control_panel_json(a_alarm_control_panel, a_alarm_control_panel->get_state(), DETAIL_ALL)
165  .c_str(),
166  "state");
167  return true;
168 }
169 #endif
170 
171 #ifdef USE_EVENT
173  // Null event type, since we are just iterating over entities
174  const std::string null_event_type = "";
175  this->web_server_->events_.send(this->web_server_->event_json(event, null_event_type, DETAIL_ALL).c_str(), "state");
176  return true;
177 }
178 #endif
179 
180 } // namespace web_server
181 } // namespace esphome
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:854
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:437
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:53
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:472
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.
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:969
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:486
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:731
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:521
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:921
std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config)
Dump the datetime state with its value as a JSON string.
Base-class for all selects.
Definition: select.h:31
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:573
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.
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:549
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:805