11#if !defined(USE_ESP32) && defined(USE_ARDUINO)
12#include "StreamString.h"
29#ifdef USE_WEBSERVER_LOCAL
30#if USE_WEBSERVER_VERSION == 2
32#elif USE_WEBSERVER_VERSION == 3
40static const char *
const TAG =
"web_server";
42#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
43static const char *
const HEADER_PNA_NAME =
"Private-Network-Access-Name";
44static const char *
const HEADER_PNA_ID =
"Private-Network-Access-ID";
45static const char *
const HEADER_CORS_REQ_PNA =
"Access-Control-Request-Private-Network";
46static const char *
const HEADER_CORS_ALLOW_PNA =
"Access-Control-Allow-Private-Network";
50static UrlMatch match_url(
const char *url_ptr,
size_t url_len,
bool only_domain) {
54 if (url_len < 2 || url_ptr[0] !=
'/') {
59 const char *start = url_ptr + 1;
60 const char *
end = url_ptr + url_len;
63 const char *domain_end = (
const char *) memchr(start,
'/',
end - start);
71 match.domain_len = domain_end - start;
79 if (domain_end + 1 >=
end) {
83 const char *id_start = domain_end + 1;
84 const char *id_end = (
const char *) memchr(id_start,
'/',
end - id_start);
89 match.id_len =
end - id_start;
95 match.id_len = id_end - id_start;
98 if (id_end + 1 <
end) {
99 match.method = id_end + 1;
100 match.method_len =
end - (id_end + 1);
106#if !defined(USE_ESP32) && defined(USE_ARDUINO)
109 DeferredEvent item(source, message_generator);
118 this->deferred_queue_.push_back(item);
125 if (this->send(
message.c_str(),
"state") != DISCARDED) {
137 ESP_LOGW(TAG,
"Closing stuck EventSource connection after %" PRIu16
" failed sends",
160 if (source ==
nullptr)
162 if (event_type ==
nullptr)
164 if (message_generator ==
nullptr)
167 if (0 != strcmp(event_type,
"state_detail_all") && 0 != strcmp(event_type,
"state")) {
168 ESP_LOGE(TAG,
"Can't defer non-state event");
178 if (this->send(
message.c_str(),
"state") == DISCARDED) {
188 uint32_t reconnect) {
189 this->send(
message, event,
id, reconnect);
206 uint32_t reconnect) {
216 es->onConnect([
this, ws, es](AsyncEventSourceClient *client) {
220 es->onDisconnect([
this, ws, es](AsyncEventSourceClient *client) {
224 es->handleRequest(request);
233#ifdef USE_WEBSERVER_SORTING
236 JsonObject root = builder.
root();
237 root[
"name"] = group.second.name;
238 root[
"sorting_weight"] = group.second.weight;
258 this->remove(source);
265#ifdef USE_WEBSERVER_CSS_INCLUDE
268#ifdef USE_WEBSERVER_JS_INCLUDE
274 JsonObject root = builder.
root();
278#if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA)
297 [
this](
int level,
const char *tag,
const char *
message,
size_t message_len) {
324#ifdef USE_WEBSERVER_LOCAL
327 AsyncWebServerResponse *response = request->beginResponse(200,
"text/html", INDEX_GZ,
sizeof(INDEX_GZ));
329 AsyncWebServerResponse *response = request->beginResponse_P(200,
"text/html", INDEX_GZ,
sizeof(INDEX_GZ));
331 response->addHeader(
"Content-Encoding",
"gzip");
332 request->send(response);
334#elif USE_WEBSERVER_VERSION >= 2
337 AsyncWebServerResponse *response =
340 AsyncWebServerResponse *response =
344 request->send(response);
348#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
350 AsyncWebServerResponse *response = request->beginResponse(200,
"");
351 response->addHeader(HEADER_CORS_ALLOW_PNA,
"true");
352 response->addHeader(HEADER_PNA_NAME,
App.
get_name().c_str());
354 response->addHeader(HEADER_PNA_ID, mac.c_str());
355 request->send(response);
359#ifdef USE_WEBSERVER_CSS_INCLUDE
362 AsyncWebServerResponse *response =
365 AsyncWebServerResponse *response =
368 response->addHeader(
"Content-Encoding",
"gzip");
369 request->send(response);
373#ifdef USE_WEBSERVER_JS_INCLUDE
376 AsyncWebServerResponse *response =
379 AsyncWebServerResponse *response =
382 response->addHeader(
"Content-Encoding",
"gzip");
383 request->send(response);
388static void set_json_id(JsonObject &root,
EntityBase *obj,
const char *prefix,
JsonDetail start_config) {
391 snprintf(id_buf,
sizeof(id_buf),
"%s-%s", prefix, object_id.c_str());
399 root[
"is_disabled_by_default"] = is_disabled;
406static void set_json_value(JsonObject &root, EntityBase *obj,
const char *prefix,
const T &value,
408 set_json_id(root, obj, prefix, start_config);
409 root[
"value"] = value;
413static void set_json_icon_state_value(JsonObject &root, EntityBase *obj,
const char *prefix,
const std::string &
state,
415 set_json_value(root, obj, prefix, value, start_config);
416 root[
"state"] =
state;
420static JsonDetail get_request_detail(AsyncWebServerRequest *request) {
421 auto *param = request->getParam(
"detail");
435 if (request->method() == HTTP_GET && match.
method_empty()) {
436 auto detail = get_request_detail(request);
437 std::string data = this->
sensor_json(obj, obj->state, detail);
438 request->send(200,
"application/json", data.c_str());
452 JsonObject root = builder.
root();
458 if (std::isnan(value)) {
463 set_json_icon_state_value(root, obj,
"sensor",
state, value, start_config);
466 if (!uom_ref.empty())
467 root[
"uom"] = uom_ref;
474#ifdef USE_TEXT_SENSOR
484 if (request->method() == HTTP_GET && match.
method_empty()) {
485 auto detail = get_request_detail(request);
487 request->send(200,
"application/json", data.c_str());
504 JsonObject root = builder.
root();
506 set_json_icon_state_value(root, obj,
"text_sensor", value, value, start_config);
526 if (request->method() == HTTP_GET && match.
method_empty()) {
527 auto detail = get_request_detail(request);
528 std::string data = this->
switch_json(obj, obj->state, detail);
529 request->send(200,
"application/json", data.c_str());
534 enum SwitchAction { NONE, TOGGLE, TURN_ON, TURN_OFF };
535 SwitchAction action = NONE;
545 if (action != NONE) {
546 this->
defer([obj, action]() {
577 JsonObject root = builder.
root();
579 set_json_icon_state_value(root, obj,
"switch", value ?
"ON" :
"OFF", value, start_config);
594 if (request->method() == HTTP_GET && match.
method_empty()) {
595 auto detail = get_request_detail(request);
597 request->send(200,
"application/json", data.c_str());
599 this->
defer([obj]() { obj->press(); });
617 JsonObject root = builder.
root();
619 set_json_id(root, obj,
"button", start_config);
628#ifdef USE_BINARY_SENSOR
638 if (request->method() == HTTP_GET && match.
method_empty()) {
639 auto detail = get_request_detail(request);
641 request->send(200,
"application/json", data.c_str());
657 JsonObject root = builder.
root();
659 set_json_icon_state_value(root, obj,
"binary_sensor", value ?
"ON" :
"OFF", value, start_config);
679 if (request->method() == HTTP_GET && match.
method_empty()) {
680 auto detail = get_request_detail(request);
681 std::string data = this->
fan_json(obj, detail);
682 request->send(200,
"application/json", data.c_str());
684 this->
defer([obj]() { obj->toggle().perform(); });
691 if (request->hasParam(
"oscillation")) {
692 auto speed = request->getParam(
"oscillation")->value();
696 call.set_oscillating(
true);
699 call.set_oscillating(
false);
702 call.set_oscillating(!obj->oscillating);
726 JsonObject root = builder.
root();
728 set_json_icon_state_value(root, obj,
"fan", obj->
state ?
"ON" :
"OFF", obj->
state, start_config);
730 if (traits.supports_speed()) {
731 root[
"speed_level"] = obj->
speed;
732 root[
"speed_count"] = traits.supported_speed_count();
755 if (request->method() == HTTP_GET && match.
method_empty()) {
756 auto detail = get_request_detail(request);
757 std::string data = this->
light_json(obj, detail);
758 request->send(200,
"application/json", data.c_str());
760 this->
defer([obj]() { obj->toggle().perform(); });
763 auto call = obj->turn_on();
782 auto call = obj->turn_off();
801 JsonObject root = builder.
root();
803 set_json_id(root, obj,
"light", start_config);
808 JsonArray opt = root[
"effects"].to<JsonArray>();
811 opt.add(option->get_name());
831 if (request->method() == HTTP_GET && match.
method_empty()) {
832 auto detail = get_request_detail(request);
833 std::string data = this->
cover_json(obj, detail);
834 request->send(200,
"application/json", data.c_str());
838 auto call = obj->make_call();
841 static const struct {
852 for (
const auto &method : METHODS) {
854 (
call.*method.action)();
865 auto traits = obj->get_traits();
866 if ((request->hasParam(
"position") && !traits.get_supports_position()) ||
867 (request->hasParam(
"tilt") && !traits.get_supports_tilt())) {
889 JsonObject root = builder.
root();
898 root[
"tilt"] = obj->
tilt;
914 for (
auto *obj :
App.get_numbers()) {
918 if (request->method() == HTTP_GET && match.
method_empty()) {
919 auto detail = get_request_detail(request);
920 std::string data = this->
number_json(obj, obj->state, detail);
921 request->send(200,
"application/json", data.c_str());
929 auto call = obj->make_call();
947 JsonObject root = builder.
root();
951 set_json_id(root, obj,
"number", start_config);
959 if (!uom_ref.empty())
960 root[
"uom"] = uom_ref;
963 if (std::isnan(value)) {
964 root[
"value"] =
"\"NaN\"";
965 root[
"state"] =
"NA";
976#ifdef USE_DATETIME_DATE
983 for (
auto *obj :
App.get_dates()) {
986 if (request->method() == HTTP_GET && match.
method_empty()) {
987 auto detail = get_request_detail(request);
988 std::string data = this->
date_json(obj, detail);
989 request->send(200,
"application/json", data.c_str());
997 auto call = obj->make_call();
999 if (!request->hasParam(
"value")) {
1021 JsonObject root = builder.
root();
1023 set_json_id(root, obj,
"date", start_config);
1025 root[
"value"] = value;
1026 root[
"state"] = value;
1035#ifdef USE_DATETIME_TIME
1042 for (
auto *obj :
App.get_times()) {
1045 if (request->method() == HTTP_GET && match.
method_empty()) {
1046 auto detail = get_request_detail(request);
1047 std::string data = this->
time_json(obj, detail);
1048 request->send(200,
"application/json", data.c_str());
1056 auto call = obj->make_call();
1058 if (!request->hasParam(
"value")) {
1079 JsonObject root = builder.
root();
1081 set_json_id(root, obj,
"time", start_config);
1083 root[
"value"] = value;
1084 root[
"state"] = value;
1093#ifdef USE_DATETIME_DATETIME
1100 for (
auto *obj :
App.get_datetimes()) {
1103 if (request->method() == HTTP_GET && match.
method_empty()) {
1104 auto detail = get_request_detail(request);
1106 request->send(200,
"application/json", data.c_str());
1114 auto call = obj->make_call();
1116 if (!request->hasParam(
"value")) {
1137 JsonObject root = builder.
root();
1139 set_json_id(root, obj,
"datetime", start_config);
1142 root[
"value"] = value;
1143 root[
"state"] = value;
1159 for (
auto *obj :
App.get_texts()) {
1163 if (request->method() == HTTP_GET && match.
method_empty()) {
1164 auto detail = get_request_detail(request);
1165 std::string data = this->
text_json(obj, obj->state, detail);
1166 request->send(200,
"application/json", data.c_str());
1174 auto call = obj->make_call();
1192 JsonObject root = builder.
root();
1194 set_json_id(root, obj,
"text", start_config);
1199 root[
"state"] =
"********";
1201 root[
"state"] = value;
1203 root[
"value"] = value;
1220 for (
auto *obj :
App.get_selects()) {
1224 if (request->method() == HTTP_GET && match.
method_empty()) {
1225 auto detail = get_request_detail(request);
1226 std::string data = this->
select_json(obj, obj->state, detail);
1227 request->send(200,
"application/json", data.c_str());
1236 auto call = obj->make_call();
1253 JsonObject root = builder.
root();
1255 set_json_icon_state_value(root, obj,
"select", value, value, start_config);
1257 JsonArray opt = root[
"option"].to<JsonArray>();
1269#define PSTR_LOCAL(mode_s) ESPHOME_strncpy_P(buf, (ESPHOME_PGM_P) ((mode_s)), 15)
1278 for (
auto *obj :
App.get_climates()) {
1282 if (request->method() == HTTP_GET && match.
method_empty()) {
1283 auto detail = get_request_detail(request);
1285 request->send(200,
"application/json", data.c_str());
1294 auto call = obj->make_call();
1323 JsonObject root = builder.
root();
1324 set_json_id(root, obj,
"climate", start_config);
1327 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
1331 JsonArray opt = root[
"modes"].to<JsonArray>();
1334 if (!traits.get_supported_custom_fan_modes().empty()) {
1335 JsonArray opt = root[
"fan_modes"].to<JsonArray>();
1340 if (!traits.get_supported_custom_fan_modes().empty()) {
1341 JsonArray opt = root[
"custom_fan_modes"].to<JsonArray>();
1342 for (
auto const &
custom_fan_mode : traits.get_supported_custom_fan_modes())
1345 if (traits.get_supports_swing_modes()) {
1346 JsonArray opt = root[
"swing_modes"].to<JsonArray>();
1347 for (
auto swing_mode : traits.get_supported_swing_modes())
1351 JsonArray opt = root[
"presets"].to<JsonArray>();
1356 JsonArray opt = root[
"custom_presets"].to<JsonArray>();
1357 for (
auto const &
custom_preset : traits.get_supported_custom_presets())
1363 bool has_state =
false;
1364 root[
"mode"] = PSTR_LOCAL(climate_mode_to_string(obj->
mode));
1367 root[
"step"] = traits.get_visual_target_temperature_step();
1368 if (traits.get_supports_action()) {
1369 root[
"action"] = PSTR_LOCAL(climate_action_to_string(obj->
action));
1370 root[
"state"] = root[
"action"];
1374 root[
"fan_mode"] = PSTR_LOCAL(climate_fan_mode_to_string(obj->
fan_mode.
value()));
1380 root[
"preset"] = PSTR_LOCAL(climate_preset_to_string(obj->
preset.
value()));
1385 if (traits.get_supports_swing_modes()) {
1386 root[
"swing_mode"] = PSTR_LOCAL(climate_swing_mode_to_string(obj->
swing_mode));
1388 if (traits.get_supports_current_temperature()) {
1392 root[
"current_temperature"] =
"NA";
1395 if (traits.get_supports_two_point_target_temperature()) {
1405 root[
"state"] = root[
"target_temperature"];
1424 if (request->method() == HTTP_GET && match.
method_empty()) {
1425 auto detail = get_request_detail(request);
1426 std::string data = this->
lock_json(obj, obj->state, detail);
1427 request->send(200,
"application/json", data.c_str());
1432 enum LockAction { NONE, LOCK, UNLOCK, OPEN };
1433 LockAction action = NONE;
1443 if (action != NONE) {
1444 this->
defer([obj, action]() {
1475 JsonObject root = builder.
root();
1497 if (request->method() == HTTP_GET && match.
method_empty()) {
1498 auto detail = get_request_detail(request);
1499 std::string data = this->
valve_json(obj, detail);
1500 request->send(200,
"application/json", data.c_str());
1504 auto call = obj->make_call();
1507 static const struct {
1518 for (
const auto &method : METHODS) {
1520 (
call.*method.action)();
1531 auto traits = obj->get_traits();
1532 if (request->hasParam(
"position") && !traits.get_supports_position()) {
1553 JsonObject root = builder.
root();
1569#ifdef USE_ALARM_CONTROL_PANEL
1580 if (request->method() == HTTP_GET && match.
method_empty()) {
1581 auto detail = get_request_detail(request);
1583 request->send(200,
"application/json", data.c_str());
1587 auto call = obj->make_call();
1591 static const struct {
1603 for (
const auto &method : METHODS) {
1605 (
call.*method.action)();
1636 JsonObject root = builder.
root();
1639 set_json_icon_state_value(root, obj,
"alarm-control-panel", PSTR_LOCAL(alarm_control_panel_state_to_string(value)),
1640 value, start_config);
1659 if (request->method() == HTTP_GET && match.
method_empty()) {
1660 auto detail = get_request_detail(request);
1661 std::string data = this->
event_json(obj,
"", detail);
1662 request->send(200,
"application/json", data.c_str());
1669static std::string get_event_type(
event::Event *event) {
1670 return (event && event->
last_event_type) ? *
event->last_event_type :
"";
1683 JsonObject root = builder.
root();
1685 set_json_id(root, obj,
"event", start_config);
1686 if (!event_type.empty()) {
1687 root[
"event_type"] = event_type;
1690 JsonArray event_types = root[
"event_types"].to<JsonArray>();
1692 event_types.add(event_type);
1708 return "UPDATE AVAILABLE";
1710 return "INSTALLING";
1726 if (request->method() == HTTP_GET && match.
method_empty()) {
1727 auto detail = get_request_detail(request);
1728 std::string data = this->
update_json(obj, detail);
1729 request->send(200,
"application/json", data.c_str());
1738 this->
defer([obj]()
mutable { obj->perform(); });
1755 JsonObject root = builder.
root();
1757 set_json_id(root, obj,
"update", start_config);
1759 root[
"state"] = update_state_to_string(obj->
state);
1774 const auto &url = request->url();
1775 const auto method = request->method();
1778 static const char *
const STATIC_URLS[] = {
1780#if !defined(USE_ESP32) && defined(USE_ARDUINO)
1783#ifdef USE_WEBSERVER_CSS_INCLUDE
1786#ifdef USE_WEBSERVER_JS_INCLUDE
1791 for (
const auto &static_url : STATIC_URLS) {
1792 if (url == static_url)
1796#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
1797 if (method == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA))
1802 UrlMatch match = match_url(url.c_str(), url.length(),
true);
1807 bool is_get = method == HTTP_GET;
1808 bool is_post = method == HTTP_POST;
1809 bool is_get_or_post = is_get || is_post;
1811 if (!is_get_or_post)
1815 static const char *
const GET_ONLY_DOMAINS[] = {
1819#ifdef USE_BINARY_SENSOR
1822#ifdef USE_TEXT_SENSOR
1830 static const char *
const GET_POST_DOMAINS[] = {
1849#ifdef USE_DATETIME_DATE
1852#ifdef USE_DATETIME_TIME
1855#ifdef USE_DATETIME_DATETIME
1873#ifdef USE_ALARM_CONTROL_PANEL
1874 "alarm_control_panel",
1883 for (
const auto &domain : GET_ONLY_DOMAINS) {
1890 if (is_get_or_post) {
1891 for (
const auto &domain : GET_POST_DOMAINS) {
1900 const auto &url = request->url();
1908#if !defined(USE_ESP32) && defined(USE_ARDUINO)
1909 if (url ==
"/events") {
1910 this->
events_.add_new_client(
this, request);
1915#ifdef USE_WEBSERVER_CSS_INCLUDE
1916 if (url ==
"/0.css") {
1922#ifdef USE_WEBSERVER_JS_INCLUDE
1923 if (url ==
"/0.js") {
1929#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
1930 if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
1937 UrlMatch match = match_url(url.c_str(), url.length(),
false);
1940 struct ComponentRoute {
1945 static const ComponentRoute ROUTES[] = {
1955#ifdef USE_BINARY_SENSOR
1964#ifdef USE_TEXT_SENSOR
1973#ifdef USE_DATETIME_DATE
1976#ifdef USE_DATETIME_TIME
1979#ifdef USE_DATETIME_DATETIME
1997#ifdef USE_ALARM_CONTROL_PANEL
2006 for (
const auto &route : ROUTES) {
2008 (this->*route.handler)(request, match);
2014 ESP_LOGV(TAG,
"Request for unknown URL: %s", url.c_str());
2015 request->send(404,
"text/plain",
"Not Found");
2021#ifdef USE_WEBSERVER_SORTING
2024 if (this->
sorting_groups_.find(this->sorting_entitys_[entity].group_id) != this->sorting_groups_.end()) {
2031#ifdef USE_WEBSERVER_SORTING
std::string get_comment() const
Get the comment of this Application set by pre_setup().
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
const std::string & get_name() const
Get the name of this Application set by pre_setup().
auto & get_binary_sensors() const
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void begin(bool include_internal=false)
void setup_controller(bool include_internal=false)
StringRef get_device_class_ref() const
Get the device class as StringRef.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
bool is_disabled_by_default() const
std::string get_object_id() const
EntityCategory get_entity_category() const
AlarmControlPanelCall & arm_night()
AlarmControlPanelCall & disarm()
AlarmControlPanelCall & arm_away()
AlarmControlPanelCall & arm_home()
AlarmControlPanelCall & arm_vacation()
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
float target_temperature
The target temperature of the climate device.
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
ClimateSwingMode swing_mode
The active swing mode of the climate device.
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
optional< std::string > custom_preset
The active custom preset mode of the climate device.
float current_temperature
The current temperature of the climate device, as reported from the integration.
ClimateAction action
The active state of the climate device.
optional< ClimatePreset > preset
The active preset of the climate device.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
int8_t get_target_temperature_accuracy_decimals() const
CoverCall & set_command_toggle()
Set the command to toggle the cover.
CoverCall & set_command_open()
Set the command to open the cover.
CoverCall & set_command_close()
Set the command to close the cover.
CoverCall & set_command_stop()
Set the command to stop the cover.
Base class for all cover devices.
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
float tilt
The current tilt value of the cover from 0.0 to 1.0.
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
virtual CoverTraits get_traits()=0
bool get_supports_position() const
bool get_supports_tilt() const
std::set< std::string > get_event_types() const
const std::string * last_event_type
virtual FanTraits get_traits()=0
bool oscillating
The current oscillation state of the fan.
bool state
The current on/off state of the fan.
int speed
The current fan speed level.
bool supports_oscillation() const
Return if this fan supports oscillation.
Builder class for creating JSON documents without lambdas.
bool is_on() const
Get the binary true/false state of these light color values.
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
LightColorValues remote_values
The remote color values reported to the frontend.
Base class for all locks.
void add_on_log_callback(std::function< void(uint8_t, const char *, const char *, size_t)> &&callback)
Register a callback that will be called for every log message sent.
Base-class for all numbers.
float get_min_value() const
float get_max_value() const
NumberMode get_mode() const
value_type const & value() const
Base-class for all selects.
const std::vector< std::string > & get_options() const
Base-class for all sensors.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Base class for all switches.
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Base-class for all text inputs.
TextMode get_mode() const
int get_max_length() const
std::string get_pattern() const
int get_min_length() const
const UpdateState & state
const UpdateInfo & update_info
ValveCall & set_command_close()
Set the command to close the valve.
ValveCall & set_command_toggle()
Set the command to toggle the valve.
ValveCall & set_command_stop()
Set the command to stop the valve.
ValveCall & set_command_open()
Set the command to open the valve.
Base class for all valve devices.
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
virtual ValveTraits get_traits()=0
bool get_supports_position() const
void process_deferred_queue_()
void try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES
uint16_t consecutive_send_failures_
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
ListEntitiesIterator entities_iterator_
std::vector< DeferredEvent > deferred_queue_
void add_new_client(WebServer *ws, AsyncWebServerRequest *request)
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
void try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void on_client_connect_(WebServer *ws, DeferredUpdateEventSource *source)
void on_client_disconnect_(DeferredUpdateEventSource *source)
This class allows users to create a web server with their ESP nodes.
void setup() override
Setup the internal web server and register handlers.
void on_update(update::UpdateEntity *obj) override
static std::string text_sensor_all_json_generator(WebServer *web_server, void *source)
std::string light_json(light::LightState *obj, JsonDetail start_config)
Dump the light state as a JSON string.
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::map< EntityBase *, SortingComponents > sorting_entitys_
static std::string binary_sensor_state_json_generator(WebServer *web_server, void *source)
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.
static std::string button_state_json_generator(WebServer *web_server, void *source)
static std::string lock_all_json_generator(WebServer *web_server, void *source)
void on_light_update(light::LightState *obj) override
static std::string date_all_json_generator(WebServer *web_server, void *source)
std::string update_json(update::UpdateEntity *obj, JsonDetail start_config)
Dump the update state with its value as a JSON string.
void on_cover_update(cover::Cover *obj) override
static std::string text_state_json_generator(WebServer *web_server, void *source)
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
std::string number_json(number::Number *obj, float value, JsonDetail start_config)
Dump the number state with its value as a JSON string.
static std::string event_state_json_generator(WebServer *web_server, void *source)
std::string cover_json(cover::Cover *obj, JsonDetail start_config)
Dump the cover state as a JSON string.
static std::string datetime_all_json_generator(WebServer *web_server, void *source)
static std::string sensor_all_json_generator(WebServer *web_server, void *source)
bool isRequestHandlerTrivial() const override
This web handle is not trivial.
WebServer(web_server_base::WebServerBase *base)
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.
void on_number_update(number::Number *obj, float state) override
void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a switch request under '/switch/<id>/</turn_on/turn_off/toggle>'.
void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a event request under '/event<id>'.
void parse_light_param_uint_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(uint32_t), uint32_t scale=1)
void dump_config() override
std::string button_json(button::Button *obj, JsonDetail start_config)
Dump the button details with its value as a JSON string.
std::string valve_json(valve::Valve *obj, JsonDetail start_config)
Dump the valve state as a JSON string.
void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a button request under '/button/<id>/press'.
void on_date_update(datetime::DateEntity *obj) override
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 add_entity_config(EntityBase *entity, float weight, uint64_t group)
std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config)
Dump the datetime state with its value as a JSON string.
void handle_css_request(AsyncWebServerRequest *request)
Handle included css request under '/0.css'.
static std::string sensor_state_json_generator(WebServer *web_server, void *source)
void on_valve_update(valve::Valve *obj) override
void on_climate_update(climate::Climate *obj) override
void on_sensor_update(sensor::Sensor *obj, float state) override
static std::string switch_state_json_generator(WebServer *web_server, void *source)
void add_sorting_info_(JsonObject &root, EntityBase *entity)
void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a light request under '/light/<id>/</turn_on/turn_off/toggle>'.
static std::string event_all_json_generator(WebServer *web_server, void *source)
static std::string climate_state_json_generator(WebServer *web_server, void *source)
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
static std::string number_all_json_generator(WebServer *web_server, void *source)
void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text input request under '/text/<id>'.
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a cover request under '/cover/<id>/<open/close/stop/set>'.
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
static std::string date_state_json_generator(WebServer *web_server, void *source)
static std::string valve_all_json_generator(WebServer *web_server, void *source)
static std::string text_all_json_generator(WebServer *web_server, void *source)
web_server_base::WebServerBase * base_
static std::string binary_sensor_all_json_generator(WebServer *web_server, void *source)
static std::string light_state_json_generator(WebServer *web_server, void *source)
void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a lock request under '/lock/<id>/</lock/unlock/open>'.
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
static std::string light_all_json_generator(WebServer *web_server, void *source)
std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config)
Dump the switch state with its value as a JSON string.
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text sensor request under '/text_sensor/<id>'.
std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config)
Dump the sensor state with its value as a JSON string.
void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a date request under '/date/<id>'.
static std::string cover_all_json_generator(WebServer *web_server, void *source)
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a sensor request under '/sensor/<id>'.
static std::string text_sensor_state_json_generator(WebServer *web_server, void *source)
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a number request under '/number/<id>'.
static std::string alarm_control_panel_state_json_generator(WebServer *web_server, void *source)
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
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.
void on_text_update(text::Text *obj, const std::string &state) override
static std::string fan_state_json_generator(WebServer *web_server, void *source)
static std::string update_state_json_generator(WebServer *web_server, void *source)
void handleRequest(AsyncWebServerRequest *request) override
Override the web handler's handleRequest method.
static std::string climate_all_json_generator(WebServer *web_server, void *source)
void on_datetime_update(datetime::DateTimeEntity *obj) override
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.
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config)
Dump the time state with its value as a JSON string.
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a fan request under '/fan/<id>/</turn_on/turn_off/toggle>'.
static std::string cover_state_json_generator(WebServer *web_server, void *source)
static std::string lock_state_json_generator(WebServer *web_server, void *source)
void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a valve request under '/valve/<id>/<open/close/stop/set>'.
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a binary sensor request under '/binary_sensor/<id>'.
static std::string alarm_control_panel_all_json_generator(WebServer *web_server, void *source)
static std::string number_state_json_generator(WebServer *web_server, void *source)
void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a time request under '/time/<id>'.
std::map< uint64_t, SortingGroup > sorting_groups_
void set_css_include(const char *css_include)
Set local path to the script that's embedded in the index page.
static std::string valve_state_json_generator(WebServer *web_server, void *source)
bool canHandle(AsyncWebServerRequest *request) const override
Override the web handler's canHandle method.
std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config)
Dump the lock state with its value as a JSON string.
void handle_pna_cors_request(AsyncWebServerRequest *request)
std::string fan_json(fan::Fan *obj, JsonDetail start_config)
Dump the fan state as a JSON string.
void on_fan_update(fan::Fan *obj) override
void parse_light_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(float), float scale=1.0f)
void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a datetime request under '/datetime/<id>'.
void parse_string_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(const std::string &))
void on_event(event::Event *obj, const std::string &event_type) override
void parse_float_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(float))
void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a alarm_control_panel request under '/alarm_control_panel/<id>'.
static std::string time_state_json_generator(WebServer *web_server, void *source)
void on_lock_update(lock::Lock *obj) override
static std::string button_all_json_generator(WebServer *web_server, void *source)
static std::string select_state_json_generator(WebServer *web_server, void *source)
float get_setup_priority() const override
MQTT setup priority.
void parse_int_param_(AsyncWebServerRequest *request, const char *param_name, T &call, Ret(T::*setter)(int))
void on_time_update(datetime::TimeEntity *obj) override
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.
static std::string update_all_json_generator(WebServer *web_server, void *source)
void on_switch_update(switch_::Switch *obj, bool state) override
void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a update request under '/update/<id>'.
std::string climate_json(climate::Climate *obj, JsonDetail start_config)
Dump the climate details.
static std::string fan_all_json_generator(WebServer *web_server, void *source)
static std::string switch_all_json_generator(WebServer *web_server, void *source)
static std::string time_all_json_generator(WebServer *web_server, void *source)
void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight)
static std::string select_all_json_generator(WebServer *web_server, void *source)
const char * css_include_
static std::string datetime_state_json_generator(WebServer *web_server, void *source)
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a climate request under '/climate/<id>'.
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.
void add_handler(AsyncWebHandler *handler)
uint16_t get_port() const
ClimateSwingMode swing_mode
const LogString * climate_swing_mode_to_string(ClimateSwingMode swing_mode)
Convert the given ClimateSwingMode to a human-readable string.
const LogString * climate_preset_to_string(ClimatePreset preset)
Convert the given PresetMode to a human-readable string.
ClimatePreset
Enum for all preset modes.
const LogString * climate_fan_mode_to_string(ClimateFanMode fan_mode)
Convert the given ClimateFanMode to a human-readable string.
ClimateMode
Enum for all modes a climate device can be in.
const LogString * climate_mode_to_string(ClimateMode mode)
Convert the given ClimateMode to a human-readable string.
const char * cover_operation_to_str(CoverOperation op)
LockState
Enum for all states a lock can be in.
const char * lock_state_to_string(LockState state)
std::string get_use_address()
Get the active network hostname.
@ UPDATE_STATE_INSTALLING
const char * valve_operation_to_str(ValveOperation op)
std::string(WebServer *, void *) message_generator_t
Providing packet encoding functions for exchanging data with a remote host.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
std::string value_accuracy_with_uom_to_string(float value, int8_t accuracy_decimals, StringRef unit_of_measurement)
Create a string from a value, an accuracy in decimals, and a unit of measurement.
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
std::string str_sprintf(const char *fmt,...)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
std::string current_version
std::string latest_version
Internal helper struct that is used to parse incoming URLs.
const char * domain
Pointer to domain within URL, for example "sensor".
bool valid
Whether this match is valid.
bool method_empty() const
bool domain_equals(const char *str) const
bool method_equals(const char *str) const
bool id_equals(const std::string &str) const
friend class DeferredUpdateEventSource
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE