ESPHome 2025.12.5
Loading...
Searching...
No Matches
web_server.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_WEBSERVER
10#ifdef USE_LOGGER
12#endif
13
14#include <functional>
15#include <list>
16#include <map>
17#include <string>
18#include <utility>
19#include <vector>
20
21#if USE_WEBSERVER_VERSION >= 2
22extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
23extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
24#endif
25
26#ifdef USE_WEBSERVER_CSS_INCLUDE
27extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
28extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
29#endif
30
31#ifdef USE_WEBSERVER_JS_INCLUDE
32extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
33extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
34#endif
35
36namespace esphome {
37namespace web_server {
38
40struct UrlMatch {
41 const char *domain;
42 const char *id;
43 const char *method;
44 uint8_t domain_len;
45 uint8_t id_len;
46 uint8_t method_len;
47 bool valid;
48
49 // Helper methods for string comparisons
50 bool domain_equals(const char *str) const {
51 return domain && domain_len == strlen(str) && memcmp(domain, str, domain_len) == 0;
52 }
53
54 bool id_equals_entity(EntityBase *entity) const {
55 // Zero-copy comparison using StringRef
56 StringRef static_ref = entity->get_object_id_ref_for_api_();
57 if (!static_ref.empty()) {
58 return id && id_len == static_ref.size() && memcmp(id, static_ref.c_str(), id_len) == 0;
59 }
60 // Fallback to allocation (rare)
61 const auto &obj_id = entity->get_object_id();
62 return id && id_len == obj_id.length() && memcmp(id, obj_id.c_str(), id_len) == 0;
63 }
64
65 bool method_equals(const char *str) const {
66 return method && method_len == strlen(str) && memcmp(method, str, method_len) == 0;
67 }
68
69 bool method_empty() const { return method_len == 0; }
70};
71
72#ifdef USE_WEBSERVER_SORTING
74 float weight;
75 uint64_t group_id;
76};
77
79 std::string name;
80 float weight;
81};
82#endif
83
85
86/*
87 In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events.
88 This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred
89 update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with
90 multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's
91 implemented in a more straightforward way for ESP-IDF. Arduino platform will eventually go away and this workaround
92 can be forgotten.
93*/
94#if !defined(USE_ESP32) && defined(USE_ARDUINO)
95using message_generator_t = std::string(WebServer *, void *);
96
98class DeferredUpdateEventSource : public AsyncEventSource {
100
101 /*
102 This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
103 that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
104 the same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a
105 std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per
106 entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
107 because of dedup) would take up only 0.8 kB.
108 */
109 struct DeferredEvent {
110 friend class DeferredUpdateEventSource;
111
112 protected:
113 void *source_;
115
116 public:
117 DeferredEvent(void *source, message_generator_t *message_generator)
118 : source_(source), message_generator_(message_generator) {}
119 bool operator==(const DeferredEvent &test) const {
120 return (source_ == test.source_ && message_generator_ == test.message_generator_);
121 }
122 } __attribute__((packed));
123
124 protected:
125 // surface a couple methods from the base class
126 using AsyncEventSource::handleRequest;
127 using AsyncEventSource::send;
128
130 // vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
131 // footprint is more important than speed here)
132 std::vector<DeferredEvent> deferred_queue_;
135 static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
136
137 // helper for allowing only unique entries in the queue
138 void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
139
141
142 public:
143 DeferredUpdateEventSource(WebServer *ws, const String &url)
144 : AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
145
146 void loop();
147
148 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
149 void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0);
150};
151
152class DeferredUpdateEventSourceList : public std::list<DeferredUpdateEventSource *> {
153 protected:
156
157 public:
158 void loop();
159
160 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
161 void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0);
162
163 void add_new_client(WebServer *ws, AsyncWebServerRequest *request);
164};
165#endif
166
176class WebServer : public Controller,
177 public Component,
178 public AsyncWebHandler
179#ifdef USE_LOGGER
180 ,
182#endif
183{
184#if !defined(USE_ESP32) && defined(USE_ARDUINO)
186#endif
187
188 public:
190
191#if USE_WEBSERVER_VERSION == 1
197 void set_css_url(const char *css_url);
198
204 void set_js_url(const char *js_url);
205#endif
206
207#ifdef USE_WEBSERVER_CSS_INCLUDE
212 void set_css_include(const char *css_include);
213#endif
214
215#ifdef USE_WEBSERVER_JS_INCLUDE
220 void set_js_include(const char *js_include);
221#endif
222
228 void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
233 void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
234
235 // ========== INTERNAL METHODS ==========
236 // (In most use cases you won't need these)
238 void setup() override;
239 void loop() override;
240
241 void dump_config() override;
242
243#ifdef USE_LOGGER
244 void on_log(uint8_t level, const char *tag, const char *message, size_t message_len) override;
245#endif
246
248 float get_setup_priority() const override;
249
251 void handle_index_request(AsyncWebServerRequest *request);
252
254 std::string get_config_json();
255
256#ifdef USE_WEBSERVER_CSS_INCLUDE
258 void handle_css_request(AsyncWebServerRequest *request);
259#endif
260
261#ifdef USE_WEBSERVER_JS_INCLUDE
263 void handle_js_request(AsyncWebServerRequest *request);
264#endif
265
266#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
267 // Handle Private Network Access CORS OPTIONS request
268 void handle_pna_cors_request(AsyncWebServerRequest *request);
269#endif
270
271#ifdef USE_SENSOR
272 void on_sensor_update(sensor::Sensor *obj) override;
274 void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
275
276 static std::string sensor_state_json_generator(WebServer *web_server, void *source);
277 static std::string sensor_all_json_generator(WebServer *web_server, void *source);
279 std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
280#endif
281
282#ifdef USE_SWITCH
283 void on_switch_update(switch_::Switch *obj) override;
284
286 void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
287
288 static std::string switch_state_json_generator(WebServer *web_server, void *source);
289 static std::string switch_all_json_generator(WebServer *web_server, void *source);
291 std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
292#endif
293
294#ifdef USE_BUTTON
296 void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
297
298 static std::string button_state_json_generator(WebServer *web_server, void *source);
299 static std::string button_all_json_generator(WebServer *web_server, void *source);
301 std::string button_json(button::Button *obj, JsonDetail start_config);
302#endif
303
304#ifdef USE_BINARY_SENSOR
306
308 void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
309
310 static std::string binary_sensor_state_json_generator(WebServer *web_server, void *source);
311 static std::string binary_sensor_all_json_generator(WebServer *web_server, void *source);
313 std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
314#endif
315
316#ifdef USE_FAN
317 void on_fan_update(fan::Fan *obj) override;
318
320 void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
321
322 static std::string fan_state_json_generator(WebServer *web_server, void *source);
323 static std::string fan_all_json_generator(WebServer *web_server, void *source);
325 std::string fan_json(fan::Fan *obj, JsonDetail start_config);
326#endif
327
328#ifdef USE_LIGHT
329 void on_light_update(light::LightState *obj) override;
330
332 void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
333
334 static std::string light_state_json_generator(WebServer *web_server, void *source);
335 static std::string light_all_json_generator(WebServer *web_server, void *source);
337 std::string light_json(light::LightState *obj, JsonDetail start_config);
338#endif
339
340#ifdef USE_TEXT_SENSOR
342
344 void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
345
346 static std::string text_sensor_state_json_generator(WebServer *web_server, void *source);
347 static std::string text_sensor_all_json_generator(WebServer *web_server, void *source);
349 std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
350#endif
351
352#ifdef USE_COVER
353 void on_cover_update(cover::Cover *obj) override;
354
356 void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
357
358 static std::string cover_state_json_generator(WebServer *web_server, void *source);
359 static std::string cover_all_json_generator(WebServer *web_server, void *source);
361 std::string cover_json(cover::Cover *obj, JsonDetail start_config);
362#endif
363
364#ifdef USE_NUMBER
365 void on_number_update(number::Number *obj) override;
367 void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
368
369 static std::string number_state_json_generator(WebServer *web_server, void *source);
370 static std::string number_all_json_generator(WebServer *web_server, void *source);
372 std::string number_json(number::Number *obj, float value, JsonDetail start_config);
373#endif
374
375#ifdef USE_DATETIME_DATE
376 void on_date_update(datetime::DateEntity *obj) override;
378 void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
379
380 static std::string date_state_json_generator(WebServer *web_server, void *source);
381 static std::string date_all_json_generator(WebServer *web_server, void *source);
383 std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
384#endif
385
386#ifdef USE_DATETIME_TIME
387 void on_time_update(datetime::TimeEntity *obj) override;
389 void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
390
391 static std::string time_state_json_generator(WebServer *web_server, void *source);
392 static std::string time_all_json_generator(WebServer *web_server, void *source);
394 std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
395#endif
396
397#ifdef USE_DATETIME_DATETIME
400 void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
401
402 static std::string datetime_state_json_generator(WebServer *web_server, void *source);
403 static std::string datetime_all_json_generator(WebServer *web_server, void *source);
405 std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
406#endif
407
408#ifdef USE_TEXT
409 void on_text_update(text::Text *obj) override;
411 void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
412
413 static std::string text_state_json_generator(WebServer *web_server, void *source);
414 static std::string text_all_json_generator(WebServer *web_server, void *source);
416 std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
417#endif
418
419#ifdef USE_SELECT
420 void on_select_update(select::Select *obj) override;
422 void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
423
424 static std::string select_state_json_generator(WebServer *web_server, void *source);
425 static std::string select_all_json_generator(WebServer *web_server, void *source);
427 std::string select_json(select::Select *obj, const char *value, JsonDetail start_config);
428#endif
429
430#ifdef USE_CLIMATE
431 void on_climate_update(climate::Climate *obj) override;
433 void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
434
435 static std::string climate_state_json_generator(WebServer *web_server, void *source);
436 static std::string climate_all_json_generator(WebServer *web_server, void *source);
438 std::string climate_json(climate::Climate *obj, JsonDetail start_config);
439#endif
440
441#ifdef USE_LOCK
442 void on_lock_update(lock::Lock *obj) override;
443
445 void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
446
447 static std::string lock_state_json_generator(WebServer *web_server, void *source);
448 static std::string lock_all_json_generator(WebServer *web_server, void *source);
450 std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
451#endif
452
453#ifdef USE_VALVE
454 void on_valve_update(valve::Valve *obj) override;
455
457 void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
458
459 static std::string valve_state_json_generator(WebServer *web_server, void *source);
460 static std::string valve_all_json_generator(WebServer *web_server, void *source);
462 std::string valve_json(valve::Valve *obj, JsonDetail start_config);
463#endif
464
465#ifdef USE_ALARM_CONTROL_PANEL
467
469 void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
470
471 static std::string alarm_control_panel_state_json_generator(WebServer *web_server, void *source);
472 static std::string alarm_control_panel_all_json_generator(WebServer *web_server, void *source);
476#endif
477
478#ifdef USE_EVENT
479 void on_event(event::Event *obj) override;
480
481 static std::string event_state_json_generator(WebServer *web_server, void *source);
482 static std::string event_all_json_generator(WebServer *web_server, void *source);
483
485 void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match);
486
488 std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
489#endif
490
491#ifdef USE_UPDATE
492 void on_update(update::UpdateEntity *obj) override;
493
495 void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
496
497 static std::string update_state_json_generator(WebServer *web_server, void *source);
498 static std::string update_all_json_generator(WebServer *web_server, void *source);
500 std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
501#endif
502
504 bool canHandle(AsyncWebServerRequest *request) const override;
506 void handleRequest(AsyncWebServerRequest *request) override;
508 bool isRequestHandlerTrivial() const override; // NOLINT(readability-identifier-naming)
509
510#ifdef USE_WEBSERVER_SORTING
511 void add_entity_config(EntityBase *entity, float weight, uint64_t group);
512 void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight);
513
514 std::map<EntityBase *, SortingComponents> sorting_entitys_;
515 std::map<uint64_t, SortingGroup> sorting_groups_;
516#endif
517
518 bool include_internal_{false};
519
520 protected:
521 void add_sorting_info_(JsonObject &root, EntityBase *entity);
522
523#ifdef USE_LIGHT
524 // Helper to parse and apply a float parameter with optional scaling
525 template<typename T, typename Ret>
526 void parse_light_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(float),
527 float scale = 1.0f) {
528 if (request->hasParam(param_name)) {
529 auto value = parse_number<float>(request->getParam(param_name)->value().c_str());
530 if (value.has_value()) {
531 (call.*setter)(*value / scale);
532 }
533 }
534 }
535
536 // Helper to parse and apply a uint32_t parameter with optional scaling
537 template<typename T, typename Ret>
538 void parse_light_param_uint_(AsyncWebServerRequest *request, const char *param_name, T &call,
539 Ret (T::*setter)(uint32_t), uint32_t scale = 1) {
540 if (request->hasParam(param_name)) {
541 auto value = parse_number<uint32_t>(request->getParam(param_name)->value().c_str());
542 if (value.has_value()) {
543 (call.*setter)(*value * scale);
544 }
545 }
546 }
547#endif
548
549 // Generic helper to parse and apply a float parameter
550 template<typename T, typename Ret>
551 void parse_float_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(float)) {
552 if (request->hasParam(param_name)) {
553 auto value = parse_number<float>(request->getParam(param_name)->value().c_str());
554 if (value.has_value()) {
555 (call.*setter)(*value);
556 }
557 }
558 }
559
560 // Generic helper to parse and apply an int parameter
561 template<typename T, typename Ret>
562 void parse_int_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret (T::*setter)(int)) {
563 if (request->hasParam(param_name)) {
564 auto value = parse_number<int>(request->getParam(param_name)->value().c_str());
565 if (value.has_value()) {
566 (call.*setter)(*value);
567 }
568 }
569 }
570
571 // Generic helper to parse and apply a string parameter
572 template<typename T, typename Ret>
573 void parse_string_param_(AsyncWebServerRequest *request, const char *param_name, T &call,
574 Ret (T::*setter)(const std::string &)) {
575 if (request->hasParam(param_name)) {
576 // .c_str() is required for Arduino framework where value() returns Arduino String instead of std::string
577 std::string value = request->getParam(param_name)->value().c_str(); // NOLINT(readability-redundant-string-cstr)
578 (call.*setter)(value);
579 }
580 }
581
583#ifdef USE_ESP32
584 AsyncEventSource events_{"/events", this};
585#elif USE_ARDUINO
587#endif
588
589#if USE_WEBSERVER_VERSION == 1
590 const char *css_url_{nullptr};
591 const char *js_url_{nullptr};
592#endif
593#ifdef USE_WEBSERVER_CSS_INCLUDE
594 const char *css_include_{nullptr};
595#endif
596#ifdef USE_WEBSERVER_JS_INCLUDE
597 const char *js_include_{nullptr};
598#endif
599 bool expose_log_{true};
600};
601
602} // namespace web_server
603} // namespace esphome
604#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:25
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:177
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:91
Base class for all locks.
Definition lock.h:111
Interface for receiving log messages without std::function overhead.
Definition logger.h:56
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:30
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:24
Base class for all valve devices.
Definition valve.h:106
DeferredUpdateEventSource(WebServer *ws, const String &url)
Definition web_server.h:143
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:135
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:132
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:183
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:233
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:514
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:538
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len) override
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:586
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:228
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:582
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:515
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:526
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:573
void parse_float_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(float))
Definition web_server.h:551
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:562
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:25
std::string(WebServer *, void *) message_generator_t
Definition web_server.h:95
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:536
Internal helper struct that is used to parse incoming URLs.
Definition web_server.h:40
const char * domain
Pointer to domain within URL, for example "sensor".
Definition web_server.h:41
const char * id
Pointer to id within URL, for example "living_room_fan".
Definition web_server.h:42
bool valid
Whether this match is valid.
Definition web_server.h:47
bool id_equals_entity(EntityBase *entity) const
Definition web_server.h:54
uint8_t domain_len
Length of domain string.
Definition web_server.h:44
uint8_t method_len
Length of method string.
Definition web_server.h:46
const char * method
Pointer to method within URL, for example "turn_on".
Definition web_server.h:43
bool domain_equals(const char *str) const
Definition web_server.h:50
uint8_t id_len
Length of id string.
Definition web_server.h:45
bool method_equals(const char *str) const
Definition web_server.h:65
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:27
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