ESPHome  2023.8.3
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 <deque>
12 #include <freertos/FreeRTOS.h>
13 #include <freertos/semphr.h>
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_SENSOR
134  void on_sensor_update(sensor::Sensor *obj, float state) override;
136  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
137 
139  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
140 #endif
141 
142 #ifdef USE_SWITCH
143  void on_switch_update(switch_::Switch *obj, bool state) override;
144 
146  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
147 
149  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
150 #endif
151 
152 #ifdef USE_BUTTON
153  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
155 
157  std::string button_json(button::Button *obj, JsonDetail start_config);
158 #endif
159 
160 #ifdef USE_BINARY_SENSOR
161  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
162 
164  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
165 
167  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
168 #endif
169 
170 #ifdef USE_FAN
171  void on_fan_update(fan::Fan *obj) override;
172 
174  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
175 
177  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
178 #endif
179 
180 #ifdef USE_LIGHT
181  void on_light_update(light::LightState *obj) override;
182 
184  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
185 
187  std::string light_json(light::LightState *obj, JsonDetail start_config);
188 #endif
189 
190 #ifdef USE_TEXT_SENSOR
191  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
192 
194  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
195 
197  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
198 #endif
199 
200 #ifdef USE_COVER
201  void on_cover_update(cover::Cover *obj) override;
202 
204  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
205 
207  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
208 #endif
209 
210 #ifdef USE_NUMBER
211  void on_number_update(number::Number *obj, float state) override;
213  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
214 
216  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
217 #endif
218 
219 #ifdef USE_SELECT
220  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
222  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
223 
225  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
226 #endif
227 
228 #ifdef USE_CLIMATE
229  void on_climate_update(climate::Climate *obj) override;
231  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
232 
234  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
235 #endif
236 
237 #ifdef USE_LOCK
238  void on_lock_update(lock::Lock *obj) override;
239 
241  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
242 
244  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
245 #endif
246 
247 #ifdef USE_ALARM_CONTROL_PANEL
248  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
249 
251  void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match);
252 
254  std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj,
256 #endif
257 
259  bool canHandle(AsyncWebServerRequest *request) override;
261  void handleRequest(AsyncWebServerRequest *request) override;
263  bool isRequestHandlerTrivial() override;
264 
265  protected:
266  void schedule_(std::function<void()> &&f);
269  AsyncEventSource events_{"/events"};
271 #if USE_WEBSERVER_VERSION == 1
272  const char *css_url_{nullptr};
273  const char *js_url_{nullptr};
274 #endif
275 #ifdef USE_WEBSERVER_CSS_INCLUDE
276  const char *css_include_{nullptr};
277 #endif
278 #ifdef USE_WEBSERVER_JS_INCLUDE
279  const char *js_include_{nullptr};
280 #endif
281  bool include_internal_{false};
282  bool allow_ota_{true};
283  bool expose_log_{true};
284 #ifdef USE_ESP32
285  std::deque<std::function<void()>> to_schedule_;
286  SemaphoreHandle_t to_schedule_lock_;
287 #endif
288 };
289 
290 } // namespace web_server
291 } // 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:286
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
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:268
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:270
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:285
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:161
bool state
Definition: fan.h:34