ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
api_server.cpp
Go to the documentation of this file.
1#include "api_server.h"
2#ifdef USE_API
3#include <cerrno>
4#include "api_connection.h"
9#include "esphome/core/hal.h"
10#include "esphome/core/log.h"
11#include "esphome/core/util.h"
13#ifdef USE_API_HOMEASSISTANT_SERVICES
15#endif
16
17#ifdef USE_LOGGER
19#endif
20
21#include <algorithm>
22#include <utility>
23
24namespace esphome::api {
25
26static const char *const TAG = "api";
27
28// APIServer
29APIServer *global_api_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
30
32
33void APIServer::socket_failed_(const LogString *msg) {
34 ESP_LOGW(TAG, "Socket %s: errno %d", LOG_STR_ARG(msg), errno);
35 this->destroy_socket_();
36 this->mark_failed();
37}
38
41
42#ifdef USE_API_NOISE
43 uint32_t hash = 88491486UL;
44
46
47#ifndef USE_API_NOISE_PSK_FROM_YAML
48 // Only load saved PSK if not set from YAML
49 if (this->load_and_apply_noise_psk_()) {
50 ESP_LOGD(TAG, "Loaded saved Noise PSK");
51 }
52#endif
53#endif
54
55 this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0).release(); // monitored for incoming connections
56 if (this->socket_ == nullptr) {
57 this->socket_failed_(LOG_STR("creation"));
58 return;
59 }
60 int enable = 1;
61 int err = this->socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
62 if (err != 0) {
63 ESP_LOGW(TAG, "Socket reuseaddr: errno %d", errno);
64 // we can still continue
65 }
66 err = this->socket_->setblocking(false);
67 if (err != 0) {
68 this->socket_failed_(LOG_STR("nonblocking"));
69 return;
70 }
71
72 struct sockaddr_storage server;
73
74 socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
75 if (sl == 0) {
76 this->socket_failed_(LOG_STR("set sockaddr"));
77 return;
78 }
79
80 err = this->socket_->bind((struct sockaddr *) &server, sl);
81 if (err != 0) {
82 this->socket_failed_(LOG_STR("bind"));
83 return;
84 }
85
86 err = this->socket_->listen(this->listen_backlog_);
87 if (err != 0) {
88 this->socket_failed_(LOG_STR("listen"));
89 return;
90 }
91
92#ifdef USE_LOGGER
93 if (logger::global_logger != nullptr) {
95 this, [](void *self, uint8_t level, const char *tag, const char *message, size_t message_len) {
96 static_cast<APIServer *>(self)->on_log(level, tag, message, message_len);
97 });
98 }
99#endif
100
101#ifdef USE_CAMERA
102 if (camera::Camera::instance() != nullptr && !camera::Camera::instance()->is_internal()) {
104 }
105#endif
106
107 // Initialize last_connected_ for reboot timeout tracking
109 // Set warning status if reboot timeout is enabled
110 if (this->reboot_timeout_ != 0) {
111 this->status_set_warning(LOG_STR("waiting for client connection"));
112 }
113}
114
116 // Accept new clients only if the socket exists and has incoming connections
117 if (this->socket_ && this->socket_->ready()) {
118 this->accept_new_connections_();
119 }
120
121 if (this->clients_.empty()) {
122 // Check reboot timeout - done in loop to avoid scheduler heap churn
123 // (cancelled scheduler items sit in heap memory until their scheduled time)
124 if (this->reboot_timeout_ != 0) {
126 if (now - this->last_connected_ > this->reboot_timeout_) {
127 ESP_LOGE(TAG, "No clients; rebooting");
128 App.reboot();
129 }
130 }
131 return;
132 }
133
134 // Process clients and remove disconnected ones in a single pass
135 // Check network connectivity once for all clients
136 if (!network::is_connected()) {
137 // Network is down - disconnect all clients
138 for (auto &client : this->clients_) {
139 client->on_fatal_error();
140 client->log_client_(ESPHOME_LOG_LEVEL_WARN, LOG_STR("Network down; disconnect"));
141 }
142 // Continue to process and clean up the clients below
143 }
144
145 size_t client_index = 0;
146 while (client_index < this->clients_.size()) {
147 auto &client = this->clients_[client_index];
148
149 // Common case: process active client
150 if (!client->flags_.remove) {
151 client->loop();
152 }
153 // Handle disconnection promptly - close socket to free LWIP PCB
154 // resources and prevent retransmit crashes on ESP8266.
155 if (client->flags_.remove) {
156 // Rare case: handle disconnection (don't increment - swapped element needs processing)
157 this->remove_client_(client_index);
158 } else {
159 client_index++;
160 }
161 }
162}
163
164void APIServer::remove_client_(size_t client_index) {
165 auto &client = this->clients_[client_index];
166
167#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
169#endif
170 ESP_LOGV(TAG, "Remove connection %s", client->get_name());
171
172#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
173 // Save client info before closing socket and removal for the trigger
174 char peername_buf[socket::SOCKADDR_STR_LEN];
175 std::string client_name(client->get_name());
176 std::string client_peername(client->get_peername_to(peername_buf));
177#endif
178
179 // Close socket now (was deferred from on_fatal_error to allow getpeername)
180 client->helper_->close();
181
182 // Swap with the last element and pop (avoids expensive vector shifts)
183 if (client_index < this->clients_.size() - 1) {
184 std::swap(this->clients_[client_index], this->clients_.back());
185 }
186 this->clients_.pop_back();
187
188 // Last client disconnected - set warning and start tracking for reboot timeout
189 if (this->clients_.empty() && this->reboot_timeout_ != 0) {
190 this->status_set_warning(LOG_STR("waiting for client connection"));
192 }
193
194#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
195 // Fire trigger after client is removed so api.connected reflects the true state
196 this->client_disconnected_trigger_.trigger(client_name, client_peername);
197#endif
198}
199
200void __attribute__((flatten)) APIServer::accept_new_connections_() {
201 while (true) {
202 struct sockaddr_storage source_addr;
203 socklen_t addr_len = sizeof(source_addr);
204
205 auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
206 if (!sock)
207 break;
208
209 char peername[socket::SOCKADDR_STR_LEN];
210 sock->getpeername_to(peername);
211
212 // Check if we're at the connection limit
213 if (this->clients_.size() >= this->max_connections_) {
214 ESP_LOGW(TAG, "Max connections (%d), rejecting %s", this->max_connections_, peername);
215 // Immediately close - socket destructor will handle cleanup
216 sock.reset();
217 continue;
218 }
219
220 ESP_LOGD(TAG, "Accept %s", peername);
221
222 auto *conn = new APIConnection(std::move(sock), this);
223 this->clients_.emplace_back(conn);
224 conn->start();
225
226 // First client connected - clear warning and update timestamp
227 if (this->clients_.size() == 1 && this->reboot_timeout_ != 0) {
228 this->status_clear_warning();
230 }
231 }
232}
233
235 ESP_LOGCONFIG(TAG,
236 "Server:\n"
237 " Address: %s:%u\n"
238 " Listen backlog: %u\n"
239 " Max connections: %u",
241#ifdef USE_API_NOISE
242 ESP_LOGCONFIG(TAG, " Noise encryption: %s", YESNO(this->noise_ctx_.has_psk()));
243 if (!this->noise_ctx_.has_psk()) {
244 ESP_LOGCONFIG(TAG, " Supports encryption: YES");
245 }
246#else
247 ESP_LOGCONFIG(TAG, " Noise encryption: NO");
248#endif
249}
250
252
253// Macro for controller update dispatch
254#define API_DISPATCH_UPDATE(entity_type, entity_name) \
255 void APIServer::on_##entity_name##_update(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \
256 if (obj->is_internal()) \
257 return; \
258 for (auto &c : this->clients_) { \
259 if (c->flags_.state_subscription) \
260 c->send_##entity_name##_state(obj); \
261 } \
262 }
263
264#ifdef USE_BINARY_SENSOR
266#endif
267
268#ifdef USE_COVER
270#endif
271
272#ifdef USE_FAN
274#endif
275
276#ifdef USE_LIGHT
278#endif
279
280#ifdef USE_SENSOR
282#endif
283
284#ifdef USE_SWITCH
286#endif
287
288#ifdef USE_TEXT_SENSOR
290#endif
291
292#ifdef USE_CLIMATE
294#endif
295
296#ifdef USE_NUMBER
298#endif
299
300#ifdef USE_DATETIME_DATE
302#endif
303
304#ifdef USE_DATETIME_TIME
306#endif
307
308#ifdef USE_DATETIME_DATETIME
310#endif
311
312#ifdef USE_TEXT
314#endif
315
316#ifdef USE_SELECT
318#endif
319
320#ifdef USE_LOCK
322#endif
323
324#ifdef USE_VALVE
326#endif
327
328#ifdef USE_MEDIA_PLAYER
330#endif
331
332#ifdef USE_WATER_HEATER
334#endif
335
336#ifdef USE_EVENT
338 if (obj->is_internal())
339 return;
340 for (auto &c : this->clients_) {
341 if (c->flags_.state_subscription)
342 c->send_event(obj);
343 }
344}
345#endif
346
347#ifdef USE_UPDATE
348// Update is a special case - the method is called on_update, not on_update_update
350 if (obj->is_internal())
351 return;
352 for (auto &c : this->clients_) {
353 if (c->flags_.state_subscription)
354 c->send_update_state(obj);
355 }
356}
357#endif
358
359#ifdef USE_ZWAVE_PROXY
361 // We could add code to manage a second subscription type, but, since this message type is
362 // very infrequent and small, we simply send it to all clients
363 for (auto &c : this->clients_)
364 c->send_message(msg);
365}
366#endif
367
368#ifdef USE_IR_RF
370 const std::vector<int32_t> *timings) {
372#ifdef USE_DEVICES
373 resp.device_id = device_id;
374#endif
375 resp.key = key;
376 resp.timings = timings;
377
378 for (auto &c : this->clients_)
379 c->send_infrared_rf_receive_event(resp);
380}
381#endif
382
383#ifdef USE_ALARM_CONTROL_PANEL
385#endif
386
388
389void APIServer::set_port(uint16_t port) { this->port_ = port; }
390
391void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; }
392
393#ifdef USE_API_HOMEASSISTANT_SERVICES
395 for (auto &client : this->clients_) {
396 client->send_homeassistant_action(call);
397 }
398}
399#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
401 this->action_response_callbacks_.push_back({call_id, std::move(callback)});
402}
403
404void APIServer::handle_action_response(uint32_t call_id, bool success, StringRef error_message) {
405 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
406 if (it->call_id == call_id) {
407 auto callback = std::move(it->callback);
408 this->action_response_callbacks_.erase(it);
409 ActionResponse response(success, error_message);
410 callback(response);
411 return;
412 }
413 }
414}
415#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
416void APIServer::handle_action_response(uint32_t call_id, bool success, StringRef error_message,
417 const uint8_t *response_data, size_t response_data_len) {
418 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
419 if (it->call_id == call_id) {
420 auto callback = std::move(it->callback);
421 this->action_response_callbacks_.erase(it);
422 ActionResponse response(success, error_message, response_data, response_data_len);
423 callback(response);
424 return;
425 }
426 }
427}
428#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
429#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
430#endif // USE_API_HOMEASSISTANT_SERVICES
431
432#ifdef USE_API_HOMEASSISTANT_STATES
433// Helper to add subscription (reduces duplication)
434void APIServer::add_state_subscription_(const char *entity_id, const char *attribute,
435 std::function<void(StringRef)> &&f, bool once) {
437 .entity_id = entity_id, .attribute = attribute, .callback = std::move(f), .once = once,
438 // entity_id_dynamic_storage and attribute_dynamic_storage remain nullptr (no heap allocation)
439 });
440}
441
442// Helper to add subscription with heap-allocated strings (reduces duplication)
443void APIServer::add_state_subscription_(std::string entity_id, optional<std::string> attribute,
444 std::function<void(StringRef)> &&f, bool once) {
446 // Allocate heap storage for the strings
447 sub.entity_id_dynamic_storage = std::make_unique<std::string>(std::move(entity_id));
448 sub.entity_id = sub.entity_id_dynamic_storage->c_str();
449
450 if (attribute.has_value()) {
451 sub.attribute_dynamic_storage = std::make_unique<std::string>(std::move(attribute.value()));
452 sub.attribute = sub.attribute_dynamic_storage->c_str();
453 } else {
454 sub.attribute = nullptr;
455 }
456
457 sub.callback = std::move(f);
458 sub.once = once;
459 this->state_subs_.push_back(std::move(sub));
460}
461
462// New const char* overload (for internal components - zero allocation)
463void APIServer::subscribe_home_assistant_state(const char *entity_id, const char *attribute,
464 std::function<void(StringRef)> &&f) {
465 this->add_state_subscription_(entity_id, attribute, std::move(f), false);
466}
467
468void APIServer::get_home_assistant_state(const char *entity_id, const char *attribute,
469 std::function<void(StringRef)> &&f) {
470 this->add_state_subscription_(entity_id, attribute, std::move(f), true);
471}
472
473// std::string overload with StringRef callback (zero-allocation callback)
474void APIServer::subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
475 std::function<void(StringRef)> &&f) {
476 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false);
477}
478
479void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
480 std::function<void(StringRef)> &&f) {
481 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true);
482}
483
484// Legacy helper: wraps std::string callback and delegates to StringRef version
485void APIServer::add_state_subscription_(std::string entity_id, optional<std::string> attribute,
486 std::function<void(const std::string &)> &&f, bool once) {
487 // Wrap callback to convert StringRef -> std::string, then delegate
488 this->add_state_subscription_(std::move(entity_id), std::move(attribute),
489 std::function<void(StringRef)>([f = std::move(f)](StringRef state) { f(state.str()); }),
490 once);
491}
492
493// Legacy std::string overload (for custom_api_device.h - converts StringRef to std::string)
494void APIServer::subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
495 std::function<void(const std::string &)> &&f) {
496 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false);
497}
498
499void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
500 std::function<void(const std::string &)> &&f) {
501 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true);
502}
503
504const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
505 return this->state_subs_;
506}
507#endif
508
509uint16_t APIServer::get_port() const { return this->port_; }
510
511void APIServer::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
512
513#ifdef USE_API_NOISE
514bool APIServer::update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg,
515 const LogString *fail_log_msg, bool make_active) {
516 if (!this->noise_pref_.save(&new_psk)) {
517 ESP_LOGW(TAG, "%s", LOG_STR_ARG(fail_log_msg));
518 return false;
519 }
520 // ensure it's written immediately
521 if (!global_preferences->sync()) {
522 ESP_LOGW(TAG, "Failed to sync preferences");
523 return false;
524 }
525 ESP_LOGD(TAG, "%s", LOG_STR_ARG(save_log_msg));
526 if (make_active) {
527 this->set_timeout(100, [this]() {
528 // Re-read the PSK from preferences rather than capturing the 32-byte array
529 // in the lambda (which would exceed std::function SBO and heap-allocate).
530 if (!this->load_and_apply_noise_psk_()) {
531 ESP_LOGW(TAG, "Failed to load saved PSK for activation");
532 return;
533 }
534 ESP_LOGW(TAG, "Disconnecting all clients to reset PSK");
535 for (auto &c : this->clients_) {
537 c->send_message(req);
538 }
539 });
540 }
541 return true;
542}
543
545 SavedNoisePsk saved{};
546 if (!this->noise_pref_.load(&saved))
547 return false;
548 this->set_noise_psk(saved.psk);
549 return true;
550}
551
552bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
553#ifdef USE_API_NOISE_PSK_FROM_YAML
554 // When PSK is set from YAML, this function should never be called
555 // but if it is, reject the change
556 ESP_LOGW(TAG, "Key set in YAML");
557 return false;
558#else
559 auto &old_psk = this->noise_ctx_.get_psk();
560 if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
561 ESP_LOGW(TAG, "New PSK matches old");
562 return true;
563 }
564
565 SavedNoisePsk new_saved_psk{psk};
566 return this->update_noise_psk_(new_saved_psk, LOG_STR("Noise PSK saved"), LOG_STR("Failed to save Noise PSK"),
567 make_active);
568#endif
569}
570bool APIServer::clear_noise_psk(bool make_active) {
571#ifdef USE_API_NOISE_PSK_FROM_YAML
572 // When PSK is set from YAML, this function should never be called
573 // but if it is, reject the change
574 ESP_LOGW(TAG, "Key set in YAML");
575 return false;
576#else
577 SavedNoisePsk empty_psk{};
578 return this->update_noise_psk_(empty_psk, LOG_STR("Noise PSK cleared"), LOG_STR("Failed to clear Noise PSK"),
579 make_active);
580#endif
581}
582#endif
583
584#ifdef USE_HOMEASSISTANT_TIME
586 for (auto &client : this->clients_) {
587 if (!client->flags_.remove && client->is_authenticated()) {
588 client->send_time_request();
589 return; // Only request from one client to avoid clock conflicts
590 }
591 }
592}
593#endif
594
596 for (const auto &client : this->clients_) {
597 if (client->flags_.state_subscription) {
598 return true;
599 }
600 }
601 return false;
602}
603
604#ifdef USE_LOGGER
605void APIServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
606 if (this->shutting_down_) {
607 // Don't try to send logs during shutdown
608 // as it could result in a recursion and
609 // we would be filling a buffer we are trying to clear
610 return;
611 }
612 for (auto &c : this->clients_) {
613 if (!c->flags_.remove && c->get_log_subscription_level() >= level)
614 c->try_send_log_message(level, tag, message, message_len);
615 }
616}
617#endif
618
619#ifdef USE_CAMERA
620void APIServer::on_camera_image(const std::shared_ptr<camera::CameraImage> &image) {
621 for (auto &c : this->clients_) {
622 if (!c->flags_.remove)
623 c->set_camera_state(image);
624 }
625}
626#endif
627
629 this->shutting_down_ = true;
630
631 // Close the listening socket to prevent new connections
632 this->destroy_socket_();
633
634 // Change batch delay to 5ms for quick flushing during shutdown
635 this->batch_delay_ = 5;
636
637 // Send disconnect requests to all connected clients
638 for (auto &c : this->clients_) {
640 if (!c->send_message(req)) {
641 // If we can't send the disconnect request directly (tx_buffer full),
642 // schedule it at the front of the batch so it will be sent with priority
643 c->schedule_message_front_(nullptr, DisconnectRequest::MESSAGE_TYPE, DisconnectRequest::ESTIMATED_SIZE);
644 }
645 }
646}
647
649 // If network is disconnected, no point trying to flush buffers
650 if (!network::is_connected()) {
651 return true;
652 }
653 this->loop();
654
655 // Return true only when all clients have been torn down
656 return this->clients_.empty();
657}
658
659#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
660// Timeout for action calls - matches aioesphomeapi client timeout (default 30s)
661// Can be overridden via USE_API_ACTION_CALL_TIMEOUT_MS define for testing
662#ifndef USE_API_ACTION_CALL_TIMEOUT_MS
663#define USE_API_ACTION_CALL_TIMEOUT_MS 30000 // NOLINT
664#endif
665
667 uint32_t action_call_id = this->next_action_call_id_++;
668 // Handle wraparound (skip 0 as it means "no call")
669 if (this->next_action_call_id_ == 0) {
670 this->next_action_call_id_ = 1;
671 }
672 this->active_action_calls_.push_back({action_call_id, client_call_id, conn});
673
674 // Schedule automatic cleanup after timeout (client will have given up by then)
675 // Uses numeric ID overload to avoid heap allocation from str_sprintf
676 this->set_timeout(action_call_id, USE_API_ACTION_CALL_TIMEOUT_MS, [this, action_call_id]() {
677 ESP_LOGD(TAG, "Action call %u timed out", action_call_id);
678 this->unregister_active_action_call(action_call_id);
679 });
680
681 return action_call_id;
682}
683
685 // Cancel the timeout for this action call (uses numeric ID overload)
686 this->cancel_timeout(action_call_id);
687
688 // Swap-and-pop is more efficient than remove_if for unordered vectors
689 for (size_t i = 0; i < this->active_action_calls_.size(); i++) {
690 if (this->active_action_calls_[i].action_call_id == action_call_id) {
691 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
692 this->active_action_calls_.pop_back();
693 return;
694 }
695 }
696}
697
699 // Remove all active action calls for disconnected connection using swap-and-pop
700 for (size_t i = 0; i < this->active_action_calls_.size();) {
701 if (this->active_action_calls_[i].connection == conn) {
702 // Cancel the timeout for this action call (uses numeric ID overload)
703 this->cancel_timeout(this->active_action_calls_[i].action_call_id);
704
705 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
706 this->active_action_calls_.pop_back();
707 // Don't increment i - need to check the swapped element
708 } else {
709 i++;
710 }
711 }
712}
713
714void APIServer::send_action_response(uint32_t action_call_id, bool success, StringRef error_message) {
715 for (auto &call : this->active_action_calls_) {
716 if (call.action_call_id == action_call_id) {
717 call.connection->send_execute_service_response(call.client_call_id, success, error_message);
718 return;
719 }
720 }
721 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
722}
723#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
724void APIServer::send_action_response(uint32_t action_call_id, bool success, StringRef error_message,
725 const uint8_t *response_data, size_t response_data_len) {
726 for (auto &call : this->active_action_calls_) {
727 if (call.action_call_id == action_call_id) {
728 call.connection->send_execute_service_response(call.client_call_id, success, error_message, response_data,
729 response_data_len);
730 return;
731 }
732 }
733 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
734}
735#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
736#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
737
738} // namespace esphome::api
739#endif
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:532
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:454
void status_clear_warning()
Definition component.h:306
static void register_controller(Controller *controller)
Register a controller to receive entity state updates.
bool is_internal() const
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
const psk_t & get_psk() const
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len)
bool is_connected_with_state_subscription() const
void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback)
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:277
void send_infrared_rf_receive_event(uint32_t device_id, uint32_t key, const std::vector< int32_t > *timings)
void add_state_subscription_(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f, bool once)
void get_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void on_camera_image(const std::shared_ptr< camera::CameraImage > &image) override
void socket_failed_(const LogString *msg)
void set_port(uint16_t port)
void dump_config() override
void unregister_active_action_calls_for_connection(APIConnection *conn)
void handle_disconnect(APIConnection *conn)
void set_batch_delay(uint16_t batch_delay)
void set_reboot_timeout(uint32_t reboot_timeout)
void send_action_response(uint32_t action_call_id, bool success, StringRef error_message)
bool save_noise_psk(psk_t psk, bool make_active=true)
void setup() override
bool teardown() override
APINoiseContext noise_ctx_
Definition api_server.h:319
void unregister_active_action_call(uint32_t action_call_id)
void send_homeassistant_action(const HomeassistantActionRequest &call)
socket::ListenSocket * socket_
Definition api_server.h:264
void on_event(event::Event *obj) override
void on_update(update::UpdateEntity *obj) override
std::vector< PendingActionResponse > action_response_callbacks_
Definition api_server.h:305
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void handle_action_response(uint32_t call_id, bool success, StringRef error_message)
std::function< void(const class ActionResponse &)> ActionResponseCallback
Definition api_server.h:138
bool update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg, const LogString *fail_log_msg, bool make_active)
ESPPreferenceObject noise_pref_
Definition api_server.h:320
Trigger< std::string, std::string > client_disconnected_trigger_
Definition api_server.h:269
std::vector< HomeAssistantStateSubscription > state_subs_
Definition api_server.h:284
bool clear_noise_psk(bool make_active=true)
uint16_t get_port() const
std::vector< ActiveActionCall > active_action_calls_
Definition api_server.h:296
void set_noise_psk(psk_t psk)
Definition api_server.h:74
float get_setup_priority() const override
uint32_t register_active_action_call(uint32_t client_call_id, APIConnection *conn)
void on_shutdown() override
void on_zwave_proxy_request(const ZWaveProxyRequest &msg)
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:425
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:426
Base class for all binary_sensor-type classes.
virtual void add_listener(CameraListener *listener)=0
Add a listener to receive camera events.
static Camera * instance()
The singleton instance of the camera implementation.
Definition camera.cpp:19
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:187
Base class for all cover devices.
Definition cover.h:110
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
Base class for all locks.
Definition lock.h:110
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:187
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:47
bool ready() const
Check if the socket has buffered data ready to read.
Definition socket.h:70
int bind(const struct sockaddr *addr, socklen_t addrlen)
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
std::unique_ptr< BSDSocketImpl > accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen)
Base class for all switches.
Definition switch.h:38
Base-class for all text inputs.
Definition text.h:21
Base class for all valve devices.
Definition valve.h:104
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
const char * message
Definition component.cpp:35
uint16_t addr_len
bool state
Definition fan.h:2
uint32_t socklen_t
Definition headers.h:99
APIServer * global_api_server
API_DISPATCH_UPDATE(binary_sensor::BinarySensor, binary_sensor) API_DISPATCH_UPDATE(cover
std::array< uint8_t, 32 > psk_t
Logger * global_logger
Definition logger.cpp:272
ESPHOME_ALWAYS_INLINE const char * get_use_address()
Get the active network hostname.
Definition util.h:57
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:27
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:52
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition socket.cpp:143
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:95
const char * tag
Definition log.h:74
ESPPreferences * global_preferences
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
bool sync()
Commit pending writes to flash.
Definition preferences.h:32
std::unique_ptr< std::string > entity_id_dynamic_storage
Definition api_server.h:201
std::unique_ptr< std::string > attribute_dynamic_storage
Definition api_server.h:202