ESPHome  2024.11.1
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  uint64_t group_id;
48 };
49 
50 struct SortingGroup {
51  std::string name;
52  float weight;
53 };
54 
56 
66 class WebServer : public Controller, public Component, public AsyncWebHandler {
67  public:
69 
70 #if USE_WEBSERVER_VERSION == 1
71 
76  void set_css_url(const char *css_url);
77 
83  void set_js_url(const char *js_url);
84 #endif
85 
86 #ifdef USE_WEBSERVER_CSS_INCLUDE
87 
91  void set_css_include(const char *css_include);
92 #endif
93 
94 #ifdef USE_WEBSERVER_JS_INCLUDE
95 
99  void set_js_include(const char *js_include);
100 #endif
101 
107  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
112  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
117  void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
118 
119  // ========== INTERNAL METHODS ==========
120  // (In most use cases you won't need these)
122  void setup() override;
123  void loop() override;
124 
125  void dump_config() override;
126 
128  float get_setup_priority() const override;
129 
131  void handle_index_request(AsyncWebServerRequest *request);
132 
134  std::string get_config_json();
135 
136 #ifdef USE_WEBSERVER_CSS_INCLUDE
137  void handle_css_request(AsyncWebServerRequest *request);
139 #endif
140 
141 #ifdef USE_WEBSERVER_JS_INCLUDE
142  void handle_js_request(AsyncWebServerRequest *request);
144 #endif
145 
146 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
147  // Handle Private Network Access CORS OPTIONS request
148  void handle_pna_cors_request(AsyncWebServerRequest *request);
149 #endif
150 
151 #ifdef USE_SENSOR
152  void on_sensor_update(sensor::Sensor *obj, float state) override;
154  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
155 
157  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
158 #endif
159 
160 #ifdef USE_SWITCH
161  void on_switch_update(switch_::Switch *obj, bool state) override;
162 
164  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
165 
167  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
168 #endif
169 
170 #ifdef USE_BUTTON
171  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
173 
175  std::string button_json(button::Button *obj, JsonDetail start_config);
176 #endif
177 
178 #ifdef USE_BINARY_SENSOR
179  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
180 
182  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
183 
185  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
186 #endif
187 
188 #ifdef USE_FAN
189  void on_fan_update(fan::Fan *obj) override;
190 
192  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
193 
195  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
196 #endif
197 
198 #ifdef USE_LIGHT
199  void on_light_update(light::LightState *obj) override;
200 
202  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
203 
205  std::string light_json(light::LightState *obj, JsonDetail start_config);
206 #endif
207 
208 #ifdef USE_TEXT_SENSOR
209  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
210 
212  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
213 
215  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
216 #endif
217 
218 #ifdef USE_COVER
219  void on_cover_update(cover::Cover *obj) override;
220 
222  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
223 
225  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
226 #endif
227 
228 #ifdef USE_NUMBER
229  void on_number_update(number::Number *obj, float state) override;
231  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
232 
234  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
235 #endif
236 
237 #ifdef USE_DATETIME_DATE
238  void on_date_update(datetime::DateEntity *obj) override;
240  void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
241 
243  std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
244 #endif
245 
246 #ifdef USE_DATETIME_TIME
247  void on_time_update(datetime::TimeEntity *obj) override;
249  void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
250 
252  std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
253 #endif
254 
255 #ifdef USE_DATETIME_DATETIME
256  void on_datetime_update(datetime::DateTimeEntity *obj) override;
258  void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
259 
261  std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
262 #endif
263 
264 #ifdef USE_TEXT
265  void on_text_update(text::Text *obj, const std::string &state) override;
267  void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
268 
270  std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
271 #endif
272 
273 #ifdef USE_SELECT
274  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
276  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
277 
279  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
280 #endif
281 
282 #ifdef USE_CLIMATE
283  void on_climate_update(climate::Climate *obj) override;
285  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
286 
288  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
289 #endif
290 
291 #ifdef USE_LOCK
292  void on_lock_update(lock::Lock *obj) override;
293 
295  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
296 
298  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
299 #endif
300 
301 #ifdef USE_VALVE
302  void on_valve_update(valve::Valve *obj) override;
303 
305  void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
306 
308  std::string valve_json(valve::Valve *obj, JsonDetail start_config);
309 #endif
310 
311 #ifdef USE_ALARM_CONTROL_PANEL
312  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
313 
315  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
316 
318  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
320 #endif
321 
322 #ifdef USE_EVENT
323  void on_event(event::Event *obj, const std::string &event_type) override;
324 
326  void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match);
327 
329  std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
330 #endif
331 
332 #ifdef USE_UPDATE
333  void on_update(update::UpdateEntity *obj) override;
334 
336  void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
337 
339  std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
340 #endif
341 
343  bool canHandle(AsyncWebServerRequest *request) override;
345  void handleRequest(AsyncWebServerRequest *request) override;
347  bool isRequestHandlerTrivial() override; // NOLINT(readability-identifier-naming)
348 
349  void add_entity_config(EntityBase *entity, float weight, uint64_t group);
350  void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight);
351 
352  protected:
353  void schedule_(std::function<void()> &&f);
356  AsyncEventSource events_{"/events"};
358  std::map<EntityBase *, SortingComponents> sorting_entitys_;
359  std::map<uint64_t, SortingGroup> sorting_groups_;
360 
361 #if USE_WEBSERVER_VERSION == 1
362  const char *css_url_{nullptr};
363  const char *js_url_{nullptr};
364 #endif
365 #ifdef USE_WEBSERVER_CSS_INCLUDE
366  const char *css_include_{nullptr};
367 #endif
368 #ifdef USE_WEBSERVER_JS_INCLUDE
369  const char *js_include_{nullptr};
370 #endif
371  bool include_internal_{false};
372  bool allow_ota_{true};
373  bool expose_log_{true};
374 #ifdef USE_ESP32
375  std::deque<std::function<void()>> to_schedule_;
376  SemaphoreHandle_t to_schedule_lock_;
377 #endif
378 };
379 
380 } // namespace web_server
381 } // namespace esphome
382 #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:63
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:112
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:376
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:66
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition: web_server.h:358
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:107
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:117
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:38
std::map< uint64_t, SortingGroup > sorting_groups_
Definition: web_server.h:359
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:355
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:357
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:375
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