ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
web_server_v1.cpp
Go to the documentation of this file.
1#include "web_server.h"
3
4#if USE_WEBSERVER_VERSION == 1
5
6namespace esphome::web_server {
7
8// Write HTML-escaped text to stream (escapes ", &, <, >)
9static void write_html_escaped(AsyncResponseStream *stream, const char *text) {
10 for (const char *p = text; *p; ++p) {
11 switch (*p) {
12 case '"':
13 stream->print("&quot;");
14 break;
15 case '&':
16 stream->print("&amp;");
17 break;
18 case '<':
19 stream->print("&lt;");
20 break;
21 case '>':
22 stream->print("&gt;");
23 break;
24 default:
25 stream->write(*p);
26 break;
27 }
28 }
29}
30
31void write_row(AsyncResponseStream *stream, EntityBase *obj, const std::string &klass, const std::string &action,
32 const std::function<void(AsyncResponseStream &stream, EntityBase *obj)> &action_func = nullptr) {
33 stream->print("<tr class=\"");
34 stream->print(klass.c_str());
35 if (obj->is_internal())
36 stream->print(" internal");
37 stream->print("\" id=\"");
38 stream->print(klass.c_str());
39 stream->print("-");
40 char object_id_buf[OBJECT_ID_MAX_LEN];
41 stream->print(obj->get_object_id_to(object_id_buf).c_str());
42 // Add data attributes for hierarchical URL support
43 stream->print("\" data-domain=\"");
44 stream->print(klass.c_str());
45 char name_buf[ENTITY_NAME_BUF_SIZE];
46 const char *name = obj->get_name_to(name_buf).c_str();
47 stream->print("\" data-name=\"");
48 write_html_escaped(stream, name);
49#ifdef USE_DEVICES
50 Device *device = obj->get_device();
51 if (device != nullptr) {
52 stream->print("\" data-device=\"");
53 write_html_escaped(stream, device->get_name());
54 }
55#endif
56 stream->print("\"><td>");
57#ifdef USE_DEVICES
58 if (device != nullptr) {
59 stream->print("[");
60 write_html_escaped(stream, device->get_name());
61 stream->print("] ");
62 }
63#endif
64 write_html_escaped(stream, name);
65 stream->print("</td><td></td><td>");
66 stream->print(action.c_str());
67 if (action_func) {
68 action_func(*stream, obj);
69 }
70 stream->print("</td>");
71 stream->print("</tr>");
72}
73
74void WebServer::handle_index_request(AsyncWebServerRequest *request) {
75 AsyncResponseStream *stream = request->beginResponseStream(ESPHOME_F("text/html"));
76 const auto &title = App.get_name();
77 stream->print(ESPHOME_F("<!DOCTYPE html><html lang=\"en\"><head><meta charset=UTF-8><meta "
78 "name=viewport content=\"width=device-width, initial-scale=1,user-scalable=no\"><title>"));
79 stream->print(title.c_str());
80 stream->print(ESPHOME_F("</title>"));
81#ifdef USE_WEBSERVER_CSS_INCLUDE
82 stream->print(ESPHOME_F("<link rel=\"stylesheet\" href=\"/0.css\">"));
83#endif
84 if (strlen(this->css_url_) > 0) {
85 stream->print(ESPHOME_F(R"(<link rel="stylesheet" href=")"));
86 stream->print(this->css_url_);
87 stream->print(ESPHOME_F("\">"));
88 }
89 stream->print(ESPHOME_F("</head><body>"));
90 stream->print(ESPHOME_F("<article class=\"markdown-body\"><h1>"));
91 stream->print(title.c_str());
92 stream->print(ESPHOME_F("</h1>"));
93 stream->print(ESPHOME_F("<h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
94
95#ifdef USE_SENSOR
96 for (auto *obj : App.get_sensors()) {
97 if (this->include_internal_ || !obj->is_internal())
98 write_row(stream, obj, "sensor", "");
99 }
100#endif
101
102#ifdef USE_SWITCH
103 for (auto *obj : App.get_switches()) {
104 if (this->include_internal_ || !obj->is_internal())
105 write_row(stream, obj, "switch", "<button>Toggle</button>");
106 }
107#endif
108
109#ifdef USE_BUTTON
110 for (auto *obj : App.get_buttons())
111 write_row(stream, obj, "button", "<button>Press</button>");
112#endif
113
114#ifdef USE_BINARY_SENSOR
115 for (auto *obj : App.get_binary_sensors()) {
116 if (this->include_internal_ || !obj->is_internal())
117 write_row(stream, obj, "binary_sensor", "");
118 }
119#endif
120
121#ifdef USE_FAN
122 for (auto *obj : App.get_fans()) {
123 if (this->include_internal_ || !obj->is_internal())
124 write_row(stream, obj, "fan", "<button>Toggle</button>");
125 }
126#endif
127
128#ifdef USE_LIGHT
129 for (auto *obj : App.get_lights()) {
130 if (this->include_internal_ || !obj->is_internal())
131 write_row(stream, obj, "light", "<button>Toggle</button>");
132 }
133#endif
134
135#ifdef USE_TEXT_SENSOR
136 for (auto *obj : App.get_text_sensors()) {
137 if (this->include_internal_ || !obj->is_internal())
138 write_row(stream, obj, "text_sensor", "");
139 }
140#endif
141
142#ifdef USE_COVER
143 for (auto *obj : App.get_covers()) {
144 if (this->include_internal_ || !obj->is_internal())
145 write_row(stream, obj, "cover", "<button>Open</button><button>Close</button>");
146 }
147#endif
148
149#ifdef USE_NUMBER
150 for (auto *obj : App.get_numbers()) {
151 if (this->include_internal_ || !obj->is_internal()) {
152 write_row(stream, obj, "number", "", [](AsyncResponseStream &stream, EntityBase *obj) {
153 number::Number *number = (number::Number *) obj;
154 stream.print(R"(<input type="number" min=")");
155 stream.print(number->traits.get_min_value());
156 stream.print(R"(" max=")");
157 stream.print(number->traits.get_max_value());
158 stream.print(R"(" step=")");
159 stream.print(number->traits.get_step());
160 stream.print(R"(" value=")");
161 stream.print(number->state);
162 stream.print(R"("/>)");
163 });
164 }
165 }
166#endif
167
168#ifdef USE_TEXT
169 for (auto *obj : App.get_texts()) {
170 if (this->include_internal_ || !obj->is_internal()) {
171 write_row(stream, obj, "text", "", [](AsyncResponseStream &stream, EntityBase *obj) {
172 text::Text *text = (text::Text *) obj;
173 auto mode = (int) text->traits.get_mode();
174 stream.print(R"(<input type=")");
175 if (mode == 2) {
176 stream.print(R"(password)");
177 } else { // default
178 stream.print(R"(text)");
179 }
180 stream.print(R"(" minlength=")");
181 stream.print(text->traits.get_min_length());
182 stream.print(R"(" maxlength=")");
183 stream.print(text->traits.get_max_length());
184 stream.print(R"(" pattern=")");
185 stream.print(text->traits.get_pattern_c_str());
186 stream.print(R"(" value=")");
187 stream.print(text->state.c_str());
188 stream.print(R"("/>)");
189 });
190 }
191 }
192#endif
193
194#ifdef USE_SELECT
195 for (auto *obj : App.get_selects()) {
196 if (this->include_internal_ || !obj->is_internal()) {
197 write_row(stream, obj, "select", "", [](AsyncResponseStream &stream, EntityBase *obj) {
198 select::Select *select = (select::Select *) obj;
199 stream.print("<select>");
200 stream.print("<option></option>");
201 for (auto const &option : select->traits.get_options()) {
202 stream.print("<option>");
203 stream.print(option);
204 stream.print("</option>");
205 }
206 stream.print("</select>");
207 });
208 }
209 }
210#endif
211
212#ifdef USE_LOCK
213 for (auto *obj : App.get_locks()) {
214 if (this->include_internal_ || !obj->is_internal()) {
215 write_row(stream, obj, "lock", "", [](AsyncResponseStream &stream, EntityBase *obj) {
216 lock::Lock *lock = (lock::Lock *) obj;
217 stream.print("<button>Lock</button><button>Unlock</button>");
218 if (lock->traits.get_supports_open()) {
219 stream.print("<button>Open</button>");
220 }
221 });
222 }
223 }
224#endif
225
226#ifdef USE_CLIMATE
227 for (auto *obj : App.get_climates()) {
228 if (this->include_internal_ || !obj->is_internal())
229 write_row(stream, obj, "climate", "");
230 }
231#endif
232
233#ifdef USE_WATER_HEATER
234 for (auto *obj : App.get_water_heaters()) {
235 if (this->include_internal_ || !obj->is_internal())
236 write_row(stream, obj, "water_heater", "");
237 }
238#endif
239
240 stream->print(ESPHOME_F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/\">ESPHome Web API</a> for "
241 "REST API documentation.</p>"));
242#if defined(USE_WEBSERVER_OTA) && !defined(USE_WEBSERVER_OTA_DISABLED)
243 // Show OTA form only if web_server OTA is not explicitly disabled
244 // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal
245 stream->print(
246 ESPHOME_F("<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input "
247 "type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
248#endif
249 stream->print(ESPHOME_F("<h2>Debug Log</h2><pre id=\"log\"></pre>"));
250#ifdef USE_WEBSERVER_JS_INCLUDE
251 if (this->js_include_ != nullptr) {
252 stream->print(ESPHOME_F("<script type=\"module\" src=\"/0.js\"></script>"));
253 }
254#endif
255 if (strlen(this->js_url_) > 0) {
256 stream->print(ESPHOME_F("<script src=\""));
257 stream->print(this->js_url_);
258 stream->print(ESPHOME_F("\"></script>"));
259 }
260 stream->print(ESPHOME_F("</article></body></html>"));
261 request->send(stream);
262}
263
264} // namespace esphome::web_server
265#endif
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
Application App
Global storage of Application pointer - only one Application can exist.
SemaphoreHandle_t lock