ESPHome 2026.1.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 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_WATER_HEATER
340#endif
341
342#ifdef USE_EVENT
343// Event is a special case - unlike other entities with simple state fields,
344// events store their state in a member accessed via obj->get_last_event_type()
346 if (obj->is_internal())
347 return;
348 for (auto &c : this->clients_)
349 c->send_event(obj, obj->get_last_event_type());
350}
351#endif
352
353#ifdef USE_UPDATE
354// Update is a special case - the method is called on_update, not on_update_update
356 if (obj->is_internal())
357 return;
358 for (auto &c : this->clients_)
359 c->send_update_state(obj);
360}
361#endif
362
363#ifdef USE_ZWAVE_PROXY
365 // We could add code to manage a second subscription type, but, since this message type is
366 // very infrequent and small, we simply send it to all clients
367 for (auto &c : this->clients_)
368 c->send_message(msg, api::ZWaveProxyRequest::MESSAGE_TYPE);
369}
370#endif
371
372#ifdef USE_ALARM_CONTROL_PANEL
374#endif
375
377
378void APIServer::set_port(uint16_t port) { this->port_ = port; }
379
380#ifdef USE_API_PASSWORD
381void APIServer::set_password(const std::string &password) { this->password_ = password; }
382#endif
383
384void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; }
385
386#ifdef USE_API_HOMEASSISTANT_SERVICES
388 for (auto &client : this->clients_) {
389 client->send_homeassistant_action(call);
390 }
391}
392#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
394 this->action_response_callbacks_.push_back({call_id, std::move(callback)});
395}
396
397void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) {
398 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
399 if (it->call_id == call_id) {
400 auto callback = std::move(it->callback);
401 this->action_response_callbacks_.erase(it);
402 ActionResponse response(success, error_message);
403 callback(response);
404 return;
405 }
406 }
407}
408#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
409void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
410 const uint8_t *response_data, size_t response_data_len) {
411 for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
412 if (it->call_id == call_id) {
413 auto callback = std::move(it->callback);
414 this->action_response_callbacks_.erase(it);
415 ActionResponse response(success, error_message, response_data, response_data_len);
416 callback(response);
417 return;
418 }
419 }
420}
421#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
422#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
423#endif // USE_API_HOMEASSISTANT_SERVICES
424
425#ifdef USE_API_HOMEASSISTANT_STATES
426// Helper to add subscription (reduces duplication)
427void APIServer::add_state_subscription_(const char *entity_id, const char *attribute,
428 std::function<void(std::string)> f, bool once) {
430 .entity_id = entity_id, .attribute = attribute, .callback = std::move(f), .once = once,
431 // entity_id_dynamic_storage and attribute_dynamic_storage remain nullptr (no heap allocation)
432 });
433}
434
435// Helper to add subscription with heap-allocated strings (reduces duplication)
436void APIServer::add_state_subscription_(std::string entity_id, optional<std::string> attribute,
437 std::function<void(std::string)> f, bool once) {
439 // Allocate heap storage for the strings
440 sub.entity_id_dynamic_storage = std::make_unique<std::string>(std::move(entity_id));
441 sub.entity_id = sub.entity_id_dynamic_storage->c_str();
442
443 if (attribute.has_value()) {
444 sub.attribute_dynamic_storage = std::make_unique<std::string>(std::move(attribute.value()));
445 sub.attribute = sub.attribute_dynamic_storage->c_str();
446 } else {
447 sub.attribute = nullptr;
448 }
449
450 sub.callback = std::move(f);
451 sub.once = once;
452 this->state_subs_.push_back(std::move(sub));
453}
454
455// New const char* overload (for internal components - zero allocation)
456void APIServer::subscribe_home_assistant_state(const char *entity_id, const char *attribute,
457 std::function<void(std::string)> f) {
458 this->add_state_subscription_(entity_id, attribute, std::move(f), false);
459}
460
461void APIServer::get_home_assistant_state(const char *entity_id, const char *attribute,
462 std::function<void(std::string)> f) {
463 this->add_state_subscription_(entity_id, attribute, std::move(f), true);
464}
465
466// Existing std::string overload (for custom_api_device.h - heap allocation)
468 std::function<void(std::string)> f) {
469 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false);
470}
471
472void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
473 std::function<void(std::string)> f) {
474 this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true);
475}
476
477const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
478 return this->state_subs_;
479}
480#endif
481
482uint16_t APIServer::get_port() const { return this->port_; }
483
484void APIServer::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
485
486#ifdef USE_API_NOISE
487bool APIServer::update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg,
488 const LogString *fail_log_msg, const psk_t &active_psk, bool make_active) {
489 if (!this->noise_pref_.save(&new_psk)) {
490 ESP_LOGW(TAG, "%s", LOG_STR_ARG(fail_log_msg));
491 return false;
492 }
493 // ensure it's written immediately
494 if (!global_preferences->sync()) {
495 ESP_LOGW(TAG, "Failed to sync preferences");
496 return false;
497 }
498 ESP_LOGD(TAG, "%s", LOG_STR_ARG(save_log_msg));
499 if (make_active) {
500 this->set_timeout(100, [this, active_psk]() {
501 ESP_LOGW(TAG, "Disconnecting all clients to reset PSK");
502 this->set_noise_psk(active_psk);
503 for (auto &c : this->clients_) {
505 c->send_message(req, DisconnectRequest::MESSAGE_TYPE);
506 }
507 });
508 }
509 return true;
510}
511
512bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
513#ifdef USE_API_NOISE_PSK_FROM_YAML
514 // When PSK is set from YAML, this function should never be called
515 // but if it is, reject the change
516 ESP_LOGW(TAG, "Key set in YAML");
517 return false;
518#else
519 auto &old_psk = this->noise_ctx_.get_psk();
520 if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
521 ESP_LOGW(TAG, "New PSK matches old");
522 return true;
523 }
524
525 SavedNoisePsk new_saved_psk{psk};
526 return this->update_noise_psk_(new_saved_psk, LOG_STR("Noise PSK saved"), LOG_STR("Failed to save Noise PSK"), psk,
527 make_active);
528#endif
529}
530bool APIServer::clear_noise_psk(bool make_active) {
531#ifdef USE_API_NOISE_PSK_FROM_YAML
532 // When PSK is set from YAML, this function should never be called
533 // but if it is, reject the change
534 ESP_LOGW(TAG, "Key set in YAML");
535 return false;
536#else
537 SavedNoisePsk empty_psk{};
538 psk_t empty{};
539 return this->update_noise_psk_(empty_psk, LOG_STR("Noise PSK cleared"), LOG_STR("Failed to clear Noise PSK"), empty,
540 make_active);
541#endif
542}
543#endif
544
545#ifdef USE_HOMEASSISTANT_TIME
547 for (auto &client : this->clients_) {
548 if (!client->flags_.remove && client->is_authenticated())
549 client->send_time_request();
550 }
551}
552#endif
553
554bool APIServer::is_connected(bool state_subscription_only) const {
555 if (!state_subscription_only) {
556 return !this->clients_.empty();
557 }
558
559 for (const auto &client : this->clients_) {
560 if (client->flags_.state_subscription) {
561 return true;
562 }
563 }
564 return false;
565}
566
567#ifdef USE_LOGGER
568void APIServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
569 if (this->shutting_down_) {
570 // Don't try to send logs during shutdown
571 // as it could result in a recursion and
572 // we would be filling a buffer we are trying to clear
573 return;
574 }
575 for (auto &c : this->clients_) {
576 if (!c->flags_.remove && c->get_log_subscription_level() >= level)
577 c->try_send_log_message(level, tag, message, message_len);
578 }
579}
580#endif
581
582#ifdef USE_CAMERA
583void APIServer::on_camera_image(const std::shared_ptr<camera::CameraImage> &image) {
584 for (auto &c : this->clients_) {
585 if (!c->flags_.remove)
586 c->set_camera_state(image);
587 }
588}
589#endif
590
592 this->shutting_down_ = true;
593
594 // Close the listening socket to prevent new connections
595 if (this->socket_) {
596 this->socket_->close();
597 this->socket_ = nullptr;
598 }
599
600 // Change batch delay to 5ms for quick flushing during shutdown
601 this->batch_delay_ = 5;
602
603 // Send disconnect requests to all connected clients
604 for (auto &c : this->clients_) {
606 if (!c->send_message(req, DisconnectRequest::MESSAGE_TYPE)) {
607 // If we can't send the disconnect request directly (tx_buffer full),
608 // schedule it at the front of the batch so it will be sent with priority
611 }
612 }
613}
614
616 // If network is disconnected, no point trying to flush buffers
617 if (!network::is_connected()) {
618 return true;
619 }
620 this->loop();
621
622 // Return true only when all clients have been torn down
623 return this->clients_.empty();
624}
625
626#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
627// Timeout for action calls - matches aioesphomeapi client timeout (default 30s)
628// Can be overridden via USE_API_ACTION_CALL_TIMEOUT_MS define for testing
629#ifndef USE_API_ACTION_CALL_TIMEOUT_MS
630#define USE_API_ACTION_CALL_TIMEOUT_MS 30000 // NOLINT
631#endif
632
633uint32_t APIServer::register_active_action_call(uint32_t client_call_id, APIConnection *conn) {
634 uint32_t action_call_id = this->next_action_call_id_++;
635 // Handle wraparound (skip 0 as it means "no call")
636 if (this->next_action_call_id_ == 0) {
637 this->next_action_call_id_ = 1;
638 }
639 this->active_action_calls_.push_back({action_call_id, client_call_id, conn});
640
641 // Schedule automatic cleanup after timeout (client will have given up by then)
642 this->set_timeout(str_sprintf("action_call_%u", action_call_id), USE_API_ACTION_CALL_TIMEOUT_MS,
643 [this, action_call_id]() {
644 ESP_LOGD(TAG, "Action call %u timed out", action_call_id);
645 this->unregister_active_action_call(action_call_id);
646 });
647
648 return action_call_id;
649}
650
651void APIServer::unregister_active_action_call(uint32_t action_call_id) {
652 // Cancel the timeout for this action call
653 this->cancel_timeout(str_sprintf("action_call_%u", action_call_id));
654
655 // Swap-and-pop is more efficient than remove_if for unordered vectors
656 for (size_t i = 0; i < this->active_action_calls_.size(); i++) {
657 if (this->active_action_calls_[i].action_call_id == action_call_id) {
658 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
659 this->active_action_calls_.pop_back();
660 return;
661 }
662 }
663}
664
666 // Remove all active action calls for disconnected connection using swap-and-pop
667 for (size_t i = 0; i < this->active_action_calls_.size();) {
668 if (this->active_action_calls_[i].connection == conn) {
669 // Cancel the timeout for this action call
670 this->cancel_timeout(str_sprintf("action_call_%u", this->active_action_calls_[i].action_call_id));
671
672 std::swap(this->active_action_calls_[i], this->active_action_calls_.back());
673 this->active_action_calls_.pop_back();
674 // Don't increment i - need to check the swapped element
675 } else {
676 i++;
677 }
678 }
679}
680
681void APIServer::send_action_response(uint32_t action_call_id, bool success, const std::string &error_message) {
682 for (auto &call : this->active_action_calls_) {
683 if (call.action_call_id == action_call_id) {
684 call.connection->send_execute_service_response(call.client_call_id, success, error_message);
685 return;
686 }
687 }
688 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
689}
690#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
691void APIServer::send_action_response(uint32_t action_call_id, bool success, const std::string &error_message,
692 const uint8_t *response_data, size_t response_data_len) {
693 for (auto &call : this->active_action_calls_) {
694 if (call.action_call_id == action_call_id) {
695 call.connection->send_execute_service_response(call.client_call_id, success, error_message, response_data,
696 response_data_len);
697 return;
698 }
699 }
700 ESP_LOGW(TAG, "Cannot send response: no active call found for action_call_id %u", action_call_id);
701}
702#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
703#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
704
705} // namespace esphome::api
706#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:64
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:258
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:299
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:285
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition api_server.h:250
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:262
std::function< void(const class ActionResponse &)> ActionResponseCallback
Definition api_server.h:144
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:300
std::vector< HomeAssistantStateSubscription > state_subs_
Definition api_server.h:264
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:276
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:245
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:433
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:434
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:3142
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:181
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:207
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:21
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:68
ESPPreferences * global_preferences
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:220
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:203
std::unique_ptr< std::string > attribute_dynamic_storage
Definition api_server.h:204
uint16_t length
Definition tt21100.cpp:0