1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-23 20:23:50 +01:00

Webserver v2 (#2688)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
wilberforce
2022-03-08 15:02:24 +13:00
committed by GitHub
parent dc6eff83ea
commit 3b8ca80900
6 changed files with 975 additions and 126 deletions

View File

@@ -2,9 +2,9 @@
#ifdef USE_ARDUINO
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/core/component.h"
#include "esphome/core/controller.h"
#include "esphome/components/web_server_base/web_server_base.h"
#include <vector>
@@ -19,6 +19,8 @@ struct UrlMatch {
bool valid; ///< Whether this match is valid
};
enum JsonDetail { DETAIL_ALL, DETAIL_STATE };
/** This class allows users to create a web server with their ESP nodes.
*
* Behind the scenes it's using AsyncWebServer to set up the server. It exposes 3 things:
@@ -99,7 +101,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the sensor state with its value as a JSON string.
std::string sensor_json(sensor::Sensor *obj, float value);
std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config);
#endif
#ifdef USE_SWITCH
@@ -109,12 +111,15 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the switch state with its value as a JSON string.
std::string switch_json(switch_::Switch *obj, bool value);
std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config);
#endif
#ifdef USE_BUTTON
/// Handle a button request under '/button/<id>/press'.
void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the button details with its value as a JSON string.
std::string button_json(button::Button *obj, JsonDetail start_config);
#endif
#ifdef USE_BINARY_SENSOR
@@ -124,7 +129,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the binary sensor state with its value as a JSON string.
std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value);
std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config);
#endif
#ifdef USE_FAN
@@ -134,7 +139,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the fan state as a JSON string.
std::string fan_json(fan::Fan *obj);
std::string fan_json(fan::Fan *obj, JsonDetail start_config);
#endif
#ifdef USE_LIGHT
@@ -144,7 +149,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the light state as a JSON string.
std::string light_json(light::LightState *obj);
std::string light_json(light::LightState *obj, JsonDetail start_config);
#endif
#ifdef USE_TEXT_SENSOR
@@ -154,7 +159,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the text sensor state with its value as a JSON string.
std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value);
std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config);
#endif
#ifdef USE_COVER
@@ -164,7 +169,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the cover state as a JSON string.
std::string cover_json(cover::Cover *obj);
std::string cover_json(cover::Cover *obj, JsonDetail start_config);
#endif
#ifdef USE_NUMBER
@@ -173,7 +178,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the number state with its value as a JSON string.
std::string number_json(number::Number *obj, float value);
std::string number_json(number::Number *obj, float value, JsonDetail start_config);
#endif
#ifdef USE_SELECT
@@ -181,8 +186,17 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
/// Handle a select request under '/select/<id>'.
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the number state with its value as a JSON string.
std::string select_json(select::Select *obj, const std::string &value);
/// Dump the select state with its value as a JSON string.
std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
#endif
#ifdef USE_CLIMATE
void on_climate_update(climate::Climate *obj) override;
/// Handle a climate request under '/climate/<id>'.
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the climate details
std::string climate_json(climate::Climate *obj, JsonDetail start_config);
#endif
#ifdef USE_LOCK
@@ -192,7 +206,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match);
/// Dump the lock state with its value as a JSON string.
std::string lock_json(lock::Lock *obj, lock::LockState value);
std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config);
#endif
/// Override the web handler's canHandle method.