11 #include "StreamString.h" 28 #ifdef USE_WEBSERVER_LOCAL 33 namespace web_server {
35 static const char *
const TAG =
"web_server";
37 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 38 static const char *
const HEADER_PNA_NAME =
"Private-Network-Access-Name";
39 static const char *
const HEADER_PNA_ID =
"Private-Network-Access-ID";
40 static const char *
const HEADER_CORS_REQ_PNA =
"Access-Control-Request-Private-Network";
41 static const char *
const HEADER_CORS_ALLOW_PNA =
"Access-Control-Allow-Private-Network";
44 #if USE_WEBSERVER_VERSION == 1 45 void write_row(AsyncResponseStream *stream,
EntityBase *obj,
const std::string &klass,
const std::string &action,
46 const std::function<
void(AsyncResponseStream &stream,
EntityBase *obj)> &action_func =
nullptr) {
47 stream->print(
"<tr class=\"");
48 stream->print(klass.c_str());
50 stream->print(
" internal");
51 stream->print(
"\" id=\"");
52 stream->print(klass.c_str());
55 stream->print(
"\"><td>");
57 stream->print(
"</td><td></td><td>");
58 stream->print(action.c_str());
60 action_func(*stream, obj);
62 stream->print(
"</td>");
63 stream->print(
"</tr>");
70 size_t domain_end = url.find(
'/', 1);
71 if (domain_end == std::string::npos)
73 match.
domain = url.substr(1, domain_end - 1);
78 if (url.length() == domain_end - 1)
80 size_t id_begin = domain_end + 1;
81 size_t id_end = url.find(
'/', id_begin);
83 if (id_end == std::string::npos) {
84 match.
id = url.substr(id_begin, url.length() - id_begin);
87 match.
id = url.substr(id_begin, id_end - id_begin);
88 size_t method_begin = id_end + 1;
89 match.
method = url.substr(method_begin, url.length() - method_begin);
100 #if USE_WEBSERVER_VERSION == 1 105 #ifdef USE_WEBSERVER_CSS_INCLUDE 108 #ifdef USE_WEBSERVER_JS_INCLUDE 123 ESP_LOGCONFIG(TAG,
"Setting up web server...");
137 [
this](
int level,
const char *tag,
const char *message) { this->
events_.send(message,
"log",
millis()); });
151 std::function<void()> fn;
168 ESP_LOGCONFIG(TAG,
"Web Server:");
173 #ifdef USE_WEBSERVER_LOCAL 175 AsyncWebServerResponse *response = request->beginResponse_P(200,
"text/html", INDEX_GZ,
sizeof(INDEX_GZ));
176 response->addHeader(
"Content-Encoding",
"gzip");
177 request->send(response);
179 #elif USE_WEBSERVER_VERSION == 1 181 AsyncResponseStream *stream = request->beginResponseStream(
"text/html");
183 stream->print(F(
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=UTF-8><meta " 184 "name=viewport content=\"width=device-width, initial-scale=1,user-scalable=no\"><title>"));
185 stream->print(title.c_str());
186 stream->print(F(
"</title>"));
187 #ifdef USE_WEBSERVER_CSS_INCLUDE 188 stream->print(F(
"<link rel=\"stylesheet\" href=\"/0.css\">"));
191 stream->print(F(R
"(<link rel="stylesheet" href=")")); 193 stream->print(F("\">"));
195 stream->print(F(
"</head><body>"));
196 stream->print(F(
"<article class=\"markdown-body\"><h1>"));
197 stream->print(title.c_str());
198 stream->print(F(
"</h1>"));
199 stream->print(F(
"<h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
211 write_row(stream, obj,
"switch",
"<button>Toggle</button>");
217 write_row(stream, obj,
"button",
"<button>Press</button>");
220 #ifdef USE_BINARY_SENSOR 223 write_row(stream, obj,
"binary_sensor",
"");
230 write_row(stream, obj,
"fan",
"<button>Toggle</button>");
237 write_row(stream, obj,
"light",
"<button>Toggle</button>");
241 #ifdef USE_TEXT_SENSOR 244 write_row(stream, obj,
"text_sensor",
"");
251 write_row(stream, obj,
"cover",
"<button>Open</button><button>Close</button>");
260 stream.print(R
"(<input type="number" min=")"); 262 stream.print(R"(" max=")"); 264 stream.print(R"(" step=")"); 266 stream.print(R"(" value=")"); 267 stream.print(number->state); 268 stream.print(R"("/>)"); 280 stream.print(R
"(<input type=")"); 282 stream.print(R
"(password)"); 284 stream.print(R
"(text)"); 286 stream.print(R"(" minlength=")"); 288 stream.print(R"(" maxlength=")"); 290 stream.print(R"(" pattern=")"); 292 stream.print(R"(" value=")"); 293 stream.print(text->state.c_str()); 294 stream.print(R"("/>)"); 305 stream.print(
"<select>");
306 stream.print(
"<option></option>");
308 stream.print(
"<option>");
309 stream.print(option.c_str());
310 stream.print(
"</option>");
312 stream.print(
"</select>");
323 stream.print(
"<button>Lock</button><button>Unlock</button>");
325 stream.print(
"<button>Open</button>");
339 stream->print(F(
"</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for " 340 "REST API documentation.</p>"));
343 F(
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input " 344 "type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
346 stream->print(F(
"<h2>Debug Log</h2><pre id=\"log\"></pre>"));
347 #ifdef USE_WEBSERVER_JS_INCLUDE 349 stream->print(F(
"<script type=\"module\" src=\"/0.js\"></script>"));
352 if (strlen(this->
js_url_) > 0) {
353 stream->print(F(
"<script src=\""));
355 stream->print(F(
"\"></script>"));
357 stream->print(F(
"</article></body></html>"));
358 request->send(stream);
360 #elif USE_WEBSERVER_VERSION == 2 362 AsyncWebServerResponse *response =
365 request->send(response);
369 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 371 AsyncWebServerResponse *response = request->beginResponse(200,
"");
372 response->addHeader(HEADER_CORS_ALLOW_PNA,
"true");
373 response->addHeader(HEADER_PNA_NAME,
App.
get_name().c_str());
375 response->addHeader(HEADER_PNA_ID, mac.c_str());
376 request->send(response);
380 #ifdef USE_WEBSERVER_CSS_INCLUDE 382 AsyncWebServerResponse *response =
384 response->addHeader(
"Content-Encoding",
"gzip");
385 request->send(response);
389 #ifdef USE_WEBSERVER_JS_INCLUDE 391 AsyncWebServerResponse *response =
393 response->addHeader(
"Content-Encoding",
"gzip");
394 request->send(response);
398 #define set_json_id(root, obj, sensor, start_config) \ 399 (root)["id"] = sensor; \ 400 if (((start_config) == DETAIL_ALL)) \ 401 (root)["name"] = (obj)->get_name(); 403 #define set_json_value(root, obj, sensor, value, start_config) \ 404 set_json_id((root), (obj), sensor, start_config)(root)["value"] = value; 406 #define set_json_state_value(root, obj, sensor, state, value, start_config) \ 407 set_json_value(root, obj, sensor, value, start_config)(root)["state"] = state; 409 #define set_json_icon_state_value(root, obj, sensor, state, value, start_config) \ 410 set_json_value(root, obj, sensor, value, start_config)(root)["state"] = state; \ 411 if (((start_config) == DETAIL_ALL)) \ 412 (root)["icon"] = (obj)->get_icon(); 423 request->send(200,
"application/json", data.c_str());
431 if (std::isnan(value)) {
438 set_json_icon_state_value(root, obj,
"sensor-" + obj->
get_object_id(),
state, value, start_config);
443 #ifdef USE_TEXT_SENSOR 452 request->send(200,
"application/json", data.c_str());
460 set_json_icon_state_value(root, obj,
"text_sensor-" + obj->
get_object_id(), value, value, start_config);
471 set_json_icon_state_value(root, obj,
"switch-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value, start_config);
482 if (request->method() == HTTP_GET) {
484 request->send(200,
"application/json", data.c_str());
485 }
else if (match.
method ==
"toggle") {
486 this->
schedule_([obj]() { obj->toggle(); });
488 }
else if (match.
method ==
"turn_on") {
489 this->
schedule_([obj]() { obj->turn_on(); });
491 }
else if (match.
method ==
"turn_off") {
492 this->
schedule_([obj]() { obj->turn_off(); });
506 [obj, start_config](JsonObject root) { set_json_id(root, obj,
"button-" + obj->
get_object_id(), start_config); });
513 if (request->method() == HTTP_POST && match.
method ==
"press") {
514 this->
schedule_([obj]() { obj->press(); });
526 #ifdef USE_BINARY_SENSOR 532 set_json_state_value(root, obj,
"binary_sensor-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value, start_config);
540 request->send(200,
"application/json", data.c_str());
551 set_json_state_value(root, obj,
"fan-" + obj->
get_object_id(), obj->
state ?
"ON" :
"OFF", obj->
state, start_config);
553 if (traits.supports_speed()) {
554 root[
"speed_level"] = obj->
speed;
555 root[
"speed_count"] = traits.supported_speed_count();
566 if (request->method() == HTTP_GET) {
568 request->send(200,
"application/json", data.c_str());
569 }
else if (match.
method ==
"toggle") {
570 this->
schedule_([obj]() { obj->toggle().perform(); });
572 }
else if (match.
method ==
"turn_on") {
573 auto call = obj->turn_on();
574 if (request->hasParam(
"speed_level")) {
575 auto speed_level = request->getParam(
"speed_level")->value();
576 auto val = parse_number<int>(speed_level.c_str());
577 if (!
val.has_value()) {
578 ESP_LOGW(TAG,
"Can't convert '%s' to number!", speed_level.c_str());
583 if (request->hasParam(
"oscillation")) {
584 auto speed = request->getParam(
"oscillation")->value();
588 call.set_oscillating(
true);
591 call.set_oscillating(
false);
594 call.set_oscillating(!obj->oscillating);
603 }
else if (match.
method ==
"turn_off") {
604 this->
schedule_([obj]() { obj->turn_off().perform(); });
624 if (request->method() == HTTP_GET) {
626 request->send(200,
"application/json", data.c_str());
627 }
else if (match.
method ==
"toggle") {
628 this->
schedule_([obj]() { obj->toggle().perform(); });
630 }
else if (match.
method ==
"turn_on") {
631 auto call = obj->turn_on();
632 if (request->hasParam(
"brightness")) {
633 auto brightness = parse_number<float>(request->getParam(
"brightness")->value().c_str());
634 if (brightness.has_value()) {
635 call.set_brightness(*brightness / 255.0f);
638 if (request->hasParam(
"r")) {
639 auto r = parse_number<float>(request->getParam(
"r")->value().c_str());
641 call.set_red(*r / 255.0f);
644 if (request->hasParam(
"g")) {
645 auto g = parse_number<float>(request->getParam(
"g")->value().c_str());
647 call.set_green(*g / 255.0f);
650 if (request->hasParam(
"b")) {
651 auto b = parse_number<float>(request->getParam(
"b")->value().c_str());
653 call.set_blue(*b / 255.0f);
656 if (request->hasParam(
"white_value")) {
657 auto white_value = parse_number<float>(request->getParam(
"white_value")->value().c_str());
658 if (white_value.has_value()) {
659 call.set_white(*white_value / 255.0f);
662 if (request->hasParam(
"color_temp")) {
663 auto color_temp = parse_number<float>(request->getParam(
"color_temp")->value().c_str());
664 if (color_temp.has_value()) {
665 call.set_color_temperature(*color_temp);
668 if (request->hasParam(
"flash")) {
669 auto flash = parse_number<uint32_t>(request->getParam(
"flash")->value().c_str());
670 if (flash.has_value()) {
671 call.set_flash_length(*flash * 1000);
674 if (request->hasParam(
"transition")) {
675 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
676 if (transition.has_value()) {
677 call.set_transition_length(*transition * 1000);
680 if (request->hasParam(
"effect")) {
681 const char *effect = request->getParam(
"effect")->value().c_str();
682 call.set_effect(effect);
687 }
else if (match.
method ==
"turn_off") {
688 auto call = obj->turn_off();
689 if (request->hasParam(
"transition")) {
690 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
691 if (transition.has_value()) {
692 call.set_transition_length(*transition * 1000);
706 set_json_id(root, obj,
"light-" + obj->
get_object_id(), start_config);
711 JsonArray opt = root.createNestedArray(
"effects");
714 opt.add(option->get_name());
730 if (request->method() == HTTP_GET) {
732 request->send(200,
"application/json", data.c_str());
736 auto call = obj->make_call();
737 if (match.
method ==
"open") {
738 call.set_command_open();
739 }
else if (match.
method ==
"close") {
740 call.set_command_close();
741 }
else if (match.
method ==
"stop") {
742 call.set_command_stop();
743 }
else if (match.
method !=
"set") {
748 auto traits = obj->get_traits();
749 if ((request->hasParam(
"position") && !traits.get_supports_position()) ||
750 (request->hasParam(
"tilt") && !traits.get_supports_tilt())) {
755 if (request->hasParam(
"position")) {
756 auto position = parse_number<float>(request->getParam(
"position")->value().c_str());
761 if (request->hasParam(
"tilt")) {
762 auto tilt = parse_number<float>(request->getParam(
"tilt")->value().c_str());
763 if (
tilt.has_value()) {
781 root[
"tilt"] = obj->
tilt;
795 if (request->method() == HTTP_GET) {
797 request->send(200,
"application/json", data.c_str());
800 if (match.
method !=
"set") {
805 auto call = obj->make_call();
806 if (request->hasParam(
"value")) {
807 auto value = parse_number<float>(request->getParam(
"value")->value().c_str());
808 if (value.has_value())
809 call.set_value(*value);
821 set_json_id(root, obj,
"number-" + obj->
get_object_id(), start_config);
828 if (std::isnan(value)) {
829 root[
"value"] =
"\"NaN\"";
830 root[
"state"] =
"NA";
832 root[
"value"] = value;
836 root[
"state"] =
state;
851 if (request->method() == HTTP_GET) {
853 request->send(200,
"text/json", data.c_str());
856 if (match.
method !=
"set") {
861 auto call = obj->make_call();
862 if (request->hasParam(
"value")) {
863 String value = request->getParam(
"value")->value();
864 call.set_value(value.c_str());
876 set_json_id(root, obj,
"text-" + obj->
get_object_id(), start_config);
884 root[
"state"] =
"********";
886 root[
"state"] = value;
888 root[
"value"] = value;
902 if (request->method() == HTTP_GET) {
904 auto *param = request->getParam(
"detail");
905 if (param && param->value() ==
"all") {
908 std::string data = this->
select_json(obj, obj->state, detail);
909 request->send(200,
"application/json", data.c_str());
913 if (match.
method !=
"set") {
918 auto call = obj->make_call();
920 if (request->hasParam(
"option")) {
921 auto option = request->getParam(
"option")->value();
922 call.set_option(option.c_str());
933 set_json_state_value(root, obj,
"select-" + obj->
get_object_id(), value, value, start_config);
935 JsonArray opt = root.createNestedArray(
"option");
945 #define PSTR_LOCAL(mode_s) strncpy_P(buf, (PGM_P) ((mode_s)), 15) 957 if (request->method() == HTTP_GET) {
959 request->send(200,
"application/json", data.c_str());
963 if (match.
method !=
"set") {
968 auto call = obj->make_call();
970 if (request->hasParam(
"mode")) {
971 auto mode = request->getParam(
"mode")->value();
975 if (request->hasParam(
"target_temperature_high")) {
976 auto target_temperature_high = parse_number<float>(request->getParam(
"target_temperature_high")->value().c_str());
981 if (request->hasParam(
"target_temperature_low")) {
982 auto target_temperature_low = parse_number<float>(request->getParam(
"target_temperature_low")->value().c_str());
987 if (request->hasParam(
"target_temperature")) {
988 auto target_temperature = parse_number<float>(request->getParam(
"target_temperature")->value().c_str());
1002 set_json_id(root, obj,
"climate-" + obj->
get_object_id(), start_config);
1005 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
1009 JsonArray opt = root.createNestedArray(
"modes");
1012 if (!traits.get_supported_custom_fan_modes().empty()) {
1013 JsonArray opt = root.createNestedArray(
"fan_modes");
1018 if (!traits.get_supported_custom_fan_modes().empty()) {
1019 JsonArray opt = root.createNestedArray(
"custom_fan_modes");
1020 for (
auto const &
custom_fan_mode : traits.get_supported_custom_fan_modes())
1023 if (traits.get_supports_swing_modes()) {
1024 JsonArray opt = root.createNestedArray(
"swing_modes");
1025 for (
auto swing_mode : traits.get_supported_swing_modes())
1029 JsonArray opt = root.createNestedArray(
"presets");
1034 JsonArray opt = root.createNestedArray(
"custom_presets");
1035 for (
auto const &
custom_preset : traits.get_supported_custom_presets())
1040 bool has_state =
false;
1044 root[
"step"] = traits.get_visual_target_temperature_step();
1045 if (traits.get_supports_action()) {
1047 root[
"state"] = root[
"action"];
1062 if (traits.get_supports_swing_modes()) {
1065 if (traits.get_supports_current_temperature()) {
1069 root[
"current_temperature"] =
"NA";
1072 if (traits.get_supports_two_point_target_temperature()) {
1082 root[
"state"] = root[
"target_temperature"];
1103 if (request->method() == HTTP_GET) {
1105 request->send(200,
"application/json", data.c_str());
1106 }
else if (match.
method ==
"lock") {
1107 this->
schedule_([obj]() { obj->lock(); });
1109 }
else if (match.
method ==
"unlock") {
1110 this->
schedule_([obj]() { obj->unlock(); });
1112 }
else if (match.
method ==
"open") {
1113 this->
schedule_([obj]() { obj->open(); });
1124 #ifdef USE_ALARM_CONTROL_PANEL 1133 set_json_icon_state_value(root, obj,
"alarm-control-panel-" + obj->
get_object_id(),
1142 if (request->method() == HTTP_GET) {
1144 request->send(200,
"application/json", data.c_str());
1153 if (request->url() ==
"/")
1156 #ifdef USE_WEBSERVER_CSS_INCLUDE 1157 if (request->url() ==
"/0.css")
1161 #ifdef USE_WEBSERVER_JS_INCLUDE 1162 if (request->url() ==
"/0.js")
1166 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1167 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1172 request->addInterestingHeader(HEADER_CORS_REQ_PNA);
1182 if (request->method() == HTTP_GET && match.
domain ==
"sensor")
1187 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"switch")
1192 if (request->method() == HTTP_POST && match.
domain ==
"button")
1196 #ifdef USE_BINARY_SENSOR 1197 if (request->method() == HTTP_GET && match.
domain ==
"binary_sensor")
1202 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"fan")
1207 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"light")
1211 #ifdef USE_TEXT_SENSOR 1212 if (request->method() == HTTP_GET && match.
domain ==
"text_sensor")
1217 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"cover")
1222 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"number")
1227 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"text")
1232 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"select")
1237 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"climate")
1242 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"lock")
1246 #ifdef USE_ALARM_CONTROL_PANEL 1247 if (request->method() == HTTP_GET && match.
domain ==
"alarm_control_panel")
1254 if (request->url() ==
"/") {
1259 #ifdef USE_WEBSERVER_CSS_INCLUDE 1260 if (request->url() ==
"/0.css") {
1266 #ifdef USE_WEBSERVER_JS_INCLUDE 1267 if (request->url() ==
"/0.js") {
1273 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1274 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1282 if (match.
domain ==
"sensor") {
1289 if (match.
domain ==
"switch") {
1296 if (match.
domain ==
"button") {
1302 #ifdef USE_BINARY_SENSOR 1303 if (match.
domain ==
"binary_sensor") {
1310 if (match.
domain ==
"fan") {
1317 if (match.
domain ==
"light") {
1323 #ifdef USE_TEXT_SENSOR 1324 if (match.
domain ==
"text_sensor") {
1331 if (match.
domain ==
"cover") {
1338 if (match.
domain ==
"number") {
1345 if (match.
domain ==
"text") {
1352 if (match.
domain ==
"select") {
1359 if (match.
domain ==
"climate") {
1366 if (match.
domain ==
"lock") {
1373 #ifdef USE_ALARM_CONTROL_PANEL 1374 if (match.
domain ==
"alarm_control_panel") {
1390 this->
defer(std::move(f));
Base class for all switches.
value_type const & value() const
bool state
The current on/off state of the fan.
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
ClimateSwingMode swing_mode
The active swing mode of the climate device.
float target_temperature_low
void handle_pna_cors_request(AsyncWebServerRequest *request)
AlarmControlPanelState get_state() const
Get the state.
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a number request under '/number/<id>'.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
bool oscillating
The current oscillation state of the fan.
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
std::string number_json(number::Number *obj, float value, JsonDetail start_config)
Dump the number state with its value as a JSON string.
std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config)
Dump the sensor state with its value as a JSON string.
void add_on_log_callback(std::function< void(int, const char *, const char *)> &&callback)
Register a callback that will be called for every log message sent.
void on_sensor_update(sensor::Sensor *obj, float state) override
bool is_on() const
Get the binary true/false state of these light color values.
Base class for all cover devices.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
WebServer(web_server_base::WebServerBase *base)
void handleRequest(AsyncWebServerRequest *request) override
Override the web handler's handleRequest method.
TextMode get_mode() const
ClimatePreset
Enum for all preset modes.
const std::vector< climate::Climate * > & get_climates()
float target_temperature
The target temperature of the climate device.
SemaphoreHandle_t to_schedule_lock_
std::string get_use_address()
Get the active network hostname.
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a binary sensor request under '/binary_sensor/<id>'.
std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config)
Dump the select state with its value as a JSON string.
LockState state
The current reported state of the lock.
const std::vector< alarm_control_panel::AlarmControlPanel * > & get_alarm_control_panels()
const char * lock_state_to_string(LockState state)
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text input request under '/text/<id>'.
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
const LogString * climate_mode_to_string(ClimateMode mode)
Convert the given ClimateMode to a human-readable string.
virtual FanTraits get_traits()=0
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
ClimateMode mode
The active mode of the climate device.
void on_lock_update(lock::Lock *obj) override
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
void write_row(AsyncResponseStream *stream, EntityBase *obj, const std::string &klass, const std::string &action, const std::function< void(AsyncResponseStream &stream, EntityBase *obj)> &action_func=nullptr)
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
void setup() override
Setup the internal web server and register handlers.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
float current_temperature
The current temperature of the climate device, as reported from the integration.
const std::vector< fan::Fan * > & get_fans()
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a light request under '/light/<id>/</turn_on/turn_off/toggle>'.
bool isRequestHandlerTrivial() override
This web handle is not trivial.
void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a lock request under '/lock/<id>/</lock/unlock/open>'.
float target_temperature_high
void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a button request under '/button/<id>/press'.
int get_max_length() const
Base-class for all text inputs.
bool supports_oscillation() const
Return if this fan supports oscillation.
void on_light_update(light::LightState *obj) override
float tilt
The current tilt value of the cover from 0.0 to 1.0.
std::string get_object_id() const
uint32_t IRAM_ATTR HOT millis()
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
ClimateSwingMode swing_mode
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE
Internal helper struct that is used to parse incoming URLs.
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
virtual CoverTraits get_traits()=0
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a sensor request under '/sensor/<id>'.
const std::vector< lock::Lock * > & get_locks()
uint16_t get_port() const
void dump_config() override
std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config)
Dump the text sensor state with its value as a JSON string.
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
std::string domain
The domain of the component, for example "sensor".
std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config)
Dump the text state with its value as a JSON string.
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
void add_handler(AsyncWebHandler *handler)
void set_css_include(const char *css_include)
Set local path to the script that's embedded in the index page.
void on_text_update(text::Text *obj, const std::string &state) override
const std::vector< button::Button * > & get_buttons()
void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a switch request under '/switch/<id>/</turn_on/turn_off/toggle>'.
const LogString * alarm_control_panel_state_to_string(AlarmControlPanelState state)
Returns a string representation of the state.
std::vector< std::string > get_options() const
optional< ClimatePreset > preset
The active preset of the climate device.
UrlMatch match_url(const std::string &url, bool only_domain=false)
const std::vector< switch_::Switch * > & get_switches()
Base-class for all numbers.
const char * cover_operation_to_str(CoverOperation op)
int speed
The current fan speed level.
void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a alarm_control_panel request under '/alarm_control_panel/<id>'.
void handle_css_request(AsyncWebServerRequest *request)
Handle included css request under '/0.css'.
bool valid
Whether this match is valid.
BedjetMode mode
BedJet operating mode.
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0...
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const std::vector< text_sensor::TextSensor * > & get_text_sensors()
const LogString * climate_preset_to_string(ClimatePreset preset)
Convert the given PresetMode to a human-readable string.
int8_t get_target_temperature_accuracy_decimals() const
const std::vector< sensor::Sensor * > & get_sensors()
Application App
Global storage of Application pointer - only one Application can exist.
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config)
Dump the switch state with its value as a JSON string.
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
void begin(bool include_internal=false)
const std::string & get_name() const
Get the name of this Application set by pre_setup().
std::string light_json(light::LightState *obj, JsonDetail start_config)
Dump the light state as a JSON string.
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
const std::vector< text::Text * > & get_texts()
ClimateMode
Enum for all modes a climate device can be in.
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
void on_climate_update(climate::Climate *obj) override
const std::vector< cover::Cover * > & get_covers()
constexpr const char * c_str() const
void set_js_url(const char *js_url)
Set the URL to the script that's embedded in the index page.
float get_setup_priority() const override
MQTT setup priority.
optional< std::string > custom_preset
The active custom preset mode of the climate device.
const LogString * climate_fan_mode_to_string(ClimateFanMode fan_mode)
Convert the given ClimateFanMode to a human-readable string.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a fan request under '/fan/<id>/</turn_on/turn_off/toggle>'.
void set_css_url(const char *css_url)
Set the URL to the CSS <link> that's sent to each client.
std::string id
The id of the device that's being accessed, for example "living_room_fan".
const std::vector< light::LightState * > & get_lights()
std::string get_comment() const
Get the comment of this Application set by pre_setup().
std::string button_json(button::Button *obj, JsonDetail start_config)
Dump the button details with its value as a JSON string.
void on_cover_update(cover::Cover *obj) override
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a cover request under '/cover/<id>/<open/close/stop/set>'.
void on_switch_update(switch_::Switch *obj, bool state) override
const char * css_include_
bool get_supports_tilt() const
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
void setup_controller(bool include_internal=false)
std::string get_config_json()
Return the webserver configuration as JSON.
Base-class for all selects.
void on_fan_update(fan::Fan *obj) override
web_server_base::WebServerBase * base_
Implementation of SPI Controller mode.
float get_min_value() const
void on_number_update(number::Number *obj, float state) override
std::string fan_json(fan::Fan *obj, JsonDetail start_config)
Dump the fan state as a JSON string.
Base class for all binary_sensor-type classes.
LightColorValues remote_values
The remote color values reported to the frontend.
LockState
Enum for all states a lock can be in.
NumberMode get_mode() const
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a climate request under '/climate/<id>'.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
int get_min_length() const
const std::vector< select::Select * > & get_selects()
Base-class for all sensors.
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
bool canHandle(AsyncWebServerRequest *request) override
Override the web handler's canHandle method.
AsyncEventSourceResponse AsyncEventSourceClient
std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj, alarm_control_panel::AlarmControlPanelState value, JsonDetail start_config)
Dump the alarm_control_panel state with its value as a JSON string.
ListEntitiesIterator entities_iterator_
std::deque< std::function< void()> > to_schedule_
const std::vector< number::Number * > & get_numbers()
float get_max_value() const
const LogString * climate_action_to_string(ClimateAction action)
Convert the given ClimateAction to a human-readable string.
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text sensor request under '/text_sensor/<id>'.
bool get_supports_open() const
void schedule_(std::function< void()> &&f)
std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config)
Dump the lock state with its value as a JSON string.
std::string get_pattern() const
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
const StringRef & get_name() const
std::string climate_json(climate::Climate *obj, JsonDetail start_config)
Dump the climate details.
std::string method
The method that's being called, for example "turn_on".
Base class for all locks.
ClimateAction action
The active state of the climate device.
ClimateDevice - This is the base class for all climate integrations.
std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config)
Dump the binary sensor state with its value as a JSON string.
std::string cover_json(cover::Cover *obj, JsonDetail start_config)
Dump the cover state as a JSON string.
void handle_js_request(AsyncWebServerRequest *request)
Handle included js request under '/0.js'.
void set_js_include(const char *js_include)
Set local path to the script that's embedded in the index page.
const LogString * climate_swing_mode_to_string(ClimateSwingMode swing_mode)
Convert the given ClimateSwingMode to a human-readable string.