ESPHome 2026.3.3
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
25
26#include <functional>
27#include <limits>
28#include <vector>
29
30namespace esphome {
31class ComponentIterator;
32} // namespace esphome
33
34namespace esphome::api {
35
36// Keepalive timeout in milliseconds
37static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
38// Maximum number of entities to process in a single batch during initial state/info sending
39// API 1.14+ clients compute object_id client-side, so messages are smaller and we can fit more per batch
40// TODO: Remove MAX_INITIAL_PER_BATCH_LEGACY before 2026.7.0 - all clients should support API 1.14 by then
41static constexpr size_t MAX_INITIAL_PER_BATCH_LEGACY = 24; // For clients < API 1.14 (includes object_id)
42static constexpr size_t MAX_INITIAL_PER_BATCH = 34; // For clients >= API 1.14 (no object_id)
43// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch
44static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_PER_BATCH,
45 "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_PER_BATCH");
46
48 public:
49 friend class APIServer;
51 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
52 virtual ~APIConnection();
53
54 void start();
55 void loop();
56
61#ifdef USE_BINARY_SENSOR
63#endif
64#ifdef USE_COVER
65 bool send_cover_state(cover::Cover *cover);
66 void on_cover_command_request(const CoverCommandRequest &msg) override;
67#endif
68#ifdef USE_FAN
69 bool send_fan_state(fan::Fan *fan);
70 void on_fan_command_request(const FanCommandRequest &msg) override;
71#endif
72#ifdef USE_LIGHT
74 void on_light_command_request(const LightCommandRequest &msg) override;
75#endif
76#ifdef USE_SENSOR
78#endif
79#ifdef USE_SWITCH
80 bool send_switch_state(switch_::Switch *a_switch);
81 void on_switch_command_request(const SwitchCommandRequest &msg) override;
82#endif
83#ifdef USE_TEXT_SENSOR
85#endif
86#ifdef USE_CAMERA
87 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
88 void on_camera_image_request(const CameraImageRequest &msg) override;
89#endif
90#ifdef USE_CLIMATE
92 void on_climate_command_request(const ClimateCommandRequest &msg) override;
93#endif
94#ifdef USE_NUMBER
96 void on_number_command_request(const NumberCommandRequest &msg) override;
97#endif
98#ifdef USE_DATETIME_DATE
100 void on_date_command_request(const DateCommandRequest &msg) override;
101#endif
102#ifdef USE_DATETIME_TIME
104 void on_time_command_request(const TimeCommandRequest &msg) override;
105#endif
106#ifdef USE_DATETIME_DATETIME
109#endif
110#ifdef USE_TEXT
111 bool send_text_state(text::Text *text);
112 void on_text_command_request(const TextCommandRequest &msg) override;
113#endif
114#ifdef USE_SELECT
115 bool send_select_state(select::Select *select);
116 void on_select_command_request(const SelectCommandRequest &msg) override;
117#endif
118#ifdef USE_BUTTON
119 void on_button_command_request(const ButtonCommandRequest &msg) override;
120#endif
121#ifdef USE_LOCK
122 bool send_lock_state(lock::Lock *a_lock);
123 void on_lock_command_request(const LockCommandRequest &msg) override;
124#endif
125#ifdef USE_VALVE
126 bool send_valve_state(valve::Valve *valve);
127 void on_valve_command_request(const ValveCommandRequest &msg) override;
128#endif
129#ifdef USE_MEDIA_PLAYER
132#endif
133 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
134#ifdef USE_API_HOMEASSISTANT_SERVICES
137 return;
138 this->send_message(call);
139 }
140#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
142#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
143#endif // USE_API_HOMEASSISTANT_SERVICES
144#ifdef USE_BLUETOOTH_PROXY
147
148 void on_bluetooth_device_request(const BluetoothDeviceRequest &msg) override;
158
159#endif
160#ifdef USE_HOMEASSISTANT_TIME
162 GetTimeRequest req;
163 this->send_message(req);
164 }
165#endif
166
167#ifdef USE_VOICE_ASSISTANT
169 void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
171 void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override;
176#endif
177
178#ifdef USE_ZWAVE_PROXY
179 void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) override;
180 void on_z_wave_proxy_request(const ZWaveProxyRequest &msg) override;
181#endif
182
183#ifdef USE_ALARM_CONTROL_PANEL
186#endif
187
188#ifdef USE_WATER_HEATER
191#endif
192
193#ifdef USE_IR_RF
196#endif
197
198#ifdef USE_SERIAL_PROXY
203 void on_serial_proxy_request(const SerialProxyRequest &msg) override;
205#endif
206
207#ifdef USE_EVENT
208 void send_event(event::Event *event);
209#endif
210
211#ifdef USE_UPDATE
213 void on_update_command_request(const UpdateCommandRequest &msg) override;
214#endif
215
216 void on_disconnect_response() override;
217 void on_ping_response() override {
218 // we initiated ping
219 this->flags_.sent_ping = false;
220 }
221#ifdef USE_API_HOMEASSISTANT_STATES
223#endif
224#ifdef USE_HOMEASSISTANT_TIME
225 void on_get_time_response(const GetTimeResponse &value) override;
226#endif
227 void on_hello_request(const HelloRequest &msg) override;
228 void on_disconnect_request() override;
229 void on_ping_request() override;
230 void on_device_info_request() override;
233 this->flags_.state_subscription = true;
234 // Start initial state iterator only if no iterator is active
235 // If list_entities is running, we'll start initial_state when it completes
238 }
239 }
241 this->flags_.log_subscription = msg.level;
242 if (msg.dump_config)
244#ifdef USE_ESP32_CRASH_HANDLER
246#endif
247#ifdef USE_RP2040_CRASH_HANDLER
249#endif
250 }
251#ifdef USE_API_HOMEASSISTANT_SERVICES
253#endif
254#ifdef USE_API_HOMEASSISTANT_STATES
256#endif
257#ifdef USE_API_USER_DEFINED_ACTIONS
258 void on_execute_service_request(const ExecuteServiceRequest &msg) override;
259#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
260 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
261#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
262 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
263 const uint8_t *response_data, size_t response_data_len);
264#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
265#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
266#endif
267#ifdef USE_API_NOISE
269#endif
270
271 bool is_authenticated() override {
273 }
274 bool is_connection_setup() override {
276 this->is_authenticated();
277 }
278 bool is_marked_for_removal() const { return this->flags_.remove; }
279 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
280
281 // Get client API version for feature detection
282 bool client_supports_api_version(uint16_t major, uint16_t minor) const {
283 return this->client_api_version_major_ > major ||
284 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
285 }
286
287 void on_fatal_error() override;
288 void on_no_setup_connection() override;
289
290 // Function pointer type for type-erased message encoding
291 using MessageEncodeFn = void (*)(const void *, ProtoWriteBuffer &);
292 // Function pointer type for type-erased size calculation
293 using CalculateSizeFn = uint32_t (*)(const void *);
294
295 template<typename T> bool send_message(const T &msg) {
296 if constexpr (T::ESTIMATED_SIZE == 0) {
297 return this->send_message_(0, T::MESSAGE_TYPE, &encode_msg_noop, &msg);
298 } else {
299 return this->send_message_(msg.calculate_size(), T::MESSAGE_TYPE, &proto_encode_msg<T>, &msg);
300 }
301 }
302
303 void prepare_first_message_buffer(APIBuffer &shared_buf, size_t header_padding, size_t total_size) {
304 shared_buf.clear();
305 // Reserve space for header padding + message + footer
306 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
307 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
308 shared_buf.reserve(total_size);
309 // Resize to add header padding so message encoding starts at the correct position
310 shared_buf.resize(header_padding);
311 }
312
313 // Convenience overload - computes frame overhead internally
315 const uint8_t header_padding = this->helper_->frame_header_padding();
316 const uint8_t footer_size = this->helper_->frame_footer_size();
317 this->prepare_first_message_buffer(shared_buf, header_padding, payload_size + header_padding + footer_size);
318 }
319
320 bool try_to_clear_buffer(bool log_out_of_space) {
321 if (this->flags_.remove)
322 return false;
323 if (this->helper_->can_write_without_blocking())
324 return true;
325 return this->try_to_clear_buffer_slow_(log_out_of_space);
326 }
327 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
328
329 const char *get_name() const { return this->helper_->get_client_name(); }
331 const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
332 return this->helper_->get_peername_to(buf);
333 }
334
335 protected:
336 bool try_to_clear_buffer_slow_(bool log_out_of_space);
337
338 // Helper function to handle authentication completion
340
341 // Pattern B helpers: send response and return success/failure
342 bool send_hello_response_(const HelloRequest &msg);
344 bool send_ping_response_();
346#ifdef USE_API_NOISE
348#endif
349#ifdef USE_BLUETOOTH_PROXY
351#endif
352#ifdef USE_VOICE_ASSISTANT
354#endif
355
356#ifdef USE_CAMERA
358#endif
359
360#ifdef USE_API_HOMEASSISTANT_STATES
362#endif
363
364 // Size thunk — converts void* back to concrete type for direct calculate_size() call
365 template<typename T> static uint32_t calc_size(const void *msg) {
366 return static_cast<const T *>(msg)->calculate_size();
367 }
368
369 // Shared no-op encode thunk for empty messages (ESTIMATED_SIZE == 0)
370 static void encode_msg_noop(const void *, ProtoWriteBuffer &) {}
371
372 // Non-template buffer management for send_message
373 bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *msg);
374
375 // Non-template buffer management for batch encoding
376 static uint16_t encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg,
377 APIConnection *conn, uint32_t remaining_size);
378
379 // Thin template wrapper — computes size, delegates buffer work to non-template helper
380 template<typename T> static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size) {
381 if constexpr (T::ESTIMATED_SIZE == 0) {
382 return encode_to_buffer(0, &encode_msg_noop, &msg, conn, remaining_size);
383 } else {
384 return encode_to_buffer(msg.calculate_size(), &proto_encode_msg<T>, &msg, conn, remaining_size);
385 }
386 }
387
388 // Non-template core — fills state fields and encodes
390 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
391 uint32_t remaining_size);
392
393 // Thin template wrapper
394 template<typename T>
395 static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn,
396 uint32_t remaining_size) {
397 return fill_and_encode_entity_state(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
398 }
399
400 // Non-template core — fills info fields, allocates buffers, and encodes
402 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
403 uint32_t remaining_size);
404
405 // Thin template wrapper
406 template<typename T>
407 static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn,
408 uint32_t remaining_size) {
409 return fill_and_encode_entity_info(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
410 }
411
412 // Non-template core — fills device_class, then delegates to fill_and_encode_entity_info
414 StringRef &device_class_field, CalculateSizeFn size_fn,
415 MessageEncodeFn encode_fn, APIConnection *conn,
416 uint32_t remaining_size);
417
418 // Thin template wrapper
419 template<typename T>
421 StringRef &device_class_field, APIConnection *conn,
422 uint32_t remaining_size) {
423 return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &calc_size<T>,
424 &proto_encode_msg<T>, conn, remaining_size);
425 }
426
427#ifdef USE_VOICE_ASSISTANT
428 // Helper to check voice assistant validity and connection ownership
429 inline bool check_voice_assistant_api_connection_() const;
430#endif
431
432 // Get the max batch size based on client API version
433 // API 1.14+ clients don't receive object_id, so messages are smaller and more fit per batch
434 // TODO: Remove this method before 2026.7.0 and use MAX_INITIAL_PER_BATCH directly
435 size_t get_max_batch_size_() const {
436 return this->client_supports_api_version(1, 14) ? MAX_INITIAL_PER_BATCH : MAX_INITIAL_PER_BATCH_LEGACY;
437 }
438
439 // Send keepalive ping or disconnect unresponsive client.
440 // Cold path — extracted from loop() to reduce instruction cache pressure.
441 void __attribute__((noinline)) check_keepalive_(uint32_t now);
442
443 // Process active iterator (list_entities/initial_state) during connection setup.
444 // Extracted from loop() — only runs during initial handshake, NONE in steady state.
445 void __attribute__((noinline)) process_active_iterator_();
446
447 // Helper method to process multiple entities from an iterator in a batch.
448 // Takes ComponentIterator base class reference to avoid duplicate template instantiations.
450
451#ifdef USE_BINARY_SENSOR
452 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
453 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
454#endif
455#ifdef USE_COVER
456 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
457 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
458#endif
459#ifdef USE_FAN
460 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
461 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
462#endif
463#ifdef USE_LIGHT
464 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
465 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
466#endif
467#ifdef USE_SENSOR
468 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
469 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
470#endif
471#ifdef USE_SWITCH
472 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
473 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
474#endif
475#ifdef USE_TEXT_SENSOR
476 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
477 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
478#endif
479#ifdef USE_CLIMATE
480 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
481 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
482#endif
483#ifdef USE_NUMBER
484 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
485 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
486#endif
487#ifdef USE_DATETIME_DATE
488 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
489 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
490#endif
491#ifdef USE_DATETIME_TIME
492 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
493 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
494#endif
495#ifdef USE_DATETIME_DATETIME
496 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
497 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
498#endif
499#ifdef USE_TEXT
500 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
501 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
502#endif
503#ifdef USE_SELECT
504 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
505 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
506#endif
507#ifdef USE_BUTTON
508 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
509#endif
510#ifdef USE_LOCK
511 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
512 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
513#endif
514#ifdef USE_VALVE
515 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
516 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
517#endif
518#ifdef USE_MEDIA_PLAYER
519 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
520 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
521#endif
522#ifdef USE_ALARM_CONTROL_PANEL
523 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
524 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
525#endif
526#ifdef USE_WATER_HEATER
527 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
528 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
529#endif
530#ifdef USE_INFRARED
531 static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
532#endif
533#ifdef USE_EVENT
534 static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
535 uint32_t remaining_size);
536 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
537#endif
538#ifdef USE_UPDATE
539 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
540 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
541#endif
542#ifdef USE_CAMERA
543 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
544#endif
545
546 // Method for ListEntitiesDone batching
547 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
548
549 // Method for DisconnectRequest batching
550 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
551
552 // Batch message method for ping requests
553 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
554
555 // === Optimal member ordering for 32-bit systems ===
556
557 // Group 1: Pointers (4 bytes each on 32-bit)
558#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
559 std::unique_ptr<APIFrameHelper> helper_;
560#elif defined(USE_API_NOISE)
561 std::unique_ptr<APINoiseFrameHelper> helper_;
562#elif defined(USE_API_PLAINTEXT)
563 std::unique_ptr<APIPlaintextFrameHelper> helper_;
564#endif
566
567 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
568 // These iterators are never active simultaneously - list_entities runs to completion
569 // before initial_state begins, so we use a union with explicit construction/destruction.
570 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
571
575 // Constructor/destructor do nothing - use placement new/explicit destructor
579
580 // Helper methods for iterator lifecycle management
583#ifdef USE_CAMERA
584 std::unique_ptr<camera::CameraImageReader> image_reader_;
585#endif
586
587 // Group 3: 4-byte types
589#ifdef USE_API_HOMEASSISTANT_STATES
591#endif
592
593 // Function pointer type for message encoding
594 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size);
595
596 // Generic batching mechanism for both state updates and entity info
598 // Sentinel value for unused aux_data_index
599 static constexpr uint8_t AUX_DATA_UNUSED = std::numeric_limits<uint8_t>::max();
600
601 struct BatchItem {
602 EntityBase *entity; // 4 bytes - Entity pointer
603 uint8_t message_type; // 1 byte - Message type for protocol and dispatch
604 uint8_t estimated_size; // 1 byte - Estimated message size (max 255 bytes)
605 uint8_t aux_data_index{AUX_DATA_UNUSED}; // 1 byte - For events: index into entity's event_types
606 // 1 byte padding
607 };
608
609 std::vector<BatchItem> items;
611
612 // No pre-allocation - log connections never use batching, and for
613 // connections that do, buffers are released after initial sync anyway
614
615 // Add item to the batch (with deduplication)
616 void add_item(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
617 uint8_t aux_data_index = AUX_DATA_UNUSED);
618 // Add item to the front of the batch (for high priority messages like ping)
619 void add_item_front(EntityBase *entity, uint8_t message_type, uint8_t estimated_size);
620 // Single push_back site to avoid duplicate _M_realloc_insert instantiation
621 void push_item(const BatchItem &item);
622
623 // Clear all items
624 void clear() {
625 items.clear();
627 }
628
629 // Remove processed items from the front — noinline to keep memmove out of warm callers
630 void remove_front(size_t count) __attribute__((noinline)) { items.erase(items.begin(), items.begin() + count); }
631
632 bool empty() const { return items.empty(); }
633 size_t size() const { return items.size(); }
634 const BatchItem &operator[](size_t index) const { return items[index]; }
635
636 // Release excess capacity - only releases if items already empty
638 // Safe to call: batch is processed before release_buffer is called,
639 // and if any items remain (partial processing), we must not clear them.
640 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
641 if (items.empty()) {
642 std::vector<BatchItem>().swap(items);
643 }
644 }
645 };
646
647 // DeferredBatch here (16 bytes, 4-byte aligned)
649
650 // ConnectionState enum for type safety
651 enum class ConnectionState : uint8_t {
653 CONNECTED = 1,
654 AUTHENTICATED = 2,
655 };
656
657 // Group 5: Pack all small members together to minimize padding
658 // This group starts at a 4-byte boundary after DeferredBatch
659 struct APIFlags {
660 // Connection state only needs 2 bits (3 states)
661 uint8_t connection_state : 2;
662 // Log subscription needs 3 bits (log levels 0-7)
663 uint8_t log_subscription : 3;
664 // Boolean flags (1 bit each)
665 uint8_t remove : 1;
667 uint8_t sent_ping : 1;
668
670 uint8_t next_close : 1;
671 uint8_t batch_scheduled : 1;
672 uint8_t batch_first_message : 1; // For batch buffer allocation
673 uint8_t should_try_send_immediately : 1; // True after initial states are sent
674#ifdef HAS_PROTO_MESSAGE_DUMP
675 uint8_t log_only_mode : 1;
676#endif
677 } flags_{}; // 2 bytes total
678
679 // 2-byte types immediately after flags_ (no padding between them)
682 // 1-byte type to fill padding
684 // Total: 2 (flags) + 2 + 2 + 1 = 7 bytes, then 1 byte padding to next 4-byte boundary
685
687 // Message will use 8 more bytes than the minimum size, and typical
688 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
689 // If its IPv6 the header is 40 bytes, and if its IPv4
690 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
691 // available for the payload. But we also need to add the size of
692 // the protobuf overhead, which is 8 bytes.
693 //
694 // To be safe we pick 1390 bytes as the maximum size
695 // to send in one go. This is the maximum size of a single packet
696 // that can be sent over the network.
697 // This is to avoid fragmentation of the packet.
698 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
699
700 bool schedule_batch_();
701 void process_batch_();
702 void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size)
703 __attribute__((noinline));
705 this->deferred_batch_.clear();
706 this->flags_.batch_scheduled = false;
707 }
708
709 // Dispatch message encoding based on message_type - replaces function pointer storage
710 // Switch assigns pointer, single call site for smaller code size
711 uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first);
712
713#ifdef HAS_PROTO_MESSAGE_DUMP
715 this->flags_.log_only_mode = true;
716 this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true);
717 this->flags_.log_only_mode = false;
718 }
719#endif
720
721 // Helper to check if a message type should bypass batching
722 // Returns true if:
723 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
724 // the main loop is blocked, e.g., during OTA updates)
725 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
726 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
727 // AND batch_delay = 0)
728 inline bool should_send_immediately_(uint8_t message_type) const {
729 return (
730#ifdef USE_UPDATE
731 message_type == UpdateStateResponse::MESSAGE_TYPE ||
732#endif
733#ifdef USE_EVENT
734 message_type == EventResponse::MESSAGE_TYPE ||
735#endif
736 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
737 }
738
739 // Helper method to send a message either immediately or via batching
740 // Tries immediate send if should_send_immediately_() returns true and buffer has space
741 // Falls back to batching if immediate send fails or isn't applicable
742 bool send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
743 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED);
744
745 // Helper function to schedule a deferred message with known message type
746 bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
747 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED) {
748 this->deferred_batch_.add_item(entity, message_type, estimated_size, aux_data_index);
749 return this->schedule_batch_();
750 }
751
752 // Helper function to schedule a high priority message at the front of the batch
753 bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size) {
754 this->deferred_batch_.add_item_front(entity, message_type, estimated_size);
755 return this->schedule_batch_();
756 }
757
758 // Helper function to log client messages with name and peername
759 void log_client_(int level, const LogString *message);
760 // Helper function to log API errors with errno
761 void log_warning_(const LogString *message, APIError err);
762 // Helper to handle fatal errors with logging
763 inline void fatal_error_with_log_(const LogString *message, APIError err) {
764 this->on_fatal_error();
765 this->log_warning_(message, err);
766 }
767};
768
769} // namespace esphome::api
770#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(size_t n) ESPHOME_ALWAYS_INLINE
Definition api_buffer.h:39
void resize(size_t n) ESPHOME_ALWAYS_INLINE
Definition api_buffer.h:43
struct esphome::api::APIConnection::APIFlags flags_
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) override
void on_subscribe_bluetooth_connections_free_request() override
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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_serial_proxy_request(const SerialProxyRequest &msg) override
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_request(const ZWaveProxyRequest &msg) override
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_subscribe_logs_request(const SubscribeLogsRequest &msg) override
void on_execute_service_request(const ExecuteServiceRequest &msg) override
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_date_time_command_request(const DateTimeCommandRequest &msg) override
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 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_switch_command_request(const SwitchCommandRequest &msg) override
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_unsubscribe_bluetooth_le_advertisements_request() override
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) override
void on_lock_command_request(const LockCommandRequest &msg) override
void on_bluetooth_device_request(const BluetoothDeviceRequest &msg) override
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) override
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)
void on_time_command_request(const TimeCommandRequest &msg) override
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_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg) override
void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg) override
bool send_fan_state(fan::Fan *fan)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
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)
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)
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)
void(*)(const void *, ProtoWriteBuffer &) MessageEncodeFn
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)
void on_homeassistant_action_response(const HomeassistantActionResponse &msg) override
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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
void on_text_command_request(const TextCommandRequest &msg) override
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor)
bool send_valve_state(valve::Valve *valve)
bool send_voice_assistant_get_configuration_response_(const VoiceAssistantConfigurationRequest &msg)
bool send_select_state(select::Select *select)
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_disconnect_response() override
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_media_player_command_request(const MediaPlayerCommandRequest &msg) override
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_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg) override
void log_client_(int level, const LogString *message)
static uint16_t encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg, APIConnection *conn, uint32_t remaining_size)
void on_hello_request(const HelloRequest &msg) override
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg) override
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)
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 prepare_first_message_buffer(APIBuffer &shared_buf, size_t payload_size)
bool send_update_state(update::UpdateEntity *update)
void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg) override
void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override
void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg) override
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static void encode_msg_noop(const void *, ProtoWriteBuffer &)
void on_update_command_request(const UpdateCommandRequest &msg) override
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 log_warning_(const LogString *message, APIError err)
void on_fan_command_request(const FanCommandRequest &msg) override
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void begin_iterator_(ActiveIterator type)
std::unique_ptr< camera::CameraImageReader > image_reader_
void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg) override
void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg) override
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
void on_serial_proxy_write_request(const SerialProxyWriteRequest &msg) override
void on_select_command_request(const SelectCommandRequest &msg) override
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)
void on_camera_image_request(const CameraImageRequest &msg) override
bool send_date_state(datetime::DateEntity *date)
void on_light_command_request(const LightCommandRequest &msg) override
void on_subscribe_home_assistant_states_request() override
bool is_connection_setup() override
void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg) override
void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg) override
bool send_number_state(number::Number *number)
void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) override
void on_list_entities_request() override
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) override
std::unique_ptr< APINoiseFrameHelper > helper_
bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size)
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
bool send_hello_response_(const HelloRequest &msg)
void on_cover_command_request(const CoverCommandRequest &msg) override
void send_homeassistant_action(const HomeassistantActionRequest &call)
bool send_light_state(light::LightState *light)
void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg) override
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
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
static uint16_t try_send_time_info(EntityBase *entity, 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)
void on_get_time_response(const GetTimeResponse &value) override
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg) override
void on_water_heater_command_request(const WaterHeaterCommandRequest &msg) override
void on_button_command_request(const ButtonCommandRequest &msg) override
void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg) override
bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *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_subscribe_states_request() override
void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg) override
bool send_datetime_state(datetime::DateTimeEntity *datetime)
void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) override
void on_subscribe_homeassistant_services_request() override
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_valve_command_request(const ValveCommandRequest &msg) override
void on_date_command_request(const DateCommandRequest &msg) override
bool send_text_state(text::Text *text)
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_number_command_request(const NumberCommandRequest &msg) override
static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len)
void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg) override
void process_iterator_batch_(ComponentIterator &iterator)
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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)
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override
void on_climate_command_request(const ClimateCommandRequest &msg) override
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 __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)
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2821
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:2961
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:186
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__
const char * message
Definition component.cpp:38
uint16_t type
void proto_encode_msg(const void *msg, ProtoWriteBuffer &buf)
Definition proto.h:672
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 push_item(const BatchItem &item)
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()