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