ESPHome  2022.6.2
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 
13 namespace esphome {
14 namespace web_server {
15 
17 struct UrlMatch {
18  std::string domain;
19  std::string id;
20  std::string method;
21  bool valid;
22 };
23 
25 
35 class WebServer : public Controller, public Component, public AsyncWebHandler {
36  public:
37  WebServer(web_server_base::WebServerBase *base) : base_(base), entities_iterator_(ListEntitiesIterator(this)) {}
38 
44  void set_css_url(const char *css_url);
45 
50  void set_css_include(const char *css_include);
51 
57  void set_js_url(const char *js_url);
58 
63  void set_js_include(const char *js_include);
64 
70  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
75  void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
76 
77  // ========== INTERNAL METHODS ==========
78  // (In most use cases you won't need these)
80  void setup() override;
81  void loop() override;
82 
83  void dump_config() override;
84 
86  float get_setup_priority() const override;
87 
89  void handle_index_request(AsyncWebServerRequest *request);
90 
91 #ifdef USE_WEBSERVER_CSS_INCLUDE
92  void handle_css_request(AsyncWebServerRequest *request);
94 #endif
95 
96 #ifdef USE_WEBSERVER_JS_INCLUDE
97  void handle_js_request(AsyncWebServerRequest *request);
99 #endif
100 
101 #ifdef USE_SENSOR
102  void on_sensor_update(sensor::Sensor *obj, float state) override;
104  void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
105 
107  std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
108 #endif
109 
110 #ifdef USE_SWITCH
111  void on_switch_update(switch_::Switch *obj, bool state) override;
112 
114  void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
115 
117  std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
118 #endif
119 
120 #ifdef USE_BUTTON
121  void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
123 
125  std::string button_json(button::Button *obj, JsonDetail start_config);
126 #endif
127 
128 #ifdef USE_BINARY_SENSOR
129  void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
130 
132  void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
133 
135  std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
136 #endif
137 
138 #ifdef USE_FAN
139  void on_fan_update(fan::Fan *obj) override;
140 
142  void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
143 
145  std::string fan_json(fan::Fan *obj, JsonDetail start_config);
146 #endif
147 
148 #ifdef USE_LIGHT
149  void on_light_update(light::LightState *obj) override;
150 
152  void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
153 
155  std::string light_json(light::LightState *obj, JsonDetail start_config);
156 #endif
157 
158 #ifdef USE_TEXT_SENSOR
159  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
160 
162  void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
163 
165  std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
166 #endif
167 
168 #ifdef USE_COVER
169  void on_cover_update(cover::Cover *obj) override;
170 
172  void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
173 
175  std::string cover_json(cover::Cover *obj, JsonDetail start_config);
176 #endif
177 
178 #ifdef USE_NUMBER
179  void on_number_update(number::Number *obj, float state) override;
181  void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
182 
184  std::string number_json(number::Number *obj, float value, JsonDetail start_config);
185 #endif
186 
187 #ifdef USE_SELECT
188  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
190  void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
191 
193  std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
194 #endif
195 
196 #ifdef USE_CLIMATE
197  void on_climate_update(climate::Climate *obj) override;
199  void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
200 
202  std::string climate_json(climate::Climate *obj, JsonDetail start_config);
203 #endif
204 
205 #ifdef USE_LOCK
206  void on_lock_update(lock::Lock *obj) override;
207 
209  void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
210 
212  std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
213 #endif
214 
216  bool canHandle(AsyncWebServerRequest *request) override;
218  void handleRequest(AsyncWebServerRequest *request) override;
220  bool isRequestHandlerTrivial() override;
221 
222  protected:
225  AsyncEventSource events_{"/events"};
227  const char *css_url_{nullptr};
228  const char *css_include_{nullptr};
229  const char *js_url_{nullptr};
230  const char *js_include_{nullptr};
231  bool include_internal_{false};
232  bool allow_ota_{true};
233 };
234 
235 } // namespace web_server
236 } // namespace esphome
237 
238 #endif // USE_ARDUINO
void setup()
Base class for all switches.
Definition: switch.h:33
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
void loop()
Base class for all cover devices.
Definition: cover.h:111
WebServer(web_server_base::WebServerBase *base)
Definition: web_server.h:37
void set_allow_ota(bool allow_ota)
Set whether or not the webserver should expose the OTA form and handler.
Definition: web_server.h:75
This class allows users to create a web server with their ESP nodes.
Definition: web_server.h:35
Base class for all buttons.
Definition: button.h:22
void set_include_internal(bool include_internal)
Determine whether internal components should be displayed on the web server.
Definition: web_server.h:70
Internal helper struct that is used to parse incoming URLs.
Definition: web_server.h:17
std::string domain
The domain of the component, for example "sensor".
Definition: web_server.h:18
Base-class for all numbers.
Definition: number.h:29
bool valid
Whether this match is valid.
Definition: web_server.h:21
std::string id
The id of the device that&#39;s being accessed, for example "living_room_fan".
Definition: web_server.h:19
Base-class for all selects.
Definition: select.h:24
web_server_base::WebServerBase * base_
Definition: web_server.h:224
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
Base-class for all sensors.
Definition: sensor.h:47
ListEntitiesIterator entities_iterator_
Definition: web_server.h:226
std::string method
The method that&#39;s being called, for example "turn_on".
Definition: web_server.h:20
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
bool state
Definition: fan.h:34