ESPHome 2026.3.0
Loading...
Searching...
No Matches
web_server.cpp
Go to the documentation of this file.
1#include "web_server.h"
2#ifdef USE_WEBSERVER
11#include "esphome/core/log.h"
12#include "esphome/core/util.h"
13
14#if !defined(USE_ESP32) && defined(USE_ARDUINO)
15#include "StreamString.h"
16#endif
17
18#include <cstdlib>
19
20#ifdef USE_LIGHT
22#endif
23
24#ifdef USE_LOGGER
26#endif
27
28#ifdef USE_CLIMATE
30#endif
31
32#ifdef USE_UPDATE
34#endif
35
36#ifdef USE_WATER_HEATER
38#endif
39
40#ifdef USE_INFRARED
42#endif
43
44#ifdef USE_WEBSERVER_LOCAL
45#if USE_WEBSERVER_VERSION == 2
46#include "server_index_v2.h"
47#elif USE_WEBSERVER_VERSION == 3
48#include "server_index_v3.h"
49#endif
50#endif
51
52namespace esphome::web_server {
53
54static const char *const TAG = "web_server";
55
56// Longest: UPDATE AVAILABLE (16 chars + null terminator, rounded up)
57static constexpr size_t PSTR_LOCAL_SIZE = 18;
58#define PSTR_LOCAL(mode_s) ESPHOME_strncpy_P(buf, (ESPHOME_PGM_P) ((mode_s)), PSTR_LOCAL_SIZE - 1)
59
60// Parse URL and return match info
61// URL formats (disambiguated by HTTP method for 3-segment case):
62// GET /{domain}/{entity_name} - main device state
63// POST /{domain}/{entity_name}/{action} - main device action
64// GET /{domain}/{device_name}/{entity_name} - sub-device state (USE_DEVICES only)
65// POST /{domain}/{device_name}/{entity_name}/{action} - sub-device action (USE_DEVICES only)
66static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain, bool is_post = false) {
67 // URL must start with '/' and have content after it
68 if (url_len < 2 || url_ptr[0] != '/')
69 return UrlMatch{};
70
71 const char *p = url_ptr + 1;
72 const char *end = url_ptr + url_len;
73
74 // Helper to find next segment: returns pointer after '/' or nullptr if no more slashes
75 auto next_segment = [&end](const char *start) -> const char * {
76 const char *slash = (const char *) memchr(start, '/', end - start);
77 return slash ? slash + 1 : nullptr;
78 };
79
80 // Helper to make StringRef from segment start to next segment (or end)
81 auto make_ref = [&end](const char *start, const char *next_start) -> StringRef {
82 return StringRef(start, (next_start ? next_start - 1 : end) - start);
83 };
84
85 // Parse domain segment
86 const char *s1 = p;
87 const char *s2 = next_segment(s1);
88
89 // Must have domain with trailing slash
90 if (!s2)
91 return UrlMatch{};
92
93 UrlMatch match{};
94 match.domain = make_ref(s1, s2);
95 match.valid = true;
96
97 if (only_domain || s2 >= end)
98 return match;
99
100 // Parse remaining segments only when needed
101 const char *s3 = next_segment(s2);
102 const char *s4 = s3 ? next_segment(s3) : nullptr;
103
104 StringRef seg2 = make_ref(s2, s3);
105 StringRef seg3 = s3 ? make_ref(s3, s4) : StringRef();
106 StringRef seg4 = s4 ? make_ref(s4, nullptr) : StringRef();
107
108 // Reject empty segments
109 if (seg2.empty() || (s3 && seg3.empty()) || (s4 && seg4.empty()))
110 return UrlMatch{};
111
112 // Interpret based on segment count
113 if (!s3) {
114 // 1 segment after domain: /{domain}/{entity}
115 match.id = seg2;
116 } else if (!s4) {
117 // 2 segments after domain: /{domain}/{X}/{Y}
118 // HTTP method disambiguates: GET = device/entity, POST = entity/action
119 if (is_post) {
120 match.id = seg2;
121 match.method = seg3;
122 return match;
123 }
124#ifdef USE_DEVICES
125 match.device_name = seg2;
126 match.id = seg3;
127#else
128 return UrlMatch{}; // 3-segment GET not supported without USE_DEVICES
129#endif
130 } else {
131 // 3 segments after domain: /{domain}/{device}/{entity}/{action}
132#ifdef USE_DEVICES
133 if (!is_post) {
134 return UrlMatch{}; // 4-segment GET not supported (action requires POST)
135 }
136 match.device_name = seg2;
137 match.id = seg3;
138 match.method = seg4;
139#else
140 return UrlMatch{}; // Not supported without USE_DEVICES
141#endif
142 }
143
144 return match;
145}
146
148 EntityMatchResult result{false, this->method.empty()};
149
150#ifdef USE_DEVICES
151 Device *entity_device = entity->get_device();
152 bool url_has_device = !this->device_name.empty();
153 bool entity_has_device = (entity_device != nullptr);
154
155 // Device matching: URL device segment must match entity's device
156 if (url_has_device != entity_has_device) {
157 return result; // Mismatch: one has device, other doesn't
158 }
159 if (url_has_device && this->device_name != entity_device->get_name()) {
160 return result; // Device name doesn't match
161 }
162#endif
163
164 // Try matching by entity name (new format)
165 if (this->id == entity->get_name()) {
166 result.matched = true;
167 return result;
168 }
169
170 // Fall back to object_id (deprecated format)
171 char object_id_buf[OBJECT_ID_MAX_LEN];
172 StringRef object_id = entity->get_object_id_to(object_id_buf);
173 if (this->id == object_id) {
174 result.matched = true;
175 // Log deprecation warning
176#ifdef USE_DEVICES
177 Device *device = entity->get_device();
178 if (device != nullptr) {
179 ESP_LOGW(TAG,
180 "Deprecated URL format: /%.*s/%.*s/%.*s - use entity name '/%.*s/%s/%s' instead. "
181 "Object ID URLs will be removed in 2026.7.0.",
182 (int) this->domain.size(), this->domain.c_str(), (int) this->device_name.size(),
183 this->device_name.c_str(), (int) this->id.size(), this->id.c_str(), (int) this->domain.size(),
184 this->domain.c_str(), device->get_name(), entity->get_name().c_str());
185 } else
186#endif
187 {
188 ESP_LOGW(TAG,
189 "Deprecated URL format: /%.*s/%.*s - use entity name '/%.*s/%s' instead. "
190 "Object ID URLs will be removed in 2026.7.0.",
191 (int) this->domain.size(), this->domain.c_str(), (int) this->id.size(), this->id.c_str(),
192 (int) this->domain.size(), this->domain.c_str(), entity->get_name().c_str());
193 }
194 }
195
196 return result;
197}
198
199#if !defined(USE_ESP32) && defined(USE_ARDUINO)
200// helper for allowing only unique entries in the queue
201void __attribute__((flatten))
203 DeferredEvent item(source, message_generator);
204
205 // Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
206 for (auto &event : this->deferred_queue_) {
207 if (event == item) {
208 return; // Already in queue, no need to update since items are equal
209 }
210 }
211 this->deferred_queue_.push_back(item);
212}
213
215 while (!deferred_queue_.empty()) {
216 DeferredEvent &de = deferred_queue_.front();
217 auto message = de.message_generator_(web_server_, de.source_);
218 if (this->send(message.c_str(), "state") != DISCARDED) {
219 // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
220 deferred_queue_.erase(deferred_queue_.begin());
221 this->consecutive_send_failures_ = 0; // Reset failure count on successful send
222 } else {
223 // NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::process_buffer_()
224 // The implementations differ due to platform-specific APIs (DISCARDED vs HTTPD_SOCK_ERR_TIMEOUT, close() vs
225 // fd_.store(0)), but the failure counting and timeout logic should be kept in sync. If you change this logic,
226 // also update the ESP-IDF implementation.
229 // Too many failures, connection is likely dead
230 ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu16 " failed sends",
232 this->close();
233 this->deferred_queue_.clear();
234 }
235 break;
236 }
237 }
238}
239
245
247 message_generator_t *message_generator) {
248 // Skip if no connected clients to avoid unnecessary deferred queue processing
249 if (this->count() == 0)
250 return;
251
252 // allow all json "details_all" to go through before publishing bare state events, this avoids unnamed entries showing
253 // up in the web GUI and reduces event load during initial connect
254 if (!entities_iterator_.completed() && 0 != strcmp(event_type, "state_detail_all"))
255 return;
256
257 if (source == nullptr)
258 return;
259 if (event_type == nullptr)
260 return;
261 if (message_generator == nullptr)
262 return;
263
264 if (0 != strcmp(event_type, "state_detail_all") && 0 != strcmp(event_type, "state")) {
265 ESP_LOGE(TAG, "Can't defer non-state event");
266 }
267
268 if (!deferred_queue_.empty())
270 if (!deferred_queue_.empty()) {
271 // deferred queue still not empty which means downstream event queue full, no point trying to send first
272 deq_push_back_with_dedup_(source, message_generator);
273 } else {
274 auto message = message_generator(web_server_, source);
275 if (this->send(message.c_str(), "state") == DISCARDED) {
276 deq_push_back_with_dedup_(source, message_generator);
277 } else {
278 this->consecutive_send_failures_ = 0; // Reset failure count on successful send
279 }
280 }
281}
282
283// used for logs plus the initial ping/config
284void DeferredUpdateEventSource::try_send_nodefer(const char *message, const char *event, uint32_t id,
285 uint32_t reconnect) {
286 this->send(message, event, id, reconnect);
287}
288
290 for (DeferredUpdateEventSource *dues : *this) {
291 dues->loop();
292 }
293}
294
296 message_generator_t *message_generator) {
297 // Skip if no event sources (no connected clients) to avoid unnecessary iteration
298 if (this->empty())
299 return;
300 for (DeferredUpdateEventSource *dues : *this) {
301 dues->deferrable_send_state(source, event_type, message_generator);
302 }
303}
304
306 uint32_t reconnect) {
307 for (DeferredUpdateEventSource *dues : *this) {
308 dues->try_send_nodefer(message, event, id, reconnect);
309 }
310}
311
312void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) {
314 this->push_back(es);
315
316 es->onConnect([this, es](AsyncEventSourceClient *client) { this->on_client_connect_(es); });
317
318 es->onDisconnect([this, es](AsyncEventSourceClient *client) { this->on_client_disconnect_(es); });
319
320 es->handleRequest(request);
321}
322
324 WebServer *ws = source->web_server_;
325 ws->defer([ws, source]() {
326 // Configure reconnect timeout and send config
327 // this should always go through since the AsyncEventSourceClient event queue is empty on connect
328 auto message = ws->get_config_json();
329 source->try_send_nodefer(message.c_str(), "ping", millis(), 30000);
330
331#ifdef USE_WEBSERVER_SORTING
332 for (auto &group : ws->sorting_groups_) {
333 json::JsonBuilder builder;
334 JsonObject root = builder.root();
335 root[ESPHOME_F("name")] = group.second.name;
336 root[ESPHOME_F("sorting_weight")] = group.second.weight;
337 auto group_msg = builder.serialize();
338
339 // up to 31 groups should be able to be queued initially without defer
340 source->try_send_nodefer(group_msg.c_str(), "sorting_group");
341 }
342#endif
343
344 source->entities_iterator_.begin(ws->include_internal_);
345
346 // just dump them all up-front and take advantage of the deferred queue
347 // on second thought that takes too long, but leaving the commented code here for debug purposes
348 // while(!source->entities_iterator_.completed()) {
349 // source->entities_iterator_.advance();
350 //}
351 });
352}
353
355 source->web_server_->defer([this, source]() {
356 // This method was called via WebServer->defer() and is no longer executing in the
357 // context of the network callback. The object is now dead and can be safely deleted.
358 this->remove(source);
359 delete source; // NOLINT
360 });
361}
362#endif
363
365
366#ifdef USE_WEBSERVER_CSS_INCLUDE
367void WebServer::set_css_include(const char *css_include) { this->css_include_ = css_include; }
368#endif
369#ifdef USE_WEBSERVER_JS_INCLUDE
370void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_include; }
371#endif
372
374 json::JsonBuilder builder;
375 JsonObject root = builder.root();
376
377 root[ESPHOME_F("title")] = App.get_friendly_name().empty() ? App.get_name().c_str() : App.get_friendly_name().c_str();
378 char comment_buffer[Application::ESPHOME_COMMENT_SIZE_MAX];
379 App.get_comment_string(comment_buffer);
380 root[ESPHOME_F("comment")] = comment_buffer;
381#if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA)
382 root[ESPHOME_F("ota")] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal
383#else
384 root[ESPHOME_F("ota")] = true;
385#endif
386 root[ESPHOME_F("log")] = this->expose_log_;
387 root[ESPHOME_F("lang")] = "en";
388 root[ESPHOME_F("uptime")] = static_cast<uint32_t>(millis_64() / 1000);
389
390 return builder.serialize();
391}
392
395 this->base_->init();
396
397#ifdef USE_LOGGER
398 if (logger::global_logger != nullptr && this->expose_log_) {
400 this, [](void *self, uint8_t level, const char *tag, const char *message, size_t message_len) {
401 static_cast<WebServer *>(self)->on_log(level, tag, message, message_len);
402 });
403 }
404#endif
405
406#ifdef USE_ESP32
407 this->base_->add_handler(&this->events_);
408#endif
409 this->base_->add_handler(this);
410
411 // OTA is now handled by the web_server OTA platform
412
413 // doesn't need defer functionality - if the queue is full, the client JS knows it's alive because it's clearly
414 // getting a lot of events
415 this->set_interval(10000, [this]() {
416 char buf[32];
417 auto uptime = static_cast<uint32_t>(millis_64() / 1000);
418 buf_append_printf(buf, sizeof(buf), 0, "{\"uptime\":%u}", uptime);
419 this->events_.try_send_nodefer(buf, "ping", millis(), 30000);
420 });
421}
422void WebServer::loop() { this->events_.loop(); }
423
424#ifdef USE_LOGGER
425void WebServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
426 (void) level;
427 (void) tag;
428 (void) message_len;
429 this->events_.try_send_nodefer(message, "log", millis());
430}
431#endif
432
434 ESP_LOGCONFIG(TAG,
435 "Web Server:\n"
436 " Address: %s:%u",
438}
440
441#ifdef USE_WEBSERVER_LOCAL
442void WebServer::handle_index_request(AsyncWebServerRequest *request) {
443#ifndef USE_ESP8266
444 AsyncWebServerResponse *response = request->beginResponse(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
445#else
446 AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
447#endif
448#ifdef USE_WEBSERVER_GZIP
449 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
450#else
451 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("br"));
452#endif
453 request->send(response);
454}
455#elif USE_WEBSERVER_VERSION >= 2
456void WebServer::handle_index_request(AsyncWebServerRequest *request) {
457#ifndef USE_ESP8266
458 AsyncWebServerResponse *response =
459 request->beginResponse(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
460#else
461 AsyncWebServerResponse *response =
462 request->beginResponse_P(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
463#endif
464 // No gzip header here because the HTML file is so small
465 request->send(response);
466}
467#endif
468
469#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
470void WebServer::handle_pna_cors_request(AsyncWebServerRequest *request) {
471 AsyncWebServerResponse *response = request->beginResponse(200, ESPHOME_F(""));
472 response->addHeader(ESPHOME_F("Access-Control-Allow-Private-Network"), ESPHOME_F("true"));
473 response->addHeader(ESPHOME_F("Private-Network-Access-Name"), App.get_name().c_str());
474 char mac_s[18];
475 response->addHeader(ESPHOME_F("Private-Network-Access-ID"), get_mac_address_pretty_into_buffer(mac_s));
476 request->send(response);
477}
478#endif
479
480#ifdef USE_WEBSERVER_CSS_INCLUDE
481void WebServer::handle_css_request(AsyncWebServerRequest *request) {
482#ifndef USE_ESP8266
483 AsyncWebServerResponse *response =
484 request->beginResponse(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
485#else
486 AsyncWebServerResponse *response =
487 request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
488#endif
489 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
490 request->send(response);
491}
492#endif
493
494#ifdef USE_WEBSERVER_JS_INCLUDE
495void WebServer::handle_js_request(AsyncWebServerRequest *request) {
496#ifndef USE_ESP8266
497 AsyncWebServerResponse *response =
498 request->beginResponse(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
499#else
500 AsyncWebServerResponse *response =
501 request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
502#endif
503 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
504 request->send(response);
505}
506#endif
507
508// Helper functions to reduce code size by avoiding macro expansion
509// Build unique id as: {domain}/{device_name}/{entity_name} or {domain}/{entity_name}
510// Uses names (not object_id) to avoid UTF-8 collision issues
511static void set_json_id(JsonObject &root, EntityBase *obj, const char *prefix, JsonDetail start_config) {
512 const StringRef &name = obj->get_name();
513 size_t prefix_len = strlen(prefix);
514 size_t name_len = name.size();
515
516#ifdef USE_DEVICES
517 Device *device = obj->get_device();
518 const char *device_name = device ? device->get_name() : nullptr;
519 size_t device_len = device_name ? strlen(device_name) : 0;
520#endif
521
522 // Single stack buffer for both id formats - ArduinoJson copies the string before we overwrite
523 // Buffer sizes use constants from entity_base.h validated in core/config.py
524 // Note: Device name uses ESPHOME_FRIENDLY_NAME_MAX_LEN (sub-device max 120), not ESPHOME_DEVICE_NAME_MAX_LEN
525 // (hostname)
526 // Without USE_DEVICES: legacy id ({prefix}-{object_id}) is the largest format
527 // With USE_DEVICES: name_id ({prefix}/{device}/{name}) is the largest format
528 static constexpr size_t LEGACY_ID_SIZE = ESPHOME_DOMAIN_MAX_LEN + 1 + OBJECT_ID_MAX_LEN;
529#ifdef USE_DEVICES
530 static constexpr size_t ID_BUF_SIZE =
531 std::max(ESPHOME_DOMAIN_MAX_LEN + 1 + ESPHOME_FRIENDLY_NAME_MAX_LEN + 1 + ESPHOME_FRIENDLY_NAME_MAX_LEN + 1,
532 LEGACY_ID_SIZE);
533#else
534 static constexpr size_t ID_BUF_SIZE =
535 std::max(ESPHOME_DOMAIN_MAX_LEN + 1 + ESPHOME_FRIENDLY_NAME_MAX_LEN + 1, LEGACY_ID_SIZE);
536#endif
537 char id_buf[ID_BUF_SIZE];
538 memcpy(id_buf, prefix, prefix_len); // NOLINT(bugprone-not-null-terminated-result)
539
540 // name_id: new format {prefix}/{device?}/{name} - frontend should prefer this
541 // Remove in 2026.8.0 when id switches to new format permanently
542 char *p = id_buf + prefix_len;
543 *p++ = '/';
544#ifdef USE_DEVICES
545 if (device_name) {
546 memcpy(p, device_name, device_len);
547 p += device_len;
548 *p++ = '/';
549 }
550#endif
551 memcpy(p, name.c_str(), name_len);
552 p[name_len] = '\0';
553 root[ESPHOME_F("name_id")] = id_buf;
554
555 // id: old format {prefix}-{object_id} for backward compatibility
556 // Will switch to new format in 2026.8.0 - reuses prefix already in id_buf
557 id_buf[prefix_len] = '-';
558 obj->write_object_id_to(id_buf + prefix_len + 1, ID_BUF_SIZE - prefix_len - 1);
559 root[ESPHOME_F("id")] = id_buf;
560
561 if (start_config == DETAIL_ALL) {
562 root[ESPHOME_F("domain")] = prefix;
563 // Use .c_str() to avoid instantiating set<StringRef> template (saves ~24B)
564 root[ESPHOME_F("name")] = name.c_str();
565#ifdef USE_DEVICES
566 if (device_name) {
567 root[ESPHOME_F("device")] = device_name;
568 }
569#endif
570#ifdef USE_ENTITY_ICON
571 char icon_buf[MAX_ICON_LENGTH];
572 root[ESPHOME_F("icon")] = obj->get_icon_to(icon_buf);
573#endif
574 root[ESPHOME_F("entity_category")] = obj->get_entity_category();
575 bool is_disabled = obj->is_disabled_by_default();
576 if (is_disabled)
577 root[ESPHOME_F("is_disabled_by_default")] = is_disabled;
578 }
579}
580
581// Keep as separate function even though only used once: reduces code size by ~48 bytes
582// by allowing compiler to share code between template instantiations (bool, float, etc.)
583template<typename T>
584static void set_json_value(JsonObject &root, EntityBase *obj, const char *prefix, const T &value,
585 JsonDetail start_config) {
586 set_json_id(root, obj, prefix, start_config);
587 root[ESPHOME_F("value")] = value;
588}
589
590template<typename T>
591static void set_json_icon_state_value(JsonObject &root, EntityBase *obj, const char *prefix, const char *state,
592 const T &value, JsonDetail start_config) {
593 set_json_value(root, obj, prefix, value, start_config);
594 root[ESPHOME_F("state")] = state;
595}
596
597// Helper to get request detail parameter
598static JsonDetail get_request_detail(AsyncWebServerRequest *request) {
599 return request->arg(ESPHOME_F("detail")) == "all" ? DETAIL_ALL : DETAIL_STATE;
600}
601
602#ifdef USE_SENSOR
604 if (!this->include_internal_ && obj->is_internal())
605 return;
606 this->events_.deferrable_send_state(obj, "state", sensor_state_json_generator);
607}
608void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
609 for (sensor::Sensor *obj : App.get_sensors()) {
610 auto entity_match = match.match_entity(obj);
611 if (!entity_match.matched)
612 continue;
613 // Note: request->method() is always HTTP_GET here (canHandle ensures this)
614 if (entity_match.action_is_empty) {
615 auto detail = get_request_detail(request);
616 auto data = this->sensor_json_(obj, obj->state, detail);
617 request->send(200, "application/json", data.c_str());
618 return;
619 }
620 }
621 request->send(404);
622}
627 return web_server->sensor_json_((sensor::Sensor *) (source), ((sensor::Sensor *) (source))->state, DETAIL_ALL);
628}
629json::SerializationBuffer<> WebServer::sensor_json_(sensor::Sensor *obj, float value, JsonDetail start_config) {
630 json::JsonBuilder builder;
631 JsonObject root = builder.root();
632
633 const auto uom_ref = obj->get_unit_of_measurement_ref();
634 char buf[VALUE_ACCURACY_MAX_LEN];
635 const char *state = std::isnan(value)
636 ? "NA"
637 : (value_accuracy_with_uom_to_buf(buf, value, obj->get_accuracy_decimals(), uom_ref), buf);
638 set_json_icon_state_value(root, obj, "sensor", state, value, start_config);
639 if (start_config == DETAIL_ALL) {
640 this->add_sorting_info_(root, obj);
641 if (!uom_ref.empty())
642 root[ESPHOME_F("uom")] = uom_ref.c_str();
643 }
644
645 return builder.serialize();
646}
647#endif
648
649#ifdef USE_TEXT_SENSOR
651 if (!this->include_internal_ && obj->is_internal())
652 return;
653 this->events_.deferrable_send_state(obj, "state", text_sensor_state_json_generator);
654}
655void WebServer::handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
656 for (text_sensor::TextSensor *obj : App.get_text_sensors()) {
657 auto entity_match = match.match_entity(obj);
658 if (!entity_match.matched)
659 continue;
660 // Note: request->method() is always HTTP_GET here (canHandle ensures this)
661 if (entity_match.action_is_empty) {
662 auto detail = get_request_detail(request);
663 auto data = this->text_sensor_json_(obj, obj->state, detail);
664 request->send(200, "application/json", data.c_str());
665 return;
666 }
667 }
668 request->send(404);
669}
678json::SerializationBuffer<> WebServer::text_sensor_json_(text_sensor::TextSensor *obj, const std::string &value,
679 JsonDetail start_config) {
680 json::JsonBuilder builder;
681 JsonObject root = builder.root();
682
683 set_json_icon_state_value(root, obj, "text_sensor", value.c_str(), value.c_str(), start_config);
684 if (start_config == DETAIL_ALL) {
685 this->add_sorting_info_(root, obj);
686 }
687
688 return builder.serialize();
689}
690#endif
691
692#ifdef USE_SWITCH
694
695static void execute_switch_action(switch_::Switch *obj, SwitchAction action) {
696 switch (action) {
698 obj->toggle();
699 break;
701 obj->turn_on();
702 break;
704 obj->turn_off();
705 break;
706 default:
707 break;
708 }
709}
710
712 if (!this->include_internal_ && obj->is_internal())
713 return;
714 this->events_.deferrable_send_state(obj, "state", switch_state_json_generator);
715}
716void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match) {
717 for (switch_::Switch *obj : App.get_switches()) {
718 auto entity_match = match.match_entity(obj);
719 if (!entity_match.matched)
720 continue;
721
722 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
723 auto detail = get_request_detail(request);
724 auto data = this->switch_json_(obj, obj->state, detail);
725 request->send(200, "application/json", data.c_str());
726 return;
727 }
728
730
731 if (match.method_equals(ESPHOME_F("toggle"))) {
732 action = SWITCH_ACTION_TOGGLE;
733 } else if (match.method_equals(ESPHOME_F("turn_on"))) {
734 action = SWITCH_ACTION_TURN_ON;
735 } else if (match.method_equals(ESPHOME_F("turn_off"))) {
736 action = SWITCH_ACTION_TURN_OFF;
737 }
738
739 if (action != SWITCH_ACTION_NONE) {
740 this->defer([obj, action]() { execute_switch_action(obj, action); });
741 request->send(200);
742 } else {
743 request->send(404);
744 }
745 return;
746 }
747 request->send(404);
748}
755json::SerializationBuffer<> WebServer::switch_json_(switch_::Switch *obj, bool value, JsonDetail start_config) {
756 json::JsonBuilder builder;
757 JsonObject root = builder.root();
758
759 set_json_icon_state_value(root, obj, "switch", value ? "ON" : "OFF", value, start_config);
760 if (start_config == DETAIL_ALL) {
761 root[ESPHOME_F("assumed_state")] = obj->assumed_state();
762 this->add_sorting_info_(root, obj);
763 }
764
765 return builder.serialize();
766}
767#endif
768
769#ifdef USE_BUTTON
770void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match) {
771 for (button::Button *obj : App.get_buttons()) {
772 auto entity_match = match.match_entity(obj);
773 if (!entity_match.matched)
774 continue;
775 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
776 auto detail = get_request_detail(request);
777 auto data = this->button_json_(obj, detail);
778 request->send(200, "application/json", data.c_str());
779 } else if (match.method_equals(ESPHOME_F("press"))) {
780 DEFER_ACTION(obj, obj->press());
781 request->send(200);
782 return;
783 } else {
784 request->send(404);
785 }
786 return;
787 }
788 request->send(404);
789}
791 return web_server->button_json_((button::Button *) (source), DETAIL_ALL);
792}
793json::SerializationBuffer<> WebServer::button_json_(button::Button *obj, JsonDetail start_config) {
794 json::JsonBuilder builder;
795 JsonObject root = builder.root();
796
797 set_json_id(root, obj, "button", start_config);
798 if (start_config == DETAIL_ALL) {
799 this->add_sorting_info_(root, obj);
800 }
801
802 return builder.serialize();
803}
804#endif
805
806#ifdef USE_BINARY_SENSOR
808 if (!this->include_internal_ && obj->is_internal())
809 return;
810 this->events_.deferrable_send_state(obj, "state", binary_sensor_state_json_generator);
811}
812void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
814 auto entity_match = match.match_entity(obj);
815 if (!entity_match.matched)
816 continue;
817 // Note: request->method() is always HTTP_GET here (canHandle ensures this)
818 if (entity_match.action_is_empty) {
819 auto detail = get_request_detail(request);
820 auto data = this->binary_sensor_json_(obj, obj->state, detail);
821 request->send(200, "application/json", data.c_str());
822 return;
823 }
824 }
825 request->send(404);
826}
835json::SerializationBuffer<> WebServer::binary_sensor_json_(binary_sensor::BinarySensor *obj, bool value,
836 JsonDetail start_config) {
837 json::JsonBuilder builder;
838 JsonObject root = builder.root();
839
840 set_json_icon_state_value(root, obj, "binary_sensor", value ? "ON" : "OFF", value, start_config);
841 if (start_config == DETAIL_ALL) {
842 this->add_sorting_info_(root, obj);
843 }
844
845 return builder.serialize();
846}
847#endif
848
849#ifdef USE_FAN
851 if (!this->include_internal_ && obj->is_internal())
852 return;
853 this->events_.deferrable_send_state(obj, "state", fan_state_json_generator);
854}
855void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match) {
856 for (fan::Fan *obj : App.get_fans()) {
857 auto entity_match = match.match_entity(obj);
858 if (!entity_match.matched)
859 continue;
860
861 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
862 auto detail = get_request_detail(request);
863 auto data = this->fan_json_(obj, detail);
864 request->send(200, "application/json", data.c_str());
865 } else if (match.method_equals(ESPHOME_F("toggle"))) {
866 DEFER_ACTION(obj, obj->toggle().perform());
867 request->send(200);
868 } else {
869 bool is_on = match.method_equals(ESPHOME_F("turn_on"));
870 bool is_off = match.method_equals(ESPHOME_F("turn_off"));
871 if (!is_on && !is_off) {
872 request->send(404);
873 return;
874 }
875 auto call = is_on ? obj->turn_on() : obj->turn_off();
876
877 parse_num_param_(request, ESPHOME_F("speed_level"), call, &decltype(call)::set_speed);
878
879 if (request->hasArg(ESPHOME_F("oscillation"))) {
880 auto speed = request->arg(ESPHOME_F("oscillation"));
881 auto val = parse_on_off(speed.c_str());
882 switch (val) {
883 case PARSE_ON:
884 call.set_oscillating(true);
885 break;
886 case PARSE_OFF:
887 call.set_oscillating(false);
888 break;
889 case PARSE_TOGGLE:
890 call.set_oscillating(!obj->oscillating);
891 break;
892 case PARSE_NONE:
893 request->send(404);
894 return;
895 }
896 }
897 DEFER_ACTION(call, call.perform());
898 request->send(200);
899 }
900 return;
901 }
902 request->send(404);
903}
905 return web_server->fan_json_((fan::Fan *) (source), DETAIL_STATE);
906}
908 return web_server->fan_json_((fan::Fan *) (source), DETAIL_ALL);
909}
910json::SerializationBuffer<> WebServer::fan_json_(fan::Fan *obj, JsonDetail start_config) {
911 json::JsonBuilder builder;
912 JsonObject root = builder.root();
913
914 set_json_icon_state_value(root, obj, "fan", obj->state ? "ON" : "OFF", obj->state, start_config);
915 const auto traits = obj->get_traits();
916 if (traits.supports_speed()) {
917 root[ESPHOME_F("speed_level")] = obj->speed;
918 root[ESPHOME_F("speed_count")] = traits.supported_speed_count();
919 }
920 if (obj->get_traits().supports_oscillation())
921 root[ESPHOME_F("oscillation")] = obj->oscillating;
922 if (start_config == DETAIL_ALL) {
923 this->add_sorting_info_(root, obj);
924 }
925
926 return builder.serialize();
927}
928#endif
929
930#ifdef USE_LIGHT
932 if (!this->include_internal_ && obj->is_internal())
933 return;
934 this->events_.deferrable_send_state(obj, "state", light_state_json_generator);
935}
936void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match) {
937 for (light::LightState *obj : App.get_lights()) {
938 auto entity_match = match.match_entity(obj);
939 if (!entity_match.matched)
940 continue;
941
942 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
943 auto detail = get_request_detail(request);
944 auto data = this->light_json_(obj, detail);
945 request->send(200, "application/json", data.c_str());
946 } else if (match.method_equals(ESPHOME_F("toggle"))) {
947 DEFER_ACTION(obj, obj->toggle().perform());
948 request->send(200);
949 } else {
950 bool is_on = match.method_equals(ESPHOME_F("turn_on"));
951 bool is_off = match.method_equals(ESPHOME_F("turn_off"));
952 if (!is_on && !is_off) {
953 request->send(404);
954 return;
955 }
956 auto call = is_on ? obj->turn_on() : obj->turn_off();
957
958 if (is_on) {
959 // Parse color parameters
960 parse_light_param_(request, ESPHOME_F("brightness"), call, &decltype(call)::set_brightness, 255.0f);
961 parse_light_param_(request, ESPHOME_F("r"), call, &decltype(call)::set_red, 255.0f);
962 parse_light_param_(request, ESPHOME_F("g"), call, &decltype(call)::set_green, 255.0f);
963 parse_light_param_(request, ESPHOME_F("b"), call, &decltype(call)::set_blue, 255.0f);
964 parse_light_param_(request, ESPHOME_F("white_value"), call, &decltype(call)::set_white, 255.0f);
965 parse_light_param_(request, ESPHOME_F("color_temp"), call, &decltype(call)::set_color_temperature);
966
967 // Parse timing parameters
968 parse_light_param_uint_(request, ESPHOME_F("flash"), call, &decltype(call)::set_flash_length, 1000);
969 }
970 parse_light_param_uint_(request, ESPHOME_F("transition"), call, &decltype(call)::set_transition_length, 1000);
971
972 if (is_on) {
974 request, ESPHOME_F("effect"), call,
975 static_cast<light::LightCall &(light::LightCall::*) (const char *, size_t)>(&decltype(call)::set_effect));
976 }
977
978 DEFER_ACTION(call, call.perform());
979 request->send(200);
980 }
981 return;
982 }
983 request->send(404);
984}
989 return web_server->light_json_((light::LightState *) (source), DETAIL_ALL);
990}
991json::SerializationBuffer<> WebServer::light_json_(light::LightState *obj, JsonDetail start_config) {
992 json::JsonBuilder builder;
993 JsonObject root = builder.root();
994
995 set_json_value(root, obj, "light", obj->remote_values.is_on() ? "ON" : "OFF", start_config);
996
998 if (start_config == DETAIL_ALL) {
999 JsonArray opt = root[ESPHOME_F("effects")].to<JsonArray>();
1000 opt.add("None");
1001 for (auto const &option : obj->get_effects()) {
1002 opt.add(option->get_name());
1003 }
1004 this->add_sorting_info_(root, obj);
1005 }
1006
1007 return builder.serialize();
1008}
1009#endif
1010
1011#ifdef USE_COVER
1013 if (!this->include_internal_ && obj->is_internal())
1014 return;
1015 this->events_.deferrable_send_state(obj, "state", cover_state_json_generator);
1016}
1017void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1018 for (cover::Cover *obj : App.get_covers()) {
1019 auto entity_match = match.match_entity(obj);
1020 if (!entity_match.matched)
1021 continue;
1022
1023 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1024 auto detail = get_request_detail(request);
1025 auto data = this->cover_json_(obj, detail);
1026 request->send(200, "application/json", data.c_str());
1027 return;
1028 }
1029
1030 auto call = obj->make_call();
1031
1032 // Lookup table for cover methods
1033 static const struct {
1034 const char *name;
1035 cover::CoverCall &(cover::CoverCall::*action)();
1036 } METHODS[] = {
1041 };
1042
1043 bool found = false;
1044 for (const auto &method : METHODS) {
1045 if (match.method_equals(method.name)) {
1046 (call.*method.action)();
1047 found = true;
1048 break;
1049 }
1050 }
1051
1052 if (!found && !match.method_equals(ESPHOME_F("set"))) {
1053 request->send(404);
1054 return;
1055 }
1056
1057 auto traits = obj->get_traits();
1058 if ((request->hasArg(ESPHOME_F("position")) && !traits.get_supports_position()) ||
1059 (request->hasArg(ESPHOME_F("tilt")) && !traits.get_supports_tilt())) {
1060 request->send(409);
1061 return;
1062 }
1063
1064 parse_num_param_(request, ESPHOME_F("position"), call, &decltype(call)::set_position);
1065 parse_num_param_(request, ESPHOME_F("tilt"), call, &decltype(call)::set_tilt);
1066
1067 DEFER_ACTION(call, call.perform());
1068 request->send(200);
1069 return;
1070 }
1071 request->send(404);
1072}
1074 return web_server->cover_json_((cover::Cover *) (source), DETAIL_STATE);
1075}
1077 return web_server->cover_json_((cover::Cover *) (source), DETAIL_ALL);
1078}
1079json::SerializationBuffer<> WebServer::cover_json_(cover::Cover *obj, JsonDetail start_config) {
1080 json::JsonBuilder builder;
1081 JsonObject root = builder.root();
1082
1083 set_json_icon_state_value(root, obj, "cover", obj->is_fully_closed() ? "CLOSED" : "OPEN", obj->position,
1084 start_config);
1085 char buf[PSTR_LOCAL_SIZE];
1086 root[ESPHOME_F("current_operation")] = PSTR_LOCAL(cover::cover_operation_to_str(obj->current_operation));
1087
1088 if (obj->get_traits().get_supports_position())
1089 root[ESPHOME_F("position")] = obj->position;
1090 if (obj->get_traits().get_supports_tilt())
1091 root[ESPHOME_F("tilt")] = obj->tilt;
1092 if (start_config == DETAIL_ALL) {
1093 this->add_sorting_info_(root, obj);
1094 }
1095
1096 return builder.serialize();
1097}
1098#endif
1099
1100#ifdef USE_NUMBER
1102 if (!this->include_internal_ && obj->is_internal())
1103 return;
1104 this->events_.deferrable_send_state(obj, "state", number_state_json_generator);
1105}
1106void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1107 for (auto *obj : App.get_numbers()) {
1108 auto entity_match = match.match_entity(obj);
1109 if (!entity_match.matched)
1110 continue;
1111
1112 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1113 auto detail = get_request_detail(request);
1114 auto data = this->number_json_(obj, obj->state, detail);
1115 request->send(200, "application/json", data.c_str());
1116 return;
1117 }
1118 if (!match.method_equals(ESPHOME_F("set"))) {
1119 request->send(404);
1120 return;
1121 }
1122
1123 auto call = obj->make_call();
1124 parse_num_param_(request, ESPHOME_F("value"), call, &decltype(call)::set_value);
1125
1126 DEFER_ACTION(call, call.perform());
1127 request->send(200);
1128 return;
1129 }
1130 request->send(404);
1131}
1132
1137 return web_server->number_json_((number::Number *) (source), ((number::Number *) (source))->state, DETAIL_ALL);
1138}
1139json::SerializationBuffer<> WebServer::number_json_(number::Number *obj, float value, JsonDetail start_config) {
1140 json::JsonBuilder builder;
1141 JsonObject root = builder.root();
1142
1143 const auto uom_ref = obj->get_unit_of_measurement_ref();
1144 const int8_t accuracy = step_to_accuracy_decimals(obj->traits.get_step());
1145
1146 // Need two buffers: one for value, one for state with UOM
1147 char val_buf[VALUE_ACCURACY_MAX_LEN];
1148 char state_buf[VALUE_ACCURACY_MAX_LEN];
1149 const char *val_str = std::isnan(value) ? "\"NaN\"" : (value_accuracy_to_buf(val_buf, value, accuracy), val_buf);
1150 const char *state_str =
1151 std::isnan(value) ? "NA" : (value_accuracy_with_uom_to_buf(state_buf, value, accuracy, uom_ref), state_buf);
1152 set_json_icon_state_value(root, obj, "number", state_str, val_str, start_config);
1153 if (start_config == DETAIL_ALL) {
1154 // ArduinoJson copies the string immediately, so we can reuse val_buf
1155 root[ESPHOME_F("min_value")] = (value_accuracy_to_buf(val_buf, obj->traits.get_min_value(), accuracy), val_buf);
1156 root[ESPHOME_F("max_value")] = (value_accuracy_to_buf(val_buf, obj->traits.get_max_value(), accuracy), val_buf);
1157 root[ESPHOME_F("step")] = (value_accuracy_to_buf(val_buf, obj->traits.get_step(), accuracy), val_buf);
1158 root[ESPHOME_F("mode")] = (int) obj->traits.get_mode();
1159 if (!uom_ref.empty())
1160 root[ESPHOME_F("uom")] = uom_ref.c_str();
1161 this->add_sorting_info_(root, obj);
1162 }
1163
1164 return builder.serialize();
1165}
1166#endif
1167
1168#ifdef USE_DATETIME_DATE
1170 if (!this->include_internal_ && obj->is_internal())
1171 return;
1172 this->events_.deferrable_send_state(obj, "state", date_state_json_generator);
1173}
1174void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1175 for (auto *obj : App.get_dates()) {
1176 auto entity_match = match.match_entity(obj);
1177 if (!entity_match.matched)
1178 continue;
1179 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1180 auto detail = get_request_detail(request);
1181 auto data = this->date_json_(obj, detail);
1182 request->send(200, "application/json", data.c_str());
1183 return;
1184 }
1185 if (!match.method_equals(ESPHOME_F("set"))) {
1186 request->send(404);
1187 return;
1188 }
1189
1190 auto call = obj->make_call();
1191
1192 const auto &value = request->arg(ESPHOME_F("value"));
1193 // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility
1194 if (value.length() == 0) { // NOLINT(readability-container-size-empty)
1195 request->send(409);
1196 return;
1197 }
1198 call.set_date(value.c_str(), value.length());
1199
1200 DEFER_ACTION(call, call.perform());
1201 request->send(200);
1202 return;
1203 }
1204 request->send(404);
1205}
1206
1211 return web_server->date_json_((datetime::DateEntity *) (source), DETAIL_ALL);
1212}
1213json::SerializationBuffer<> WebServer::date_json_(datetime::DateEntity *obj, JsonDetail start_config) {
1214 json::JsonBuilder builder;
1215 JsonObject root = builder.root();
1216
1217 // Format: YYYY-MM-DD (max 10 chars + null)
1218 char value[12];
1219 buf_append_printf(value, sizeof(value), 0, "%d-%02d-%02d", obj->year, obj->month, obj->day);
1220 set_json_icon_state_value(root, obj, "date", value, value, start_config);
1221 if (start_config == DETAIL_ALL) {
1222 this->add_sorting_info_(root, obj);
1223 }
1224
1225 return builder.serialize();
1226}
1227#endif // USE_DATETIME_DATE
1228
1229#ifdef USE_DATETIME_TIME
1231 if (!this->include_internal_ && obj->is_internal())
1232 return;
1233 this->events_.deferrable_send_state(obj, "state", time_state_json_generator);
1234}
1235void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1236 for (auto *obj : App.get_times()) {
1237 auto entity_match = match.match_entity(obj);
1238 if (!entity_match.matched)
1239 continue;
1240 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1241 auto detail = get_request_detail(request);
1242 auto data = this->time_json_(obj, detail);
1243 request->send(200, "application/json", data.c_str());
1244 return;
1245 }
1246 if (!match.method_equals(ESPHOME_F("set"))) {
1247 request->send(404);
1248 return;
1249 }
1250
1251 auto call = obj->make_call();
1252
1253 const auto &value = request->arg(ESPHOME_F("value"));
1254 // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility
1255 if (value.length() == 0) { // NOLINT(readability-container-size-empty)
1256 request->send(409);
1257 return;
1258 }
1259 call.set_time(value.c_str(), value.length());
1260
1261 DEFER_ACTION(call, call.perform());
1262 request->send(200);
1263 return;
1264 }
1265 request->send(404);
1266}
1271 return web_server->time_json_((datetime::TimeEntity *) (source), DETAIL_ALL);
1272}
1273json::SerializationBuffer<> WebServer::time_json_(datetime::TimeEntity *obj, JsonDetail start_config) {
1274 json::JsonBuilder builder;
1275 JsonObject root = builder.root();
1276
1277 // Format: HH:MM:SS (8 chars + null)
1278 char value[12];
1279 buf_append_printf(value, sizeof(value), 0, "%02d:%02d:%02d", obj->hour, obj->minute, obj->second);
1280 set_json_icon_state_value(root, obj, "time", value, value, start_config);
1281 if (start_config == DETAIL_ALL) {
1282 this->add_sorting_info_(root, obj);
1283 }
1284
1285 return builder.serialize();
1286}
1287#endif // USE_DATETIME_TIME
1288
1289#ifdef USE_DATETIME_DATETIME
1291 if (!this->include_internal_ && obj->is_internal())
1292 return;
1293 this->events_.deferrable_send_state(obj, "state", datetime_state_json_generator);
1294}
1295void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1296 for (auto *obj : App.get_datetimes()) {
1297 auto entity_match = match.match_entity(obj);
1298 if (!entity_match.matched)
1299 continue;
1300 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1301 auto detail = get_request_detail(request);
1302 auto data = this->datetime_json_(obj, detail);
1303 request->send(200, "application/json", data.c_str());
1304 return;
1305 }
1306 if (!match.method_equals(ESPHOME_F("set"))) {
1307 request->send(404);
1308 return;
1309 }
1310
1311 auto call = obj->make_call();
1312
1313 const auto &value = request->arg(ESPHOME_F("value"));
1314 // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility
1315 if (value.length() == 0) { // NOLINT(readability-container-size-empty)
1316 request->send(409);
1317 return;
1318 }
1319 call.set_datetime(value.c_str(), value.length());
1320
1321 DEFER_ACTION(call, call.perform());
1322 request->send(200);
1323 return;
1324 }
1325 request->send(404);
1326}
1333json::SerializationBuffer<> WebServer::datetime_json_(datetime::DateTimeEntity *obj, JsonDetail start_config) {
1334 json::JsonBuilder builder;
1335 JsonObject root = builder.root();
1336
1337 // Format: YYYY-MM-DD HH:MM:SS (max 19 chars + null)
1338 char value[24];
1339 buf_append_printf(value, sizeof(value), 0, "%d-%02d-%02d %02d:%02d:%02d", obj->year, obj->month, obj->day, obj->hour,
1340 obj->minute, obj->second);
1341 set_json_icon_state_value(root, obj, "datetime", value, value, start_config);
1342 if (start_config == DETAIL_ALL) {
1343 this->add_sorting_info_(root, obj);
1344 }
1345
1346 return builder.serialize();
1347}
1348#endif // USE_DATETIME_DATETIME
1349
1350#ifdef USE_TEXT
1352 if (!this->include_internal_ && obj->is_internal())
1353 return;
1354 this->events_.deferrable_send_state(obj, "state", text_state_json_generator);
1355}
1356void WebServer::handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1357 for (auto *obj : App.get_texts()) {
1358 auto entity_match = match.match_entity(obj);
1359 if (!entity_match.matched)
1360 continue;
1361
1362 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1363 auto detail = get_request_detail(request);
1364 auto data = this->text_json_(obj, obj->state, detail);
1365 request->send(200, "application/json", data.c_str());
1366 return;
1367 }
1368 if (!match.method_equals(ESPHOME_F("set"))) {
1369 request->send(404);
1370 return;
1371 }
1372
1373 auto call = obj->make_call();
1375 request, ESPHOME_F("value"), call,
1376 static_cast<text::TextCall &(text::TextCall::*) (const char *, size_t)>(&decltype(call)::set_value));
1377
1378 DEFER_ACTION(call, call.perform());
1379 request->send(200);
1380 return;
1381 }
1382 request->send(404);
1383}
1384
1386 return web_server->text_json_((text::Text *) (source), ((text::Text *) (source))->state, DETAIL_STATE);
1387}
1389 return web_server->text_json_((text::Text *) (source), ((text::Text *) (source))->state, DETAIL_ALL);
1390}
1391json::SerializationBuffer<> WebServer::text_json_(text::Text *obj, const std::string &value, JsonDetail start_config) {
1392 json::JsonBuilder builder;
1393 JsonObject root = builder.root();
1394
1395 const char *state = obj->traits.get_mode() == text::TextMode::TEXT_MODE_PASSWORD ? "********" : value.c_str();
1396 set_json_icon_state_value(root, obj, "text", state, value.c_str(), start_config);
1397 root[ESPHOME_F("min_length")] = obj->traits.get_min_length();
1398 root[ESPHOME_F("max_length")] = obj->traits.get_max_length();
1399 root[ESPHOME_F("pattern")] = obj->traits.get_pattern_c_str();
1400 if (start_config == DETAIL_ALL) {
1401 root[ESPHOME_F("mode")] = (int) obj->traits.get_mode();
1402 this->add_sorting_info_(root, obj);
1403 }
1404
1405 return builder.serialize();
1406}
1407#endif
1408
1409#ifdef USE_SELECT
1411 if (!this->include_internal_ && obj->is_internal())
1412 return;
1413 this->events_.deferrable_send_state(obj, "state", select_state_json_generator);
1414}
1415void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1416 for (auto *obj : App.get_selects()) {
1417 auto entity_match = match.match_entity(obj);
1418 if (!entity_match.matched)
1419 continue;
1420
1421 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1422 auto detail = get_request_detail(request);
1423 auto data = this->select_json_(obj, obj->has_state() ? obj->current_option() : StringRef(), detail);
1424 request->send(200, "application/json", data.c_str());
1425 return;
1426 }
1427
1428 if (!match.method_equals(ESPHOME_F("set"))) {
1429 request->send(404);
1430 return;
1431 }
1432
1433 auto call = obj->make_call();
1435 request, ESPHOME_F("option"), call,
1436 static_cast<select::SelectCall &(select::SelectCall::*) (const char *, size_t)>(&decltype(call)::set_option));
1437
1438 DEFER_ACTION(call, call.perform());
1439 request->send(200);
1440 return;
1441 }
1442 request->send(404);
1443}
1445 auto *obj = (select::Select *) (source);
1446 return web_server->select_json_(obj, obj->has_state() ? obj->current_option() : StringRef(), DETAIL_STATE);
1447}
1449 auto *obj = (select::Select *) (source);
1450 return web_server->select_json_(obj, obj->has_state() ? obj->current_option() : StringRef(), DETAIL_ALL);
1451}
1452json::SerializationBuffer<> WebServer::select_json_(select::Select *obj, StringRef value, JsonDetail start_config) {
1453 json::JsonBuilder builder;
1454 JsonObject root = builder.root();
1455
1456 // value points to null-terminated string literals from codegen (via current_option())
1457 set_json_icon_state_value(root, obj, "select", value.c_str(), value.c_str(), start_config);
1458 if (start_config == DETAIL_ALL) {
1459 JsonArray opt = root[ESPHOME_F("option")].to<JsonArray>();
1460 for (auto &option : obj->traits.get_options()) {
1461 opt.add(option);
1462 }
1463 this->add_sorting_info_(root, obj);
1464 }
1465
1466 return builder.serialize();
1467}
1468#endif
1469
1470#ifdef USE_CLIMATE
1472 if (!this->include_internal_ && obj->is_internal())
1473 return;
1474 this->events_.deferrable_send_state(obj, "state", climate_state_json_generator);
1475}
1476void WebServer::handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1477 for (auto *obj : App.get_climates()) {
1478 auto entity_match = match.match_entity(obj);
1479 if (!entity_match.matched)
1480 continue;
1481
1482 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1483 auto detail = get_request_detail(request);
1484 auto data = this->climate_json_(obj, detail);
1485 request->send(200, "application/json", data.c_str());
1486 return;
1487 }
1488
1489 if (!match.method_equals(ESPHOME_F("set"))) {
1490 request->send(404);
1491 return;
1492 }
1493
1494 auto call = obj->make_call();
1495
1496 // Parse string mode parameters
1498 request, ESPHOME_F("mode"), call,
1499 static_cast<climate::ClimateCall &(climate::ClimateCall::*) (const char *, size_t)>(&decltype(call)::set_mode));
1500 parse_cstr_param_(request, ESPHOME_F("fan_mode"), call,
1501 static_cast<climate::ClimateCall &(climate::ClimateCall::*) (const char *, size_t)>(
1502 &decltype(call)::set_fan_mode));
1503 parse_cstr_param_(request, ESPHOME_F("swing_mode"), call,
1504 static_cast<climate::ClimateCall &(climate::ClimateCall::*) (const char *, size_t)>(
1505 &decltype(call)::set_swing_mode));
1506 parse_cstr_param_(request, ESPHOME_F("preset"), call,
1507 static_cast<climate::ClimateCall &(climate::ClimateCall::*) (const char *, size_t)>(
1508 &decltype(call)::set_preset));
1509
1510 // Parse temperature parameters
1511 // static_cast needed to disambiguate overloaded setters (float vs optional<float>)
1512 using ClimateCall = decltype(call);
1513 parse_num_param_(request, ESPHOME_F("target_temperature_high"), call,
1514 static_cast<ClimateCall &(ClimateCall::*) (float)>(&ClimateCall::set_target_temperature_high));
1515 parse_num_param_(request, ESPHOME_F("target_temperature_low"), call,
1516 static_cast<ClimateCall &(ClimateCall::*) (float)>(&ClimateCall::set_target_temperature_low));
1517 parse_num_param_(request, ESPHOME_F("target_temperature"), call,
1518 static_cast<ClimateCall &(ClimateCall::*) (float)>(&ClimateCall::set_target_temperature));
1519
1520 DEFER_ACTION(call, call.perform());
1521 request->send(200);
1522 return;
1523 }
1524 request->send(404);
1525}
1527 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
1528 return web_server->climate_json_((climate::Climate *) (source), DETAIL_STATE);
1529}
1531 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
1532 return web_server->climate_json_((climate::Climate *) (source), DETAIL_ALL);
1533}
1534json::SerializationBuffer<> WebServer::climate_json_(climate::Climate *obj, JsonDetail start_config) {
1535 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
1536 json::JsonBuilder builder;
1537 JsonObject root = builder.root();
1538 set_json_id(root, obj, "climate", start_config);
1539 const auto traits = obj->get_traits();
1540 int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
1541 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
1542 char buf[PSTR_LOCAL_SIZE];
1543 char temp_buf[VALUE_ACCURACY_MAX_LEN];
1544
1545 if (start_config == DETAIL_ALL) {
1546 JsonArray opt = root[ESPHOME_F("modes")].to<JsonArray>();
1547 for (climate::ClimateMode m : traits.get_supported_modes())
1548 opt.add(PSTR_LOCAL(climate::climate_mode_to_string(m)));
1549 if (traits.get_supports_fan_modes()) {
1550 JsonArray opt = root[ESPHOME_F("fan_modes")].to<JsonArray>();
1551 for (climate::ClimateFanMode m : traits.get_supported_fan_modes())
1552 opt.add(PSTR_LOCAL(climate::climate_fan_mode_to_string(m)));
1553 }
1554
1555 if (!traits.get_supported_custom_fan_modes().empty()) {
1556 JsonArray opt = root[ESPHOME_F("custom_fan_modes")].to<JsonArray>();
1557 for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
1558 opt.add(custom_fan_mode);
1559 }
1560 if (traits.get_supports_swing_modes()) {
1561 JsonArray opt = root[ESPHOME_F("swing_modes")].to<JsonArray>();
1562 for (auto swing_mode : traits.get_supported_swing_modes())
1564 }
1565 if (traits.get_supports_presets()) {
1566 JsonArray opt = root[ESPHOME_F("presets")].to<JsonArray>();
1567 for (climate::ClimatePreset m : traits.get_supported_presets())
1568 opt.add(PSTR_LOCAL(climate::climate_preset_to_string(m)));
1569 }
1570 if (!traits.get_supported_custom_presets().empty()) {
1571 JsonArray opt = root[ESPHOME_F("custom_presets")].to<JsonArray>();
1572 for (auto const &custom_preset : traits.get_supported_custom_presets())
1573 opt.add(custom_preset);
1574 }
1575 root[ESPHOME_F("max_temp")] =
1576 (value_accuracy_to_buf(temp_buf, traits.get_visual_max_temperature(), target_accuracy), temp_buf);
1577 root[ESPHOME_F("min_temp")] =
1578 (value_accuracy_to_buf(temp_buf, traits.get_visual_min_temperature(), target_accuracy), temp_buf);
1579 root[ESPHOME_F("step")] = traits.get_visual_target_temperature_step();
1580 this->add_sorting_info_(root, obj);
1581 }
1582
1583 bool has_state = false;
1584 root[ESPHOME_F("mode")] = PSTR_LOCAL(climate_mode_to_string(obj->mode));
1585 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_ACTION)) {
1586 root[ESPHOME_F("action")] = PSTR_LOCAL(climate_action_to_string(obj->action));
1587 root[ESPHOME_F("state")] = root[ESPHOME_F("action")];
1588 has_state = true;
1589 }
1590 if (traits.get_supports_fan_modes() && obj->fan_mode.has_value()) {
1591 root[ESPHOME_F("fan_mode")] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value()));
1592 }
1593 if (!traits.get_supported_custom_fan_modes().empty() && obj->has_custom_fan_mode()) {
1594 root[ESPHOME_F("custom_fan_mode")] = obj->get_custom_fan_mode();
1595 }
1596 if (traits.get_supports_presets() && obj->preset.has_value()) {
1597 root[ESPHOME_F("preset")] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
1598 }
1599 if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) {
1600 root[ESPHOME_F("custom_preset")] = obj->get_custom_preset();
1601 }
1602 if (traits.get_supports_swing_modes()) {
1603 root[ESPHOME_F("swing_mode")] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));
1604 }
1605 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE)) {
1606 root[ESPHOME_F("current_temperature")] =
1607 std::isnan(obj->current_temperature)
1608 ? "NA"
1609 : (value_accuracy_to_buf(temp_buf, obj->current_temperature, current_accuracy), temp_buf);
1610 }
1611 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY)) {
1612 root[ESPHOME_F("current_humidity")] = std::isnan(obj->current_humidity)
1613 ? "NA"
1614 : (value_accuracy_to_buf(temp_buf, obj->current_humidity, 0), temp_buf);
1615 }
1616 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE |
1618 root[ESPHOME_F("target_temperature_low")] =
1619 (value_accuracy_to_buf(temp_buf, obj->target_temperature_low, target_accuracy), temp_buf);
1620 root[ESPHOME_F("target_temperature_high")] =
1621 (value_accuracy_to_buf(temp_buf, obj->target_temperature_high, target_accuracy), temp_buf);
1622 if (!has_state) {
1623 root[ESPHOME_F("state")] =
1625 target_accuracy),
1626 temp_buf);
1627 }
1628 } else {
1629 root[ESPHOME_F("target_temperature")] =
1630 (value_accuracy_to_buf(temp_buf, obj->target_temperature, target_accuracy), temp_buf);
1631 if (!has_state)
1632 root[ESPHOME_F("state")] = root[ESPHOME_F("target_temperature")];
1633 }
1634
1635 return builder.serialize();
1636 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
1637}
1638#endif
1639
1640#ifdef USE_LOCK
1642
1643static void execute_lock_action(lock::Lock *obj, LockAction action) {
1644 switch (action) {
1645 case LOCK_ACTION_LOCK:
1646 obj->lock();
1647 break;
1648 case LOCK_ACTION_UNLOCK:
1649 obj->unlock();
1650 break;
1651 case LOCK_ACTION_OPEN:
1652 obj->open();
1653 break;
1654 default:
1655 break;
1656 }
1657}
1658
1660 if (!this->include_internal_ && obj->is_internal())
1661 return;
1662 this->events_.deferrable_send_state(obj, "state", lock_state_json_generator);
1663}
1664void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1665 for (lock::Lock *obj : App.get_locks()) {
1666 auto entity_match = match.match_entity(obj);
1667 if (!entity_match.matched)
1668 continue;
1669
1670 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1671 auto detail = get_request_detail(request);
1672 auto data = this->lock_json_(obj, obj->state, detail);
1673 request->send(200, "application/json", data.c_str());
1674 return;
1675 }
1676
1678
1679 if (match.method_equals(ESPHOME_F("lock"))) {
1680 action = LOCK_ACTION_LOCK;
1681 } else if (match.method_equals(ESPHOME_F("unlock"))) {
1682 action = LOCK_ACTION_UNLOCK;
1683 } else if (match.method_equals(ESPHOME_F("open"))) {
1684 action = LOCK_ACTION_OPEN;
1685 }
1686
1687 if (action != LOCK_ACTION_NONE) {
1688 this->defer([obj, action]() { execute_lock_action(obj, action); });
1689 request->send(200);
1690 } else {
1691 request->send(404);
1692 }
1693 return;
1694 }
1695 request->send(404);
1696}
1698 return web_server->lock_json_((lock::Lock *) (source), ((lock::Lock *) (source))->state, DETAIL_STATE);
1699}
1701 return web_server->lock_json_((lock::Lock *) (source), ((lock::Lock *) (source))->state, DETAIL_ALL);
1702}
1703json::SerializationBuffer<> WebServer::lock_json_(lock::Lock *obj, lock::LockState value, JsonDetail start_config) {
1704 json::JsonBuilder builder;
1705 JsonObject root = builder.root();
1706
1707 char buf[PSTR_LOCAL_SIZE];
1708 set_json_icon_state_value(root, obj, "lock", PSTR_LOCAL(lock::lock_state_to_string(value)), value, start_config);
1709 if (start_config == DETAIL_ALL) {
1710 this->add_sorting_info_(root, obj);
1711 }
1712
1713 return builder.serialize();
1714}
1715#endif
1716
1717#ifdef USE_VALVE
1719 if (!this->include_internal_ && obj->is_internal())
1720 return;
1721 this->events_.deferrable_send_state(obj, "state", valve_state_json_generator);
1722}
1723void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1724 for (valve::Valve *obj : App.get_valves()) {
1725 auto entity_match = match.match_entity(obj);
1726 if (!entity_match.matched)
1727 continue;
1728
1729 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1730 auto detail = get_request_detail(request);
1731 auto data = this->valve_json_(obj, detail);
1732 request->send(200, "application/json", data.c_str());
1733 return;
1734 }
1735
1736 auto call = obj->make_call();
1737
1738 // Lookup table for valve methods
1739 static const struct {
1740 const char *name;
1741 valve::ValveCall &(valve::ValveCall::*action)();
1742 } METHODS[] = {
1747 };
1748
1749 bool found = false;
1750 for (const auto &method : METHODS) {
1751 if (match.method_equals(method.name)) {
1752 (call.*method.action)();
1753 found = true;
1754 break;
1755 }
1756 }
1757
1758 if (!found && !match.method_equals(ESPHOME_F("set"))) {
1759 request->send(404);
1760 return;
1761 }
1762
1763 auto traits = obj->get_traits();
1764 if (request->hasArg(ESPHOME_F("position")) && !traits.get_supports_position()) {
1765 request->send(409);
1766 return;
1767 }
1768
1769 parse_num_param_(request, ESPHOME_F("position"), call, &decltype(call)::set_position);
1770
1771 DEFER_ACTION(call, call.perform());
1772 request->send(200);
1773 return;
1774 }
1775 request->send(404);
1776}
1778 return web_server->valve_json_((valve::Valve *) (source), DETAIL_STATE);
1779}
1781 return web_server->valve_json_((valve::Valve *) (source), DETAIL_ALL);
1782}
1783json::SerializationBuffer<> WebServer::valve_json_(valve::Valve *obj, JsonDetail start_config) {
1784 json::JsonBuilder builder;
1785 JsonObject root = builder.root();
1786
1787 set_json_icon_state_value(root, obj, "valve", obj->is_fully_closed() ? "CLOSED" : "OPEN", obj->position,
1788 start_config);
1789 char buf[PSTR_LOCAL_SIZE];
1790 root[ESPHOME_F("current_operation")] = PSTR_LOCAL(valve::valve_operation_to_str(obj->current_operation));
1791
1792 if (obj->get_traits().get_supports_position())
1793 root[ESPHOME_F("position")] = obj->position;
1794 if (start_config == DETAIL_ALL) {
1795 this->add_sorting_info_(root, obj);
1796 }
1797
1798 return builder.serialize();
1799}
1800#endif
1801
1802#ifdef USE_ALARM_CONTROL_PANEL
1804 if (!this->include_internal_ && obj->is_internal())
1805 return;
1806 this->events_.deferrable_send_state(obj, "state", alarm_control_panel_state_json_generator);
1807}
1808void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1809 for (alarm_control_panel::AlarmControlPanel *obj : App.get_alarm_control_panels()) {
1810 auto entity_match = match.match_entity(obj);
1811 if (!entity_match.matched)
1812 continue;
1813
1814 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1815 auto detail = get_request_detail(request);
1816 auto data = this->alarm_control_panel_json_(obj, obj->get_state(), detail);
1817 request->send(200, "application/json", data.c_str());
1818 return;
1819 }
1820
1821 auto call = obj->make_call();
1823 request, ESPHOME_F("code"), call,
1825 alarm_control_panel::AlarmControlPanelCall::*) (const char *, size_t)>(&decltype(call)::set_code));
1826
1827 // Lookup table for alarm control panel methods
1828 static const struct {
1829 const char *name;
1831 } METHODS[] = {
1837 };
1838
1839 bool found = false;
1840 for (const auto &method : METHODS) {
1841 if (match.method_equals(method.name)) {
1842 (call.*method.action)();
1843 found = true;
1844 break;
1845 }
1846 }
1847
1848 if (!found) {
1849 request->send(404);
1850 return;
1851 }
1852
1853 DEFER_ACTION(call, call.perform());
1854 request->send(200);
1855 return;
1856 }
1857 request->send(404);
1858}
1869json::SerializationBuffer<> WebServer::alarm_control_panel_json_(alarm_control_panel::AlarmControlPanel *obj,
1871 JsonDetail start_config) {
1872 json::JsonBuilder builder;
1873 JsonObject root = builder.root();
1874
1875 char buf[PSTR_LOCAL_SIZE];
1876 set_json_icon_state_value(root, obj, "alarm-control-panel", PSTR_LOCAL(alarm_control_panel_state_to_string(value)),
1877 value, start_config);
1878 if (start_config == DETAIL_ALL) {
1879 this->add_sorting_info_(root, obj);
1880 }
1881
1882 return builder.serialize();
1883}
1884#endif
1885
1886#ifdef USE_WATER_HEATER
1888 if (!this->include_internal_ && obj->is_internal())
1889 return;
1890 this->events_.deferrable_send_state(obj, "state", water_heater_state_json_generator);
1891}
1892void WebServer::handle_water_heater_request(AsyncWebServerRequest *request, const UrlMatch &match) {
1893 for (water_heater::WaterHeater *obj : App.get_water_heaters()) {
1894 auto entity_match = match.match_entity(obj);
1895 if (!entity_match.matched)
1896 continue;
1897
1898 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
1899 auto detail = get_request_detail(request);
1900 auto data = this->water_heater_json_(obj, detail);
1901 request->send(200, "application/json", data.c_str());
1902 return;
1903 }
1904 if (!match.method_equals(ESPHOME_F("set"))) {
1905 request->send(404);
1906 return;
1907 }
1908 auto call = obj->make_call();
1909 // Use base class reference for template deduction (make_call returns WaterHeaterCallInternal)
1911
1912 // Parse mode parameter
1914 request, ESPHOME_F("mode"), base_call,
1915 static_cast<water_heater::WaterHeaterCall &(water_heater::WaterHeaterCall::*) (const char *, size_t)>(
1917
1918 // Parse temperature parameters
1919 parse_num_param_(request, ESPHOME_F("target_temperature"), base_call,
1921 parse_num_param_(request, ESPHOME_F("target_temperature_low"), base_call,
1923 parse_num_param_(request, ESPHOME_F("target_temperature_high"), base_call,
1925
1926 // Parse away mode parameter
1927 parse_bool_param_(request, ESPHOME_F("away"), base_call, &water_heater::WaterHeaterCall::set_away);
1928
1929 // Parse on/off parameter
1930 parse_bool_param_(request, ESPHOME_F("is_on"), base_call, &water_heater::WaterHeaterCall::set_on);
1931
1932 DEFER_ACTION(call, call.perform());
1933 request->send(200);
1934 return;
1935 }
1936 request->send(404);
1937}
1938
1940 return web_server->water_heater_json_(static_cast<water_heater::WaterHeater *>(source), DETAIL_STATE);
1941}
1943 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
1944 return web_server->water_heater_json_(static_cast<water_heater::WaterHeater *>(source), DETAIL_ALL);
1945}
1946json::SerializationBuffer<> WebServer::water_heater_json_(water_heater::WaterHeater *obj, JsonDetail start_config) {
1947 json::JsonBuilder builder;
1948 JsonObject root = builder.root();
1949 char buf[PSTR_LOCAL_SIZE];
1950
1951 const auto mode = obj->get_mode();
1952 const char *mode_s = PSTR_LOCAL(water_heater::water_heater_mode_to_string(mode));
1953
1954 set_json_icon_state_value(root, obj, "water_heater", mode_s, mode, start_config);
1955
1956 auto traits = obj->get_traits();
1957
1958 if (start_config == DETAIL_ALL) {
1959 JsonArray modes = root[ESPHOME_F("modes")].to<JsonArray>();
1960 for (auto m : traits.get_supported_modes())
1961 modes.add(PSTR_LOCAL(water_heater::water_heater_mode_to_string(m)));
1962 root[ESPHOME_F("min_temp")] = traits.get_min_temperature();
1963 root[ESPHOME_F("max_temp")] = traits.get_max_temperature();
1964 root[ESPHOME_F("step")] = traits.get_target_temperature_step();
1965 this->add_sorting_info_(root, obj);
1966 }
1967
1968 if (traits.get_supports_current_temperature()) {
1969 float current = obj->get_current_temperature();
1970 if (!std::isnan(current))
1971 root[ESPHOME_F("current_temperature")] = current;
1972 }
1973
1974 if (traits.get_supports_two_point_target_temperature()) {
1975 float low = obj->get_target_temperature_low();
1976 float high = obj->get_target_temperature_high();
1977 if (!std::isnan(low))
1978 root[ESPHOME_F("target_temperature_low")] = low;
1979 if (!std::isnan(high))
1980 root[ESPHOME_F("target_temperature_high")] = high;
1981 } else {
1982 float target = obj->get_target_temperature();
1983 if (!std::isnan(target))
1984 root[ESPHOME_F("target_temperature")] = target;
1985 }
1986
1987 if (traits.get_supports_away_mode()) {
1988 root[ESPHOME_F("away")] = obj->is_away();
1989 }
1990
1991 if (traits.has_feature_flags(water_heater::WATER_HEATER_SUPPORTS_ON_OFF)) {
1992 root[ESPHOME_F("is_on")] = obj->is_on();
1993 }
1994
1995 return builder.serialize();
1996}
1997#endif
1998
1999#ifdef USE_INFRARED
2000void WebServer::handle_infrared_request(AsyncWebServerRequest *request, const UrlMatch &match) {
2001 for (infrared::Infrared *obj : App.get_infrareds()) {
2002 auto entity_match = match.match_entity(obj);
2003 if (!entity_match.matched)
2004 continue;
2005
2006 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
2007 auto detail = get_request_detail(request);
2008 auto data = this->infrared_json_(obj, detail);
2009 request->send(200, ESPHOME_F("application/json"), data.c_str());
2010 return;
2011 }
2012 if (!match.method_equals(ESPHOME_F("transmit"))) {
2013 request->send(404);
2014 return;
2015 }
2016
2017 // Only allow transmit if the device supports it
2018 if (!obj->has_transmitter()) {
2019 request->send(400, ESPHOME_F("text/plain"), ESPHOME_F("Device does not support transmission"));
2020 return;
2021 }
2022
2023 // Parse parameters
2024 auto call = obj->make_call();
2025
2026 // Parse carrier frequency (optional)
2027 {
2028 auto value = parse_number<uint32_t>(request->arg(ESPHOME_F("carrier_frequency")).c_str());
2029 if (value.has_value()) {
2030 call.set_carrier_frequency(*value);
2031 }
2032 }
2033
2034 // Parse repeat count (optional, defaults to 1)
2035 {
2036 auto value = parse_number<uint32_t>(request->arg(ESPHOME_F("repeat_count")).c_str());
2037 if (value.has_value()) {
2038 call.set_repeat_count(*value);
2039 }
2040 }
2041
2042 // Parse base64url-encoded raw timings (required)
2043 // Base64url is URL-safe: uses A-Za-z0-9-_ (no special characters needing escaping)
2044 const auto &data_arg = request->arg(ESPHOME_F("data"));
2045
2046 // Validate base64url is not empty (also catches missing parameter since arg() returns empty string)
2047 // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility
2048 if (data_arg.length() == 0) { // NOLINT(readability-container-size-empty)
2049 request->send(400, ESPHOME_F("text/plain"), ESPHOME_F("Missing or empty 'data' parameter"));
2050 return;
2051 }
2052
2053 // Defer to main loop for thread safety. Move encoded string into lambda to ensure
2054 // it outlives the call - set_raw_timings_base64url stores a pointer, so the string
2055 // must remain valid until perform() completes.
2056 // ESP8266 also needs this because ESPAsyncWebServer callbacks run in "sys" context.
2057 this->defer([call, encoded = std::string(data_arg.c_str(), data_arg.length())]() mutable {
2058 call.set_raw_timings_base64url(encoded);
2059 call.perform();
2060 });
2061
2062 request->send(200);
2063 return;
2064 }
2065 request->send(404);
2066}
2067
2069 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
2070 return web_server->infrared_json_(static_cast<infrared::Infrared *>(source), DETAIL_ALL);
2071}
2072
2073json::SerializationBuffer<> WebServer::infrared_json_(infrared::Infrared *obj, JsonDetail start_config) {
2074 json::JsonBuilder builder;
2075 JsonObject root = builder.root();
2076
2077 set_json_icon_state_value(root, obj, "infrared", "", 0, start_config);
2078
2079 auto traits = obj->get_traits();
2080
2081 root[ESPHOME_F("supports_transmitter")] = traits.get_supports_transmitter();
2082 root[ESPHOME_F("supports_receiver")] = traits.get_supports_receiver();
2083
2084 if (start_config == DETAIL_ALL) {
2085 this->add_sorting_info_(root, obj);
2086 }
2087
2088 return builder.serialize();
2089}
2090#endif
2091
2092#ifdef USE_EVENT
2094 if (!this->include_internal_ && obj->is_internal())
2095 return;
2096 this->events_.deferrable_send_state(obj, "state", event_state_json_generator);
2097}
2098
2099void WebServer::handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match) {
2100 for (event::Event *obj : App.get_events()) {
2101 auto entity_match = match.match_entity(obj);
2102 if (!entity_match.matched)
2103 continue;
2104
2105 // Note: request->method() is always HTTP_GET here (canHandle ensures this)
2106 if (entity_match.action_is_empty) {
2107 auto detail = get_request_detail(request);
2108 auto data = this->event_json_(obj, StringRef(), detail);
2109 request->send(200, "application/json", data.c_str());
2110 return;
2111 }
2112 }
2113 request->send(404);
2114}
2115
2116static StringRef get_event_type(event::Event *event) { return event ? event->get_last_event_type() : StringRef(); }
2117
2119 auto *event = static_cast<event::Event *>(source);
2120 return web_server->event_json_(event, get_event_type(event), DETAIL_STATE);
2121}
2122// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
2124 auto *event = static_cast<event::Event *>(source);
2125 return web_server->event_json_(event, get_event_type(event), DETAIL_ALL);
2126}
2127json::SerializationBuffer<> WebServer::event_json_(event::Event *obj, StringRef event_type, JsonDetail start_config) {
2128 json::JsonBuilder builder;
2129 JsonObject root = builder.root();
2130
2131 set_json_id(root, obj, "event", start_config);
2132 if (!event_type.empty()) {
2133 root[ESPHOME_F("event_type")] = event_type;
2134 }
2135 if (start_config == DETAIL_ALL) {
2136 JsonArray event_types = root[ESPHOME_F("event_types")].to<JsonArray>();
2137 for (const char *event_type : obj->get_event_types()) {
2138 event_types.add(event_type);
2139 }
2140 char dc_buf[MAX_DEVICE_CLASS_LENGTH];
2141 root[ESPHOME_F("device_class")] = obj->get_device_class_to(dc_buf);
2142 this->add_sorting_info_(root, obj);
2143 }
2144
2145 return builder.serialize();
2146}
2147// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
2148#endif
2149
2150#ifdef USE_UPDATE
2152 this->events_.deferrable_send_state(obj, "state", update_state_json_generator);
2153}
2154void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match) {
2155 for (update::UpdateEntity *obj : App.get_updates()) {
2156 auto entity_match = match.match_entity(obj);
2157 if (!entity_match.matched)
2158 continue;
2159
2160 if (request->method() == HTTP_GET && entity_match.action_is_empty) {
2161 auto detail = get_request_detail(request);
2162 auto data = this->update_json_(obj, detail);
2163 request->send(200, "application/json", data.c_str());
2164 return;
2165 }
2166
2167 if (!match.method_equals(ESPHOME_F("install"))) {
2168 request->send(404);
2169 return;
2170 }
2171
2172 DEFER_ACTION(obj, obj->perform());
2173 request->send(200);
2174 return;
2175 }
2176 request->send(404);
2177}
2179 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
2180 return web_server->update_json_((update::UpdateEntity *) (source), DETAIL_STATE);
2181}
2183 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
2184 return web_server->update_json_((update::UpdateEntity *) (source), DETAIL_ALL);
2185}
2186json::SerializationBuffer<> WebServer::update_json_(update::UpdateEntity *obj, JsonDetail start_config) {
2187 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
2188 json::JsonBuilder builder;
2189 JsonObject root = builder.root();
2190
2191 char buf[PSTR_LOCAL_SIZE];
2192 set_json_icon_state_value(root, obj, "update", PSTR_LOCAL(update::update_state_to_string(obj->state)),
2193 obj->update_info.latest_version, start_config);
2194 if (start_config == DETAIL_ALL) {
2195 root[ESPHOME_F("current_version")] = obj->update_info.current_version;
2196 root[ESPHOME_F("title")] = obj->update_info.title;
2197 root[ESPHOME_F("summary")] = obj->update_info.summary;
2198 root[ESPHOME_F("release_url")] = obj->update_info.release_url;
2199 this->add_sorting_info_(root, obj);
2200 }
2201
2202 return builder.serialize();
2203 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
2204}
2205#endif
2206
2207bool WebServer::canHandle(AsyncWebServerRequest *request) const {
2208#ifdef USE_ESP32
2209 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
2210 StringRef url = request->url_to(url_buf);
2211#else
2212 const auto &url = request->url();
2213#endif
2214 const auto method = request->method();
2215
2216 // Static URL checks - use ESPHOME_F to keep strings in flash on ESP8266
2217 if (url == ESPHOME_F("/"))
2218 return true;
2219#if !defined(USE_ESP32) && defined(USE_ARDUINO)
2220 if (url == ESPHOME_F("/events"))
2221 return true;
2222#endif
2223#ifdef USE_WEBSERVER_CSS_INCLUDE
2224 if (url == ESPHOME_F("/0.css"))
2225 return true;
2226#endif
2227#ifdef USE_WEBSERVER_JS_INCLUDE
2228 if (url == ESPHOME_F("/0.js"))
2229 return true;
2230#endif
2231
2232#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
2233 if (method == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network")))
2234 return true;
2235#endif
2236
2237 // Parse URL for component checks
2238 UrlMatch match = match_url(url.c_str(), url.length(), true);
2239 if (!match.valid)
2240 return false;
2241
2242 // Common pattern check
2243 bool is_get = method == HTTP_GET;
2244 bool is_post = method == HTTP_POST;
2245 bool is_get_or_post = is_get || is_post;
2246
2247 if (!is_get_or_post)
2248 return false;
2249
2250 // Check GET-only domains - use ESPHOME_F to keep strings in flash on ESP8266
2251 if (is_get) {
2252#ifdef USE_SENSOR
2253 if (match.domain_equals(ESPHOME_F("sensor")))
2254 return true;
2255#endif
2256#ifdef USE_BINARY_SENSOR
2257 if (match.domain_equals(ESPHOME_F("binary_sensor")))
2258 return true;
2259#endif
2260#ifdef USE_TEXT_SENSOR
2261 if (match.domain_equals(ESPHOME_F("text_sensor")))
2262 return true;
2263#endif
2264#ifdef USE_EVENT
2265 if (match.domain_equals(ESPHOME_F("event")))
2266 return true;
2267#endif
2268 }
2269
2270 // Check GET+POST domains
2271 if (is_get_or_post) {
2272#ifdef USE_SWITCH
2273 if (match.domain_equals(ESPHOME_F("switch")))
2274 return true;
2275#endif
2276#ifdef USE_BUTTON
2277 if (match.domain_equals(ESPHOME_F("button")))
2278 return true;
2279#endif
2280#ifdef USE_FAN
2281 if (match.domain_equals(ESPHOME_F("fan")))
2282 return true;
2283#endif
2284#ifdef USE_LIGHT
2285 if (match.domain_equals(ESPHOME_F("light")))
2286 return true;
2287#endif
2288#ifdef USE_COVER
2289 if (match.domain_equals(ESPHOME_F("cover")))
2290 return true;
2291#endif
2292#ifdef USE_NUMBER
2293 if (match.domain_equals(ESPHOME_F("number")))
2294 return true;
2295#endif
2296#ifdef USE_DATETIME_DATE
2297 if (match.domain_equals(ESPHOME_F("date")))
2298 return true;
2299#endif
2300#ifdef USE_DATETIME_TIME
2301 if (match.domain_equals(ESPHOME_F("time")))
2302 return true;
2303#endif
2304#ifdef USE_DATETIME_DATETIME
2305 if (match.domain_equals(ESPHOME_F("datetime")))
2306 return true;
2307#endif
2308#ifdef USE_TEXT
2309 if (match.domain_equals(ESPHOME_F("text")))
2310 return true;
2311#endif
2312#ifdef USE_SELECT
2313 if (match.domain_equals(ESPHOME_F("select")))
2314 return true;
2315#endif
2316#ifdef USE_CLIMATE
2317 if (match.domain_equals(ESPHOME_F("climate")))
2318 return true;
2319#endif
2320#ifdef USE_LOCK
2321 if (match.domain_equals(ESPHOME_F("lock")))
2322 return true;
2323#endif
2324#ifdef USE_VALVE
2325 if (match.domain_equals(ESPHOME_F("valve")))
2326 return true;
2327#endif
2328#ifdef USE_ALARM_CONTROL_PANEL
2329 if (match.domain_equals(ESPHOME_F("alarm_control_panel")))
2330 return true;
2331#endif
2332#ifdef USE_UPDATE
2333 if (match.domain_equals(ESPHOME_F("update")))
2334 return true;
2335#endif
2336#ifdef USE_WATER_HEATER
2337 if (match.domain_equals(ESPHOME_F("water_heater")))
2338 return true;
2339#endif
2340#ifdef USE_INFRARED
2341 if (match.domain_equals(ESPHOME_F("infrared")))
2342 return true;
2343#endif
2344 }
2345
2346 return false;
2347}
2348void WebServer::handleRequest(AsyncWebServerRequest *request) {
2349#ifdef USE_ESP32
2350 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
2351 StringRef url = request->url_to(url_buf);
2352#else
2353 const auto &url = request->url();
2354#endif
2355
2356 // Handle static routes first
2357 if (url == ESPHOME_F("/")) {
2358 this->handle_index_request(request);
2359 return;
2360 }
2361
2362#if !defined(USE_ESP32) && defined(USE_ARDUINO)
2363 if (url == ESPHOME_F("/events")) {
2364 this->events_.add_new_client(this, request);
2365 return;
2366 }
2367#endif
2368
2369#ifdef USE_WEBSERVER_CSS_INCLUDE
2370 if (url == ESPHOME_F("/0.css")) {
2371 this->handle_css_request(request);
2372 return;
2373 }
2374#endif
2375
2376#ifdef USE_WEBSERVER_JS_INCLUDE
2377 if (url == ESPHOME_F("/0.js")) {
2378 this->handle_js_request(request);
2379 return;
2380 }
2381#endif
2382
2383#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
2384 if (request->method() == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network"))) {
2385 this->handle_pna_cors_request(request);
2386 return;
2387 }
2388#endif
2389
2390 // Parse URL for component routing
2391 // Pass HTTP method to disambiguate 3-segment URLs (GET=sub-device state, POST=main device action)
2392 UrlMatch match = match_url(url.c_str(), url.length(), false, request->method() == HTTP_POST);
2393
2394 // Route to appropriate handler based on domain
2395 // NOLINTNEXTLINE(readability-simplify-boolean-expr)
2396 if (false) { // Start chain for else-if macro pattern
2397 }
2398#ifdef USE_SENSOR
2399 else if (match.domain_equals(ESPHOME_F("sensor"))) {
2400 this->handle_sensor_request(request, match);
2401 }
2402#endif
2403#ifdef USE_SWITCH
2404 else if (match.domain_equals(ESPHOME_F("switch"))) {
2405 this->handle_switch_request(request, match);
2406 }
2407#endif
2408#ifdef USE_BUTTON
2409 else if (match.domain_equals(ESPHOME_F("button"))) {
2410 this->handle_button_request(request, match);
2411 }
2412#endif
2413#ifdef USE_BINARY_SENSOR
2414 else if (match.domain_equals(ESPHOME_F("binary_sensor"))) {
2415 this->handle_binary_sensor_request(request, match);
2416 }
2417#endif
2418#ifdef USE_FAN
2419 else if (match.domain_equals(ESPHOME_F("fan"))) {
2420 this->handle_fan_request(request, match);
2421 }
2422#endif
2423#ifdef USE_LIGHT
2424 else if (match.domain_equals(ESPHOME_F("light"))) {
2425 this->handle_light_request(request, match);
2426 }
2427#endif
2428#ifdef USE_TEXT_SENSOR
2429 else if (match.domain_equals(ESPHOME_F("text_sensor"))) {
2430 this->handle_text_sensor_request(request, match);
2431 }
2432#endif
2433#ifdef USE_COVER
2434 else if (match.domain_equals(ESPHOME_F("cover"))) {
2435 this->handle_cover_request(request, match);
2436 }
2437#endif
2438#ifdef USE_NUMBER
2439 else if (match.domain_equals(ESPHOME_F("number"))) {
2440 this->handle_number_request(request, match);
2441 }
2442#endif
2443#ifdef USE_DATETIME_DATE
2444 else if (match.domain_equals(ESPHOME_F("date"))) {
2445 this->handle_date_request(request, match);
2446 }
2447#endif
2448#ifdef USE_DATETIME_TIME
2449 else if (match.domain_equals(ESPHOME_F("time"))) {
2450 this->handle_time_request(request, match);
2451 }
2452#endif
2453#ifdef USE_DATETIME_DATETIME
2454 else if (match.domain_equals(ESPHOME_F("datetime"))) {
2455 this->handle_datetime_request(request, match);
2456 }
2457#endif
2458#ifdef USE_TEXT
2459 else if (match.domain_equals(ESPHOME_F("text"))) {
2460 this->handle_text_request(request, match);
2461 }
2462#endif
2463#ifdef USE_SELECT
2464 else if (match.domain_equals(ESPHOME_F("select"))) {
2465 this->handle_select_request(request, match);
2466 }
2467#endif
2468#ifdef USE_CLIMATE
2469 else if (match.domain_equals(ESPHOME_F("climate"))) {
2470 this->handle_climate_request(request, match);
2471 }
2472#endif
2473#ifdef USE_LOCK
2474 else if (match.domain_equals(ESPHOME_F("lock"))) {
2475 this->handle_lock_request(request, match);
2476 }
2477#endif
2478#ifdef USE_VALVE
2479 else if (match.domain_equals(ESPHOME_F("valve"))) {
2480 this->handle_valve_request(request, match);
2481 }
2482#endif
2483#ifdef USE_ALARM_CONTROL_PANEL
2484 else if (match.domain_equals(ESPHOME_F("alarm_control_panel"))) {
2485 this->handle_alarm_control_panel_request(request, match);
2486 }
2487#endif
2488#ifdef USE_UPDATE
2489 else if (match.domain_equals(ESPHOME_F("update"))) {
2490 this->handle_update_request(request, match);
2491 }
2492#endif
2493#ifdef USE_WATER_HEATER
2494 else if (match.domain_equals(ESPHOME_F("water_heater"))) {
2495 this->handle_water_heater_request(request, match);
2496 }
2497#endif
2498#ifdef USE_INFRARED
2499 else if (match.domain_equals(ESPHOME_F("infrared"))) {
2500 this->handle_infrared_request(request, match);
2501 }
2502#endif
2503 else {
2504 // No matching handler found - send 404
2505 ESP_LOGV(TAG, "Request for unknown URL: %s", url.c_str());
2506 request->send(404, ESPHOME_F("text/plain"), ESPHOME_F("Not Found"));
2507 }
2508}
2509
2510bool WebServer::isRequestHandlerTrivial() const { return false; }
2511
2512void WebServer::add_sorting_info_(JsonObject &root, EntityBase *entity) {
2513#ifdef USE_WEBSERVER_SORTING
2514 if (this->sorting_entitys_.find(entity) != this->sorting_entitys_.end()) {
2515 root[ESPHOME_F("sorting_weight")] = this->sorting_entitys_[entity].weight;
2516 if (this->sorting_groups_.find(this->sorting_entitys_[entity].group_id) != this->sorting_groups_.end()) {
2517 root[ESPHOME_F("sorting_group")] = this->sorting_groups_[this->sorting_entitys_[entity].group_id].name;
2518 }
2519 }
2520#endif
2521}
2522
2523#ifdef USE_WEBSERVER_SORTING
2524void WebServer::add_entity_config(EntityBase *entity, float weight, uint64_t group) {
2525 this->sorting_entitys_[entity] = SortingComponents{weight, group};
2526}
2527
2528void WebServer::add_sorting_group(uint64_t group_id, const std::string &group_name, float weight) {
2529 this->sorting_groups_[group_id] = SortingGroup{group_name, weight};
2530}
2531#endif
2532
2533} // namespace esphome::web_server
2534#endif
media_source::MediaSource * source
BedjetMode mode
BedJet operating mode.
uint8_t m
Definition bl0906.h:1
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
static constexpr size_t ESPHOME_COMMENT_SIZE_MAX
Maximum size of the comment buffer (including null terminator)
void get_comment_string(std::span< char, ESPHOME_COMMENT_SIZE_MAX > buffer)
Copy the comment string into the provided buffer.
auto & get_binary_sensors() const
auto & get_events() const
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:501
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:358
static void register_controller(Controller *controller)
Register a controller to receive entity state updates.
const char * get_name()
Definition device.h:10
const char * get_device_class_to(std::span< char, MAX_DEVICE_CLASS_LENGTH > buffer) const
bool is_internal() const
const StringRef & get_name() const
ESPDEPRECATED("Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be " "removed in ESPHome 2026.9.0", "2026.3.0") std const char * get_icon_to(std::span< char, MAX_ICON_LENGTH > buffer) const
Get the unit of measurement as std::string (deprecated, prefer get_unit_of_measurement_ref())
size_t write_object_id_to(char *buf, size_t buf_size) const
Write object_id directly to buffer, returns length written (excluding null) Useful for building compo...
bool is_disabled_by_default() const
ESPDEPRECATED("Use get_device_class_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") std StringRef get_unit_of_measurement_ref() const
Device * get_device() const
bool has_state() const
EntityCategory get_entity_category() const
StringRef get_object_id_to(std::span< char, OBJECT_ID_MAX_LEN > buf) const
Get object_id with zero heap allocation For static case: returns StringRef to internal storage (buffe...
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type length() const
Definition string_ref.h:75
constexpr bool empty() const
Definition string_ref.h:76
constexpr size_type size() const
Definition string_ref.h:74
AlarmControlPanelCall make_call()
Make a AlarmControlPanelCall.
Base class for all binary_sensor-type classes.
Base class for all buttons.
Definition button.h:25
This class is used to encode all control actions on a climate device.
Definition climate.h:33
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:186
ClimateMode mode
The active mode of the climate device.
Definition climate.h:266
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:260
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:494
float target_temperature
The target temperature of the climate device.
Definition climate.h:247
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:243
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:272
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:250
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:237
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:240
ClimateAction action
The active state of the climate device.
Definition climate.h:269
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:278
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:234
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:263
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:252
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:275
int8_t get_target_temperature_accuracy_decimals() const
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:58
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:46
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:50
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:54
Base class for all cover devices.
Definition cover.h:110
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:115
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:140
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:123
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
Definition cover.cpp:189
virtual CoverTraits get_traits()=0
bool get_supports_position() const
const FixedVector< const char * > & get_event_types() const
Return the event types supported by this event.
Definition event.h:43
FanCall turn_on()
Definition fan.cpp:141
FanCall turn_off()
Definition fan.cpp:142
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:143
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
bool supports_oscillation() const
Return if this fan supports oscillation.
Definition fan_traits.h:18
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:110
InfraredTraits & get_traits()
Get the traits for this infrared implementation.
Definition infrared.h:129
bool get_supports_transmitter() const
Definition infrared.h:98
Builder class for creating JSON documents without lambdas.
Definition json_util.h:170
SerializationBuffer serialize()
Serialize the JSON document to a SerializationBuffer (stack-first allocation) Uses 512-byte stack buf...
Definition json_util.cpp:69
Buffer for JSON serialization that uses stack allocation for small payloads.
Definition json_util.h:22
This class represents a requested change in a light state.
Definition light_call.h:21
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...
Definition light_state.h:93
LightColorValues remote_values
The remote color values reported to the frontend.
const FixedVector< LightEffect * > & get_effects() const
Get all effects for this light state.
Base class for all locks.
Definition lock.h:110
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:20
void lock()
Turn this lock on.
Definition lock.cpp:28
LockState state
The current reported state of the lock.
Definition lock.h:129
void unlock()
Turn this lock off.
Definition lock.cpp:29
void open()
Open (unlatch) this lock.
Definition lock.cpp:30
void add_log_callback(void *instance, void(*fn)(void *, uint8_t, const char *, const char *, size_t))
Register a log callback to receive log messages.
Definition logger.h:190
Base-class for all numbers.
Definition number.h:29
NumberCall make_call()
Definition number.h:35
NumberTraits traits
Definition number.h:39
NumberMode get_mode() const
Base-class for all selects.
Definition select.h:29
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
SelectTraits traits
Definition select.h:31
const FixedVector< const char * > & get_options() const
Base-class for all sensors.
Definition sensor.h:47
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:125
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:45
Base class for all switches.
Definition switch.h:38
void toggle()
Toggle this switch.
Definition switch.cpp:28
void turn_on()
Turn this switch on.
Definition switch.cpp:20
void turn_off()
Turn this switch off.
Definition switch.cpp:24
bool state
The current reported state of the binary sensor.
Definition switch.h:55
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:70
Base-class for all text inputs.
Definition text.h:21
TextCall make_call()
Instantiate a TextCall object to modify this text component's state.
Definition text.h:31
TextTraits traits
Definition text.h:24
TextMode get_mode() const
Definition text_traits.h:30
const char * get_pattern_c_str() const
Definition text_traits.h:25
const UpdateState & state
const UpdateInfo & update_info
ValveCall & set_command_close()
Set the command to close the valve.
Definition valve.cpp:54
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:62
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:58
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:50
Base class for all valve devices.
Definition valve.h:104
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
Definition valve.cpp:168
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:115
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:126
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:109
virtual ValveTraits get_traits()=0
bool get_supports_position() const
WaterHeaterCall & set_away(bool away)
WaterHeaterCall & set_target_temperature_high(float temperature)
WaterHeaterCall & set_mode(WaterHeaterMode mode)
WaterHeaterCall & set_target_temperature_low(float temperature)
WaterHeaterCall & set_on(bool on)
WaterHeaterCall & set_target_temperature(float temperature)
bool is_on() const
Check if the water heater is on.
bool is_away() const
Check if away mode is currently active.
virtual WaterHeaterCallInternal make_call()=0
WaterHeaterMode get_mode() const
virtual WaterHeaterTraits get_traits()
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
Definition web_server.h:149
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)
std::vector< DeferredEvent > deferred_queue_
Definition web_server.h:146
void on_client_connect_(DeferredUpdateEventSource *source)
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_disconnect_(DeferredUpdateEventSource *source)
This class allows users to create a web server with their ESP nodes.
Definition web_server.h:190
void setup() override
Setup the internal web server and register handlers.
void on_update(update::UpdateEntity *obj) override
void on_water_heater_update(water_heater::WaterHeater *obj) override
json::SerializationBuffer get_config_json()
Return the webserver configuration as JSON.
std::map< EntityBase *, SortingComponents > sorting_entitys_
Definition web_server.h:496
static json::SerializationBuffer text_state_json_generator(WebServer *web_server, void *source)
void on_text_update(text::Text *obj) override
void on_light_update(light::LightState *obj) override
static json::SerializationBuffer datetime_state_json_generator(WebServer *web_server, void *source)
void on_cover_update(cover::Cover *obj) override
static json::SerializationBuffer lock_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer alarm_control_panel_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer text_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer switch_all_json_generator(WebServer *web_server, void *source)
void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a select request under '/select/<id>'.
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len)
static json::SerializationBuffer event_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer update_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer text_sensor_all_json_generator(WebServer *web_server, void *source)
void handle_water_heater_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a water_heater request under '/water_heater/<id>/<mode/set>'.
bool isRequestHandlerTrivial() const override
This web handle is not trivial.
static json::SerializationBuffer cover_all_json_generator(WebServer *web_server, void *source)
WebServer(web_server_base::WebServerBase *base)
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, ParamNameType param_name, T &call, Ret(T::*setter)(uint32_t), uint32_t scale=1)
Definition web_server.h:518
static json::SerializationBuffer datetime_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer light_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer climate_state_json_generator(WebServer *web_server, void *source)
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
static json::SerializationBuffer date_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer sensor_state_json_generator(WebServer *web_server, void *source)
void on_number_update(number::Number *obj) override
void add_entity_config(EntityBase *entity, float weight, uint64_t group)
void parse_light_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(float), float scale=1.0f)
Definition web_server.h:508
void handle_css_request(AsyncWebServerRequest *request)
Handle included css request under '/0.css'.
static json::SerializationBuffer select_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer infrared_all_json_generator(WebServer *web_server, void *source)
void on_valve_update(valve::Valve *obj) override
void on_climate_update(climate::Climate *obj) override
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 json::SerializationBuffer sensor_all_json_generator(WebServer *web_server, void *source)
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text input request under '/text/<id>'.
static json::SerializationBuffer number_all_json_generator(WebServer *web_server, void *source)
void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a cover request under '/cover/<id>/<open/close/stop/set>'.
static json::SerializationBuffer select_state_json_generator(WebServer *web_server, void *source)
void on_switch_update(switch_::Switch *obj) override
static json::SerializationBuffer water_heater_state_json_generator(WebServer *web_server, void *source)
web_server_base::WebServerBase * base_
Definition web_server.h:569
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 json::SerializationBuffer time_state_json_generator(WebServer *web_server, void *source)
void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a text sensor request under '/text_sensor/<id>'.
void parse_bool_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(bool))
Definition web_server.h:551
void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a date request under '/date/<id>'.
void handle_infrared_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle an infrared request under '/infrared/<id>/transmit'.
void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a sensor request under '/sensor/<id>'.
void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a number request under '/number/<id>'.
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
void handle_js_request(AsyncWebServerRequest *request)
Handle included js request under '/0.js'.
static json::SerializationBuffer valve_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer fan_all_json_generator(WebServer *web_server, void *source)
void set_js_include(const char *js_include)
Set local path to the script that's embedded in the index page.
static json::SerializationBuffer lock_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer update_state_json_generator(WebServer *web_server, void *source)
void handleRequest(AsyncWebServerRequest *request) override
Override the web handler's handleRequest method.
static json::SerializationBuffer button_all_json_generator(WebServer *web_server, void *source)
void on_datetime_update(datetime::DateTimeEntity *obj) override
void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a fan request under '/fan/<id>/</turn_on/turn_off/toggle>'.
static json::SerializationBuffer alarm_control_panel_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer time_all_json_generator(WebServer *web_server, void *source)
void parse_cstr_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(const char *, size_t))
Definition web_server.h:538
static json::SerializationBuffer water_heater_all_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>'.
static json::SerializationBuffer date_state_json_generator(WebServer *web_server, void *source)
void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a binary sensor request under '/binary_sensor/<id>'.
void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a time request under '/time/<id>'.
void on_sensor_update(sensor::Sensor *obj) override
std::map< uint64_t, SortingGroup > sorting_groups_
Definition web_server.h:497
void set_css_include(const char *css_include)
Set local path to the script that's embedded in the index page.
bool canHandle(AsyncWebServerRequest *request) const override
Override the web handler's canHandle method.
static json::SerializationBuffer climate_all_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer fan_state_json_generator(WebServer *web_server, void *source)
void on_event(event::Event *obj) override
static json::SerializationBuffer cover_state_json_generator(WebServer *web_server, void *source)
void handle_pna_cors_request(AsyncWebServerRequest *request)
static json::SerializationBuffer binary_sensor_state_json_generator(WebServer *web_server, void *source)
void on_fan_update(fan::Fan *obj) override
void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a datetime request under '/datetime/<id>'.
static json::SerializationBuffer event_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer binary_sensor_all_json_generator(WebServer *web_server, void *source)
void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a alarm_control_panel request under '/alarm_control_panel/<id>'.
void on_lock_update(lock::Lock *obj) override
static json::SerializationBuffer switch_state_json_generator(WebServer *web_server, void *source)
float get_setup_priority() const override
MQTT setup priority.
void on_select_update(select::Select *obj) override
void on_time_update(datetime::TimeEntity *obj) override
void parse_num_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call, Ret(T::*setter)(NumT))
Definition web_server.h:529
static json::SerializationBuffer text_sensor_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer number_state_json_generator(WebServer *web_server, void *source)
static json::SerializationBuffer valve_all_json_generator(WebServer *web_server, void *source)
void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a update request under '/update/<id>'.
static json::SerializationBuffer light_all_json_generator(WebServer *web_server, void *source)
void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight)
void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match)
Handle a climate request under '/climate/<id>'.
void on_text_sensor_update(text_sensor::TextSensor *obj) override
void add_handler(AsyncWebHandler *handler)
struct @65::@66 __attribute__
ClimateSwingMode swing_mode
Definition climate.h:11
uint8_t custom_preset
Definition climate.h:9
uint8_t custom_fan_mode
Definition climate.h:4
const char * message
Definition component.cpp:38
int speed
Definition fan.h:3
bool state
Definition fan.h:2
mopeka_std_values val[3]
const LogString * climate_action_to_string(ClimateAction action)
Convert the given ClimateAction to a human-readable string.
@ CLIMATE_SUPPORTS_CURRENT_HUMIDITY
@ CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE
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 NOTE: If adding values, update ClimatePresetMask in climate_traits....
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.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
const LogString * cover_operation_to_str(CoverOperation op)
Definition cover.cpp:25
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:15
LockState
Enum for all states a lock can be in.
Definition lock.h:23
Logger * global_logger
Definition logger.cpp:278
const char * get_use_address()
Get the active network hostname.
Definition util.cpp:45
constexpr float WIFI
Definition component.h:36
const char *const TAG
Definition spi.cpp:7
const LogString * update_state_to_string(UpdateState state)
const LogString * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:29
@ WATER_HEATER_SUPPORTS_ON_OFF
The water heater can be turned on/off.
const LogString * water_heater_mode_to_string(WaterHeaterMode mode)
Convert the given WaterHeaterMode to a human-readable string for logging.
const char * tag
Definition log.h:74
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
Definition helpers.cpp:490
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:454
if(written< 0)
Definition helpers.h:938
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1007
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
Definition helpers.cpp:514
uint64_t HOT millis_64()
Definition core.cpp:27
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
Definition helpers.cpp:817
size_t value_accuracy_with_uom_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals, StringRef unit_of_measurement)
Format value with accuracy and UOM to buffer, returns chars written (excluding null)
Definition helpers.cpp:500
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
@ PARSE_ON
Definition helpers.h:1533
@ PARSE_TOGGLE
Definition helpers.h:1535
@ PARSE_OFF
Definition helpers.h:1534
@ PARSE_NONE
Definition helpers.h:1532
static void uint32_t
Result of matching a URL against an entity.
Definition web_server.h:54
Internal helper struct that is used to parse incoming URLs.
Definition web_server.h:60
StringRef device_name
Device name within URL, empty for main device.
Definition web_server.h:65
bool valid
Whether this match is valid.
Definition web_server.h:67
EntityMatchResult match_entity(EntityBase *entity) const
Match entity by name first, then fall back to object_id with deprecation warning Returns EntityMatchR...
StringRef method
Method within URL, for example "turn_on".
Definition web_server.h:63
bool domain_equals(const char *str) const
Definition web_server.h:70
bool method_equals(const char *str) const
Definition web_server.h:71
StringRef domain
Domain within URL, for example "sensor".
Definition web_server.h:61
uint8_t end[39]
Definition sun_gtil2.cpp:17
const size_t ESPHOME_WEBSERVER_INDEX_HTML_SIZE
const size_t ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE
const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE