ESPHome 2025.12.2
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 global_api_server = this;
33 // Pre-allocate shared write buffer
34 shared_write_buffer_.reserve(64);
35}
36
39
40#ifdef USE_API_NOISE
41 uint32_t hash = 88491486UL;
42
44
45#ifndef USE_API_NOISE_PSK_FROM_YAML
46 // Only load saved PSK if not set from YAML
47 SavedNoisePsk noise_pref_saved{};
48 if (this->noise_pref_.load(&noise_pref_saved)) {
49 ESP_LOGD(TAG, "Loaded saved Noise PSK");
50 this->set_noise_psk(noise_pref_saved.psk);
51 }
52#endif
53#endif
54
55 this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
56 if (this->socket_ == nullptr) {
57 ESP_LOGW(TAG, "Could not create socket");
58 this->mark_failed();
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 unable to set reuseaddr: errno %d", err);
65 // we can still continue
66 }
67 err = this->socket_->setblocking(false);
68 if (err != 0) {
69 ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err);
70 this->mark_failed();
71 return;
72 }
73
74 struct sockaddr_storage server;
75
76 socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
77 if (sl == 0) {
78 ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno);
79 this->mark_failed();
80 return;
81 }
82
83 err = this->socket_->bind((struct sockaddr *) &server, sl);
84 if (err != 0) {
85 ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno);
86 this->mark_failed();
87 return;
88 }
89
90 err = this->socket_->listen(this->listen_backlog_);
91 if (err != 0) {
92 ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno);
93 this->mark_failed();
94 return;
95 }
96
97#ifdef USE_LOGGER
98 if (logger::global_logger != nullptr) {
100 }
101#endif
102
103#ifdef USE_CAMERA
104 if (camera::Camera::instance() != nullptr && !camera::Camera::instance()->is_internal()) {
106 }
107#endif
108
109 // Initialize last_connected_ for reboot timeout tracking
111 // Set warning status if reboot timeout is enabled
112 if (this->reboot_timeout_ != 0) {
113 this->status_set_warning();
114 }
115}
116
118 // Accept new clients only if the socket exists and has incoming connections
119 if (this->socket_ && this->socket_->ready()) {
120 while (true) {
121 struct sockaddr_storage source_addr;
122 socklen_t addr_len = sizeof(source_addr);
123
124 auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
125 if (!sock)
126 break;
127
128 // Check if we're at the connection limit
129 if (this->clients_.size() >= this->max_connections_) {
130 ESP_LOGW(TAG, "Max connections (%d), rejecting %s", this->max_connections_, sock->getpeername().c_str());
131 // Immediately close - socket destructor will handle cleanup
132 sock.reset();
133 continue;
134 }
135
136 ESP_LOGD(TAG, "Accept %s", sock->getpeername().c_str());
137
138 auto *conn = new APIConnection(std::move(sock), this);
139 this->clients_.emplace_back(conn);
140 conn->start();
141
142 // First client connected - clear warning and update timestamp
143 if (this->clients_.size() == 1 && this->reboot_timeout_ != 0) {
144 this->status_clear_warning();
146 }
147 }
148 }
149
150 if (this->clients_.empty()) {
151 // Check reboot timeout - done in loop to avoid scheduler heap churn
152 // (cancelled scheduler items sit in heap memory until their scheduled time)
153 if (this->reboot_timeout_ != 0) {
154 const uint32_t now = App.get_loop_component_start_time();
155 if (now - this->last_connected_ > this->reboot_timeout_) {
156 ESP_LOGE(TAG, "No clients; rebooting");
157 App.reboot();
158 }
159 }
160 return;
161 }
162
163 // Process clients and remove disconnected ones in a single pass
164 // Check network connectivity once for all clients
165 if (!network::is_connected()) {
166 // Network is down - disconnect all clients
167 for (auto &client : this->clients_) {
168 client->on_fatal_error();
169 ESP_LOGW(TAG, "%s (%s): Network down; disconnect", client->client_info_.name.c_str(),
170 client->client_info_.peername.c_str());
171 }
172 // Continue to process and clean up the clients below
173 }
174
175 size_t client_index = 0;
176 while (client_index < this->clients_.size()) {
177 auto &client = this->clients_[client_index];
178
179 if (!client->flags_.remove) {
180 // Common case: process active client
181 client->loop();
182 client_index++;
183 continue;
184 }
185
186 // Rare case: handle disconnection
187#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
188 this->client_disconnected_trigger_->trigger(client->client_info_.name, client->client_info_.peername);
189#endif
190#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
192#endif
193 ESP_LOGV(TAG, "Remove connection %s", client->client_info_.name.c_str());
194
195 // Swap with the last element and pop (avoids expensive vector shifts)
196 if (client_index < this->clients_.size() - 1) {
197 std::swap(this->clients_[client_index], this->clients_.back());
198 }
199 this->clients_.pop_back();
200
201 // Last client disconnected - set warning and start tracking for reboot timeout
202 if (this->clients_.empty() && this->reboot_timeout_ != 0) {
203 this->status_set_warning();
205 }
206 // Don't increment client_index since we need to process the swapped element
207 }
208}
209
211 ESP_LOGCONFIG(TAG,
212 "Server:\n"
213 " Address: %s:%u\n"
214 " Listen backlog: %u\n"
215 " Max connections: %u",
217#ifdef USE_API_NOISE
218 ESP_LOGCONFIG(TAG, " Noise encryption: %s", YESNO(this->noise_ctx_.has_psk()));
219 if (!this->noise_ctx_.has_psk()) {
220 ESP_LOGCONFIG(TAG, " Supports encryption: YES");
221 }
222#else
223 ESP_LOGCONFIG(TAG, " Noise encryption: NO");
224#endif
225}
226
227#ifdef USE_API_PASSWORD
228bool APIServer::check_password(const uint8_t *password_data, size_t password_len) const {
229 // depend only on input password length
230 const char *a = this->password_.c_str();
231 uint32_t len_a = this->password_.length();
232 const char *b = reinterpret_cast<const char *>(password_data);
233 uint32_t len_b = password_len;
234
235 // disable optimization with volatile
236 volatile uint32_t length = len_b;
237 volatile const char *left = nullptr;
238 volatile const char *right = b;
239 uint8_t result = 0;
240
241 if (len_a == length) {
242 left = *((volatile const char **) &a);
243 result = 0;
244 }
245 if (len_a != length) {
246 left = b;
247 result = 1;
248 }
249
250 for (size_t i = 0; i < length; i++) {
251 result |= *left++ ^ *right++; // NOLINT
252 }
253
254 return result == 0;
255}
256
257#endif
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->clients_) \
267 c->send_##entity_name##_state(obj); \
268 }
269
270#ifdef USE_BINARY_SENSOR
272#endif
273
274#ifdef USE_COVER
276#endif
277
278#ifdef USE_FAN
280#endif
281
282#ifdef USE_LIGHT
284#endif
285
286#ifdef USE_SENSOR
288#endif
289
290#ifdef USE_SWITCH
292#endif
293
294#ifdef USE_TEXT_SENSOR
296#endif
297
298#ifdef USE_CLIMATE
300#endif
301
302#ifdef USE_NUMBER
304#endif
305
306#ifdef USE_DATETIME_DATE
308#endif
309
310#ifdef USE_DATETIME_TIME
312#endif
313
314#ifdef USE_DATETIME_DATETIME
316#endif
317
318#ifdef USE_TEXT
320#endif
321
322#ifdef USE_SELECT
324#endif
325
326#ifdef USE_LOCK
328#endif
329
330#ifdef USE_VALVE
332#endif
333
334#ifdef USE_MEDIA_PLAYER
336#endif
337
338#ifdef USE_EVENT
339// Event is a special case - unlike other entities with simple state fields,
340// events store their state in a member accessed via obj->get_last_event_type()
342 if (obj->is_internal())
343 return;
344 for (auto &c : this->clients_)
345 c->send_event(obj, obj->get_last_event_type());
346}
347#endif
348
349#ifdef USE_UPDATE
350// Update is a special case - the method is called on_update, not on_update_update
352 if (obj->is_internal())
353 return;
354 for (auto &c : this->clients_)
355 c->send_update_state(obj);
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, api::ZWaveProxyRequest::MESSAGE_TYPE);
365}
366#endif
367
368#ifdef USE_ALARM_CONTROL_PANEL
370#endif
371
373
374void APIServer::set_port(uint16_t port) { this->port_ = port; }
375
376#ifdef USE_API_PASSWORD
377void APIServer::set_password(const std::string &password) { this->password_ = password; }
378#endif
379
380void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; }
381
382#ifdef USE_API_HOMEASSISTANT_SERVICES
384 for (auto &client : this->clients_) {
385 client->send_homeassistant_action(call);
386 }
387}
388#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
390 this->action_response_callbacks_.push_back({call_id, std::move(callback)});
391}
392
393void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) {
394 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
395 if (it->call_id == call_id) {
396 auto callback = std::move(it->callback);
397 this->action_response_callbacks_.erase(it);
398 ActionResponse response(success, error_message);
399 callback(response);
400 return;
401 }
402 }
403}
404#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
405void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
406 const uint8_t *response_data, size_t response_data_len) {
407 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
408 if (it->call_id == call_id) {
409 auto callback = std::move(it->callback);
410 this->action_response_callbacks_.erase(it);
411 ActionResponse response(success, error_message, response_data, response_data_len);
412 callback(response);
413 return;
414 }
415 }
416}
417#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
418#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
419#endif // USE_API_HOMEASSISTANT_SERVICES
420
421#ifdef USE_API_HOMEASSISTANT_STATES
422// Helper to add subscription (reduces duplication)
423void APIServer::add_state_subscription_(const char *entity_id, const char *attribute,
424 std::function<void(std::string)> f, bool once) {
426 .entity_id = entity_id, .attribute = attribute, .callback = std::move(f), .once = once,
427 // entity_id_dynamic_storage and attribute_dynamic_storage remain nullptr (no heap allocation)
428 });
429}
430
431// Helper to add subscription with heap-allocated strings (reduces duplication)
432void APIServer::add_state_subscription_(std::string entity_id, optional<std::string> attribute,
433 std::function<void(std::string)> f, bool once) {
435 // Allocate heap storage for the strings
436 sub.entity_id_dynamic_storage = std::make_unique<std::string>(std::move(entity_id));
437 sub.entity_id = sub.entity_id_dynamic_storage->c_str();
438
439 if (attribute.has_value()) {
440 sub.attribute_dynamic_storage = std::make_unique<std::string>(std::move(attribute.value()));
441 sub.attribute = sub.attribute_dynamic_storage->c_str();
442 } else {
443 sub.attribute = nullptr;
444 }
445
446 sub.callback = std::move(f);
447 sub.once = once;
448 this->state_subs_.push_back(std::move(sub));
449}
450
451// New const char* overload (for internal components - zero allocation)
452void APIServer::subscribe_home_assistant_state(const char *entity_id, const char *attribute,
453 std::function<void(std::string)> f) {
454 this->add_state_subscription_(entity_id, attribute, std::move(f), false);
455}
456
457void APIServer::get_home_assistant_state(const char *entity_id, const char *attribute,
458 std::function<void(std::string)> f) {
459 this->add_state_subscription_(entity_id, attribute, std::move(f), true);
460}
461
462// Existing std::string overload (for custom_api_device.h - heap allocation)
464 std::function<void(std::string)> f) {
465 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false);
466}
467
468void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
469 std::function<void(std::string)> f) {
470 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true);
471}
472
473const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
474 return this->state_subs_;
475}
476#endif
477
478uint16_t APIServer::get_port() const { return this->port_; }
479
480void APIServer::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
481
482#ifdef USE_API_NOISE
483bool APIServer::update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg,
484 const LogString *fail_log_msg, const psk_t &active_psk, bool make_active) {
485 if (!this->noise_pref_.save(&new_psk)) {
486 ESP_LOGW(TAG, "%s", LOG_STR_ARG(fail_log_msg));
487 return false;
488 }
489 // ensure it's written immediately
490 if (!global_preferences->sync()) {
491 ESP_LOGW(TAG, "Failed to sync preferences");
492 return false;
493 }
494 ESP_LOGD(TAG, "%s", LOG_STR_ARG(save_log_msg));
495 if (make_active) {
496 this->set_timeout(100, [this, active_psk]() {
497 ESP_LOGW(TAG, "Disconnecting all clients to reset PSK");
498 this->set_noise_psk(active_psk);
499 for (auto &c : this->clients_) {
501 c->send_message(req, DisconnectRequest::MESSAGE_TYPE);
502 }
503 });
504 }
505 return true;
506}
507
508bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
509#ifdef USE_API_NOISE_PSK_FROM_YAML
510 // When PSK is set from YAML, this function should never be called
511 // but if it is, reject the change
512 ESP_LOGW(TAG, "Key set in YAML");
513 return false;
514#else
515 auto &old_psk = this->noise_ctx_.get_psk();
516 if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
517 ESP_LOGW(TAG, "New PSK matches old");
518 return true;
519 }
520
521 SavedNoisePsk new_saved_psk{psk};
522 return this->update_noise_psk_(new_saved_psk, LOG_STR("Noise PSK saved"), LOG_STR("Failed to save Noise PSK"), psk,
523 make_active);
524#endif
525}
526bool APIServer::clear_noise_psk(bool make_active) {
527#ifdef USE_API_NOISE_PSK_FROM_YAML
528 // When PSK is set from YAML, this function should never be called
529 // but if it is, reject the change
530 ESP_LOGW(TAG, "Key set in YAML");
531 return false;
532#else
533 SavedNoisePsk empty_psk{};
534 psk_t empty{};
535 return this->update_noise_psk_(empty_psk, LOG_STR("Noise PSK cleared"), LOG_STR("Failed to clear Noise PSK"), empty,
536 make_active);
537#endif
538}
539#endif
540
541#ifdef USE_HOMEASSISTANT_TIME
543 for (auto &client : this->clients_) {
544 if (!client->flags_.remove && client->is_authenticated())
545 client->send_time_request();
546 }
547}
548#endif
549
550bool APIServer::is_connected(bool state_subscription_only) const {
551 if (!state_subscription_only) {
552 return !this->clients_.empty();
553 }
554
555 for (const auto &client : this->clients_) {
556 if (client->flags_.state_subscription) {
557 return true;
558 }
559 }
560 return false;
561}
562
563#ifdef USE_LOGGER
564void APIServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
565 if (this->shutting_down_) {
566 // Don't try to send logs during shutdown
567 // as it could result in a recursion and
568 // we would be filling a buffer we are trying to clear
569 return;
570 }
571 for (auto &c : this->clients_) {
572 if (!c->flags_.remove && c->get_log_subscription_level() >= level)
573 c->try_send_log_message(level, tag, message, message_len);
574 }
575}
576#endif
577
578#ifdef USE_CAMERA
579void APIServer::on_camera_image(const std::shared_ptr<camera::CameraImage> &image) {
580 for (auto &c : this->clients_) {
581 if (!c->flags_.remove)
582 c->set_camera_state(image);
583 }
584}
585#endif
586
588 this->shutting_down_ = true;
589
590 // Close the listening socket to prevent new connections
591 if (this->socket_) {
592 this->socket_->close();
593 this->socket_ = nullptr;
594 }
595
596 // Change batch delay to 5ms for quick flushing during shutdown
597 this->batch_delay_ = 5;
598
599 // Send disconnect requests to all connected clients
600 for (auto &c : this->clients_) {
602 if (!c->send_message(req, DisconnectRequest::MESSAGE_TYPE)) {
603 // If we can't send the disconnect request directly (tx_buffer full),
604 // schedule it at the front of the batch so it will be sent with priority
607 }
608 }
609}
610
612 // If network is disconnected, no point trying to flush buffers
613 if (!network::is_connected()) {
614 return true;
615 }
616 this->loop();
617
618 // Return true only when all clients have been torn down
619 return this->clients_.empty();
620}
621
622#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
623// Timeout for action calls - matches aioesphomeapi client timeout (default 30s)
624// Can be overridden via USE_API_ACTION_CALL_TIMEOUT_MS define for testing
625#ifndef USE_API_ACTION_CALL_TIMEOUT_MS
626#define USE_API_ACTION_CALL_TIMEOUT_MS 30000 // NOLINT
627#endif
628
629uint32_t APIServer::register_active_action_call(uint32_t client_call_id, APIConnection *conn) {
630 uint32_t action_call_id = this->next_action_call_id_++;
631 // Handle wraparound (skip 0 as it means "no call")
632 if (this->next_action_call_id_ == 0) {
633 this->next_action_call_id_ = 1;
634 }
635 this->active_action_calls_.push_back({action_call_id, client_call_id, conn});
636
637 // Schedule automatic cleanup after timeout (client will have given up by then)
638 this->set_timeout(str_sprintf("action_call_%u", action_call_id), USE_API_ACTION_CALL_TIMEOUT_MS,
639 [this, action_call_id]() {
640 ESP_LOGD(TAG, "Action call %u timed out", action_call_id);
641 this->unregister_active_action_call(action_call_id);
642 });
643
644 return action_call_id;
645}
646
647void APIServer::unregister_active_action_call(uint32_t action_call_id) {
648 // Cancel the timeout for this action call
649 this->cancel_timeout(str_sprintf("action_call_%u", action_call_id));
650
651 // Swap-and-pop is more efficient than remove_if for unordered vectors
652 for (size_t i = 0; i < this->active_action_calls_.size(); i++) {
653 if (this->active_action_calls_[i].action_call_id == action_call_id) {
654 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
655 this->active_action_calls_.pop_back();
656 return;
657 }
658 }
659}
660
662 // Remove all active action calls for disconnected connection using swap-and-pop
663 for (size_t i = 0; i < this->active_action_calls_.size();) {
664 if (this->active_action_calls_[i].connection == conn) {
665 // Cancel the timeout for this action call
666 this->cancel_timeout(str_sprintf("action_call_%u", this->active_action_calls_[i].action_call_id));
667
668 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
669 this->active_action_calls_.pop_back();
670 // Don't increment i - need to check the swapped element
671 } else {
672 i++;
673 }
674 }
675}
676
677void APIServer::send_action_response(uint32_t action_call_id, bool success, const std::string &error_message) {
678 for (auto &call : this->active_action_calls_) {
679 if (call.action_call_id == action_call_id) {
680 call.connection->send_execute_service_response(call.client_call_id, success, error_message);
681 return;
682 }
683 }
684 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
685}
686#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
687void APIServer::send_action_response(uint32_t action_call_id, bool success, const std::string &error_message,
688 const uint8_t *response_data, size_t response_data_len) {
689 for (auto &call : this->active_action_calls_) {
690 if (call.action_call_id == action_call_id) {
691 call.connection->send_execute_service_response(call.client_call_id, success, error_message, response_data,
692 response_data_len);
693 return;
694 }
695 }
696 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
697}
698#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
699#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
700
701} // namespace esphome::api
702#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.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
static void register_controller(Controller *controller)
Register a controller to receive entity state updates.
bool save(const T *src)
Definition preferences.h:21
virtual bool sync()=0
Commit pending writes to flash.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
bool is_internal() const
Definition entity_base.h:51
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
const psk_t & get_psk() const
void get_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(std::string)> f)
void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback)
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:255
void set_password(const std::string &password)
void on_camera_image(const std::shared_ptr< camera::CameraImage > &image) override
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)
bool save_noise_psk(psk_t psk, bool make_active=true)
void setup() override
void handle_action_response(uint32_t call_id, bool success, const std::string &error_message)
bool teardown() override
APINoiseContext noise_ctx_
Definition api_server.h:296
void unregister_active_action_call(uint32_t action_call_id)
void send_homeassistant_action(const HomeassistantActionRequest &call)
void on_event(event::Event *obj) override
void on_update(update::UpdateEntity *obj) override
bool check_password(const uint8_t *password_data, size_t password_len) const
std::vector< PendingActionResponse > action_response_callbacks_
Definition api_server.h:282
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition api_server.h:247
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:259
std::function< void(const class ActionResponse &)> ActionResponseCallback
Definition api_server.h:141
bool update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg, const LogString *fail_log_msg, const psk_t &active_psk, bool make_active)
bool is_connected(bool state_subscription_only=false) const
ESPPreferenceObject noise_pref_
Definition api_server.h:297
std::vector< HomeAssistantStateSubscription > state_subs_
Definition api_server.h:261
void on_zwave_proxy_request(const esphome::api::ProtoMessage &msg)
bool clear_noise_psk(bool make_active=true)
uint16_t get_port() const
std::vector< ActiveActionCall > active_action_calls_
Definition api_server.h:273
void set_noise_psk(psk_t psk)
Definition api_server.h:80
void send_action_response(uint32_t action_call_id, bool success, const std::string &error_message)
void add_state_subscription_(const char *entity_id, const char *attribute, std::function< void(std::string)> f, bool once)
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:242
uint32_t register_active_action_call(uint32_t client_call_id, APIConnection *conn)
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(std::string)> f)
void on_shutdown() override
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len) override
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:414
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:415
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:3052
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:177
Base class for all cover devices.
Definition cover.h:112
const char * get_last_event_type() const
Return the last triggered event type (pointer to string in types_), or nullptr if no event triggered ...
Definition event.h:48
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
Base class for all locks.
Definition lock.h:111
void add_log_listener(LogListener *listener)
Register a log listener to receive log messages.
Definition logger.h:218
Base-class for all numbers.
Definition number.h:29
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
Base-class for all selects.
Definition select.h:30
Base-class for all sensors.
Definition sensor.h:43
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:24
Base class for all valve devices.
Definition valve.h:106
const char * message
Definition component.cpp:38
uint16_t addr_len
uint32_t socklen_t
Definition headers.h:97
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:297
const char * get_use_address()
Get the active network hostname.
Definition util.cpp:88
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.cpp:26
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:88
std::unique_ptr< Socket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:44
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:91
ESPPreferences * global_preferences
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:222
Application App
Global storage of Application pointer - only one Application can exist.
std::unique_ptr< std::string > entity_id_dynamic_storage
Definition api_server.h:200
std::unique_ptr< std::string > attribute_dynamic_storage
Definition api_server.h:201
uint16_t length
Definition tt21100.cpp:0