ESPHome  2024.7.1
web_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "list_entities.h"
4 
9 
10 #include <map>
11 #include <vector>
12 #ifdef USE_ESP32
13 #include <freertos/FreeRTOS.h>
14 #include <freertos/semphr.h>
15 #include <deque>
16 #endif
17 
18 #if USE_WEBSERVER_VERSION >= 2
19 extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
20 extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
21 #endif
22 
23 #ifdef USE_WEBSERVER_CSS_INCLUDE
24 extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
25 extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
26 #endif
27 
28 #ifdef USE_WEBSERVER_JS_INCLUDE
29 extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
30 extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
31 #endif
32 
33 namespace esphome {
34 namespace web_server {
35 
37 struct UrlMatch {
38  std::string domain;
39  std::string id;
40  std::string method;
41  bool valid;
42 };
43 
45  float weight;
46 };
47 
49 
59 class WebServer : public Controller, public Component, public AsyncWebHandler {
60  public:
62 
63 #if USE_WEBSERVER_VERSION == 1
64 
69  void set_css_url(const char *css_url);
70 
76  void set_js_url(const char *js_url);
77 #endif
78 
79 #ifdef USE_WEBSERVER_CSS_INCLUDE
80 
84  void set_css_include(const char *css_include);
85 #endif
86 
87 #ifdef USE_WEBSERVER_JS_INCLUDE
88 
92  void set_js_include(const char *js_include);
93 #endif
94 
100  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
105  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
110  void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
111 
112  // ========== INTERNAL METHODS ==========
113  // (In most use cases you won't need these)
115  void setup() override;
116  void loop() override;
117 
118  void dump_config() override;
119 
121  float get_setup_priority() const override;
122 
124  void handle_index_request(AsyncWebServerRequest *request);
125 
127  std::string get_config_json();
128 
129 #ifdef USE_WEBSERVER_CSS_INCLUDE
130  void handle_css_request(AsyncWebServerRequest *request);
132 #endif
133 
134 #ifdef USE_WEBSERVER_JS_INCLUDE
135  void handle_js_request(AsyncWebServerRequest *request);
137 #endif
138 
139 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
140  // Handle Private Network Access CORS OPTIONS request
141  void handle_pna_cors_request(AsyncWebServerRequest *request);
142 #endif
143 
144 #ifdef USE_SENSOR
145  void on_sensor_update(sensor::Sensor *obj, float state) override;
147  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
148 
150  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
151 #endif
152 
153 #ifdef USE_SWITCH
154  void on_switch_update(switch_::Switch *obj, bool state) override;
155 
157  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
158 
160  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
161 #endif
162 
163 #ifdef USE_BUTTON
164  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
166 
168  std::string button_json(button::Button *obj, JsonDetail start_config);
169 #endif
170 
171 #ifdef USE_BINARY_SENSOR
172  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
173 
175  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
176 
178  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
179 #endif
180 
181 #ifdef USE_FAN
182  void on_fan_update(fan::Fan *obj) override;
183 
185  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
186 
188  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
189 #endif
190 
191 #ifdef USE_LIGHT
192  void on_light_update(light::LightState *obj) override;
193 
195  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
196 
198  std::string light_json(light::LightState *obj, JsonDetail start_config);
199 #endif
200 
201 #ifdef USE_TEXT_SENSOR
202  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
203 
205  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
206 
208  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
209 #endif
210 
211 #ifdef USE_COVER
212  void on_cover_update(cover::Cover *obj) override;
213 
215  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
216 
218  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
219 #endif
220 
221 #ifdef USE_NUMBER
222  void on_number_update(number::Number *obj, float state) override;
224  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
225 
227  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
228 #endif
229 
230 #ifdef USE_DATETIME_DATE
231  void on_date_update(datetime::DateEntity *obj) override;
233  void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
234 
236  std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
237 #endif
238 
239 #ifdef USE_DATETIME_TIME
240  void on_time_update(datetime::TimeEntity *obj) override;
242  void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
243 
245  std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
246 #endif
247 
248 #ifdef USE_DATETIME_DATETIME
249  void on_datetime_update(datetime::DateTimeEntity *obj) override;
251  void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
252 
254  std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
255 #endif
256 
257 #ifdef USE_TEXT
258  void on_text_update(text::Text *obj, const std::string &state) override;
260  void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
261 
263  std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
264 #endif
265 
266 #ifdef USE_SELECT
267  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
269  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
270 
272  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
273 #endif
274 
275 #ifdef USE_CLIMATE
276  void on_climate_update(climate::Climate *obj) override;
278  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
279 
281  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
282 #endif
283 
284 #ifdef USE_LOCK
285  void on_lock_update(lock::Lock *obj) override;
286 
288  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
289 
291  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
292 #endif
293 
294 #ifdef USE_VALVE
295  void on_valve_update(valve::Valve *obj) override;
296 
298  void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
299 
301  std::string valve_json(valve::Valve *obj, JsonDetail start_config);
302 #endif
303 
304 #ifdef USE_ALARM_CONTROL_PANEL
305  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
306 
308  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
309 
311  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
313 #endif
314 
315 #ifdef USE_EVENT
316  void on_event(event::Event *obj, const std::string &event_type) override;
317 
319  std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
320 #endif
321 
322 #ifdef USE_UPDATE
323  void on_update(update::UpdateEntity *obj) override;
324 
326  void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
327 
329  std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
330 #endif
331 
333  bool canHandle(AsyncWebServerRequest *request) override;
335  void handleRequest(AsyncWebServerRequest *request) override;
337  bool isRequestHandlerTrivial() override;
338 
339  void add_entity_to_sorting_list(EntityBase *entity, float weight);
340 
341  protected:
342  void schedule_(std::function<void()> &&f);
345  AsyncEventSource events_{"/events"};
347  std::map<EntityBase *, SortingComponents> sorting_entitys_;
348 #if USE_WEBSERVER_VERSION == 1
349  const char *css_url_{nullptr};
350  const char *js_url_{nullptr};
351 #endif
352 #ifdef USE_WEBSERVER_CSS_INCLUDE
353  const char *css_include_{nullptr};
354 #endif
355 #ifdef USE_WEBSERVER_JS_INCLUDE
356  const char *js_include_{nullptr};
357 #endif
358  bool include_internal_{false};
359  bool allow_ota_{true};
360  bool expose_log_{true};
361 #ifdef USE_ESP32
362  std::deque<std::function<void()>> to_schedule_;
363  SemaphoreHandle_t to_schedule_lock_;
364 #endif
365 };
366 
367 } // namespace web_server
368 } // namespace esphome
void setup()
Base class for all switches.
Definition: switch.h:39
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
void loop()
Base class for all cover devices.
Definition: cover.h:111
void set_allow_ota(bool allow_ota)
Set whether or not the webserver should expose the OTA form and handler.
Definition: web_server.h:105
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:363
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:59
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition: web_server.h:347
Base class for all buttons.
Definition: button.h:29
void set_include_internal(bool include_internal)
Determine whether internal components should be displayed on the web server.
Definition: web_server.h:100
Base-class for all text inputs.
Definition: text.h:24
void set_expose_log(bool expose_log)
Set whether or not the webserver should expose the Log.
Definition: web_server.h:110
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:37
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
std::string domain
The domain of the component, for example "sensor".
Definition: web_server.h:38
Base-class for all numbers.
Definition: number.h:39
bool valid
Whether this match is valid.
Definition: web_server.h:41
std::string id
The id of the device that&#39;s being accessed, for example "living_room_fan".
Definition: web_server.h:39
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML [] PROGMEM
Definition: web_server.h:24
Base-class for all selects.
Definition: select.h:31
web_server_base::WebServerBase * base_
Definition: web_server.h:344
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
Base-class for all sensors.
Definition: sensor.h:57
ListEntitiesIterator entities_iterator_
Definition: web_server.h:346
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:362
std::string method
The method that&#39;s being called, for example "turn_on".
Definition: web_server.h:40
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
bool state
Definition: fan.h:34