ESPHome  2024.9.2
web_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "list_entities.h"
4 
6 #ifdef USE_WEBSERVER
10 
11 #include <map>
12 #include <vector>
13 #ifdef USE_ESP32
14 #include <freertos/FreeRTOS.h>
15 #include <freertos/semphr.h>
16 #include <deque>
17 #endif
18 
19 #if USE_WEBSERVER_VERSION >= 2
20 extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
21 extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
22 #endif
23 
24 #ifdef USE_WEBSERVER_CSS_INCLUDE
25 extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
26 extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
27 #endif
28 
29 #ifdef USE_WEBSERVER_JS_INCLUDE
30 extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
31 extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
32 #endif
33 
34 namespace esphome {
35 namespace web_server {
36 
38 struct UrlMatch {
39  std::string domain;
40  std::string id;
41  std::string method;
42  bool valid;
43 };
44 
46  float weight;
47 };
48 
50 
60 class WebServer : public Controller, public Component, public AsyncWebHandler {
61  public:
63 
64 #if USE_WEBSERVER_VERSION == 1
65 
70  void set_css_url(const char *css_url);
71 
77  void set_js_url(const char *js_url);
78 #endif
79 
80 #ifdef USE_WEBSERVER_CSS_INCLUDE
81 
85  void set_css_include(const char *css_include);
86 #endif
87 
88 #ifdef USE_WEBSERVER_JS_INCLUDE
89 
93  void set_js_include(const char *js_include);
94 #endif
95 
101  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
106  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
111  void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
112 
113  // ========== INTERNAL METHODS ==========
114  // (In most use cases you won't need these)
116  void setup() override;
117  void loop() override;
118 
119  void dump_config() override;
120 
122  float get_setup_priority() const override;
123 
125  void handle_index_request(AsyncWebServerRequest *request);
126 
128  std::string get_config_json();
129 
130 #ifdef USE_WEBSERVER_CSS_INCLUDE
131  void handle_css_request(AsyncWebServerRequest *request);
133 #endif
134 
135 #ifdef USE_WEBSERVER_JS_INCLUDE
136  void handle_js_request(AsyncWebServerRequest *request);
138 #endif
139 
140 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
141  // Handle Private Network Access CORS OPTIONS request
142  void handle_pna_cors_request(AsyncWebServerRequest *request);
143 #endif
144 
145 #ifdef USE_SENSOR
146  void on_sensor_update(sensor::Sensor *obj, float state) override;
148  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
149 
151  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
152 #endif
153 
154 #ifdef USE_SWITCH
155  void on_switch_update(switch_::Switch *obj, bool state) override;
156 
158  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
159 
161  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
162 #endif
163 
164 #ifdef USE_BUTTON
165  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
167 
169  std::string button_json(button::Button *obj, JsonDetail start_config);
170 #endif
171 
172 #ifdef USE_BINARY_SENSOR
173  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
174 
176  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
177 
179  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
180 #endif
181 
182 #ifdef USE_FAN
183  void on_fan_update(fan::Fan *obj) override;
184 
186  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
187 
189  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
190 #endif
191 
192 #ifdef USE_LIGHT
193  void on_light_update(light::LightState *obj) override;
194 
196  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
197 
199  std::string light_json(light::LightState *obj, JsonDetail start_config);
200 #endif
201 
202 #ifdef USE_TEXT_SENSOR
203  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
204 
206  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
207 
209  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
210 #endif
211 
212 #ifdef USE_COVER
213  void on_cover_update(cover::Cover *obj) override;
214 
216  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
217 
219  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
220 #endif
221 
222 #ifdef USE_NUMBER
223  void on_number_update(number::Number *obj, float state) override;
225  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
226 
228  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
229 #endif
230 
231 #ifdef USE_DATETIME_DATE
232  void on_date_update(datetime::DateEntity *obj) override;
234  void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
235 
237  std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
238 #endif
239 
240 #ifdef USE_DATETIME_TIME
241  void on_time_update(datetime::TimeEntity *obj) override;
243  void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
244 
246  std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
247 #endif
248 
249 #ifdef USE_DATETIME_DATETIME
250  void on_datetime_update(datetime::DateTimeEntity *obj) override;
252  void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
253 
255  std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
256 #endif
257 
258 #ifdef USE_TEXT
259  void on_text_update(text::Text *obj, const std::string &state) override;
261  void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
262 
264  std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
265 #endif
266 
267 #ifdef USE_SELECT
268  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
270  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
271 
273  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
274 #endif
275 
276 #ifdef USE_CLIMATE
277  void on_climate_update(climate::Climate *obj) override;
279  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
280 
282  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
283 #endif
284 
285 #ifdef USE_LOCK
286  void on_lock_update(lock::Lock *obj) override;
287 
289  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
290 
292  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
293 #endif
294 
295 #ifdef USE_VALVE
296  void on_valve_update(valve::Valve *obj) override;
297 
299  void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
300 
302  std::string valve_json(valve::Valve *obj, JsonDetail start_config);
303 #endif
304 
305 #ifdef USE_ALARM_CONTROL_PANEL
306  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
307 
309  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
310 
312  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
314 #endif
315 
316 #ifdef USE_EVENT
317  void on_event(event::Event *obj, const std::string &event_type) override;
318 
320  std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
321 #endif
322 
323 #ifdef USE_UPDATE
324  void on_update(update::UpdateEntity *obj) override;
325 
327  void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
328 
330  std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
331 #endif
332 
334  bool canHandle(AsyncWebServerRequest *request) override;
336  void handleRequest(AsyncWebServerRequest *request) override;
338  bool isRequestHandlerTrivial() override; // NOLINT(readability-identifier-naming)
339 
340  void add_entity_to_sorting_list(EntityBase *entity, float weight);
341 
342  protected:
343  void schedule_(std::function<void()> &&f);
346  AsyncEventSource events_{"/events"};
348  std::map<EntityBase *, SortingComponents> sorting_entitys_;
349 #if USE_WEBSERVER_VERSION == 1
350  const char *css_url_{nullptr};
351  const char *js_url_{nullptr};
352 #endif
353 #ifdef USE_WEBSERVER_CSS_INCLUDE
354  const char *css_include_{nullptr};
355 #endif
356 #ifdef USE_WEBSERVER_JS_INCLUDE
357  const char *js_include_{nullptr};
358 #endif
359  bool include_internal_{false};
360  bool allow_ota_{true};
361  bool expose_log_{true};
362 #ifdef USE_ESP32
363  std::deque<std::function<void()>> to_schedule_;
364  SemaphoreHandle_t to_schedule_lock_;
365 #endif
366 };
367 
368 } // namespace web_server
369 } // namespace esphome
370 #endif
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:106
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:364
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:60
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition: web_server.h:348
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:101
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:111
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:38
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
std::string domain
The domain of the component, for example "sensor".
Definition: web_server.h:39
Base-class for all numbers.
Definition: number.h:39
bool valid
Whether this match is valid.
Definition: web_server.h:42
std::string id
The id of the device that&#39;s being accessed, for example "living_room_fan".
Definition: web_server.h:40
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML [] PROGMEM
Definition: web_server.h:25
Base-class for all selects.
Definition: select.h:31
web_server_base::WebServerBase * base_
Definition: web_server.h:345
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:347
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:363
std::string method
The method that&#39;s being called, for example "turn_on".
Definition: web_server.h:41
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