12 #include "StreamString.h" 29 #ifdef USE_WEBSERVER_LOCAL 34 namespace web_server {
36 static const char *
const TAG =
"web_server";
38 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 39 static const char *
const HEADER_PNA_NAME =
"Private-Network-Access-Name";
40 static const char *
const HEADER_PNA_ID =
"Private-Network-Access-ID";
41 static const char *
const HEADER_CORS_REQ_PNA =
"Access-Control-Request-Private-Network";
42 static const char *
const HEADER_CORS_ALLOW_PNA =
"Access-Control-Allow-Private-Network";
45 #if USE_WEBSERVER_VERSION == 1 46 void write_row(AsyncResponseStream *stream,
EntityBase *obj,
const std::string &klass,
const std::string &action,
47 const std::function<
void(AsyncResponseStream &stream,
EntityBase *obj)> &action_func =
nullptr) {
48 stream->print(
"<tr class=\"");
49 stream->print(klass.c_str());
51 stream->print(
" internal");
52 stream->print(
"\" id=\"");
53 stream->print(klass.c_str());
56 stream->print(
"\"><td>");
58 stream->print(
"</td><td></td><td>");
59 stream->print(action.c_str());
61 action_func(*stream, obj);
63 stream->print(
"</td>");
64 stream->print(
"</tr>");
71 size_t domain_end = url.find(
'/', 1);
72 if (domain_end == std::string::npos)
74 match.
domain = url.substr(1, domain_end - 1);
79 if (url.length() == domain_end - 1)
81 size_t id_begin = domain_end + 1;
82 size_t id_end = url.find(
'/', id_begin);
84 if (id_end == std::string::npos) {
85 match.
id = url.substr(id_begin, url.length() - id_begin);
88 match.
id = url.substr(id_begin, id_end - id_begin);
89 size_t method_begin = id_end + 1;
90 match.
method = url.substr(method_begin, url.length() - method_begin);
101 #if USE_WEBSERVER_VERSION == 1 106 #ifdef USE_WEBSERVER_CSS_INCLUDE 109 #ifdef USE_WEBSERVER_JS_INCLUDE 124 ESP_LOGCONFIG(TAG,
"Setting up web server...");
138 [
this](
int level,
const char *tag,
const char *message) { this->
events_.send(message,
"log",
millis()); });
152 std::function<void()> fn;
169 ESP_LOGCONFIG(TAG,
"Web Server:");
174 #ifdef USE_WEBSERVER_LOCAL 176 AsyncWebServerResponse *response = request->beginResponse_P(200,
"text/html", INDEX_GZ,
sizeof(INDEX_GZ));
177 response->addHeader(
"Content-Encoding",
"gzip");
178 request->send(response);
180 #elif USE_WEBSERVER_VERSION == 1 182 AsyncResponseStream *stream = request->beginResponseStream(
"text/html");
184 stream->print(F(
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=UTF-8><meta " 185 "name=viewport content=\"width=device-width, initial-scale=1,user-scalable=no\"><title>"));
186 stream->print(title.c_str());
187 stream->print(F(
"</title>"));
188 #ifdef USE_WEBSERVER_CSS_INCLUDE 189 stream->print(F(
"<link rel=\"stylesheet\" href=\"/0.css\">"));
192 stream->print(F(R
"(<link rel="stylesheet" href=")")); 194 stream->print(F("\">"));
196 stream->print(F(
"</head><body>"));
197 stream->print(F(
"<article class=\"markdown-body\"><h1>"));
198 stream->print(title.c_str());
199 stream->print(F(
"</h1>"));
200 stream->print(F(
"<h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
212 write_row(stream, obj,
"switch",
"<button>Toggle</button>");
218 write_row(stream, obj,
"button",
"<button>Press</button>");
221 #ifdef USE_BINARY_SENSOR 224 write_row(stream, obj,
"binary_sensor",
"");
231 write_row(stream, obj,
"fan",
"<button>Toggle</button>");
238 write_row(stream, obj,
"light",
"<button>Toggle</button>");
242 #ifdef USE_TEXT_SENSOR 245 write_row(stream, obj,
"text_sensor",
"");
252 write_row(stream, obj,
"cover",
"<button>Open</button><button>Close</button>");
261 stream.print(R
"(<input type="number" min=")"); 263 stream.print(R"(" max=")"); 265 stream.print(R"(" step=")"); 267 stream.print(R"(" value=")"); 268 stream.print(number->state); 269 stream.print(R"("/>)"); 281 stream.print(R
"(<input type=")"); 283 stream.print(R
"(password)"); 285 stream.print(R
"(text)"); 287 stream.print(R"(" minlength=")"); 289 stream.print(R"(" maxlength=")"); 291 stream.print(R"(" pattern=")"); 293 stream.print(R"(" value=")"); 294 stream.print(text->state.c_str()); 295 stream.print(R"("/>)"); 306 stream.print(
"<select>");
307 stream.print(
"<option></option>");
309 stream.print(
"<option>");
310 stream.print(option.c_str());
311 stream.print(
"</option>");
313 stream.print(
"</select>");
324 stream.print(
"<button>Lock</button><button>Unlock</button>");
326 stream.print(
"<button>Open</button>");
340 stream->print(F(
"</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for " 341 "REST API documentation.</p>"));
344 F(
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input " 345 "type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
347 stream->print(F(
"<h2>Debug Log</h2><pre id=\"log\"></pre>"));
348 #ifdef USE_WEBSERVER_JS_INCLUDE 350 stream->print(F(
"<script type=\"module\" src=\"/0.js\"></script>"));
353 if (strlen(this->
js_url_) > 0) {
354 stream->print(F(
"<script src=\""));
356 stream->print(F(
"\"></script>"));
358 stream->print(F(
"</article></body></html>"));
359 request->send(stream);
361 #elif USE_WEBSERVER_VERSION >= 2 363 AsyncWebServerResponse *response =
366 request->send(response);
370 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 372 AsyncWebServerResponse *response = request->beginResponse(200,
"");
373 response->addHeader(HEADER_CORS_ALLOW_PNA,
"true");
374 response->addHeader(HEADER_PNA_NAME,
App.
get_name().c_str());
376 response->addHeader(HEADER_PNA_ID, mac.c_str());
377 request->send(response);
381 #ifdef USE_WEBSERVER_CSS_INCLUDE 383 AsyncWebServerResponse *response =
385 response->addHeader(
"Content-Encoding",
"gzip");
386 request->send(response);
390 #ifdef USE_WEBSERVER_JS_INCLUDE 392 AsyncWebServerResponse *response =
394 response->addHeader(
"Content-Encoding",
"gzip");
395 request->send(response);
399 #define set_json_id(root, obj, sensor, start_config) \ 400 (root)["id"] = sensor; \ 401 if (((start_config) == DETAIL_ALL)) { \ 402 (root)["name"] = (obj)->get_name(); \ 403 (root)["icon"] = (obj)->get_icon(); \ 404 (root)["entity_category"] = (obj)->get_entity_category(); \ 405 if ((obj)->is_disabled_by_default()) \ 406 (root)["is_disabled_by_default"] = (obj)->is_disabled_by_default(); \ 409 #define set_json_value(root, obj, sensor, value, start_config) \ 410 set_json_id((root), (obj), sensor, start_config); \ 411 (root)["value"] = value; 413 #define set_json_icon_state_value(root, obj, sensor, state, value, start_config) \ 414 set_json_value(root, obj, sensor, value, start_config); \ 415 (root)["state"] = state; 426 request->send(200,
"application/json", data.c_str());
434 if (std::isnan(value)) {
441 set_json_icon_state_value(root, obj,
"sensor-" + obj->
get_object_id(),
state, value, start_config);
450 #ifdef USE_TEXT_SENSOR 459 request->send(200,
"application/json", data.c_str());
467 set_json_icon_state_value(root, obj,
"text_sensor-" + obj->
get_object_id(), value, value, start_config);
478 set_json_icon_state_value(root, obj,
"switch-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value, start_config);
489 if (request->method() == HTTP_GET && match.
method.empty()) {
491 request->send(200,
"application/json", data.c_str());
492 }
else if (match.
method ==
"toggle") {
493 this->
schedule_([obj]() { obj->toggle(); });
495 }
else if (match.
method ==
"turn_on") {
496 this->
schedule_([obj]() { obj->turn_on(); });
498 }
else if (match.
method ==
"turn_off") {
499 this->
schedule_([obj]() { obj->turn_off(); });
513 [obj, start_config](JsonObject root) { set_json_id(root, obj,
"button-" + obj->
get_object_id(), start_config); });
520 if (match.
method ==
"press") {
521 this->
schedule_([obj]() { obj->press(); });
533 #ifdef USE_BINARY_SENSOR 539 set_json_icon_state_value(root, obj,
"binary_sensor-" + obj->
get_object_id(), value ?
"ON" :
"OFF", value,
548 request->send(200,
"application/json", data.c_str());
562 if (traits.supports_speed()) {
563 root[
"speed_level"] = obj->
speed;
564 root[
"speed_count"] = traits.supported_speed_count();
575 if (request->method() == HTTP_GET && match.
method.empty()) {
577 request->send(200,
"application/json", data.c_str());
578 }
else if (match.
method ==
"toggle") {
579 this->
schedule_([obj]() { obj->toggle().perform(); });
581 }
else if (match.
method ==
"turn_on") {
582 auto call = obj->turn_on();
583 if (request->hasParam(
"speed_level")) {
584 auto speed_level = request->getParam(
"speed_level")->value();
585 auto val = parse_number<int>(speed_level.c_str());
586 if (!
val.has_value()) {
587 ESP_LOGW(TAG,
"Can't convert '%s' to number!", speed_level.c_str());
592 if (request->hasParam(
"oscillation")) {
593 auto speed = request->getParam(
"oscillation")->value();
597 call.set_oscillating(
true);
600 call.set_oscillating(
false);
603 call.set_oscillating(!obj->oscillating);
612 }
else if (match.
method ==
"turn_off") {
613 this->
schedule_([obj]() { obj->turn_off().perform(); });
633 if (request->method() == HTTP_GET && match.
method.empty()) {
635 request->send(200,
"application/json", data.c_str());
636 }
else if (match.
method ==
"toggle") {
637 this->
schedule_([obj]() { obj->toggle().perform(); });
639 }
else if (match.
method ==
"turn_on") {
640 auto call = obj->turn_on();
641 if (request->hasParam(
"brightness")) {
642 auto brightness = parse_number<float>(request->getParam(
"brightness")->value().c_str());
643 if (brightness.has_value()) {
644 call.set_brightness(*brightness / 255.0f);
647 if (request->hasParam(
"r")) {
648 auto r = parse_number<float>(request->getParam(
"r")->value().c_str());
650 call.set_red(*r / 255.0f);
653 if (request->hasParam(
"g")) {
654 auto g = parse_number<float>(request->getParam(
"g")->value().c_str());
656 call.set_green(*g / 255.0f);
659 if (request->hasParam(
"b")) {
660 auto b = parse_number<float>(request->getParam(
"b")->value().c_str());
662 call.set_blue(*b / 255.0f);
665 if (request->hasParam(
"white_value")) {
666 auto white_value = parse_number<float>(request->getParam(
"white_value")->value().c_str());
667 if (white_value.has_value()) {
668 call.set_white(*white_value / 255.0f);
671 if (request->hasParam(
"color_temp")) {
672 auto color_temp = parse_number<float>(request->getParam(
"color_temp")->value().c_str());
673 if (color_temp.has_value()) {
674 call.set_color_temperature(*color_temp);
677 if (request->hasParam(
"flash")) {
678 auto flash = parse_number<uint32_t>(request->getParam(
"flash")->value().c_str());
679 if (flash.has_value()) {
680 call.set_flash_length(*flash * 1000);
683 if (request->hasParam(
"transition")) {
684 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
685 if (transition.has_value()) {
686 call.set_transition_length(*transition * 1000);
689 if (request->hasParam(
"effect")) {
690 const char *effect = request->getParam(
"effect")->value().c_str();
691 call.set_effect(effect);
696 }
else if (match.
method ==
"turn_off") {
697 auto call = obj->turn_off();
698 if (request->hasParam(
"transition")) {
699 auto transition = parse_number<uint32_t>(request->getParam(
"transition")->value().c_str());
700 if (transition.has_value()) {
701 call.set_transition_length(*transition * 1000);
715 set_json_id(root, obj,
"light-" + obj->
get_object_id(), start_config);
720 JsonArray opt = root.createNestedArray(
"effects");
723 opt.add(option->get_name());
739 if (request->method() == HTTP_GET && match.
method.empty()) {
741 request->send(200,
"application/json", data.c_str());
745 auto call = obj->make_call();
746 if (match.
method ==
"open") {
747 call.set_command_open();
748 }
else if (match.
method ==
"close") {
749 call.set_command_close();
750 }
else if (match.
method ==
"stop") {
751 call.set_command_stop();
752 }
else if (match.
method ==
"toggle") {
753 call.set_command_toggle();
754 }
else if (match.
method !=
"set") {
759 auto traits = obj->get_traits();
760 if ((request->hasParam(
"position") && !traits.get_supports_position()) ||
761 (request->hasParam(
"tilt") && !traits.get_supports_tilt())) {
766 if (request->hasParam(
"position")) {
767 auto position = parse_number<float>(request->getParam(
"position")->value().c_str());
772 if (request->hasParam(
"tilt")) {
773 auto tilt = parse_number<float>(request->getParam(
"tilt")->value().c_str());
774 if (
tilt.has_value()) {
794 root[
"tilt"] = obj->
tilt;
808 if (request->method() == HTTP_GET && match.
method.empty()) {
810 request->send(200,
"application/json", data.c_str());
813 if (match.
method !=
"set") {
818 auto call = obj->make_call();
819 if (request->hasParam(
"value")) {
820 auto value = parse_number<float>(request->getParam(
"value")->value().c_str());
821 if (value.has_value())
822 call.set_value(*value);
834 set_json_id(root, obj,
"number-" + obj->
get_object_id(), start_config);
843 if (std::isnan(value)) {
844 root[
"value"] =
"\"NaN\"";
845 root[
"state"] =
"NA";
847 root[
"value"] = value;
851 root[
"state"] =
state;
857 #ifdef USE_DATETIME_DATE 865 if (request->method() == HTTP_GET) {
867 request->send(200,
"application/json", data.c_str());
870 if (match.
method !=
"set") {
875 auto call = obj->make_call();
877 if (!request->hasParam(
"value")) {
882 if (request->hasParam(
"value")) {
883 std::string value = request->getParam(
"value")->value().c_str();
884 call.set_date(value);
896 set_json_id(root, obj,
"date-" + obj->
get_object_id(), start_config);
898 root[
"value"] = value;
899 root[
"state"] = value;
902 #endif // USE_DATETIME_DATE 913 if (request->method() == HTTP_GET && match.
method.empty()) {
915 request->send(200,
"text/json", data.c_str());
918 if (match.
method !=
"set") {
923 auto call = obj->make_call();
924 if (request->hasParam(
"value")) {
925 String value = request->getParam(
"value")->value();
926 call.set_value(value.c_str());
938 set_json_id(root, obj,
"text-" + obj->
get_object_id(), start_config);
946 root[
"state"] =
"********";
948 root[
"state"] = value;
950 root[
"value"] = value;
964 if (request->method() == HTTP_GET && match.
method.empty()) {
966 auto *param = request->getParam(
"detail");
967 if (param && param->value() ==
"all") {
970 std::string data = this->
select_json(obj, obj->state, detail);
971 request->send(200,
"application/json", data.c_str());
975 if (match.
method !=
"set") {
980 auto call = obj->make_call();
982 if (request->hasParam(
"option")) {
983 auto option = request->getParam(
"option")->value();
984 call.set_option(option.c_str());
995 set_json_icon_state_value(root, obj,
"select-" + obj->
get_object_id(), value, value, start_config);
997 JsonArray opt = root.createNestedArray(
"option");
1007 #define PSTR_LOCAL(mode_s) strncpy_P(buf, (PGM_P) ((mode_s)), 15) 1019 if (request->method() == HTTP_GET && match.
method.empty()) {
1021 request->send(200,
"application/json", data.c_str());
1025 if (match.
method !=
"set") {
1030 auto call = obj->make_call();
1032 if (request->hasParam(
"mode")) {
1033 auto mode = request->getParam(
"mode")->value();
1037 if (request->hasParam(
"target_temperature_high")) {
1038 auto target_temperature_high = parse_number<float>(request->getParam(
"target_temperature_high")->value().c_str());
1043 if (request->hasParam(
"target_temperature_low")) {
1044 auto target_temperature_low = parse_number<float>(request->getParam(
"target_temperature_low")->value().c_str());
1049 if (request->hasParam(
"target_temperature")) {
1050 auto target_temperature = parse_number<float>(request->getParam(
"target_temperature")->value().c_str());
1064 set_json_id(root, obj,
"climate-" + obj->
get_object_id(), start_config);
1067 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
1071 JsonArray opt = root.createNestedArray(
"modes");
1074 if (!traits.get_supported_custom_fan_modes().empty()) {
1075 JsonArray opt = root.createNestedArray(
"fan_modes");
1080 if (!traits.get_supported_custom_fan_modes().empty()) {
1081 JsonArray opt = root.createNestedArray(
"custom_fan_modes");
1082 for (
auto const &
custom_fan_mode : traits.get_supported_custom_fan_modes())
1085 if (traits.get_supports_swing_modes()) {
1086 JsonArray opt = root.createNestedArray(
"swing_modes");
1087 for (
auto swing_mode : traits.get_supported_swing_modes())
1091 JsonArray opt = root.createNestedArray(
"presets");
1096 JsonArray opt = root.createNestedArray(
"custom_presets");
1097 for (
auto const &
custom_preset : traits.get_supported_custom_presets())
1102 bool has_state =
false;
1106 root[
"step"] = traits.get_visual_target_temperature_step();
1107 if (traits.get_supports_action()) {
1109 root[
"state"] = root[
"action"];
1124 if (traits.get_supports_swing_modes()) {
1127 if (traits.get_supports_current_temperature()) {
1131 root[
"current_temperature"] =
"NA";
1134 if (traits.get_supports_two_point_target_temperature()) {
1144 root[
"state"] = root[
"target_temperature"];
1165 if (request->method() == HTTP_GET && match.
method.empty()) {
1167 request->send(200,
"application/json", data.c_str());
1168 }
else if (match.
method ==
"lock") {
1169 this->
schedule_([obj]() { obj->lock(); });
1171 }
else if (match.
method ==
"unlock") {
1172 this->
schedule_([obj]() { obj->unlock(); });
1174 }
else if (match.
method ==
"open") {
1175 this->
schedule_([obj]() { obj->open(); });
1186 #ifdef USE_ALARM_CONTROL_PANEL 1195 set_json_icon_state_value(root, obj,
"alarm-control-panel-" + obj->
get_object_id(),
1204 if (request->method() == HTTP_GET && match.
method.empty()) {
1206 request->send(200,
"application/json", data.c_str());
1215 if (request->url() ==
"/")
1218 #ifdef USE_WEBSERVER_CSS_INCLUDE 1219 if (request->url() ==
"/0.css")
1223 #ifdef USE_WEBSERVER_JS_INCLUDE 1224 if (request->url() ==
"/0.js")
1228 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1229 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1234 request->addInterestingHeader(HEADER_CORS_REQ_PNA);
1244 if (request->method() == HTTP_GET && match.
domain ==
"sensor")
1249 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"switch")
1254 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"button")
1258 #ifdef USE_BINARY_SENSOR 1259 if (request->method() == HTTP_GET && match.
domain ==
"binary_sensor")
1264 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"fan")
1269 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"light")
1273 #ifdef USE_TEXT_SENSOR 1274 if (request->method() == HTTP_GET && match.
domain ==
"text_sensor")
1279 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"cover")
1284 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"number")
1288 #ifdef USE_DATETIME_DATE 1289 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"date")
1294 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"text")
1299 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"select")
1304 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"climate")
1309 if ((request->method() == HTTP_POST || request->method() == HTTP_GET) && match.
domain ==
"lock")
1313 #ifdef USE_ALARM_CONTROL_PANEL 1314 if (request->method() == HTTP_GET && match.
domain ==
"alarm_control_panel")
1321 if (request->url() ==
"/") {
1326 #ifdef USE_WEBSERVER_CSS_INCLUDE 1327 if (request->url() ==
"/0.css") {
1333 #ifdef USE_WEBSERVER_JS_INCLUDE 1334 if (request->url() ==
"/0.js") {
1340 #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS 1341 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1349 if (match.
domain ==
"sensor") {
1356 if (match.
domain ==
"switch") {
1363 if (match.
domain ==
"button") {
1369 #ifdef USE_BINARY_SENSOR 1370 if (match.
domain ==
"binary_sensor") {
1377 if (match.
domain ==
"fan") {
1384 if (match.
domain ==
"light") {
1390 #ifdef USE_TEXT_SENSOR 1391 if (match.
domain ==
"text_sensor") {
1398 if (match.
domain ==
"cover") {
1405 if (match.
domain ==
"number") {
1411 #ifdef USE_DATETIME_DATE 1412 if (match.
domain ==
"date") {
1419 if (match.
domain ==
"text") {
1426 if (match.
domain ==
"select") {
1433 if (match.
domain ==
"climate") {
1440 if (match.
domain ==
"lock") {
1447 #ifdef USE_ALARM_CONTROL_PANEL 1448 if (match.
domain ==
"alarm_control_panel") {
1464 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.
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.
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.
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.
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()
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
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 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.
Base-class for all selects.
void on_fan_update(fan::Fan *obj) override
web_server_base::WebServerBase * base_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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.
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 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.