ESPHome  2023.5.5
web_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 #include "list_entities.h"
6 
10 
11 #include <vector>
12 #ifdef USE_ESP32
13 #include <deque>
14 #include <freertos/FreeRTOS.h>
15 #include <freertos/semphr.h>
16 #endif
17 namespace esphome {
18 namespace web_server {
19 
21 struct UrlMatch {
22  std::string domain;
23  std::string id;
24  std::string method;
25  bool valid;
26 };
27 
29 
39 class WebServer : public Controller, public Component, public AsyncWebHandler {
40  public:
42 
48  void set_css_url(const char *css_url);
49 
54  void set_css_include(const char *css_include);
55 
61  void set_js_url(const char *js_url);
62 
67  void set_js_include(const char *js_include);
68 
74  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
79  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
80 
81  // ========== INTERNAL METHODS ==========
82  // (In most use cases you won't need these)
84  void setup() override;
85  void loop() override;
86 
87  void dump_config() override;
88 
90  float get_setup_priority() const override;
91 
93  void handle_index_request(AsyncWebServerRequest *request);
94 
95 #ifdef USE_WEBSERVER_CSS_INCLUDE
96  void handle_css_request(AsyncWebServerRequest *request);
98 #endif
99 
100 #ifdef USE_WEBSERVER_JS_INCLUDE
101  void handle_js_request(AsyncWebServerRequest *request);
103 #endif
104 
105 #ifdef USE_SENSOR
106  void on_sensor_update(sensor::Sensor *obj, float state) override;
108  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
109 
111  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
112 #endif
113 
114 #ifdef USE_SWITCH
115  void on_switch_update(switch_::Switch *obj, bool state) override;
116 
118  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
119 
121  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
122 #endif
123 
124 #ifdef USE_BUTTON
125  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
127 
129  std::string button_json(button::Button *obj, JsonDetail start_config);
130 #endif
131 
132 #ifdef USE_BINARY_SENSOR
133  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
134 
136  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
137 
139  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
140 #endif
141 
142 #ifdef USE_FAN
143  void on_fan_update(fan::Fan *obj) override;
144 
146  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
147 
149  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
150 #endif
151 
152 #ifdef USE_LIGHT
153  void on_light_update(light::LightState *obj) override;
154 
156  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
157 
159  std::string light_json(light::LightState *obj, JsonDetail start_config);
160 #endif
161 
162 #ifdef USE_TEXT_SENSOR
163  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
164 
166  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
167 
169  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
170 #endif
171 
172 #ifdef USE_COVER
173  void on_cover_update(cover::Cover *obj) override;
174 
176  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
177 
179  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
180 #endif
181 
182 #ifdef USE_NUMBER
183  void on_number_update(number::Number *obj, float state) override;
185  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
186 
188  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
189 #endif
190 
191 #ifdef USE_SELECT
192  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
194  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
195 
197  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
198 #endif
199 
200 #ifdef USE_CLIMATE
201  void on_climate_update(climate::Climate *obj) override;
203  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
204 
206  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
207 #endif
208 
209 #ifdef USE_LOCK
210  void on_lock_update(lock::Lock *obj) override;
211 
213  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
214 
216  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
217 #endif
218 
220  bool canHandle(AsyncWebServerRequest *request) override;
222  void handleRequest(AsyncWebServerRequest *request) override;
224  bool isRequestHandlerTrivial() override;
225 
226  protected:
227  void schedule_(std::function<void()> &&f);
230  AsyncEventSource events_{"/events"};
232  const char *css_url_{nullptr};
233  const char *css_include_{nullptr};
234  const char *js_url_{nullptr};
235  const char *js_include_{nullptr};
236  bool include_internal_{false};
237  bool allow_ota_{true};
238 #ifdef USE_ESP32
239  std::deque<std::function<void()>> to_schedule_;
240  SemaphoreHandle_t to_schedule_lock_;
241 #endif
242 };
243 
244 } // namespace web_server
245 } // namespace esphome
246 
247 #endif // USE_ARDUINO
void setup()
Base class for all switches.
Definition: switch.h:32
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:79
SemaphoreHandle_t to_schedule_lock_
Definition: web_server.h:240
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:39
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:74
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:21
std::string domain
The domain of the component, for example "sensor".
Definition: web_server.h:22
Base-class for all numbers.
Definition: number.h:39
bool valid
Whether this match is valid.
Definition: web_server.h:25
std::string id
The id of the device that&#39;s being accessed, for example "living_room_fan".
Definition: web_server.h:23
Base-class for all selects.
Definition: select.h:24
web_server_base::WebServerBase * base_
Definition: web_server.h:229
Definition: a4988.cpp:4
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:231
std::deque< std::function< void()> > to_schedule_
Definition: web_server.h:239
std::string method
The method that&#39;s being called, for example "turn_on".
Definition: web_server.h:24
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