12 #include "StreamString.h" 29 #ifdef USE_WEBSERVER_LOCAL 30 #if USE_WEBSERVER_VERSION == 2 32 #elif USE_WEBSERVER_VERSION == 3 38 namespace web_server {
40 static const char *
const TAG =
"web_server";
42 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 43 static const char *
const HEADER_PNA_NAME =
"Private-Network-Access-Name";
44 static const char *
const HEADER_PNA_ID =
"Private-Network-Access-ID";
45 static const char *
const HEADER_CORS_REQ_PNA =
"Access-Control-Request-Private-Network";
46 static const char *
const HEADER_CORS_ALLOW_PNA =
"Access-Control-Allow-Private-Network";
49 #if USE_WEBSERVER_VERSION == 1 50 void write_row(AsyncResponseStream *stream,
EntityBase *obj,
const std::string &klass,
const std::string &action,
51 const std::function<
void(AsyncResponseStream &stream,
EntityBase *obj)> &action_func =
nullptr) {
52 stream->print(
"<tr class=\"");
53 stream->print(klass.c_str());
55 stream->print(
" internal");
56 stream->print(
"\" id=\"");
57 stream->print(klass.c_str());
60 stream->print(
"\"><td>");
62 stream->print(
"</td><td></td><td>");
63 stream->print(action.c_str());
65 action_func(*stream, obj);
67 stream->print(
"</td>");
68 stream->print(
"</tr>");
75 size_t domain_end = url.find(
'/', 1);
76 if (domain_end == std::string::npos)
78 match.
domain = url.substr(1, domain_end - 1);
83 if (url.length() == domain_end - 1)
85 size_t id_begin = domain_end + 1;
86 size_t id_end = url.find(
'/', id_begin);
88 if (id_end == std::string::npos) {
89 match.
id = url.substr(id_begin, url.length() - id_begin);
92 match.
id = url.substr(id_begin, id_end - id_begin);
93 size_t method_begin = id_end + 1;
94 match.
method = url.substr(method_begin, url.length() - method_begin);
105 #if USE_WEBSERVER_VERSION == 1 110 #ifdef USE_WEBSERVER_CSS_INCLUDE 113 #ifdef USE_WEBSERVER_JS_INCLUDE 128 ESP_LOGCONFIG(TAG,
"Setting up web server...");
142 [
this](
int level,
const char *tag,
const char *message) { this->
events_.send(message,
"log",
millis()); });
156 std::function<void()> fn;
173 ESP_LOGCONFIG(TAG,
"Web Server:");
178 #ifdef USE_WEBSERVER_LOCAL 180 AsyncWebServerResponse *response = request->beginResponse_P(200,
"text/html", INDEX_GZ,
sizeof(INDEX_GZ));
181 response->addHeader(
"Content-Encoding",
"gzip");
182 request->send(response);
184 #elif USE_WEBSERVER_VERSION == 1 186 AsyncResponseStream *stream = request->beginResponseStream(
"text/html");
188 stream->print(F(
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=UTF-8><meta " 189 "name=viewport content=\"width=device-width, initial-scale=1,user-scalable=no\"><title>"));
190 stream->print(title.c_str());
191 stream->print(F(
"</title>"));
192 #ifdef USE_WEBSERVER_CSS_INCLUDE 193 stream->print(F(
"<link rel=\"stylesheet\" href=\"/0.css\">"));
196 stream->print(F(R
"(<link rel="stylesheet" href=")")); 198 stream->print(F("\">"));
200 stream->print(F(
"</head><body>"));
201 stream->print(F(
"<article class=\"markdown-body\"><h1>"));
202 stream->print(title.c_str());
203 stream->print(F(
"</h1>"));
204 stream->print(F(
"<h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
216 write_row(stream, obj,
"switch",
"<button>Toggle</button>");
222 write_row(stream, obj,
"button",
"<button>Press</button>");
225 #ifdef USE_BINARY_SENSOR 228 write_row(stream, obj,
"binary_sensor",
"");
235 write_row(stream, obj,
"fan",
"<button>Toggle</button>");
242 write_row(stream, obj,
"light",
"<button>Toggle</button>");
246 #ifdef USE_TEXT_SENSOR 249 write_row(stream, obj,
"text_sensor",
"");
256 write_row(stream, obj,
"cover",
"<button>Open</button><button>Close</button>");
265 stream.print(R
"(<input type="number" min=")"); 267 stream.print(R"(" max=")"); 269 stream.print(R"(" step=")"); 271 stream.print(R"(" value=")"); 272 stream.print(number->state); 273 stream.print(R"("/>)"); 285 stream.print(R
"(<input type=")"); 287 stream.print(R
"(password)"); 289 stream.print(R
"(text)"); 291 stream.print(R"(" minlength=")"); 293 stream.print(R"(" maxlength=")"); 295 stream.print(R"(" pattern=")"); 297 stream.print(R"(" value=")"); 298 stream.print(text->state.c_str()); 299 stream.print(R"("/>)"); 310 stream.print(
"<select>");
311 stream.print(
"<option></option>");
313 stream.print(
"<option>");
314 stream.print(option.c_str());
315 stream.print(
"</option>");
317 stream.print(
"</select>");
328 stream.print(
"<button>Lock</button><button>Unlock</button>");
330 stream.print(
"<button>Open</button>");
344 stream->print(F(
"</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for " 345 "REST API documentation.</p>"));
348 F(
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input " 349 "type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
351 stream->print(F(
"<h2>Debug Log</h2><pre id=\"log\"></pre>"));
352 #ifdef USE_WEBSERVER_JS_INCLUDE 354 stream->print(F(
"<script type=\"module\" src=\"/0.js\"></script>"));
357 if (strlen(this->
js_url_) > 0) {
358 stream->print(F(
"<script src=\""));
360 stream->print(F(
"\"></script>"));
362 stream->print(F(
"</article></body></html>"));
363 request->send(stream);
365 #elif USE_WEBSERVER_VERSION >= 2 367 AsyncWebServerResponse *response =
370 request->send(response);
374 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 376 AsyncWebServerResponse *response = request->beginResponse(200,
"");
377 response->addHeader(HEADER_CORS_ALLOW_PNA,
"true");
378 response->addHeader(HEADER_PNA_NAME,
App.
get_name().c_str());
380 response->addHeader(HEADER_PNA_ID, mac.c_str());
381 request->send(response);
385 #ifdef USE_WEBSERVER_CSS_INCLUDE 387 AsyncWebServerResponse *response =
389 response->addHeader(
"Content-Encoding",
"gzip");
390 request->send(response);
394 #ifdef USE_WEBSERVER_JS_INCLUDE 396 AsyncWebServerResponse *response =
398 response->addHeader(
"Content-Encoding",
"gzip");
399 request->send(response);
403 #define set_json_id(root, obj, sensor, start_config) \ 404 (root)["id"] = sensor; \ 405 if (((start_config) == DETAIL_ALL)) { \ 406 (root)["name"] = (obj)->get_name(); \ 407 (root)["icon"] = (obj)->get_icon(); \ 408 (root)["entity_category"] = (obj)->get_entity_category(); \ 409 if ((obj)->is_disabled_by_default()) \ 410 (root)["is_disabled_by_default"] = (obj)->is_disabled_by_default(); \ 413 #define set_json_value(root, obj, sensor, value, start_config) \ 414 set_json_id((root), (obj), sensor, start_config); \ 415 (root)["value"] = value; 417 #define set_json_icon_state_value(root, obj, sensor, state, value, start_config) \ 418 set_json_value(root, obj, sensor, value, start_config); \ 419 (root)["state"] = state; 423 if (this->
events_.count() == 0)
432 request->send(200,
"application/json", data.c_str());
440 if (std::isnan(value)) {
447 set_json_icon_state_value(root, obj,
"sensor-" + obj->
get_object_id(),
state, value, start_config);
459 #ifdef USE_TEXT_SENSOR 461 if (this->
events_.count() == 0)
470 request->send(200,
"application/json", data.c_str());
478 set_json_icon_state_value(root, obj,
"text_sensor-" + obj->
get_object_id(), value, value, start_config);
490 if (this->
events_.count() == 0)
499 if (request->method() == HTTP_GET && match.
method.empty()) {
501 request->send(200,
"application/json", data.c_str());
502 }
else if (match.
method ==
"toggle") {
503 this->
schedule_([obj]() { obj->toggle(); });
505 }
else if (match.
method ==
"turn_on") {
506 this->
schedule_([obj]() { obj->turn_on(); });
508 }
else if (match.
method ==
"turn_off") {
509 this->
schedule_([obj]() { obj->turn_off(); });
520 set_json_icon_state_value(root, obj,
"switch-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value, start_config);
536 if (match.
method ==
"press") {
537 this->
schedule_([obj]() { obj->press(); });
549 set_json_id(root, obj,
"button-" + obj->
get_object_id(), start_config);
559 #ifdef USE_BINARY_SENSOR 561 if (this->
events_.count() == 0)
570 request->send(200,
"application/json", data.c_str());
577 set_json_icon_state_value(root, obj,
"binary_sensor-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value,
590 if (this->
events_.count() == 0)
599 if (request->method() == HTTP_GET && match.
method.empty()) {
601 request->send(200,
"application/json", data.c_str());
602 }
else if (match.
method ==
"toggle") {
603 this->
schedule_([obj]() { obj->toggle().perform(); });
605 }
else if (match.
method ==
"turn_on") {
606 auto call = obj->turn_on();
607 if (request->hasParam(
"speed_level")) {
608 auto speed_level = request->getParam(
"speed_level")->value();
609 auto val = parse_number<int>(speed_level.c_str());
610 if (!
val.has_value()) {
611 ESP_LOGW(TAG,
"Can't convert '%s' to number!", speed_level.c_str());
616 if (request->hasParam(
"oscillation")) {
617 auto speed = request->getParam(
"oscillation")->value();
621 call.set_oscillating(
true);
624 call.set_oscillating(
false);
627 call.set_oscillating(!obj->oscillating);
636 }
else if (match.
method ==
"turn_off") {
637 this->
schedule_([obj]() { obj->turn_off().perform(); });
651 if (traits.supports_speed()) {
652 root[
"speed_level"] = obj->
speed;
653 root[
"speed_count"] = traits.supported_speed_count();
668 if (this->
events_.count() == 0)
677 if (request->method() == HTTP_GET && match.
method.empty()) {
679 request->send(200,
"application/json", data.c_str());
680 }
else if (match.
method ==
"toggle") {
681 this->
schedule_([obj]() { obj->toggle().perform(); });
683 }
else if (match.
method ==
"turn_on") {
684 auto call = obj->turn_on();
685 if (request->hasParam(
"brightness")) {
686 auto brightness = parse_number<float>(request->getParam(
"brightness")->value().c_str());
687 if (brightness.has_value()) {
688 call.set_brightness(*brightness / 255.0f);
691 if (request->hasParam(
"r")) {
692 auto r = parse_number<float>(request->getParam(
"r")->value().c_str());
694 call.set_red(*r / 255.0f);
697 if (request->hasParam(
"g")) {
698 auto g = parse_number<float>(request->getParam(
"g")->value().c_str());
700 call.set_green(*g / 255.0f);
703 if (request->hasParam(
"b")) {
704 auto b = parse_number<float>(request->getParam(
"b")->value().c_str());
706 call.set_blue(*b / 255.0f);
709 if (request->hasParam(
"white_value")) {
710 auto white_value = parse_number<float>(request->getParam(
"white_value")->value().c_str());
711 if (white_value.has_value()) {
712 call.set_white(*white_value / 255.0f);
715 if (request->hasParam(
"color_temp")) {
716 auto color_temp = parse_number<float>(request->getParam(
"color_temp")->value().c_str());
717 if (color_temp.has_value()) {
718 call.set_color_temperature(*color_temp);
721 if (request->hasParam(
"flash")) {
722 auto flash = parse_number<uint32_t>(request->getParam(
"flash")->value().c_str());
723 if (flash.has_value()) {
724 call.set_flash_length(*flash * 1000);
727 if (request->hasParam(
"transition")) {
728 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
729 if (transition.has_value()) {
730 call.set_transition_length(*transition * 1000);
733 if (request->hasParam(
"effect")) {
734 const char *effect = request->getParam(
"effect")->value().c_str();
735 call.set_effect(effect);
740 }
else if (match.
method ==
"turn_off") {
741 auto call = obj->turn_off();
742 if (request->hasParam(
"transition")) {
743 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
744 if (transition.has_value()) {
745 call.set_transition_length(*transition * 1000);
759 set_json_id(root, obj,
"light-" + obj->
get_object_id(), start_config);
764 JsonArray opt = root.createNestedArray(
"effects");
767 opt.add(option->get_name());
779 if (this->
events_.count() == 0)
788 if (request->method() == HTTP_GET && match.
method.empty()) {
790 request->send(200,
"application/json", data.c_str());
794 auto call = obj->make_call();
795 if (match.
method ==
"open") {
796 call.set_command_open();
797 }
else if (match.
method ==
"close") {
798 call.set_command_close();
799 }
else if (match.
method ==
"stop") {
800 call.set_command_stop();
801 }
else if (match.
method ==
"toggle") {
802 call.set_command_toggle();
803 }
else if (match.
method !=
"set") {
808 auto traits = obj->get_traits();
809 if ((request->hasParam(
"position") && !traits.get_supports_position()) ||
810 (request->hasParam(
"tilt") && !traits.get_supports_tilt())) {
815 if (request->hasParam(
"position")) {
816 auto position = parse_number<float>(request->getParam(
"position")->value().c_str());
821 if (request->hasParam(
"tilt")) {
822 auto tilt = parse_number<float>(request->getParam(
"tilt")->value().c_str());
823 if (
tilt.has_value()) {
843 root[
"tilt"] = obj->
tilt;
855 if (this->
events_.count() == 0)
864 if (request->method() == HTTP_GET && match.
method.empty()) {
866 request->send(200,
"application/json", data.c_str());
869 if (match.
method !=
"set") {
874 auto call = obj->make_call();
875 if (request->hasParam(
"value")) {
876 auto value = parse_number<float>(request->getParam(
"value")->value().c_str());
877 if (value.has_value())
878 call.set_value(*value);
890 set_json_id(root, obj,
"number-" + obj->
get_object_id(), start_config);
905 if (std::isnan(value)) {
906 root[
"value"] =
"\"NaN\"";
907 root[
"state"] =
"NA";
913 root[
"state"] =
state;
919 #ifdef USE_DATETIME_DATE 921 if (this->
events_.count() == 0)
929 if (request->method() == HTTP_GET) {
931 request->send(200,
"application/json", data.c_str());
934 if (match.
method !=
"set") {
939 auto call = obj->make_call();
941 if (!request->hasParam(
"value")) {
946 if (request->hasParam(
"value")) {
947 std::string value = request->getParam(
"value")->value().c_str();
948 call.set_date(value);
960 set_json_id(root, obj,
"date-" + obj->
get_object_id(), start_config);
962 root[
"value"] = value;
963 root[
"state"] = value;
971 #endif // USE_DATETIME_DATE 973 #ifdef USE_DATETIME_TIME 975 if (this->
events_.count() == 0)
983 if (request->method() == HTTP_GET && match.
method.empty()) {
985 request->send(200,
"application/json", data.c_str());
988 if (match.
method !=
"set") {
993 auto call = obj->make_call();
995 if (!request->hasParam(
"value")) {
1000 if (request->hasParam(
"value")) {
1001 std::string value = request->getParam(
"value")->value().c_str();
1002 call.set_time(value);
1013 set_json_id(root, obj,
"time-" + obj->
get_object_id(), start_config);
1015 root[
"value"] = value;
1016 root[
"state"] = value;
1024 #endif // USE_DATETIME_TIME 1026 #ifdef USE_DATETIME_DATETIME 1028 if (this->
events_.count() == 0)
1036 if (request->method() == HTTP_GET && match.
method.empty()) {
1038 request->send(200,
"application/json", data.c_str());
1041 if (match.
method !=
"set") {
1046 auto call = obj->make_call();
1048 if (!request->hasParam(
"value")) {
1053 if (request->hasParam(
"value")) {
1054 std::string value = request->getParam(
"value")->value().c_str();
1055 call.set_datetime(value);
1066 set_json_id(root, obj,
"datetime-" + obj->
get_object_id(), start_config);
1069 root[
"value"] = value;
1070 root[
"state"] = value;
1078 #endif // USE_DATETIME_DATETIME 1082 if (this->
events_.count() == 0)
1091 if (request->method() == HTTP_GET && match.
method.empty()) {
1093 request->send(200,
"text/json", data.c_str());
1096 if (match.
method !=
"set") {
1101 auto call = obj->make_call();
1102 if (request->hasParam(
"value")) {
1103 String value = request->getParam(
"value")->value();
1104 call.set_value(value.c_str());
1115 return json::build_json([
this, obj, value, start_config](JsonObject root) {
1116 set_json_id(root, obj,
"text-" + obj->
get_object_id(), start_config);
1121 root[
"state"] =
"********";
1123 root[
"state"] = value;
1125 root[
"value"] = value;
1138 if (this->
events_.count() == 0)
1147 if (request->method() == HTTP_GET && match.
method.empty()) {
1149 auto *param = request->getParam(
"detail");
1150 if (param && param->value() ==
"all") {
1153 std::string data = this->
select_json(obj, obj->state, detail);
1154 request->send(200,
"application/json", data.c_str());
1158 if (match.
method !=
"set") {
1163 auto call = obj->make_call();
1165 if (request->hasParam(
"option")) {
1166 auto option = request->getParam(
"option")->value();
1167 call.set_option(option.c_str());
1177 return json::build_json([
this, obj, value, start_config](JsonObject root) {
1178 set_json_icon_state_value(root, obj,
"select-" + obj->
get_object_id(), value, value, start_config);
1180 JsonArray opt = root.createNestedArray(
"option");
1193 #define PSTR_LOCAL(mode_s) strncpy_P(buf, (PGM_P) ((mode_s)), 15) 1197 if (this->
events_.count() == 0)
1206 if (request->method() == HTTP_GET && match.
method.empty()) {
1208 request->send(200,
"application/json", data.c_str());
1212 if (match.
method !=
"set") {
1217 auto call = obj->make_call();
1219 if (request->hasParam(
"mode")) {
1220 auto mode = request->getParam(
"mode")->value();
1224 if (request->hasParam(
"target_temperature_high")) {
1225 auto target_temperature_high = parse_number<float>(request->getParam(
"target_temperature_high")->value().c_str());
1230 if (request->hasParam(
"target_temperature_low")) {
1231 auto target_temperature_low = parse_number<float>(request->getParam(
"target_temperature_low")->value().c_str());
1236 if (request->hasParam(
"target_temperature")) {
1237 auto target_temperature = parse_number<float>(request->getParam(
"target_temperature")->value().c_str());
1250 set_json_id(root, obj,
"climate-" + obj->
get_object_id(), start_config);
1253 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
1257 JsonArray opt = root.createNestedArray(
"modes");
1260 if (!traits.get_supported_custom_fan_modes().empty()) {
1261 JsonArray opt = root.createNestedArray(
"fan_modes");
1266 if (!traits.get_supported_custom_fan_modes().empty()) {
1267 JsonArray opt = root.createNestedArray(
"custom_fan_modes");
1268 for (
auto const &
custom_fan_mode : traits.get_supported_custom_fan_modes())
1271 if (traits.get_supports_swing_modes()) {
1272 JsonArray opt = root.createNestedArray(
"swing_modes");
1273 for (
auto swing_mode : traits.get_supported_swing_modes())
1277 JsonArray opt = root.createNestedArray(
"presets");
1282 JsonArray opt = root.createNestedArray(
"custom_presets");
1283 for (
auto const &
custom_preset : traits.get_supported_custom_presets())
1291 bool has_state =
false;
1295 root[
"step"] = traits.get_visual_target_temperature_step();
1296 if (traits.get_supports_action()) {
1298 root[
"state"] = root[
"action"];
1313 if (traits.get_supports_swing_modes()) {
1316 if (traits.get_supports_current_temperature()) {
1320 root[
"current_temperature"] =
"NA";
1323 if (traits.get_supports_two_point_target_temperature()) {
1333 root[
"state"] = root[
"target_temperature"];
1341 if (this->
events_.count() == 0)
1350 if (request->method() == HTTP_GET && match.
method.empty()) {
1352 request->send(200,
"application/json", data.c_str());
1353 }
else if (match.
method ==
"lock") {
1354 this->
schedule_([obj]() { obj->lock(); });
1356 }
else if (match.
method ==
"unlock") {
1357 this->
schedule_([obj]() { obj->unlock(); });
1359 }
else if (match.
method ==
"open") {
1360 this->
schedule_([obj]() { obj->open(); });
1370 return json::build_json([
this, obj, value, start_config](JsonObject root) {
1384 if (this->
events_.count() == 0)
1393 if (request->method() == HTTP_GET && match.
method.empty()) {
1395 request->send(200,
"application/json", data.c_str());
1399 auto call = obj->make_call();
1400 if (match.
method ==
"open") {
1401 call.set_command_open();
1402 }
else if (match.
method ==
"close") {
1403 call.set_command_close();
1404 }
else if (match.
method ==
"stop") {
1405 call.set_command_stop();
1406 }
else if (match.
method ==
"toggle") {
1407 call.set_command_toggle();
1408 }
else if (match.
method !=
"set") {
1413 auto traits = obj->get_traits();
1414 if (request->hasParam(
"position") && !traits.get_supports_position()) {
1419 if (request->hasParam(
"position")) {
1420 auto position = parse_number<float>(request->getParam(
"position")->value().c_str());
1447 #ifdef USE_ALARM_CONTROL_PANEL 1449 if (this->
events_.count() == 0)
1458 if (request->method() == HTTP_GET && match.
method.empty()) {
1460 request->send(200,
"application/json", data.c_str());
1469 return json::build_json([
this, obj, value, start_config](JsonObject root) {
1471 set_json_icon_state_value(root, obj,
"alarm-control-panel-" + obj->
get_object_id(),
1489 set_json_id(root, obj,
"event-" + obj->
get_object_id(), start_config);
1490 if (!event_type.empty()) {
1491 root[
"event_type"] = event_type;
1494 JsonArray event_types = root.createNestedArray(
"event_types");
1496 event_types.add(event_type);
1506 if (this->
events_.count() == 0)
1515 if (request->method() == HTTP_GET && match.
method.empty()) {
1517 request->send(200,
"application/json", data.c_str());
1521 if (match.
method !=
"install") {
1526 this->
schedule_([obj]()
mutable { obj->perform(); });
1534 set_json_id(root, obj,
"update-" + obj->
get_object_id(), start_config);
1536 switch (obj->
state) {
1538 root[
"state"] =
"NO UPDATE";
1541 root[
"state"] =
"UPDATE AVAILABLE";
1544 root[
"state"] =
"INSTALLING";
1547 root[
"state"] =
"UNKNOWN";
1564 if (request->url() ==
"/")
1567 #ifdef USE_WEBSERVER_CSS_INCLUDE 1568 if (request->url() ==
"/0.css")
1572 #ifdef USE_WEBSERVER_JS_INCLUDE 1573 if (request->url() ==
"/0.js")
1577 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1578 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1583 request->addInterestingHeader(HEADER_CORS_REQ_PNA);
1593 if (request->method() == HTTP_GET && match.
domain ==
"sensor")
1598 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"switch")
1603 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"button")
1607 #ifdef USE_BINARY_SENSOR 1608 if (request->method() == HTTP_GET && match.
domain ==
"binary_sensor")
1613 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"fan")
1618 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"light")
1622 #ifdef USE_TEXT_SENSOR 1623 if (request->method() == HTTP_GET && match.
domain ==
"text_sensor")
1628 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"cover")
1633 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"number")
1637 #ifdef USE_DATETIME_DATE 1638 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"date")
1642 #ifdef USE_DATETIME_TIME 1643 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"time")
1647 #ifdef USE_DATETIME_DATETIME 1648 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"datetime")
1653 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"text")
1658 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"select")
1663 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"climate")
1668 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"lock")
1673 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"valve")
1677 #ifdef USE_ALARM_CONTROL_PANEL 1678 if (request->method() == HTTP_GET && match.
domain ==
"alarm_control_panel")
1683 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"update")
1690 if (request->url() ==
"/") {
1695 #ifdef USE_WEBSERVER_CSS_INCLUDE 1696 if (request->url() ==
"/0.css") {
1702 #ifdef USE_WEBSERVER_JS_INCLUDE 1703 if (request->url() ==
"/0.js") {
1709 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1710 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1718 if (match.
domain ==
"sensor") {
1725 if (match.
domain ==
"switch") {
1732 if (match.
domain ==
"button") {
1738 #ifdef USE_BINARY_SENSOR 1739 if (match.
domain ==
"binary_sensor") {
1746 if (match.
domain ==
"fan") {
1753 if (match.
domain ==
"light") {
1759 #ifdef USE_TEXT_SENSOR 1760 if (match.
domain ==
"text_sensor") {
1767 if (match.
domain ==
"cover") {
1774 if (match.
domain ==
"number") {
1780 #ifdef USE_DATETIME_DATE 1781 if (match.
domain ==
"date") {
1787 #ifdef USE_DATETIME_TIME 1788 if (match.
domain ==
"time") {
1794 #ifdef USE_DATETIME_DATETIME 1795 if (match.
domain ==
"datetime") {
1802 if (match.
domain ==
"text") {
1809 if (match.
domain ==
"select") {
1816 if (match.
domain ==
"climate") {
1823 if (match.
domain ==
"lock") {
1831 if (match.
domain ==
"valve") {
1837 #ifdef USE_ALARM_CONTROL_PANEL 1838 if (match.
domain ==
"alarm_control_panel") {
1846 if (match.
domain ==
"update") {
1865 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
const std::vector< datetime::DateTimeEntity * > & get_datetimes()
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.
void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a time request under '/time/<id>'.
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.
std::string get_device_class()
Get the device class, using the manual override if set.
const std::vector< update::UpdateEntity * > & get_updates()
const std::vector< alarm_control_panel::AlarmControlPanel * > & get_alarm_control_panels()
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0...
const char * lock_state_to_string(LockState state)
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
const std::vector< valve::Valve * > & get_valves()
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).
std::map< EntityBase *, SortingComponents > sorting_entitys_
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a update request under '/update/<id>'.
const LogString * climate_mode_to_string(ClimateMode mode)
Convert the given ClimateMode to a human-readable string.
virtual FanTraits get_traits()=0
std::set< std::string > get_event_types() const
void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a valve request under '/valve/<id>/<open/close/stop/set>'.
const UpdateState & state
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
bool get_supports_position() const
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.
std::string latest_version
std::string current_version
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
virtual ValveTraits get_traits()=0
void on_event(event::Event *obj, const std::string &event_type) override
float tilt
The current tilt value of the cover from 0.0 to 1.0.
const std::vector< datetime::TimeEntity * > & get_times()
std::string get_object_id() const
uint32_t IRAM_ATTR HOT millis()
std::string event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config)
Dump the event details with its value as a JSON string.
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.
std::string update_json(update::UpdateEntity *obj, JsonDetail start_config)
Dump the update 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.
const UpdateInfo & update_info
UrlMatch match_url(const std::string &url, bool only_domain=false)
const std::vector< switch_::Switch * > & get_switches()
Base-class for all numbers.
std::string str_sprintf(const char *fmt,...)
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.
std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config)
Dump the time state with its value as a JSON string.
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.
void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a datetime request under '/datetime/<id>'.
bool get_supports_position() const
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().
void add_entity_to_sorting_list(EntityBase *entity, float weight)
std::string light_json(light::LightState *obj, JsonDetail start_config)
Dump the light state as a JSON string.
void on_valve_update(valve::Valve *obj) override
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 '/'.
std::string valve_json(valve::Valve *obj, JsonDetail start_config)
Dump the valve state as a JSON string.
void on_time_update(datetime::TimeEntity *obj) override
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()
void on_date_update(datetime::DateEntity *obj) override
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
const char * valve_operation_to_str(ValveOperation op)
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)
void on_datetime_update(datetime::DateTimeEntity *obj) override
std::string date_json(datetime::DateEntity *obj, JsonDetail start_config)
Dump the date state with its value as a JSON string.
std::string get_config_json()
Return the webserver configuration as JSON.
std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config)
Dump the datetime state with its value as a JSON string.
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
Base class for all valve devices.
std::string fan_json(fan::Fan *obj, JsonDetail start_config)
Dump the fan state as a JSON string.
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
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.
const std::vector< datetime::DateEntity * > & get_dates()
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 on_update(update::UpdateEntity *obj) override
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".
void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a date request under '/date/<id>'.
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.