ESPHome 2026.7.0
Loading...
Searching...
No Matches
web_server.h
Go to the documentation of this file.
1#pragma once
2
3#include "list_entities.h"
4
7#ifdef USE_WEBSERVER
11#ifdef USE_LOGGER
13#endif
14
15#include <functional>
16#include <list>
17#include <map>
18#include <string>
19#include <utility>
20#include <vector>
21
22#if USE_WEBSERVER_VERSION >= 2
23extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
24extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
25#endif
26
27#ifdef USE_WEBSERVER_CSS_INCLUDE
28extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
29extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
30#endif
31
32#ifdef USE_WEBSERVER_JS_INCLUDE
33extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
34extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
35#endif
36
37namespace esphome::web_server {
38
39// Type for parameter names that can be stored in flash on ESP8266
40#ifdef USE_ESP8266
41using ParamNameType = const __FlashStringHelper *;
42#else
43using ParamNameType = const char *;
44#endif
45
46// All platforms need to defer actions to main loop thread.
47// Multi-core platforms need this for thread safety.
48// ESP8266 needs this because ESPAsyncWebServer callbacks run in "sys" context
49// (SDK system context), not "cont" context (continuation/main loop). Calling
50// yield() from sys context causes a panic in the Arduino core.
51#define DEFER_ACTION(capture, action) this->defer([capture]() mutable { action; })
52
58
60struct UrlMatch {
64#ifdef USE_DEVICES
66#endif
67 bool valid{false};
68
69 // Helper methods for string comparisons
70 bool domain_equals(const char *str) const { return this->domain == str; }
71 bool method_equals(const char *str) const { return this->method == str; }
72
73#ifdef USE_ESP8266
74 // Overloads for flash strings on ESP8266
75 bool domain_equals(const __FlashStringHelper *str) const { return this->domain == str; }
76 bool method_equals(const __FlashStringHelper *str) const { return this->method == str; }
77#endif
78
82};
83
84#ifdef USE_WEBSERVER_SORTING
86 float weight;
87 uint64_t group_id;
88};
89
91 std::string name;
92 float weight;
93};
94#endif
95
97
98/*
99 In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events.
100 This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred
101 update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with
102 multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's
103 implemented in a more straightforward way for ESP-IDF. Arduino platform will eventually go away and this workaround
104 can be forgotten.
105*/
106#if !defined(USE_ESP32) && defined(USE_ARDUINO)
108
110class DeferredUpdateEventSource final : public AsyncEventSource {
112
113 /*
114 This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
115 that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
116 the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
117 std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two
118 pointers per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors
119 publishing because of dedup) would take up only 0.8 kB.
120 */
121 struct DeferredEvent {
122 friend class DeferredUpdateEventSource;
123
124 protected:
125 void *source_;
126 message_generator_t *message_generator_;
127
128 public:
129 DeferredEvent(void *source, message_generator_t *message_generator)
130 : source_(source), message_generator_(message_generator) {}
131 bool operator==(const DeferredEvent &test) const {
132 return (source_ == test.source_ && message_generator_ == test.message_generator_);
133 }
134 };
135 static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
136 "DeferredEvent should have no padding");
137
138 protected:
139 // surface a couple methods from the base class
140 using AsyncEventSource::handleRequest;
141 using AsyncEventSource::send;
142
144 // vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
145 // footprint is more important than speed here)
146 std::vector<DeferredEvent> deferred_queue_;
149 static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
150
151 // helper for allowing only unique entries in the queue
152 void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
153
155
156 public:
157 DeferredUpdateEventSource(WebServer *ws, const String &url)
158 : AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
159
160 void loop();
161
162 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
163 void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
164 uint32_t reconnect = 0);
165};
166
167class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEventSource *> {
168 protected:
171
172 public:
174 bool loop();
175
176 void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator);
177 void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
178 uint32_t reconnect = 0);
179
180 void add_new_client(WebServer *ws, AsyncWebServerRequest *request);
181};
182#endif
183
193class WebServer final : public Controller, public Component, public AsyncWebHandler {
194#if !defined(USE_ESP32) && defined(USE_ARDUINO)
196#endif
197
198 public:
200
201#if USE_WEBSERVER_VERSION == 1
207 void set_css_url(const char *css_url);
208
214 void set_js_url(const char *js_url);
215#endif
216
217#ifdef USE_WEBSERVER_CSS_INCLUDE
222 void set_css_include(const char *css_include);
223#endif
224
225#ifdef USE_WEBSERVER_JS_INCLUDE
230 void set_js_include(const char *js_include);
231#endif
232
238 void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
243 void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
244
245#ifdef USE_WEBSERVER_ALLOWED_ORIGINS
258 void set_allowed_origins(std::initializer_list<const char *> origins) { this->allowed_origins_ = origins; }
259#endif
260
261 // ========== INTERNAL METHODS ==========
262 // (In most use cases you won't need these)
264 void setup() override;
265 void loop() override;
266
267 void dump_config() override;
268
269#ifdef USE_LOGGER
270 void on_log(uint8_t level, const char *tag, const char *message, size_t message_len);
271#endif
272
274 float get_setup_priority() const override;
275
277 void handle_index_request(AsyncWebServerRequest *request);
278
281
282#ifdef USE_WEBSERVER_CSS_INCLUDE
284 void handle_css_request(AsyncWebServerRequest *request);
285#endif
286
287#ifdef USE_WEBSERVER_JS_INCLUDE
289 void handle_js_request(AsyncWebServerRequest *request);
290#endif
291
292#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
293 // Handle Private Network Access CORS OPTIONS request
294 void handle_pna_cors_request(AsyncWebServerRequest *request);
295#endif
296
297#ifdef USE_SENSOR
298 void on_sensor_update(sensor::Sensor *obj) override;
300 void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
301
302 static json::SerializationBuffer<> sensor_state_json_generator(WebServer *web_server, void *source);
303 static json::SerializationBuffer<> sensor_all_json_generator(WebServer *web_server, void *source);
304#endif
305
306#ifdef USE_SWITCH
307 void on_switch_update(switch_::Switch *obj) override;
308
310 void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
311
312 static json::SerializationBuffer<> switch_state_json_generator(WebServer *web_server, void *source);
313 static json::SerializationBuffer<> switch_all_json_generator(WebServer *web_server, void *source);
314#endif
315
316#ifdef USE_BUTTON
318 void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
319
320 // Buttons are stateless, so there is no button_state_json_generator
321 static json::SerializationBuffer<> button_all_json_generator(WebServer *web_server, void *source);
322#endif
323
324#ifdef USE_BINARY_SENSOR
326
328 void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
329
332#endif
333
334#ifdef USE_FAN
335 void on_fan_update(fan::Fan *obj) override;
336
338 void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
339
340 static json::SerializationBuffer<> fan_state_json_generator(WebServer *web_server, void *source);
341 static json::SerializationBuffer<> fan_all_json_generator(WebServer *web_server, void *source);
342#endif
343
344#ifdef USE_LIGHT
345 void on_light_update(light::LightState *obj) override;
346
348 void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
349
350 static json::SerializationBuffer<> light_state_json_generator(WebServer *web_server, void *source);
351 static json::SerializationBuffer<> light_all_json_generator(WebServer *web_server, void *source);
352#endif
353
354#ifdef USE_TEXT_SENSOR
356
358 void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
359
362#endif
363
364#ifdef USE_COVER
365 void on_cover_update(cover::Cover *obj) override;
366
368 void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
369
370 static json::SerializationBuffer<> cover_state_json_generator(WebServer *web_server, void *source);
371 static json::SerializationBuffer<> cover_all_json_generator(WebServer *web_server, void *source);
372#endif
373
374#ifdef USE_NUMBER
375 void on_number_update(number::Number *obj) override;
377 void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
378
379 static json::SerializationBuffer<> number_state_json_generator(WebServer *web_server, void *source);
380 static json::SerializationBuffer<> number_all_json_generator(WebServer *web_server, void *source);
381#endif
382
383#ifdef USE_DATETIME_DATE
384 void on_date_update(datetime::DateEntity *obj) override;
386 void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
387
388 static json::SerializationBuffer<> date_state_json_generator(WebServer *web_server, void *source);
389 static json::SerializationBuffer<> date_all_json_generator(WebServer *web_server, void *source);
390#endif
391
392#ifdef USE_DATETIME_TIME
393 void on_time_update(datetime::TimeEntity *obj) override;
395 void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
396
397 static json::SerializationBuffer<> time_state_json_generator(WebServer *web_server, void *source);
398 static json::SerializationBuffer<> time_all_json_generator(WebServer *web_server, void *source);
399#endif
400
401#ifdef USE_DATETIME_DATETIME
404 void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
405
407 static json::SerializationBuffer<> datetime_all_json_generator(WebServer *web_server, void *source);
408#endif
409
410#ifdef USE_TEXT
411 void on_text_update(text::Text *obj) override;
413 void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
414
415 static json::SerializationBuffer<> text_state_json_generator(WebServer *web_server, void *source);
416 static json::SerializationBuffer<> text_all_json_generator(WebServer *web_server, void *source);
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 json::SerializationBuffer<> select_state_json_generator(WebServer *web_server, void *source);
425 static json::SerializationBuffer<> select_all_json_generator(WebServer *web_server, void *source);
426#endif
427
428#ifdef USE_CLIMATE
429 void on_climate_update(climate::Climate *obj) override;
431 void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
432
434 static json::SerializationBuffer<> climate_all_json_generator(WebServer *web_server, void *source);
435#endif
436
437#ifdef USE_LOCK
438 void on_lock_update(lock::Lock *obj) override;
439
441 void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
442
443 static json::SerializationBuffer<> lock_state_json_generator(WebServer *web_server, void *source);
444 static json::SerializationBuffer<> lock_all_json_generator(WebServer *web_server, void *source);
445#endif
446
447#ifdef USE_VALVE
448 void on_valve_update(valve::Valve *obj) override;
449
451 void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
452
453 static json::SerializationBuffer<> valve_state_json_generator(WebServer *web_server, void *source);
454 static json::SerializationBuffer<> valve_all_json_generator(WebServer *web_server, void *source);
455#endif
456
457#ifdef USE_ALARM_CONTROL_PANEL
459
461 void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
462
465#endif
466
467#ifdef USE_WATER_HEATER
469
471 void handle_water_heater_request(AsyncWebServerRequest *request, const UrlMatch &match);
472
475#endif
476
477#ifdef USE_INFRARED
479 void handle_infrared_request(AsyncWebServerRequest *request, const UrlMatch &match);
480
481 static json::SerializationBuffer<> infrared_all_json_generator(WebServer *web_server, void *source);
482#endif
483#ifdef USE_RADIO_FREQUENCY
485 void handle_radio_frequency_request(AsyncWebServerRequest *request, const UrlMatch &match);
486
488#endif
489
490#ifdef USE_EVENT
491 void on_event(event::Event *obj) override;
492
493 static json::SerializationBuffer<> event_state_json_generator(WebServer *web_server, void *source);
494 static json::SerializationBuffer<> event_all_json_generator(WebServer *web_server, void *source);
495
497 void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match);
498#endif
499
500#ifdef USE_UPDATE
501 void on_update(update::UpdateEntity *obj) override;
502
504 void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
505
506 static json::SerializationBuffer<> update_state_json_generator(WebServer *web_server, void *source);
507 static json::SerializationBuffer<> update_all_json_generator(WebServer *web_server, void *source);
508#endif
509
511 bool canHandle(AsyncWebServerRequest *request) const override;
513 void handleRequest(AsyncWebServerRequest *request) override;
515 bool isRequestHandlerTrivial() const override; // NOLINT(readability-identifier-naming)
516
517#ifdef USE_WEBSERVER_SORTING
518 void add_entity_config(EntityBase *entity, float weight, uint64_t group);
519 void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight);
520
521 std::map<EntityBase *, SortingComponents> sorting_entitys_;
522 std::map<uint64_t, SortingGroup> sorting_groups_;
523#endif
524
525 bool include_internal_{false};
526
527 protected:
528 void add_sorting_info_(JsonObject &root, EntityBase *entity);
529
530#ifdef USE_LIGHT
531 // Helper to parse and apply a float parameter with optional scaling
532 template<typename T, typename Ret>
533 void parse_light_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret (T::*setter)(float),
534 float scale = 1.0f) {
535 auto value = parse_number<float>(request->arg(param_name).c_str());
536 if (value.has_value()) {
537 (call.*setter)(*value / scale);
538 }
539 }
540
541 // Helper to parse and apply a uint32_t parameter with optional scaling
542 template<typename T, typename Ret>
543 void parse_light_param_uint_(AsyncWebServerRequest *request, ParamNameType param_name, T &call,
544 Ret (T::*setter)(uint32_t), uint32_t scale = 1) {
545 auto value = parse_number<uint32_t>(request->arg(param_name).c_str());
546 if (value.has_value()) {
547 (call.*setter)(*value * scale);
548 }
549 }
550#endif
551
552 // Generic helper to parse and apply a numeric parameter
553 template<typename NumT, typename T, typename Ret>
554 void parse_num_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret (T::*setter)(NumT)) {
555 auto value = parse_number<NumT>(request->arg(param_name).c_str());
556 if (value.has_value()) {
557 (call.*setter)(*value);
558 }
559 }
560
561 // Generic helper to parse and apply a string parameter using const char* setter (avoids std::string allocation)
562 template<typename T, typename Ret>
563 void parse_cstr_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call,
564 Ret (T::*setter)(const char *, size_t)) {
565 if (request->hasArg(param_name)) {
566 const auto &value = request->arg(param_name);
567 (call.*setter)(value.c_str(), value.length());
568 }
569 }
570
571 // Generic helper to parse and apply a bool parameter
572 // Accepts: "on", "true", "1" (case-insensitive) as true
573 // Accepts: "off", "false", "0" (case-insensitive) as false
574 // Invalid values are ignored (setter not called)
575 template<typename T, typename Ret>
576 void parse_bool_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret (T::*setter)(bool)) {
577 const auto &param_value = request->arg(param_name);
578 // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility
579 if (param_value.length() > 0) { // NOLINT(readability-container-size-empty)
580 // First check on/off (default), then true/false (custom)
581 auto val = parse_on_off(param_value.c_str());
582 if (val == PARSE_NONE) {
583 val = parse_on_off(param_value.c_str(), "true", "false");
584 }
585 if (val == PARSE_ON || param_value == "1") {
586 (call.*setter)(true);
587 } else if (val == PARSE_OFF || param_value == "0") {
588 (call.*setter)(false);
589 }
590 // PARSE_NONE/PARSE_TOGGLE: ignore invalid values
591 }
592 }
593
595#ifdef USE_ESP32
596 AsyncEventSource events_{"/events", this};
597#elif USE_ARDUINO
599#endif
600
601#if USE_WEBSERVER_VERSION == 1
602 const char *css_url_{nullptr};
603 const char *js_url_{nullptr};
604#endif
605#ifdef USE_WEBSERVER_CSS_INCLUDE
606 const char *css_include_{nullptr};
607#endif
608#ifdef USE_WEBSERVER_JS_INCLUDE
609 const char *js_include_{nullptr};
610#endif
611 bool expose_log_{true};
612#ifdef USE_WEBSERVER_ALLOWED_ORIGINS
613 // Extra origins allowed to make cross-origin browser requests ("*" means any origin).
614 // Only compiled when allowed_origins is configured; same-origin is always allowed regardless.
616#endif
617
621 bool is_request_origin_allowed_(AsyncWebServerRequest *request, const std::string &origin);
622
623 private:
624#ifdef USE_SENSOR
625 json::SerializationBuffer<> sensor_json_(sensor::Sensor *obj, float value, JsonDetail start_config);
626#endif
627#ifdef USE_SWITCH
628 json::SerializationBuffer<> switch_json_(switch_::Switch *obj, bool value, JsonDetail start_config);
629#endif
630#ifdef USE_BUTTON
631 json::SerializationBuffer<> button_json_(button::Button *obj, JsonDetail start_config);
632#endif
633#ifdef USE_BINARY_SENSOR
634 json::SerializationBuffer<> binary_sensor_json_(binary_sensor::BinarySensor *obj, bool value,
635 JsonDetail start_config);
636#endif
637#ifdef USE_FAN
638 json::SerializationBuffer<> fan_json_(fan::Fan *obj, JsonDetail start_config);
639#endif
640#ifdef USE_LIGHT
641 json::SerializationBuffer<> light_json_(light::LightState *obj, JsonDetail start_config);
642#endif
643#ifdef USE_TEXT_SENSOR
644 json::SerializationBuffer<> text_sensor_json_(text_sensor::TextSensor *obj, const std::string &value,
645 JsonDetail start_config);
646#endif
647#ifdef USE_COVER
648 json::SerializationBuffer<> cover_json_(cover::Cover *obj, JsonDetail start_config);
649#endif
650#ifdef USE_NUMBER
651 json::SerializationBuffer<> number_json_(number::Number *obj, float value, JsonDetail start_config);
652#endif
653#ifdef USE_DATETIME_DATE
654 json::SerializationBuffer<> date_json_(datetime::DateEntity *obj, JsonDetail start_config);
655#endif
656#ifdef USE_DATETIME_TIME
657 json::SerializationBuffer<> time_json_(datetime::TimeEntity *obj, JsonDetail start_config);
658#endif
659#ifdef USE_DATETIME_DATETIME
660 json::SerializationBuffer<> datetime_json_(datetime::DateTimeEntity *obj, JsonDetail start_config);
661#endif
662#ifdef USE_TEXT
663 json::SerializationBuffer<> text_json_(text::Text *obj, const std::string &value, JsonDetail start_config);
664#endif
665#ifdef USE_SELECT
666 json::SerializationBuffer<> select_json_(select::Select *obj, StringRef value, JsonDetail start_config);
667#endif
668#ifdef USE_CLIMATE
669 json::SerializationBuffer<> climate_json_(climate::Climate *obj, JsonDetail start_config);
670#endif
671#ifdef USE_LOCK
672 json::SerializationBuffer<> lock_json_(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
673#endif
674#ifdef USE_VALVE
675 json::SerializationBuffer<> valve_json_(valve::Valve *obj, JsonDetail start_config);
676#endif
677#ifdef USE_ALARM_CONTROL_PANEL
680 JsonDetail start_config);
681#endif
682#ifdef USE_EVENT
683 json::SerializationBuffer<> event_json_(event::Event *obj, StringRef event_type, JsonDetail start_config);
684#endif
685#ifdef USE_WATER_HEATER
686 json::SerializationBuffer<> water_heater_json_(water_heater::WaterHeater *obj, JsonDetail start_config);
687#endif
688#ifdef USE_INFRARED
689 json::SerializationBuffer<> infrared_json_(infrared::Infrared *obj, JsonDetail start_config);
690#endif
691#ifdef USE_RADIO_FREQUENCY
692 json::SerializationBuffer<> radio_frequency_json_(radio_frequency::RadioFrequency *obj, JsonDetail start_config);
693#endif
694#ifdef USE_UPDATE
695 json::SerializationBuffer<> update_json_(update::UpdateEntity *obj, JsonDetail start_config);
696#endif
697};
698
699} // namespace esphome::web_server
700#endif
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:534
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
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:187
Base class for all cover devices.
Definition cover.h:110
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:114
Buffer for JSON serialization that uses stack allocation for small payloads.
Definition json_util.h:21
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
Base class for all locks.
Definition lock.h:112
Base-class for all numbers.
Definition number.h:29
RadioFrequency - Base class for radio frequency implementations.
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:47
Base class for all switches.
Definition switch.h:38
Base-class for all text inputs.
Definition text.h:21
Base class for all valve devices.
Definition valve.h:103
DeferredUpdateEventSource(WebServer *ws, const String &url)
Definition web_server.h:157
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES
Definition web_server.h:149
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator)
void try_send_nodefer(const char *message, size_t message_len, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
std::vector< DeferredEvent > deferred_queue_
Definition web_server.h:146
void on_client_connect_(DeferredUpdateEventSource *source)
void try_send_nodefer(const char *message, size_t message_len, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void add_new_client(WebServer *ws, AsyncWebServerRequest *request)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
void on_client_disconnect_(DeferredUpdateEventSource *source)
bool loop()
Returns true if there are event sources remaining (including pending cleanup).
This class allows users to create a web server with their ESP nodes.
Definition web_server.h:193
void setup() override
Setup the internal web server and register handlers.
void on_update(update::UpdateEntity *obj) override
static json::SerializationBuffer radio_frequency_all_json_generator(WebServer *web_server, void *source)
void on_water_heater_update(water_heater::WaterHeater *obj) override
void set_expose_log(bool expose_log)
Set whether or not the webserver should expose the Log.
Definition web_server.h:243
json::SerializationBuffer get_config_json()
Return the webserver configuration as JSON.
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition web_server.h:521
static json::SerializationBuffer text_state_json_generator(WebServer *web_server, void *source)
void on_text_update(text::Text *obj) override
void on_light_update(light::LightState *obj) override
static json::SerializationBuffer datetime_state_json_generator(WebServer *web_server, void *source)
void on_cover_update(cover::Cover *obj) override
static json::SerializationBuffer lock_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer alarm_control_panel_all_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.
static json::SerializationBuffer text_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer switch_all_json_generator(WebServer *web_server, void *source)
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
void set_allowed_origins(std::initializer_list< const char * > origins)
Set the origins that browsers are allowed to make cross-origin requests from.
Definition web_server.h:258
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len)
static json::SerializationBuffer event_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer update_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer text_sensor_all_json_generator(WebServer *web_server, void *source)
void handle_water_heater_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a water_heater request under '/water_heater/<id>/<mode/set>'.
bool isRequestHandlerTrivial() const override
This web handle is not trivial.
static json::SerializationBuffer cover_all_json_generator(WebServer *web_server, void *source)
WebServer(web_server_base::WebServerBase *base)
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, ParamNameType param_name, T &call, Ret(T::*setter)(uint32_t), uint32_t scale=1)
Definition web_server.h:543
static json::SerializationBuffer datetime_all_json_generator(WebServer *web_server, void *source)
DeferredUpdateEventSourceList events_
Definition web_server.h:598
static json::SerializationBuffer light_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer climate_state_json_generator(WebServer *web_server, void *source)
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
static json::SerializationBuffer date_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer sensor_state_json_generator(WebServer *web_server, void *source)
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:238
void handle_radio_frequency_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a radio frequency request under '/radio_frequency/<id>/transmit'.
void parse_light_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(float), float scale=1.0f)
Definition web_server.h:533
void handle_css_request(AsyncWebServerRequest *request)
Handle included css request under '/0.css'.
static json::SerializationBuffer select_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer infrared_all_json_generator(WebServer *web_server, void *source)
void on_valve_update(valve::Valve *obj) override
void on_climate_update(climate::Climate *obj) override
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 json::SerializationBuffer sensor_all_json_generator(WebServer *web_server, void *source)
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text input request under '/text/<id>'.
static json::SerializationBuffer number_all_json_generator(WebServer *web_server, void *source)
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a cover request under '/cover/<id>/<open/close/stop/set>'.
static json::SerializationBuffer select_state_json_generator(WebServer *web_server, void *source)
void on_switch_update(switch_::Switch *obj) override
static json::SerializationBuffer water_heater_state_json_generator(WebServer *web_server, void *source)
web_server_base::WebServerBase * base_
Definition web_server.h:594
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 json::SerializationBuffer time_state_json_generator(WebServer *web_server, void *source)
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text sensor request under '/text_sensor/<id>'.
void parse_bool_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(bool))
Definition web_server.h:576
bool is_request_origin_allowed_(AsyncWebServerRequest *request, const std::string &origin)
Check whether the given request Origin is permitted.
void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a date request under '/date/<id>'.
void set_js_url(const char *js_url)
Set the URL to the script that's embedded in the index page.
void handle_infrared_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle an infrared request under '/infrared/<id>/transmit'.
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a sensor request under '/sensor/<id>'.
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a number request under '/number/<id>'.
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
void handle_js_request(AsyncWebServerRequest *request)
Handle included js request under '/0.js'.
static json::SerializationBuffer valve_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer fan_all_json_generator(WebServer *web_server, void *source)
void set_js_include(const char *js_include)
Set local path to the script that's embedded in the index page.
static json::SerializationBuffer lock_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer update_state_json_generator(WebServer *web_server, void *source)
void handleRequest(AsyncWebServerRequest *request) override
Override the web handler's handleRequest method.
static json::SerializationBuffer button_all_json_generator(WebServer *web_server, void *source)
void on_datetime_update(datetime::DateTimeEntity *obj) override
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a fan request under '/fan/<id>/</turn_on/turn_off/toggle>'.
static json::SerializationBuffer alarm_control_panel_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer time_all_json_generator(WebServer *web_server, void *source)
void parse_cstr_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(const char *, size_t))
Definition web_server.h:563
static json::SerializationBuffer water_heater_all_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>'.
static json::SerializationBuffer date_state_json_generator(WebServer *web_server, void *source)
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a binary sensor request under '/binary_sensor/<id>'.
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:522
void set_css_include(const char *css_include)
Set local path to the script that's embedded in the index page.
FixedVector< const char * > allowed_origins_
Definition web_server.h:615
bool canHandle(AsyncWebServerRequest *request) const override
Override the web handler's canHandle method.
static json::SerializationBuffer climate_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer fan_state_json_generator(WebServer *web_server, void *source)
void on_event(event::Event *obj) override
static json::SerializationBuffer cover_state_json_generator(WebServer *web_server, void *source)
void handle_pna_cors_request(AsyncWebServerRequest *request)
static json::SerializationBuffer binary_sensor_state_json_generator(WebServer *web_server, void *source)
void on_fan_update(fan::Fan *obj) override
void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a datetime request under '/datetime/<id>'.
static json::SerializationBuffer event_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer binary_sensor_all_json_generator(WebServer *web_server, void *source)
void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a alarm_control_panel request under '/alarm_control_panel/<id>'.
void on_lock_update(lock::Lock *obj) override
static json::SerializationBuffer switch_state_json_generator(WebServer *web_server, void *source)
float get_setup_priority() const override
MQTT setup priority.
void on_select_update(select::Select *obj) override
void on_time_update(datetime::TimeEntity *obj) override
void parse_num_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(NumT))
Definition web_server.h:554
static json::SerializationBuffer text_sensor_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer number_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer valve_all_json_generator(WebServer *web_server, void *source)
void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a update request under '/update/<id>'.
static json::SerializationBuffer light_all_json_generator(WebServer *web_server, void *source)
void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight)
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a climate request under '/climate/<id>'.
void on_text_sensor_update(text_sensor::TextSensor *obj) override
const LogString * message
Definition component.cpp:35
mopeka_std_values val[3]
LockState
Enum for all states a lock can be in.
Definition lock.h:23
const __FlashStringHelper * ParamNameType
Definition web_server.h:41
json::SerializationBuffer<>(WebServer *, void *) message_generator_t
Definition web_server.h:107
const char * tag
Definition log.h:74
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:400
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1157
@ PARSE_ON
Definition helpers.h:1567
@ PARSE_OFF
Definition helpers.h:1568
@ PARSE_NONE
Definition helpers.h:1566
static void uint32_t
Result of matching a URL against an entity.
Definition web_server.h:54
bool matched
True if entity matched the URL.
Definition web_server.h:55
bool action_is_empty
True if no action/method segment in URL.
Definition web_server.h:56
Internal helper struct that is used to parse incoming URLs.
Definition web_server.h:60
StringRef device_name
Device name within URL, empty for main device.
Definition web_server.h:65
bool valid
Whether this match is valid.
Definition web_server.h:67
bool domain_equals(const __FlashStringHelper *str) const
Definition web_server.h:75
EntityMatchResult match_entity(EntityBase *entity) const
Match entity by name Returns EntityMatchResult with match status and whether action segment is empty.
StringRef method
Method within URL, for example "turn_on".
Definition web_server.h:63
bool domain_equals(const char *str) const
Definition web_server.h:70
bool method_equals(const __FlashStringHelper *str) const
Definition web_server.h:76
bool method_equals(const char *str) const
Definition web_server.h:71
StringRef domain
Domain within URL, for example "sensor".
Definition web_server.h:61
StringRef id
Entity name/id within URL, for example "Temperature".
Definition web_server.h:62
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:28
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE