ESPHome  2024.5.2
web_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "list_entities.h"
4 
8 
9 #include <vector>
10 #ifdef USE_ESP32
11 #include <freertos/FreeRTOS.h>
12 #include <freertos/semphr.h>
13 #include <deque>
14 #endif
15 
16 #if USE_WEBSERVER_VERSION >= 2
17 extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
18 extern const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE;
19 #endif
20 
21 #ifdef USE_WEBSERVER_CSS_INCLUDE
22 extern const uint8_t ESPHOME_WEBSERVER_CSS_INCLUDE[] PROGMEM;
23 extern const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE;
24 #endif
25 
26 #ifdef USE_WEBSERVER_JS_INCLUDE
27 extern const uint8_t ESPHOME_WEBSERVER_JS_INCLUDE[] PROGMEM;
28 extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
29 #endif
30 
31 namespace esphome {
32 namespace web_server {
33 
35 struct UrlMatch {
36  std::string domain;
37  std::string id;
38  std::string method;
39  bool valid;
40 };
41 
43 
53 class WebServer : public Controller, public Component, public AsyncWebHandler {
54  public:
56 
57 #if USE_WEBSERVER_VERSION == 1
58 
63  void set_css_url(const char *css_url);
64 
70  void set_js_url(const char *js_url);
71 #endif
72 
73 #ifdef USE_WEBSERVER_CSS_INCLUDE
74 
78  void set_css_include(const char *css_include);
79 #endif
80 
81 #ifdef USE_WEBSERVER_JS_INCLUDE
82 
86  void set_js_include(const char *js_include);
87 #endif
88 
94  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
99  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
104  void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
105 
106  // ========== INTERNAL METHODS ==========
107  // (In most use cases you won't need these)
109  void setup() override;
110  void loop() override;
111 
112  void dump_config() override;
113 
115  float get_setup_priority() const override;
116 
118  void handle_index_request(AsyncWebServerRequest *request);
119 
121  std::string get_config_json();
122 
123 #ifdef USE_WEBSERVER_CSS_INCLUDE
124  void handle_css_request(AsyncWebServerRequest *request);
126 #endif
127 
128 #ifdef USE_WEBSERVER_JS_INCLUDE
129  void handle_js_request(AsyncWebServerRequest *request);
131 #endif
132 
133 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
134  // Handle Private Network Access CORS OPTIONS request
135  void handle_pna_cors_request(AsyncWebServerRequest *request);
136 #endif
137 
138 #ifdef USE_SENSOR
139  void on_sensor_update(sensor::Sensor *obj, float state) override;
141  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
142 
144  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
145 #endif
146 
147 #ifdef USE_SWITCH
148  void on_switch_update(switch_::Switch *obj, bool state) override;
149 
151  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
152 
154  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
155 #endif
156 
157 #ifdef USE_BUTTON
158  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
160 
162  std::string button_json(button::Button *obj, JsonDetail start_config);
163 #endif
164 
165 #ifdef USE_BINARY_SENSOR
166  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
167 
169  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
170 
172  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
173 #endif
174 
175 #ifdef USE_FAN
176  void on_fan_update(fan::Fan *obj) override;
177 
179  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
180 
182  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
183 #endif
184 
185 #ifdef USE_LIGHT
186  void on_light_update(light::LightState *obj) override;
187 
189  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
190 
192  std::string light_json(light::LightState *obj, JsonDetail start_config);
193 #endif
194 
195 #ifdef USE_TEXT_SENSOR
196  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
197 
199  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
200 
202  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
203 #endif
204 
205 #ifdef USE_COVER
206  void on_cover_update(cover::Cover *obj) override;
207 
209  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
210 
212  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
213 #endif
214 
215 #ifdef USE_NUMBER
216  void on_number_update(number::Number *obj, float state) override;
218  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
219 
221  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
222 #endif
223 
224 #ifdef USE_DATETIME_DATE
225  void on_date_update(datetime::DateEntity *obj) override;
227  void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match);
228 
230  std::string date_json(datetime::DateEntity *obj, JsonDetail start_config);
231 #endif
232 
233 #ifdef USE_DATETIME_TIME
234  void on_time_update(datetime::TimeEntity *obj) override;
236  void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match);
237 
239  std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config);
240 #endif
241 
242 #ifdef USE_DATETIME_DATETIME
243  void on_datetime_update(datetime::DateTimeEntity *obj) override;
245  void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match);
246 
248  std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config);
249 #endif
250 
251 #ifdef USE_TEXT
252  void on_text_update(text::Text *obj, const std::string &state) override;
254  void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
255 
257  std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
258 #endif
259 
260 #ifdef USE_SELECT
261  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
263  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
264 
266  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
267 #endif
268 
269 #ifdef USE_CLIMATE
270  void on_climate_update(climate::Climate *obj) override;
272  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
273 
275  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
276 #endif
277 
278 #ifdef USE_LOCK
279  void on_lock_update(lock::Lock *obj) override;
280 
282  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
283 
285  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
286 #endif
287 
288 #ifdef USE_VALVE
289  void on_valve_update(valve::Valve *obj) override;
290 
292  void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match);
293 
295  std::string valve_json(valve::Valve *obj, JsonDetail start_config);
296 #endif
297 
298 #ifdef USE_ALARM_CONTROL_PANEL
299  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
300 
302  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
303 
305  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
307 #endif
308 
309 #ifdef USE_EVENT
310  void on_event(event::Event *obj, const std::string &event_type) override;
311 
313  std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config);
314 #endif
315 
317  bool canHandle(AsyncWebServerRequest *request) override;
319  void handleRequest(AsyncWebServerRequest *request) override;
321  bool isRequestHandlerTrivial() override;
322 
323  protected:
324  void schedule_(std::function<void()> &&f);
327  AsyncEventSource events_{"/events"};
329 #if USE_WEBSERVER_VERSION == 1
330  const char *css_url_{nullptr};
331  const char *js_url_{nullptr};
332 #endif
333 #ifdef USE_WEBSERVER_CSS_INCLUDE
334  const char *css_include_{nullptr};
335 #endif
336 #ifdef USE_WEBSERVER_JS_INCLUDE
337  const char *js_include_{nullptr};
338 #endif
339  bool include_internal_{false};
340  bool allow_ota_{true};
341  bool expose_log_{true};
342 #ifdef USE_ESP32
343  std::deque<std::function<void()>> to_schedule_;
344  SemaphoreHandle_t to_schedule_lock_;
345 #endif
346 };
347 
348 } // namespace web_server
349 } // 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:99
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:344
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:53
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:94
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:104
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:35
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
std::string domain
The domain of the component, for example "sensor".
Definition: web_server.h:36
Base-class for all numbers.
Definition: number.h:39
bool valid
Whether this match is valid.
Definition: web_server.h:39
std::string id
The id of the device that&#39;s being accessed, for example "living_room_fan".
Definition: web_server.h:37
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML [] PROGMEM
Definition: web_server.h:22
Base-class for all selects.
Definition: select.h:31
web_server_base::WebServerBase * base_
Definition: web_server.h:326
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:328
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:343
std::string method
The method that&#39;s being called, for example "turn_on".
Definition: web_server.h:38
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