ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
api_connection.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_API
5#include "api_frame_helper.h"
6#ifdef USE_API_NOISE
8#endif
9#ifdef USE_API_PLAINTEXT
11#endif
12#include "api_pb2.h"
13#include "api_pb2_service.h"
14#include "api_server.h"
17#ifdef USE_ESP32_CRASH_HANDLER
19#endif
20#ifdef USE_RP2040_CRASH_HANDLER
22#endif
23#ifdef USE_ESP8266_CRASH_HANDLER
25#endif
28
29#include <functional>
30#include <limits>
31#include <vector>
32
33namespace esphome {
34class ComponentIterator;
35} // namespace esphome
36
37namespace esphome::api {
38
39// Keepalive timeout in milliseconds
40static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
41// Maximum number of entities to process in a single batch during initial state/info sending
42// API 1.14+ clients compute object_id client-side, so messages are smaller and we can fit more per batch
43// TODO: Remove MAX_INITIAL_PER_BATCH_LEGACY before 2026.7.0 - all clients should support API 1.14 by then
44static constexpr size_t MAX_INITIAL_PER_BATCH_LEGACY = 24; // For clients < API 1.14 (includes object_id)
45static constexpr size_t MAX_INITIAL_PER_BATCH = 34; // For clients >= API 1.14 (no object_id)
46// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch
47static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_PER_BATCH,
48 "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_PER_BATCH");
49
50#ifdef USE_BENCHMARK
51class APIConnection;
55#endif
56
58 public:
59 friend class APIServer;
61#ifdef USE_BENCHMARK
63 friend void bench_clear_batch(APIConnection *conn);
65#endif
66 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
68
69 void start();
70 void loop();
71
72 protected:
73 // read_message_ is defined here (instead of in APIServerConnectionBase) so the
74 // compiler can devirtualize and inline on_* handler calls within this final class.
75 void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data);
76
77 // Auth helpers defined here (not in ProtoService) so the compiler can
78 // devirtualize is_connection_setup()/on_no_setup_connection() calls
79 // within this final class.
81 if (!this->is_connection_setup()) {
83 return false;
84 }
85 return true;
86 }
87 inline bool check_authenticated_() { return this->check_connection_setup_(); }
88
89 public:
94#ifdef USE_BINARY_SENSOR
96#endif
97#ifdef USE_COVER
98 bool send_cover_state(cover::Cover *cover);
100#endif
101#ifdef USE_FAN
102 bool send_fan_state(fan::Fan *fan);
104#endif
105#ifdef USE_LIGHT
108#endif
109#ifdef USE_SENSOR
110 bool send_sensor_state(sensor::Sensor *sensor);
111#endif
112#ifdef USE_SWITCH
113 bool send_switch_state(switch_::Switch *a_switch);
115#endif
116#ifdef USE_TEXT_SENSOR
118#endif
119#ifdef USE_CAMERA
120 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
122#endif
123#ifdef USE_CLIMATE
126#endif
127#ifdef USE_NUMBER
128 bool send_number_state(number::Number *number);
130#endif
131#ifdef USE_DATETIME_DATE
134#endif
135#ifdef USE_DATETIME_TIME
138#endif
139#ifdef USE_DATETIME_DATETIME
142#endif
143#ifdef USE_TEXT
144 bool send_text_state(text::Text *text);
146#endif
147#ifdef USE_SELECT
148 bool send_select_state(select::Select *select);
150#endif
151#ifdef USE_BUTTON
153#endif
154#ifdef USE_LOCK
155 bool send_lock_state(lock::Lock *a_lock);
157#endif
158#ifdef USE_VALVE
159 bool send_valve_state(valve::Valve *valve);
161#endif
162#ifdef USE_MEDIA_PLAYER
165#endif
166 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
167#ifdef USE_API_HOMEASSISTANT_SERVICES
170 return;
171 this->send_message(call);
172 }
173#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
175#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
176#endif // USE_API_HOMEASSISTANT_SERVICES
177#ifdef USE_BLUETOOTH_PROXY
180
191
192#endif
193#ifdef USE_HOMEASSISTANT_TIME
195 GetTimeRequest req;
196 this->send_message(req);
197 }
198#endif
199
200#ifdef USE_VOICE_ASSISTANT
209#endif
210
211#ifdef USE_ZWAVE_PROXY
212 void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg);
214#endif
215
216#ifdef USE_ALARM_CONTROL_PANEL
219#endif
220
221#ifdef USE_WATER_HEATER
224#endif
225
226#ifdef USE_IR_RF
229#endif
230
231#ifdef USE_SERIAL_PROXY
238#endif
239
240#ifdef USE_EVENT
241 void send_event(event::Event *event);
242#endif
243
244#ifdef USE_UPDATE
247#endif
248
251 // we initiated ping
252 this->flags_.sent_ping = false;
253 }
254#ifdef USE_API_HOMEASSISTANT_STATES
256#endif
257#ifdef USE_HOMEASSISTANT_TIME
258 void on_get_time_response(const GetTimeResponse &value);
259#endif
260 void on_hello_request(const HelloRequest &msg);
262 void on_ping_request();
266 this->flags_.state_subscription = true;
267 // Start initial state iterator only if no iterator is active
268 // If list_entities is running, we'll start initial_state when it completes
271 }
272 }
274 this->flags_.log_subscription = msg.level;
275 if (msg.dump_config)
277#ifdef USE_ESP32_CRASH_HANDLER
280#endif
281#ifdef USE_RP2040_CRASH_HANDLER
283#endif
284#ifdef USE_ESP8266_CRASH_HANDLER
286#endif
287 }
288#ifdef USE_API_HOMEASSISTANT_SERVICES
290#endif
291#ifdef USE_API_HOMEASSISTANT_STATES
293#endif
294#ifdef USE_API_USER_DEFINED_ACTIONS
296#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
297 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
298#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
299 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
300 const uint8_t *response_data, size_t response_data_len);
301#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
302#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
303#endif
304#ifdef USE_API_NOISE
306#endif
307
313 this->is_authenticated();
314 }
315 bool is_marked_for_removal() const { return this->flags_.remove; }
316 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
317
318 // Get client API version for feature detection
319 bool client_supports_api_version(uint16_t major, uint16_t minor) const {
320 return this->client_api_version_major_ > major ||
321 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
322 }
323
324 void on_fatal_error();
326
327 // Function pointer type for type-erased message encoding
328 using MessageEncodeFn = uint8_t *(*) (const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM);
329 // Function pointer type for type-erased size calculation
330 using CalculateSizeFn = uint32_t (*)(const void *);
331
332 template<typename T> bool send_message(const T &msg) {
333 if constexpr (T::ESTIMATED_SIZE == 0) {
334 return this->send_message_(0, T::MESSAGE_TYPE, &encode_msg_noop, &msg);
335 } else {
336 return this->send_message_(msg.calculate_size(), T::MESSAGE_TYPE, &proto_encode_msg<T>, &msg);
337 }
338 }
339
340 void prepare_first_message_buffer(APIBuffer &shared_buf, size_t header_padding, size_t total_size) {
341 shared_buf.clear();
342 // Reserve space for header padding + message + footer
343 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
344 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
345 // Reserve full size but only set initial size to header padding
346 // so message encoding starts at the correct position
347 shared_buf.reserve_and_resize(total_size, header_padding);
348 }
349
350 // Convenience overload - computes frame overhead internally
352 const uint8_t header_padding = this->helper_->frame_header_padding();
353 const uint8_t footer_size = this->helper_->frame_footer_size();
354 this->prepare_first_message_buffer(shared_buf, header_padding, payload_size + header_padding + footer_size);
355 }
356
357 bool try_to_clear_buffer(bool log_out_of_space) {
358 if (this->flags_.remove)
359 return false;
360 if (this->helper_->can_write_without_blocking())
361 return true;
362 return this->try_to_clear_buffer_slow_(log_out_of_space);
363 }
364 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type);
365
366 const char *get_name() const { return this->helper_->get_client_name(); }
368 const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
369 return this->helper_->get_peername_to(buf);
370 }
371
372 protected:
373 bool try_to_clear_buffer_slow_(bool log_out_of_space);
374
375 // Helper function to handle authentication completion
377
378 // Pattern B helpers: send response and return success/failure
379 bool send_hello_response_(const HelloRequest &msg);
381 bool send_ping_response_();
383#ifdef USE_API_NOISE
385#endif
386#ifdef USE_BLUETOOTH_PROXY
388#endif
389#ifdef USE_VOICE_ASSISTANT
391#endif
392
393#ifdef USE_CAMERA
395#endif
396
397#ifdef USE_API_HOMEASSISTANT_STATES
399#endif
400
401 // Size thunk — converts void* back to concrete type for direct calculate_size() call
402 template<typename T> static uint32_t calc_size(const void *msg) {
403 return static_cast<const T *>(msg)->calculate_size();
404 }
405
406 // Shared no-op encode thunk for empty messages (ESTIMATED_SIZE == 0)
407 static uint8_t *encode_msg_noop(const void *, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM) {
408 return buf.get_pos();
409 }
410
411 // Non-template buffer management for send_message
412 bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *msg);
413
414 // Core batch encoding logic. Computes header size, checks fit, resizes buffer, encodes.
415 // ALWAYS_INLINE so the compiler can devirtualize encode_fn at hot call sites.
416 static inline uint16_t ESPHOME_ALWAYS_INLINE encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn,
417 const void *msg, APIConnection *conn,
418 uint32_t remaining_size) {
419#ifdef HAS_PROTO_MESSAGE_DUMP
420 if (conn->flags_.log_only_mode) {
421 auto *proto_msg = static_cast<const ProtoMessage *>(msg);
422 DumpBuffer dump_buf;
423 conn->log_send_message_(proto_msg->message_name(), proto_msg->dump_to(dump_buf));
424 return 1;
425 }
426#endif
427 const uint8_t footer_size = conn->helper_->frame_footer_size();
428
429 // First message uses max padding (already in buffer), subsequent use exact header size
430 size_t to_add;
431 if (conn->flags_.batch_first_message) {
432 conn->flags_.batch_first_message = false;
433 conn->batch_header_size_ = conn->helper_->frame_header_padding();
434 to_add = calculated_size;
435 } else {
436 conn->batch_header_size_ = conn->helper_->frame_header_size(calculated_size, conn->batch_message_type_);
437 to_add = calculated_size + conn->batch_header_size_ + footer_size;
438 }
439
440 // Check if it fits (using actual header size, not max padding)
441 uint16_t total_calculated_size = calculated_size + conn->batch_header_size_ + footer_size;
442 if (total_calculated_size > remaining_size)
443 return 0;
444
445 auto &shared_buf = conn->parent_->get_shared_buffer_ref();
446 shared_buf.resize(shared_buf.size() + to_add);
447 ProtoWriteBuffer buffer{&shared_buf, shared_buf.size() - calculated_size};
448 encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
449
450 return total_calculated_size;
451 }
452
453 // Noinline version of encode_to_buffer for cold paths (entity info, zero-payload messages).
454 // All cold callers share this single copy instead of each getting an ALWAYS_INLINE expansion.
455 static uint16_t encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg,
456 APIConnection *conn, uint32_t remaining_size);
457
458 // Thin template wrapper — uses noinline encode_to_buffer_slow since
459 // encode_message_to_buffer callers are cold paths (zero-payload control messages).
460 // Hot paths (state/info) go through fill_and_encode_entity_state/info instead.
461 // batch_message_type_ is already set by dispatch_message_ before reaching here.
462 template<typename T> static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size) {
463 if constexpr (T::ESTIMATED_SIZE == 0) {
464 return encode_to_buffer_slow(0, &encode_msg_noop, &msg, conn, remaining_size);
465 } else {
466 return encode_to_buffer_slow(msg.calculate_size(), &proto_encode_msg<T>, &msg, conn, remaining_size);
467 }
468 }
469
470 // Non-template core — fills state fields and encodes
472 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
473 uint32_t remaining_size);
474
475 // Thin template wrapper
476 template<typename T>
477 static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn,
478 uint32_t remaining_size) {
479 return fill_and_encode_entity_state(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
480 }
481
482 // Non-template core — fills info fields, allocates buffers, and encodes
484 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
485 uint32_t remaining_size);
486
487 // Thin template wrapper
488 template<typename T>
489 static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn,
490 uint32_t remaining_size) {
491 return fill_and_encode_entity_info(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
492 }
493
494 // Non-template core — fills device_class, then delegates to fill_and_encode_entity_info
496 StringRef &device_class_field, CalculateSizeFn size_fn,
497 MessageEncodeFn encode_fn, APIConnection *conn,
498 uint32_t remaining_size);
499
500 // Thin template wrapper
501 template<typename T>
503 StringRef &device_class_field, APIConnection *conn,
504 uint32_t remaining_size) {
505 return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &calc_size<T>,
506 &proto_encode_msg<T>, conn, remaining_size);
507 }
508
509#ifdef USE_VOICE_ASSISTANT
510 // Helper to check voice assistant validity and connection ownership
511 inline bool check_voice_assistant_api_connection_() const;
512#endif
513
514 // Get the max batch size based on client API version
515 // API 1.14+ clients don't receive object_id, so messages are smaller and more fit per batch
516 // TODO: Remove this method before 2026.7.0 and use MAX_INITIAL_PER_BATCH directly
517 size_t get_max_batch_size_() const {
518 return this->client_supports_api_version(1, 14) ? MAX_INITIAL_PER_BATCH : MAX_INITIAL_PER_BATCH_LEGACY;
519 }
520
521 // Send keepalive ping or disconnect unresponsive client.
522 // Cold path — extracted from loop() to reduce instruction cache pressure.
523 void __attribute__((noinline)) check_keepalive_(uint32_t now);
524
525 // Process active iterator (list_entities/initial_state) during connection setup.
526 // Extracted from loop() — only runs during initial handshake, NONE in steady state.
527 void __attribute__((noinline)) process_active_iterator_();
528
529 // Helper method to process multiple entities from an iterator in a batch.
530 // Takes ComponentIterator base class reference to avoid duplicate template instantiations.
532
533#ifdef USE_BINARY_SENSOR
534 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
535 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
536#endif
537#ifdef USE_COVER
538 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
539 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
540#endif
541#ifdef USE_FAN
542 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
543 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
544#endif
545#ifdef USE_LIGHT
546 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
547 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
548#endif
549#ifdef USE_SENSOR
550 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
551 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
552#endif
553#ifdef USE_SWITCH
554 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
555 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
556#endif
557#ifdef USE_TEXT_SENSOR
558 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
559 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
560#endif
561#ifdef USE_CLIMATE
562 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
563 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
564#endif
565#ifdef USE_NUMBER
566 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
567 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
568#endif
569#ifdef USE_DATETIME_DATE
570 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
571 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
572#endif
573#ifdef USE_DATETIME_TIME
574 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
575 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
576#endif
577#ifdef USE_DATETIME_DATETIME
578 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
579 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
580#endif
581#ifdef USE_TEXT
582 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
583 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
584#endif
585#ifdef USE_SELECT
586 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
587 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
588#endif
589#ifdef USE_BUTTON
590 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
591#endif
592#ifdef USE_LOCK
593 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
594 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
595#endif
596#ifdef USE_VALVE
597 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
598 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
599#endif
600#ifdef USE_MEDIA_PLAYER
601 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
602 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
603#endif
604#ifdef USE_ALARM_CONTROL_PANEL
605 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
606 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
607#endif
608#ifdef USE_WATER_HEATER
609 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
610 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
611#endif
612#ifdef USE_INFRARED
613 static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
614#endif
615#ifdef USE_EVENT
616 static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
617 uint32_t remaining_size);
618 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
619#endif
620#ifdef USE_UPDATE
621 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
622 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
623#endif
624#ifdef USE_CAMERA
625 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
626#endif
627
628 // Method for ListEntitiesDone batching
629 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
630
631 // Method for DisconnectRequest batching
632 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
633
634 // Batch message method for ping requests
635 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
636
637 // === Optimal member ordering for 32-bit systems ===
638
639 // Group 1: Pointers (4 bytes each on 32-bit)
640#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
641 std::unique_ptr<APIFrameHelper> helper_;
642#elif defined(USE_API_NOISE)
643 std::unique_ptr<APINoiseFrameHelper> helper_;
644#elif defined(USE_API_PLAINTEXT)
645 std::unique_ptr<APIPlaintextFrameHelper> helper_;
646#endif
648
649 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
650 // These iterators are never active simultaneously - list_entities runs to completion
651 // before initial_state begins, so we use a union with explicit construction/destruction.
652 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
653
657 // Constructor/destructor do nothing - use placement new/explicit destructor
661
662 // Helper methods for iterator lifecycle management
666#ifdef USE_CAMERA
667 std::unique_ptr<camera::CameraImageReader> image_reader_;
668#endif
669
670 // Group 3: 4-byte types
672#ifdef USE_API_HOMEASSISTANT_STATES
674#endif
675
676 // Function pointer type for message encoding
677 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size);
678
679 // Generic batching mechanism for both state updates and entity info
681 // Sentinel value for unused aux_data_index
682 static constexpr uint8_t AUX_DATA_UNUSED = std::numeric_limits<uint8_t>::max();
683
684 struct BatchItem {
685 EntityBase *entity; // 4 bytes - Entity pointer
686 uint8_t message_type; // 1 byte - Message type for protocol and dispatch
687 uint8_t estimated_size; // 1 byte - Estimated message size (max 255 bytes)
688 uint8_t aux_data_index{AUX_DATA_UNUSED}; // 1 byte - For events: index into entity's event_types
689 // 1 byte padding
690 };
691
692 std::vector<BatchItem> items;
694
695 // No pre-allocation - log connections never use batching, and for
696 // connections that do, buffers are released after initial sync anyway
697
698 // Add item to the batch (with deduplication)
699 void add_item(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
700 uint8_t aux_data_index = AUX_DATA_UNUSED) {
701 // Dedup: O(n) scan but optimized for RAM over performance
702 // Skip deduplication for events - they are edge-triggered, every occurrence matters
703#ifdef USE_EVENT
704 if (message_type != EventResponse::MESSAGE_TYPE)
705#endif
706 {
707 for (const auto &item : this->items) {
708 if (item.entity == entity && item.message_type == message_type)
709 return; // Already queued
710 }
711 }
712 this->items.push_back({entity, message_type, estimated_size, aux_data_index});
713 }
714 // Add item to the front of the batch (for high priority messages like ping)
715 void add_item_front(EntityBase *entity, uint8_t message_type, uint8_t estimated_size) {
716 // Swap to front avoids expensive vector::insert which shifts all elements
717 this->items.push_back({entity, message_type, estimated_size, AUX_DATA_UNUSED});
718 if (this->items.size() > 1) {
719 std::swap(this->items.front(), this->items.back());
720 }
721 }
722
723 // Clear all items
724 void clear() {
725 items.clear();
727 }
728
729 // Remove processed items from the front — noinline to keep memmove out of warm callers
730 void remove_front(size_t count) __attribute__((noinline)) { items.erase(items.begin(), items.begin() + count); }
731
732 bool empty() const { return items.empty(); }
733 size_t size() const { return items.size(); }
734 const BatchItem &operator[](size_t index) const { return items[index]; }
735
736 // Release excess capacity - only releases if items already empty
738 // Safe to call: batch is processed before release_buffer is called,
739 // and if any items remain (partial processing), we must not clear them.
740 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
741 if (items.empty()) {
742 std::vector<BatchItem>().swap(items);
743 }
744 }
745 };
746
747 // DeferredBatch here (16 bytes, 4-byte aligned)
749
750 // ConnectionState enum for type safety
751 enum class ConnectionState : uint8_t {
753 CONNECTED = 1,
754 AUTHENTICATED = 2,
755 };
756
757 // Group 5: Pack all small members together to minimize padding
758 // This group starts at a 4-byte boundary after DeferredBatch
759 struct APIFlags {
760 // Connection state only needs 2 bits (3 states)
761 uint8_t connection_state : 2;
762 // Log subscription needs 3 bits (log levels 0-7)
763 uint8_t log_subscription : 3;
764 // Boolean flags (1 bit each)
765 uint8_t remove : 1;
767 uint8_t sent_ping : 1;
768
770 uint8_t next_close : 1;
771 uint8_t batch_scheduled : 1;
772 uint8_t batch_first_message : 1; // For batch buffer allocation
773 uint8_t should_try_send_immediately : 1; // True after initial states are sent
774 uint8_t may_have_remaining_data : 1; // Read loop hit limit, retry without ready check
775#ifdef HAS_PROTO_MESSAGE_DUMP
776 uint8_t log_only_mode : 1;
777#endif
778 } flags_{}; // 2 bytes total
779
780 // 2-byte types immediately after flags_ (no padding between them)
783 // 1-byte types to fill remaining space before next 4-byte boundary
785 uint8_t batch_message_type_{0}; // Current message type during batch encoding
786 // Total: 2 (flags) + 2 + 2 + 1 + 1 = 8 bytes, aligned to 4-byte boundary
787
788 // Actual header size used by encode_to_buffer for the current message.
789 // Read by process_batch_multi_ to pass into MessageInfo.
791
792 uint32_t get_batch_delay_ms_() const { return this->parent_->get_batch_delay(); }
793 // Message will use 8 more bytes than the minimum size, and typical
794 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
795 // If its IPv6 the header is 40 bytes, and if its IPv4
796 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
797 // available for the payload. But we also need to add the size of
798 // the protobuf overhead, which is 8 bytes.
799 //
800 // To be safe we pick 1390 bytes as the maximum size
801 // to send in one go. This is the maximum size of a single packet
802 // that can be sent over the network.
803 // This is to avoid fragmentation of the packet.
804 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
805
806 bool schedule_batch_();
807 void process_batch_();
808 void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size)
809 __attribute__((noinline));
811 this->deferred_batch_.clear();
812 this->flags_.batch_scheduled = false;
813 }
814
815 // Dispatch message encoding based on message_type - replaces function pointer storage
816 // Switch assigns pointer, single call site for smaller code size
817 uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first);
818
819#ifdef HAS_PROTO_MESSAGE_DUMP
821 this->flags_.log_only_mode = true;
822 this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true);
823 this->flags_.log_only_mode = false;
824 }
825#endif
826
827 // Helper to check if a message type should bypass batching
828 // Returns true if:
829 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
830 // the main loop is blocked, e.g., during OTA updates)
831 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
832 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
833 // AND batch_delay = 0)
834 inline bool should_send_immediately_(uint8_t message_type) const {
835 return (
836#ifdef USE_UPDATE
837 message_type == UpdateStateResponse::MESSAGE_TYPE ||
838#endif
839#ifdef USE_EVENT
840 message_type == EventResponse::MESSAGE_TYPE ||
841#endif
842 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
843 }
844
845 // Helper method to send a message either immediately or via batching
846 // Tries immediate send if should_send_immediately_() returns true and buffer has space
847 // Falls back to batching if immediate send fails or isn't applicable
848 bool send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
849 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED);
850
851 // Helper function to schedule a deferred message with known message type
852 bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
853 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED) {
854 this->deferred_batch_.add_item(entity, message_type, estimated_size, aux_data_index);
855 return this->schedule_batch_();
856 }
857
858 // Helper function to schedule a high priority message at the front of the batch
859 // Out-of-line: callers (on_shutdown, check_keepalive_) are cold paths
860 bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size);
861
862 // Helper function to log client messages with name and peername
863 void log_client_(int level, const LogString *message);
864 // Helper function to log API errors with errno
865 void log_warning_(const LogString *message, APIError err);
866 // Helper to handle fatal errors with logging
867 inline void fatal_error_with_log_(const LogString *message, APIError err) {
868 this->on_fatal_error();
869 this->log_warning_(message, err);
870 }
871};
872
873} // namespace esphome::api
874#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:36
void reserve_and_resize(size_t reserve_size, size_t new_size) ESPHOME_ALWAYS_INLINE
Reserve capacity for max(reserve_size, new_size) bytes, then set size to new_size.
Definition api_buffer.h:49
void resize(size_t n) ESPHOME_ALWAYS_INLINE
Definition api_buffer.h:43
struct esphome::api::APIConnection::APIFlags flags_
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg)
void on_camera_image_request(const CameraImageRequest &msg)
void on_media_player_command_request(const MediaPlayerCommandRequest &msg)
static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_text_command_request(const TextCommandRequest &msg)
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_media_player_state(media_player::MediaPlayer *media_player)
void on_update_command_request(const UpdateCommandRequest &msg)
std::unique_ptr< APIPlaintextFrameHelper > helper_
bool send_time_state(datetime::TimeEntity *time)
bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size, uint8_t aux_data_index=DeferredBatch::AUX_DATA_UNUSED)
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg)
static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn, uint32_t remaining_size)
void __attribute__((noinline)) process_active_iterator_()
bool send_water_heater_state(water_heater::WaterHeater *water_heater)
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg)
void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message)
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_button_command_request(const ButtonCommandRequest &msg)
void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg)
void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg)
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_valve_command_request(const ValveCommandRequest &msg)
void on_bluetooth_device_request(const BluetoothDeviceRequest &msg)
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first)
static uint32_t calc_size(const void *msg)
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor)
static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg)
bool send_fan_state(fan::Fan *fan)
void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg)
bool check_voice_assistant_api_connection_() const
void send_serial_proxy_data(const SerialProxyDataReceived &msg)
bool send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size, uint8_t aux_data_index=DeferredBatch::AUX_DATA_UNUSED)
static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
union esphome::api::APIConnection::IteratorUnion iterator_storage_
static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_cover_command_request(const CoverCommandRequest &msg)
std::unique_ptr< APIFrameHelper > helper_
static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void set_camera_state(std::shared_ptr< camera::CameraImage > image)
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, T &msg, StringRef &device_class_field, APIConnection *conn, uint32_t remaining_size)
uint32_t get_batch_delay_ms_() const
bool send_sensor_state(sensor::Sensor *sensor)
const char * get_peername_to(std::span< char, socket::SOCKADDR_STR_LEN > buf) const
Get peer name (IP address) into caller-provided buffer, returns buf for convenience.
static constexpr size_t MAX_BATCH_PACKET_SIZE
void log_batch_item_(const DeferredBatch::BatchItem &item)
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_fan_command_request(const FanCommandRequest &msg)
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
const char * get_name() const
friend void bench_clear_batch(APIConnection *conn)
void on_climate_command_request(const ClimateCommandRequest &msg)
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor)
void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg)
bool send_valve_state(valve::Valve *valve)
bool send_voice_assistant_get_configuration_response_(const VoiceAssistantConfigurationRequest &msg)
bool send_select_state(select::Select *select)
void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg)
bool send_switch_state(switch_::Switch *a_switch)
void send_event(event::Event *event)
static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_request(const ZWaveProxyRequest &msg)
static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_noise_encryption_set_key_response_(const NoiseEncryptionSetKeyRequest &msg)
void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg)
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, InfoResponseProtoMessage &msg, StringRef &device_class_field, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
void on_date_command_request(const DateCommandRequest &msg)
void log_client_(int level, const LogString *message)
void on_subscribe_logs_request(const SubscribeLogsRequest &msg)
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg)
uint8_t *(*)(const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM) MessageEncodeFn
void send_infrared_rf_receive_event(const InfraredRFReceiveEvent &msg)
bool should_send_immediately_(uint8_t message_type) const
bool send_lock_state(lock::Lock *a_lock)
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg)
void on_select_command_request(const SelectCommandRequest &msg)
bool send_message(const T &msg)
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg)
void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg)
void on_homeassistant_action_response(const HomeassistantActionResponse &msg)
void prepare_first_message_buffer(APIBuffer &shared_buf, size_t payload_size)
void on_switch_command_request(const SwitchCommandRequest &msg)
bool send_update_state(update::UpdateEntity *update)
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_get_time_response(const GetTimeResponse &value)
void fatal_error_with_log_(const LogString *message, APIError err)
static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg)
void log_warning_(const LogString *message, APIError err)
void on_serial_proxy_request(const SerialProxyRequest &msg)
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_lock_command_request(const LockCommandRequest &msg)
void begin_iterator_(ActiveIterator type)
void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg)
std::unique_ptr< camera::CameraImageReader > image_reader_
void on_execute_service_request(const ExecuteServiceRequest &msg)
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_date_state(datetime::DateEntity *date)
friend void bench_enable_immediate_send(APIConnection *conn)
void on_number_command_request(const NumberCommandRequest &msg)
bool send_number_state(number::Number *number)
static uint16_t encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg, APIConnection *conn, uint32_t remaining_size)
std::unique_ptr< APINoiseFrameHelper > helper_
bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size)
bool send_hello_response_(const HelloRequest &msg)
void send_homeassistant_action(const HomeassistantActionRequest &call)
bool send_light_state(light::LightState *light)
static uint8_t * encode_msg_noop(const void *, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM)
friend void bench_process_batch(APIConnection *conn)
static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
uint16_t(*)(EntityBase *, APIConnection *, uint32_t remaining_size) MessageCreatorPtr
static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg)
void on_time_command_request(const TimeCommandRequest &msg)
static uint16_t ESPHOME_ALWAYS_INLINE encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg, APIConnection *conn, uint32_t remaining_size)
uint8_t get_log_subscription_level() const
static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *msg)
void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg)
bool send_subscribe_bluetooth_connections_free_response_()
void prepare_first_message_buffer(APIBuffer &shared_buf, size_t header_padding, size_t total_size)
void on_hello_request(const HelloRequest &msg)
void on_voice_assistant_audio(const VoiceAssistantAudio &msg)
bool send_datetime_state(datetime::DateTimeEntity *datetime)
void on_serial_proxy_write_request(const SerialProxyWriteRequest &msg)
void on_light_command_request(const LightCommandRequest &msg)
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg)
bool send_text_state(text::Text *text)
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn, uint32_t remaining_size)
void on_date_time_command_request(const DateTimeCommandRequest &msg)
void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data)
static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg)
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len)
void process_iterator_batch_(ComponentIterator &iterator)
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_unsubscribe_bluetooth_le_advertisements_request()
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type)
bool client_supports_api_version(uint16_t major, uint16_t minor) const
void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size) __attribute__((noinline))
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_water_heater_command_request(const WaterHeaterCommandRequest &msg)
bool send_climate_state(climate::Climate *climate)
bool try_to_clear_buffer(bool log_out_of_space)
bool send_cover_state(cover::Cover *cover)
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool try_to_clear_buffer_slow_(bool log_out_of_space)
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg)
void __attribute__((noinline)) check_keepalive_(uint32_t now)
uint32_t(*)(const void *) CalculateSizeFn
static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_response(const VoiceAssistantResponse &msg)
void log_send_message_(const LogString *name, const char *dump)
uint16_t get_batch_delay() const
Definition api_server.h:64
APIBuffer & get_shared_buffer_ref()
Definition api_server.h:69
Fixed-size buffer for message dumps - avoids heap allocation.
Definition proto.h:516
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2819
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:587
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:588
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2959
Base class for all binary_sensor-type classes.
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
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
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 type
uint8_t * proto_encode_msg(const void *msg, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM)
Definition proto.h:850
void bench_process_batch(APIConnection *conn)
void bench_enable_immediate_send(APIConnection *conn)
void bench_clear_batch(APIConnection *conn)
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_clear()
Clear the magic marker and mark crash data as consumed.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
void remove_front(size_t count) __attribute__((noinline))
const BatchItem & operator[](size_t index) const
void add_item_front(EntityBase *entity, uint8_t message_type, uint8_t estimated_size)
void add_item(EntityBase *entity, uint8_t message_type, uint8_t estimated_size, uint8_t aux_data_index=AUX_DATA_UNUSED)
uint32_t payload_size()