ESPHome  2024.10.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  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  std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
327 #endif
328 
329 #ifdef USE_UPDATE
330  void on_update(update::UpdateEntity *obj) override;
331 
333  void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match);
334 
336  std::string update_json(update::UpdateEntity *obj, JsonDetail start_config);
337 #endif
338 
340  bool canHandle(AsyncWebServerRequest *request) override;
342  void handleRequest(AsyncWebServerRequest *request) override;
344  bool isRequestHandlerTrivial() override; // NOLINT(readability-identifier-naming)
345 
346  void add_entity_config(EntityBase *entity, float weight, uint64_t group);
347  void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight);
348 
349  protected:
350  void schedule_(std::function<void()> &&f);
353  AsyncEventSource events_{"/events"};
355  std::map<EntityBase *, SortingComponents> sorting_entitys_;
356  std::map<uint64_t, SortingGroup> sorting_groups_;
357 
358 #if USE_WEBSERVER_VERSION == 1
359  const char *css_url_{nullptr};
360  const char *js_url_{nullptr};
361 #endif
362 #ifdef USE_WEBSERVER_CSS_INCLUDE
363  const char *css_include_{nullptr};
364 #endif
365 #ifdef USE_WEBSERVER_JS_INCLUDE
366  const char *js_include_{nullptr};
367 #endif
368  bool include_internal_{false};
369  bool allow_ota_{true};
370  bool expose_log_{true};
371 #ifdef USE_ESP32
372  std::deque<std::function<void()>> to_schedule_;
373  SemaphoreHandle_t to_schedule_lock_;
374 #endif
375 };
376 
377 } // namespace web_server
378 } // namespace esphome
379 #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:112
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:373
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:355
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:356
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:352
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:354
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:372
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