ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
web_server.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_WEBSERVER
10
11#include <functional>
12#include <list>
13#include <map>
14#include <string>
15#include <utility>
16#include <vector>
17
18#if USE_WEBSERVER_VERSION >= 2
19extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
20extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
21#endif
22
23#ifdef USE_WEBSERVER_CSS_INCLUDE
24extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
25extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
26#endif
27
28#ifdef USE_WEBSERVER_JS_INCLUDE
29extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
30extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
31#endif
32
33namespace esphome {
34namespace web_server {
35
37struct UrlMatch {
38 const char *domain;
39 const char *id;
40 const char *method;
41 uint8_t domain_len;
42 uint8_t id_len;
43 uint8_t method_len;
44 bool valid;
45
46 // Helper methods for string comparisons
47 bool domain_equals(const char *str) const {
48 return domain && domain_len == strlen(str) && memcmp(domain, str, domain_len) == 0;
49 }
50
51 bool id_equals_entity(EntityBase *entity) const {
52 // Zero-copy comparison using StringRef
53 StringRef static_ref = entity->get_object_id_ref_for_api_();
54 if (!static_ref.empty()) {
55 return id && id_len == static_ref.size() && memcmp(id, static_ref.c_str(), id_len) == 0;
56 }
57 // Fallback to allocation (rare)
58 const auto &obj_id = entity->get_object_id();
59 return id && id_len == obj_id.length() && memcmp(id, obj_id.c_str(), id_len) == 0;
60 }
61
62 bool method_equals(const char *str) const {
63 return method && method_len == strlen(str) && memcmp(method, str, method_len) == 0;
64 }
65
66 bool method_empty() const { return method_len == 0; }
67};
68
69#ifdef USE_WEBSERVER_SORTING
71 float weight;
72 uint64_t group_id;
73};
74
76 std::string name;
77 float weight;
78};
79#endif
80
82
83/*
84 In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events.
85 This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred
86 update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with
87 multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's
88 implemented in a more straightforward way for ESP-IDF. Arduino platform will eventually go away and this workaround
89 can be forgotten.
90*/
91#if !defined(USE_ESP32) && defined(USE_ARDUINO)
92using message_generator_t = std::string(WebServer *, void *);
93
95class DeferredUpdateEventSource : public AsyncEventSource {
97
98 /*
99 This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
100 that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
101 the same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a
102 std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per
103 entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
104 because of dedup) would take up only 0.8 kB.
105 */
106 struct DeferredEvent {
107 friend class DeferredUpdateEventSource;
108
109 protected:
110 void *source_;
112
113 public:
114 DeferredEvent(void *source, message_generator_t *message_generator)
115 : source_(source), message_generator_(message_generator) {}
116 bool operator==(const DeferredEvent &test) const {
117 return (source_ == test.source_ && message_generator_ == test.message_generator_);
118 }
119 } __attribute__((packed));
120
121 protected:
122 // surface a couple methods from the base class
123 using AsyncEventSource::handleRequest;
124 using AsyncEventSource::send;
125
127 // vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
128 // footprint is more important than speed here)
129 std::vector<DeferredEvent> deferred_queue_;
132 static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
133
134 // helper for allowing only unique entries in the queue
135 void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
136
138
139 public:
140 DeferredUpdateEventSource(WebServer *ws, const String &url)
141 : AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
142
143 void loop();
144
145 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
146 void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0);
147};
148
149class DeferredUpdateEventSourceList : public std::list<DeferredUpdateEventSource *> {
150 protected:
153
154 public:
155 void loop();
156
157 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
158 void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0);
159
160 void add_new_client(WebServer *ws, AsyncWebServerRequest *request);
161};
162#endif
163
173class WebServer : public Controller, public Component, public AsyncWebHandler {
174#if !defined(USE_ESP32) && defined(USE_ARDUINO)
176#endif
177
178 public:
180
181#if USE_WEBSERVER_VERSION == 1
187 void set_css_url(const char *css_url);
188
194 void set_js_url(const char *js_url);
195#endif
196
197#ifdef USE_WEBSERVER_CSS_INCLUDE
202 void set_css_include(const char *css_include);
203#endif
204
205#ifdef USE_WEBSERVER_JS_INCLUDE
210 void set_js_include(const char *js_include);
211#endif
212
218 void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
223 void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
224
225 // ========== INTERNAL METHODS ==========
226 // (In most use cases you won't need these)
228 void setup() override;
229 void loop() override;
230
231 void dump_config() override;
232
234 float get_setup_priority() const override;
235
237 void handle_index_request(AsyncWebServerRequest *request);
238
240 std::string get_config_json();
241
242#ifdef USE_WEBSERVER_CSS_INCLUDE
244 void handle_css_request(AsyncWebServerRequest *request);
245#endif
246
247#ifdef USE_WEBSERVER_JS_INCLUDE
249 void handle_js_request(AsyncWebServerRequest *request);
250#endif
251
252#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
253 // Handle Private Network Access CORS OPTIONS request
254 void handle_pna_cors_request(AsyncWebServerRequest *request);
255#endif
256
257#ifdef USE_SENSOR
258 void on_sensor_update(sensor::Sensor *obj) override;
260 void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
261
262 static std::string sensor_state_json_generator(WebServer *web_server, void *source);
263 static std::string sensor_all_json_generator(WebServer *web_server, void *source);
265 std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
266#endif
267
268#ifdef USE_SWITCH
269 void on_switch_update(switch_::Switch *obj) override;
270
272 void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
273
274 static std::string switch_state_json_generator(WebServer *web_server, void *source);
275 static std::string switch_all_json_generator(WebServer *web_server, void *source);
277 std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
278#endif
279
280#ifdef USE_BUTTON
282 void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
283
284 static std::string button_state_json_generator(WebServer *web_server, void *source);
285 static std::string button_all_json_generator(WebServer *web_server, void *source);
287 std::string button_json(button::Button *obj, JsonDetail start_config);
288#endif
289
290#ifdef USE_BINARY_SENSOR
292
294 void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
295
296 static std::string binary_sensor_state_json_generator(WebServer *web_server, void *source);
297 static std::string binary_sensor_all_json_generator(WebServer *web_server, void *source);
299 std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
300#endif
301
302#ifdef USE_FAN
303 void on_fan_update(fan::Fan *obj) override;
304
306 void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
307
308 static std::string fan_state_json_generator(WebServer *web_server, void *source);
309 static std::string fan_all_json_generator(WebServer *web_server, void *source);
311 std::string fan_json(fan::Fan *obj, JsonDetail start_config);
312#endif
313
314#ifdef USE_LIGHT
315 void on_light_update(light::LightState *obj) override;
316
318 void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
319
320 static std::string light_state_json_generator(WebServer *web_server, void *source);
321 static std::string light_all_json_generator(WebServer *web_server, void *source);
323 std::string light_json(light::LightState *obj, JsonDetail start_config);
324#endif
325
326#ifdef USE_TEXT_SENSOR
328
330 void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
331
332 static std::string text_sensor_state_json_generator(WebServer *web_server, void *source);
333 static std::string text_sensor_all_json_generator(WebServer *web_server, void *source);
335 std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
336#endif
337
338#ifdef USE_COVER
339 void on_cover_update(cover::Cover *obj) override;
340
342 void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
343
344 static std::string cover_state_json_generator(WebServer *web_server, void *source);
345 static std::string cover_all_json_generator(WebServer *web_server, void *source);
347 std::string cover_json(cover::Cover *obj, JsonDetail start_config);
348#endif
349
350#ifdef USE_NUMBER
351 void on_number_update(number::Number *obj) override;
353 void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
354
355 static std::string number_state_json_generator(WebServer *web_server, void *source);
356 static std::string number_all_json_generator(WebServer *web_server, void *source);
358 std::string number_json(number::Number *obj, float value, JsonDetail start_config);
359#endif
360
361#ifdef USE_DATETIME_DATE
362 void on_date_update(datetime::DateEntity *obj) override;
364 void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
365
366 static std::string date_state_json_generator(WebServer *web_server, void *source);
367 static std::string date_all_json_generator(WebServer *web_server, void *source);
369 std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
370#endif
371
372#ifdef USE_DATETIME_TIME
373 void on_time_update(datetime::TimeEntity *obj) override;
375 void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
376
377 static std::string time_state_json_generator(WebServer *web_server, void *source);
378 static std::string time_all_json_generator(WebServer *web_server, void *source);
380 std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
381#endif
382
383#ifdef USE_DATETIME_DATETIME
386 void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
387
388 static std::string datetime_state_json_generator(WebServer *web_server, void *source);
389 static std::string datetime_all_json_generator(WebServer *web_server, void *source);
391 std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
392#endif
393
394#ifdef USE_TEXT
395 void on_text_update(text::Text *obj) override;
397 void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
398
399 static std::string text_state_json_generator(WebServer *web_server, void *source);
400 static std::string text_all_json_generator(WebServer *web_server, void *source);
402 std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
403#endif
404
405#ifdef USE_SELECT
406 void on_select_update(select::Select *obj) override;
408 void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
409
410 static std::string select_state_json_generator(WebServer *web_server, void *source);
411 static std::string select_all_json_generator(WebServer *web_server, void *source);
413 std::string select_json(select::Select *obj, const char *value, JsonDetail start_config);
414#endif
415
416#ifdef USE_CLIMATE
417 void on_climate_update(climate::Climate *obj) override;
419 void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
420
421 static std::string climate_state_json_generator(WebServer *web_server, void *source);
422 static std::string climate_all_json_generator(WebServer *web_server, void *source);
424 std::string climate_json(climate::Climate *obj, JsonDetail start_config);
425#endif
426
427#ifdef USE_LOCK
428 void on_lock_update(lock::Lock *obj) override;
429
431 void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
432
433 static std::string lock_state_json_generator(WebServer *web_server, void *source);
434 static std::string lock_all_json_generator(WebServer *web_server, void *source);
436 std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
437#endif
438
439#ifdef USE_VALVE
440 void on_valve_update(valve::Valve *obj) override;
441
443 void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
444
445 static std::string valve_state_json_generator(WebServer *web_server, void *source);
446 static std::string valve_all_json_generator(WebServer *web_server, void *source);
448 std::string valve_json(valve::Valve *obj, JsonDetail start_config);
449#endif
450
451#ifdef USE_ALARM_CONTROL_PANEL
453
455 void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
456
457 static std::string alarm_control_panel_state_json_generator(WebServer *web_server, void *source);
458 static std::string alarm_control_panel_all_json_generator(WebServer *web_server, void *source);
462#endif
463
464#ifdef USE_EVENT
465 void on_event(event::Event *obj) override;
466
467 static std::string event_state_json_generator(WebServer *web_server, void *source);
468 static std::string event_all_json_generator(WebServer *web_server, void *source);
469
471 void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match);
472
474 std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
475#endif
476
477#ifdef USE_UPDATE
478 void on_update(update::UpdateEntity *obj) override;
479
481 void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
482
483 static std::string update_state_json_generator(WebServer *web_server, void *source);
484 static std::string update_all_json_generator(WebServer *web_server, void *source);
486 std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
487#endif
488
490 bool canHandle(AsyncWebServerRequest *request) const override;
492 void handleRequest(AsyncWebServerRequest *request) override;
494 bool isRequestHandlerTrivial() const override; // NOLINT(readability-identifier-naming)
495
496#ifdef USE_WEBSERVER_SORTING
497 void add_entity_config(EntityBase *entity, float weight, uint64_t group);
498 void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight);
499
500 std::map<EntityBase *, SortingComponents> sorting_entitys_;
501 std::map<uint64_t, SortingGroup> sorting_groups_;
502#endif
503
504 bool include_internal_{false};
505
506 protected:
507 void add_sorting_info_(JsonObject &root, EntityBase *entity);
508
509#ifdef USE_LIGHT
510 // Helper to parse and apply a float parameter with optional scaling
511 template<typename T, typename Ret>
512 void parse_light_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(float),
513 float scale = 1.0f) {
514 if (request->hasParam(param_name)) {
515 auto value = parse_number<float>(request->getParam(param_name)->value().c_str());
516 if (value.has_value()) {
517 (call.*setter)(*value / scale);
518 }
519 }
520 }
521
522 // Helper to parse and apply a uint32_t parameter with optional scaling
523 template<typename T, typename Ret>
524 void parse_light_param_uint_(AsyncWebServerRequest *request, const char *param_name, T &call,
525 Ret (T::*setter)(uint32_t), uint32_t scale = 1) {
526 if (request->hasParam(param_name)) {
527 auto value = parse_number<uint32_t>(request->getParam(param_name)->value().c_str());
528 if (value.has_value()) {
529 (call.*setter)(*value * scale);
530 }
531 }
532 }
533#endif
534
535 // Generic helper to parse and apply a float parameter
536 template<typename T, typename Ret>
537 void parse_float_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(float)) {
538 if (request->hasParam(param_name)) {
539 auto value = parse_number<float>(request->getParam(param_name)->value().c_str());
540 if (value.has_value()) {
541 (call.*setter)(*value);
542 }
543 }
544 }
545
546 // Generic helper to parse and apply an int parameter
547 template<typename T, typename Ret>
548 void parse_int_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(int)) {
549 if (request->hasParam(param_name)) {
550 auto value = parse_number<int>(request->getParam(param_name)->value().c_str());
551 if (value.has_value()) {
552 (call.*setter)(*value);
553 }
554 }
555 }
556
557 // Generic helper to parse and apply a string parameter
558 template<typename T, typename Ret>
559 void parse_string_param_(AsyncWebServerRequest *request, const char *param_name, T &call,
560 Ret (T::*setter)(const std::string &)) {
561 if (request->hasParam(param_name)) {
562 // .c_str() is required for Arduino framework where value() returns Arduino String instead of std::string
563 std::string value = request->getParam(param_name)->value().c_str(); // NOLINT(readability-redundant-string-cstr)
564 (call.*setter)(value);
565 }
566 }
567
569#ifdef USE_ESP32
570 AsyncEventSource events_{"/events", this};
571#elif USE_ARDUINO
573#endif
574
575#if USE_WEBSERVER_VERSION == 1
576 const char *css_url_{nullptr};
577 const char *js_url_{nullptr};
578#endif
579#ifdef USE_WEBSERVER_CSS_INCLUDE
580 const char *css_include_{nullptr};
581#endif
582#ifdef USE_WEBSERVER_JS_INCLUDE
583 const char *js_include_{nullptr};
584#endif
585 bool expose_log_{true};
586};
587
588} // namespace web_server
589} // namespace esphome
590#endif
std::string get_object_id() const
StringRef get_object_id_ref_for_api_() const
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
constexpr size_type size() const
Definition string_ref.h:70
Base class for all binary_sensor-type classes.
Base class for all buttons.
Definition button.h:26
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:178
Base class for all cover devices.
Definition cover.h:112
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:69
Base class for all locks.
Definition lock.h:109
Base-class for all numbers.
Definition number.h:30
Base-class for all selects.
Definition select.h:31
Base-class for all sensors.
Definition sensor.h:42
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:24
Base class for all valve devices.
Definition valve.h:105
DeferredUpdateEventSource(WebServer *ws, const String &url)
Definition web_server.h:140
void try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES
Definition web_server.h:132
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
std::vector< DeferredEvent > deferred_queue_
Definition web_server.h:129
void on_client_connect_(DeferredUpdateEventSource *source)
void add_new_client(WebServer *ws, AsyncWebServerRequest *request)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
void try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void on_client_disconnect_(DeferredUpdateEventSource *source)
This class allows users to create a web server with their ESP nodes.
Definition web_server.h:173
void setup() override
Setup the internal web server and register handlers.
void on_update(update::UpdateEntity *obj) override
static std::string text_sensor_all_json_generator(WebServer *web_server, void *source)
std::string light_json(light::LightState *obj, JsonDetail start_config)
Dump the light state as a JSON string.
void set_expose_log(bool expose_log)
Set whether or not the webserver should expose the Log.
Definition web_server.h:223
std::string date_json(datetime::DateEntity *obj, JsonDetail start_config)
Dump the date state with its value as a JSON string.
std::string get_config_json()
Return the webserver configuration as JSON.
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition web_server.h:500
static std::string binary_sensor_state_json_generator(WebServer *web_server, void *source)
void on_text_update(text::Text *obj) override
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.
static std::string button_state_json_generator(WebServer *web_server, void *source)
static std::string lock_all_json_generator(WebServer *web_server, void *source)
void on_light_update(light::LightState *obj) override
static std::string date_all_json_generator(WebServer *web_server, void *source)
std::string update_json(update::UpdateEntity *obj, JsonDetail start_config)
Dump the update state with its value as a JSON string.
void on_cover_update(cover::Cover *obj) override
static std::string text_state_json_generator(WebServer *web_server, void *source)
void set_css_url(const char *css_url)
Set the URL to the CSS <link> that's sent to each client.
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
std::string number_json(number::Number *obj, float value, JsonDetail start_config)
Dump the number state with its value as a JSON string.
static std::string event_state_json_generator(WebServer *web_server, void *source)
std::string cover_json(cover::Cover *obj, JsonDetail start_config)
Dump the cover state as a JSON string.
static std::string datetime_all_json_generator(WebServer *web_server, void *source)
static std::string sensor_all_json_generator(WebServer *web_server, void *source)
bool isRequestHandlerTrivial() const override
This web handle is not trivial.
WebServer(web_server_base::WebServerBase *base)
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.
void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a switch request under '/switch/<id>/</turn_on/turn_off/toggle>'.
void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a event request under '/event<id>'.
void parse_light_param_uint_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(uint32_t), uint32_t scale=1)
Definition web_server.h:524
std::string select_json(select::Select *obj, const char *value, JsonDetail start_config)
Dump the select state with its value 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.
std::string valve_json(valve::Valve *obj, JsonDetail start_config)
Dump the valve state as a JSON string.
DeferredUpdateEventSourceList events_
Definition web_server.h:572
void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a button request under '/button/<id>/press'.
void on_date_update(datetime::DateEntity *obj) override
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.
void on_number_update(number::Number *obj) override
void add_entity_config(EntityBase *entity, float weight, uint64_t group)
void set_include_internal(bool include_internal)
Determine whether internal components should be displayed on the web server.
Definition web_server.h:218
std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config)
Dump the datetime state with its value as a JSON string.
void handle_css_request(AsyncWebServerRequest *request)
Handle included css request under '/0.css'.
static std::string sensor_state_json_generator(WebServer *web_server, void *source)
void on_valve_update(valve::Valve *obj) override
void on_climate_update(climate::Climate *obj) override
static std::string switch_state_json_generator(WebServer *web_server, void *source)
void add_sorting_info_(JsonObject &root, EntityBase *entity)
void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a light request under '/light/<id>/</turn_on/turn_off/toggle>'.
static std::string event_all_json_generator(WebServer *web_server, void *source)
static std::string climate_state_json_generator(WebServer *web_server, void *source)
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
static std::string number_all_json_generator(WebServer *web_server, void *source)
void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text input request under '/text/<id>'.
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a cover request under '/cover/<id>/<open/close/stop/set>'.
static std::string date_state_json_generator(WebServer *web_server, void *source)
static std::string valve_all_json_generator(WebServer *web_server, void *source)
static std::string text_all_json_generator(WebServer *web_server, void *source)
void on_switch_update(switch_::Switch *obj) override
web_server_base::WebServerBase * base_
Definition web_server.h:568
static std::string binary_sensor_all_json_generator(WebServer *web_server, void *source)
static std::string light_state_json_generator(WebServer *web_server, void *source)
void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a lock request under '/lock/<id>/</lock/unlock/open>'.
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
static std::string light_all_json_generator(WebServer *web_server, void *source)
std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config)
Dump the switch state with its value as a JSON string.
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text sensor request under '/text_sensor/<id>'.
std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config)
Dump the sensor state with its value as a JSON string.
void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a date request under '/date/<id>'.
static std::string cover_all_json_generator(WebServer *web_server, void *source)
void set_js_url(const char *js_url)
Set the URL to the script that's embedded in the index page.
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a sensor request under '/sensor/<id>'.
static std::string text_sensor_state_json_generator(WebServer *web_server, void *source)
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a number request under '/number/<id>'.
static std::string alarm_control_panel_state_json_generator(WebServer *web_server, void *source)
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
void handle_js_request(AsyncWebServerRequest *request)
Handle included js request under '/0.js'.
void set_js_include(const char *js_include)
Set local path to the script that's embedded in the index page.
static std::string fan_state_json_generator(WebServer *web_server, void *source)
static std::string update_state_json_generator(WebServer *web_server, void *source)
void handleRequest(AsyncWebServerRequest *request) override
Override the web handler's handleRequest method.
static std::string climate_all_json_generator(WebServer *web_server, void *source)
void on_datetime_update(datetime::DateTimeEntity *obj) 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.
std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config)
Dump the time state with its value as a JSON string.
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a fan request under '/fan/<id>/</turn_on/turn_off/toggle>'.
static std::string cover_state_json_generator(WebServer *web_server, void *source)
static std::string lock_state_json_generator(WebServer *web_server, void *source)
void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a valve request under '/valve/<id>/<open/close/stop/set>'.
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a binary sensor request under '/binary_sensor/<id>'.
static std::string alarm_control_panel_all_json_generator(WebServer *web_server, void *source)
static std::string number_state_json_generator(WebServer *web_server, void *source)
void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a time request under '/time/<id>'.
void on_sensor_update(sensor::Sensor *obj) override
std::map< uint64_t, SortingGroup > sorting_groups_
Definition web_server.h:501
void set_css_include(const char *css_include)
Set local path to the script that's embedded in the index page.
static std::string valve_state_json_generator(WebServer *web_server, void *source)
bool canHandle(AsyncWebServerRequest *request) const override
Override the web handler's canHandle method.
std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config)
Dump the lock state with its value as a JSON string.
void on_event(event::Event *obj) override
void handle_pna_cors_request(AsyncWebServerRequest *request)
std::string fan_json(fan::Fan *obj, JsonDetail start_config)
Dump the fan state as a JSON string.
void on_fan_update(fan::Fan *obj) override
void parse_light_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(float), float scale=1.0f)
Definition web_server.h:512
void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a datetime request under '/datetime/<id>'.
void parse_string_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(const std::string &))
Definition web_server.h:559
void parse_float_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(float))
Definition web_server.h:537
void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a alarm_control_panel request under '/alarm_control_panel/<id>'.
static std::string time_state_json_generator(WebServer *web_server, void *source)
void on_lock_update(lock::Lock *obj) override
static std::string button_all_json_generator(WebServer *web_server, void *source)
static std::string select_state_json_generator(WebServer *web_server, void *source)
float get_setup_priority() const override
MQTT setup priority.
void parse_int_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(int))
Definition web_server.h:548
void on_select_update(select::Select *obj) override
void on_time_update(datetime::TimeEntity *obj) override
static std::string update_all_json_generator(WebServer *web_server, void *source)
void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a update request under '/update/<id>'.
std::string climate_json(climate::Climate *obj, JsonDetail start_config)
Dump the climate details.
static std::string fan_all_json_generator(WebServer *web_server, void *source)
static std::string switch_all_json_generator(WebServer *web_server, void *source)
static std::string time_all_json_generator(WebServer *web_server, void *source)
void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight)
static std::string select_all_json_generator(WebServer *web_server, void *source)
static std::string datetime_state_json_generator(WebServer *web_server, void *source)
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a climate request under '/climate/<id>'.
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.
void on_text_sensor_update(text_sensor::TextSensor *obj) override
struct @65::@66 __attribute__
const char * message
Definition component.cpp:38
LockState
Enum for all states a lock can be in.
Definition lock.h:26
std::string(WebServer *, void *) message_generator_t
Definition web_server.h:92
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:522
Internal helper struct that is used to parse incoming URLs.
Definition web_server.h:37
const char * domain
Pointer to domain within URL, for example "sensor".
Definition web_server.h:38
const char * id
Pointer to id within URL, for example "living_room_fan".
Definition web_server.h:39
bool valid
Whether this match is valid.
Definition web_server.h:44
bool id_equals_entity(EntityBase *entity) const
Definition web_server.h:51
uint8_t domain_len
Length of domain string.
Definition web_server.h:41
uint8_t method_len
Length of method string.
Definition web_server.h:43
const char * method
Pointer to method within URL, for example "turn_on".
Definition web_server.h:40
bool domain_equals(const char *str) const
Definition web_server.h:47
uint8_t id_len
Length of id string.
Definition web_server.h:42
bool method_equals(const char *str) const
Definition web_server.h:62
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:24
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
message_generator_t * message_generator_
Definition web_server.h:4
bool operator==(const DeferredEvent &test) const
Definition web_server.h:9
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
void * source_
Definition web_server.h:3
DeferredEvent(void *source, message_generator_t *message_generator)
Definition web_server.h:7