ESPHome  2024.4.1
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_TEXT
243  void on_text_update(text::Text *obj, const std::string &state) override;
245  void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match);
246 
248  std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config);
249 #endif
250 
251 #ifdef USE_SELECT
252  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
254  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
255 
257  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
258 #endif
259 
260 #ifdef USE_CLIMATE
261  void on_climate_update(climate::Climate *obj) override;
263  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
264 
266  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
267 #endif
268 
269 #ifdef USE_LOCK
270  void on_lock_update(lock::Lock *obj) override;
271 
273  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
274 
276  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
277 #endif
278 
279 #ifdef USE_ALARM_CONTROL_PANEL
280  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
281 
283  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
284 
286  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
288 #endif
289 
291  bool canHandle(AsyncWebServerRequest *request) override;
293  void handleRequest(AsyncWebServerRequest *request) override;
295  bool isRequestHandlerTrivial() override;
296 
297  protected:
298  void schedule_(std::function<void()> &&f);
301  AsyncEventSource events_{"/events"};
303 #if USE_WEBSERVER_VERSION == 1
304  const char *css_url_{nullptr};
305  const char *js_url_{nullptr};
306 #endif
307 #ifdef USE_WEBSERVER_CSS_INCLUDE
308  const char *css_include_{nullptr};
309 #endif
310 #ifdef USE_WEBSERVER_JS_INCLUDE
311  const char *js_include_{nullptr};
312 #endif
313  bool include_internal_{false};
314  bool allow_ota_{true};
315  bool expose_log_{true};
316 #ifdef USE_ESP32
317  std::deque<std::function<void()>> to_schedule_;
318  SemaphoreHandle_t to_schedule_lock_;
319 #endif
320 };
321 
322 } // namespace web_server
323 } // 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:318
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:300
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 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:302
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:317
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