ESPHome 2026.1.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#include "api_pb2.h"
7#include "api_pb2_service.h"
8#include "api_server.h"
12
13#include <functional>
14#include <vector>
15
16namespace esphome::api {
17
18// Client information structure
19struct ClientInfo {
20 std::string name; // Client name from Hello message
21 std::string peername; // IP:port from socket
22};
23
24// Keepalive timeout in milliseconds
25static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
26// Maximum number of entities to process in a single batch during initial state/info sending
27// This was increased from 20 to 24 after removing the unique_id field from entity info messages,
28// which reduced message sizes allowing more entities per batch without exceeding packet limits
29static constexpr size_t MAX_INITIAL_PER_BATCH = 24;
30// Maximum number of packets to process in a single batch (platform-dependent)
31// This limit exists to prevent stack overflow from the PacketInfo array in process_batch_
32// Each PacketInfo is 8 bytes, so 64 * 8 = 512 bytes, 32 * 8 = 256 bytes
33#if defined(USE_ESP32) || defined(USE_HOST)
34static constexpr size_t MAX_PACKETS_PER_BATCH = 64; // ESP32 has 8KB+ stack, HOST has plenty
35#else
36static constexpr size_t MAX_PACKETS_PER_BATCH = 32; // ESP8266/RP2040/etc have smaller stacks
37#endif
38
39class APIConnection final : public APIServerConnection {
40 public:
41 friend class APIServer;
43 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
44 virtual ~APIConnection();
45
46 void start();
47 void loop();
48
53#ifdef USE_BINARY_SENSOR
55#endif
56#ifdef USE_COVER
57 bool send_cover_state(cover::Cover *cover);
58 void cover_command(const CoverCommandRequest &msg) override;
59#endif
60#ifdef USE_FAN
61 bool send_fan_state(fan::Fan *fan);
62 void fan_command(const FanCommandRequest &msg) override;
63#endif
64#ifdef USE_LIGHT
66 void light_command(const LightCommandRequest &msg) override;
67#endif
68#ifdef USE_SENSOR
70#endif
71#ifdef USE_SWITCH
72 bool send_switch_state(switch_::Switch *a_switch);
73 void switch_command(const SwitchCommandRequest &msg) override;
74#endif
75#ifdef USE_TEXT_SENSOR
77#endif
78#ifdef USE_CAMERA
79 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
80 void camera_image(const CameraImageRequest &msg) override;
81#endif
82#ifdef USE_CLIMATE
84 void climate_command(const ClimateCommandRequest &msg) override;
85#endif
86#ifdef USE_NUMBER
88 void number_command(const NumberCommandRequest &msg) override;
89#endif
90#ifdef USE_DATETIME_DATE
92 void date_command(const DateCommandRequest &msg) override;
93#endif
94#ifdef USE_DATETIME_TIME
96 void time_command(const TimeCommandRequest &msg) override;
97#endif
98#ifdef USE_DATETIME_DATETIME
100 void datetime_command(const DateTimeCommandRequest &msg) override;
101#endif
102#ifdef USE_TEXT
103 bool send_text_state(text::Text *text);
104 void text_command(const TextCommandRequest &msg) override;
105#endif
106#ifdef USE_SELECT
107 bool send_select_state(select::Select *select);
108 void select_command(const SelectCommandRequest &msg) override;
109#endif
110#ifdef USE_BUTTON
111 void button_command(const ButtonCommandRequest &msg) override;
112#endif
113#ifdef USE_LOCK
114 bool send_lock_state(lock::Lock *a_lock);
115 void lock_command(const LockCommandRequest &msg) override;
116#endif
117#ifdef USE_VALVE
118 bool send_valve_state(valve::Valve *valve);
119 void valve_command(const ValveCommandRequest &msg) override;
120#endif
121#ifdef USE_MEDIA_PLAYER
123 void media_player_command(const MediaPlayerCommandRequest &msg) override;
124#endif
125 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
126#ifdef USE_API_HOMEASSISTANT_SERVICES
132#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
134#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
135#endif // USE_API_HOMEASSISTANT_SERVICES
136#ifdef USE_BLUETOOTH_PROXY
139
140 void bluetooth_device_request(const BluetoothDeviceRequest &msg) override;
141 void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override;
142 void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override;
146 void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override;
149
150#endif
151#ifdef USE_HOMEASSISTANT_TIME
156#endif
157
158#ifdef USE_VOICE_ASSISTANT
160 void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
162 void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override;
167#endif
168
169#ifdef USE_ZWAVE_PROXY
170 void zwave_proxy_frame(const ZWaveProxyFrame &msg) override;
171 void zwave_proxy_request(const ZWaveProxyRequest &msg) override;
172#endif
173
174#ifdef USE_ALARM_CONTROL_PANEL
177#endif
178
179#ifdef USE_WATER_HEATER
182#endif
183
184#ifdef USE_EVENT
185 void send_event(event::Event *event, const char *event_type);
186#endif
187
188#ifdef USE_UPDATE
190 void update_command(const UpdateCommandRequest &msg) override;
191#endif
192
193 void on_disconnect_response(const DisconnectResponse &value) override;
194 void on_ping_response(const PingResponse &value) override {
195 // we initiated ping
196 this->flags_.sent_ping = false;
197 }
198#ifdef USE_API_HOMEASSISTANT_STATES
200#endif
201#ifdef USE_HOMEASSISTANT_TIME
202 void on_get_time_response(const GetTimeResponse &value) override;
203#endif
204 bool send_hello_response(const HelloRequest &msg) override;
205#ifdef USE_API_PASSWORD
206 bool send_authenticate_response(const AuthenticationRequest &msg) override;
207#endif
208 bool send_disconnect_response(const DisconnectRequest &msg) override;
209 bool send_ping_response(const PingRequest &msg) override;
210 bool send_device_info_response(const DeviceInfoRequest &msg) override;
212 void subscribe_states(const SubscribeStatesRequest &msg) override {
213 this->flags_.state_subscription = true;
214 // Start initial state iterator only if no iterator is active
215 // If list_entities is running, we'll start initial_state when it completes
218 }
219 }
220 void subscribe_logs(const SubscribeLogsRequest &msg) override {
221 this->flags_.log_subscription = msg.level;
222 if (msg.dump_config)
224 }
225#ifdef USE_API_HOMEASSISTANT_SERVICES
229#endif
230#ifdef USE_API_HOMEASSISTANT_STATES
232#endif
233#ifdef USE_API_USER_DEFINED_ACTIONS
234 void execute_service(const ExecuteServiceRequest &msg) override;
235#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
236 void send_execute_service_response(uint32_t call_id, bool success, const std::string &error_message);
237#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
238 void send_execute_service_response(uint32_t call_id, bool success, const std::string &error_message,
239 const uint8_t *response_data, size_t response_data_len);
240#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
241#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
242#endif
243#ifdef USE_API_NOISE
245#endif
246
247 bool is_authenticated() override {
249 }
250 bool is_connection_setup() override {
252 this->is_authenticated();
253 }
254 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
255
256 // Get client API version for feature detection
257 bool client_supports_api_version(uint16_t major, uint16_t minor) const {
258 return this->client_api_version_major_ > major ||
259 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
260 }
261
262 void on_fatal_error() override;
263#ifdef USE_API_PASSWORD
264 void on_unauthenticated_access() override;
265#endif
266 void on_no_setup_connection() override;
267 ProtoWriteBuffer create_buffer(uint32_t reserve_size) override {
268 // FIXME: ensure no recursive writes can happen
269
270 // Get header padding size - used for both reserve and insert
271 uint8_t header_padding = this->helper_->frame_header_padding();
272 // Get shared buffer from parent server
273 std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
274 this->prepare_first_message_buffer(shared_buf, header_padding,
275 reserve_size + header_padding + this->helper_->frame_footer_size());
276 return {&shared_buf};
277 }
278
279 void prepare_first_message_buffer(std::vector<uint8_t> &shared_buf, size_t header_padding, size_t total_size) {
280 shared_buf.clear();
281 // Reserve space for header padding + message + footer
282 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
283 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
284 shared_buf.reserve(total_size);
285 // Resize to add header padding so message encoding starts at the correct position
286 shared_buf.resize(header_padding);
287 }
288
289 bool try_to_clear_buffer(bool log_out_of_space);
290 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
291
292 const std::string &get_name() const { return this->client_info_.name; }
293 const std::string &get_peername() const { return this->client_info_.peername; }
294
295 protected:
296 // Helper function to handle authentication completion
298
299#ifdef USE_CAMERA
301#endif
302
303#ifdef USE_API_HOMEASSISTANT_STATES
305#endif
306
307 // Non-template helper to encode any ProtoMessage
308 static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn,
309 uint32_t remaining_size, bool is_single);
310
311 // Helper to fill entity state base and encode message
312 static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, uint8_t message_type,
313 APIConnection *conn, uint32_t remaining_size, bool is_single) {
314 msg.key = entity->get_object_id_hash();
315#ifdef USE_DEVICES
316 msg.device_id = entity->get_device_id();
317#endif
318 return encode_message_to_buffer(msg, message_type, conn, remaining_size, is_single);
319 }
320
321 // Helper to fill entity info base and encode message
322 static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, uint8_t message_type,
323 APIConnection *conn, uint32_t remaining_size, bool is_single) {
324 // Set common fields that are shared by all entity types
325 msg.key = entity->get_object_id_hash();
326 // Get object_id with zero heap allocation
327 // Static case returns direct reference, dynamic case uses buffer
328 char object_id_buf[OBJECT_ID_MAX_LEN];
329 msg.set_object_id(entity->get_object_id_to(object_id_buf));
330
331 if (entity->has_own_name()) {
332 msg.set_name(entity->get_name());
333 }
334
335 // Set common EntityBase properties
336#ifdef USE_ENTITY_ICON
337 msg.set_icon(entity->get_icon_ref());
338#endif
340 msg.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
341#ifdef USE_DEVICES
342 msg.device_id = entity->get_device_id();
343#endif
344 return encode_message_to_buffer(msg, message_type, conn, remaining_size, is_single);
345 }
346
347#ifdef USE_VOICE_ASSISTANT
348 // Helper to check voice assistant validity and connection ownership
349 inline bool check_voice_assistant_api_connection_() const;
350#endif
351
352 // Helper method to process multiple entities from an iterator in a batch
353 template<typename Iterator> void process_iterator_batch_(Iterator &iterator) {
354 size_t initial_size = this->deferred_batch_.size();
355 while (!iterator.completed() && (this->deferred_batch_.size() - initial_size) < MAX_INITIAL_PER_BATCH) {
356 iterator.advance();
357 }
358
359 // If the batch is full, process it immediately
360 // Note: iterator.advance() already calls schedule_batch_() via schedule_message_()
361 if (this->deferred_batch_.size() >= MAX_INITIAL_PER_BATCH) {
362 this->process_batch_();
363 }
364 }
365
366#ifdef USE_BINARY_SENSOR
367 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
368 bool is_single);
369 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
370 bool is_single);
371#endif
372#ifdef USE_COVER
373 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
374 bool is_single);
375 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
376#endif
377#ifdef USE_FAN
378 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
379 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
380#endif
381#ifdef USE_LIGHT
382 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
383 bool is_single);
384 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
385#endif
386#ifdef USE_SENSOR
387 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
388 bool is_single);
389 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
390 bool is_single);
391#endif
392#ifdef USE_SWITCH
393 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
394 bool is_single);
395 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
396 bool is_single);
397#endif
398#ifdef USE_TEXT_SENSOR
399 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
400 bool is_single);
401 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
402 bool is_single);
403#endif
404#ifdef USE_CLIMATE
405 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
406 bool is_single);
407 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
408 bool is_single);
409#endif
410#ifdef USE_NUMBER
411 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
412 bool is_single);
413 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
414 bool is_single);
415#endif
416#ifdef USE_DATETIME_DATE
417 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
418 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
419#endif
420#ifdef USE_DATETIME_TIME
421 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
422 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
423#endif
424#ifdef USE_DATETIME_DATETIME
425 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
426 bool is_single);
427 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
428 bool is_single);
429#endif
430#ifdef USE_TEXT
431 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
432 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
433#endif
434#ifdef USE_SELECT
435 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
436 bool is_single);
437 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
438 bool is_single);
439#endif
440#ifdef USE_BUTTON
441 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
442 bool is_single);
443#endif
444#ifdef USE_LOCK
445 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
446 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
447#endif
448#ifdef USE_VALVE
449 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
450 bool is_single);
451 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
452#endif
453#ifdef USE_MEDIA_PLAYER
454 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
455 bool is_single);
456 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
457 bool is_single);
458#endif
459#ifdef USE_ALARM_CONTROL_PANEL
460 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
461 bool is_single);
462 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
463 bool is_single);
464#endif
465#ifdef USE_WATER_HEATER
466 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
467 bool is_single);
468 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
469 bool is_single);
470#endif
471#ifdef USE_EVENT
472 static uint16_t try_send_event_response(event::Event *event, const char *event_type, APIConnection *conn,
473 uint32_t remaining_size, bool is_single);
474 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
475#endif
476#ifdef USE_UPDATE
477 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
478 bool is_single);
479 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
480 bool is_single);
481#endif
482#ifdef USE_CAMERA
483 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
484 bool is_single);
485#endif
486
487 // Method for ListEntitiesDone batching
488 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
489 bool is_single);
490
491 // Method for DisconnectRequest batching
492 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
493 bool is_single);
494
495 // Batch message method for ping requests
496 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
497 bool is_single);
498
499 // === Optimal member ordering for 32-bit systems ===
500
501 // Group 1: Pointers (4 bytes each on 32-bit)
502 std::unique_ptr<APIFrameHelper> helper_;
504
505 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
506 // These iterators are never active simultaneously - list_entities runs to completion
507 // before initial_state begins, so we use a union with explicit construction/destruction.
508 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
509
513 // Constructor/destructor do nothing - use placement new/explicit destructor
517
518 // Helper methods for iterator lifecycle management
521#ifdef USE_CAMERA
522 std::unique_ptr<camera::CameraImageReader> image_reader_;
523#endif
524
525 // Group 3: Client info struct (24 bytes on 32-bit: 2 strings × 12 bytes each)
527
528 // Group 4: 4-byte types
530#ifdef USE_API_HOMEASSISTANT_STATES
532#endif
533
534 // Function pointer type for message encoding
535 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single);
536
538 public:
539 MessageCreator(MessageCreatorPtr ptr) { data_.function_ptr = ptr; }
540 explicit MessageCreator(const char *str_value) { data_.const_char_ptr = str_value; }
541
542 // Call operator - uses message_type to determine union type
543 uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single,
544 uint8_t message_type) const;
545
546 private:
547 union Data {
548 MessageCreatorPtr function_ptr;
549 const char *const_char_ptr;
550 } data_; // 4 bytes on 32-bit, 8 bytes on 64-bit
551 };
552
553 // Generic batching mechanism for both state updates and entity info
555 struct BatchItem {
556 EntityBase *entity; // Entity pointer
557 MessageCreator creator; // Function that creates the message when needed
558 uint8_t message_type; // Message type for overhead calculation (max 255)
559 uint8_t estimated_size; // Estimated message size (max 255 bytes)
560
561 // Constructor for creating BatchItem
564 };
565
566 std::vector<BatchItem> items;
567 uint32_t batch_start_time{0};
568
569 // No pre-allocation - log connections never use batching, and for
570 // connections that do, buffers are released after initial sync anyway
571
572 // Add item to the batch
573 void add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
574 // Add item to the front of the batch (for high priority messages like ping)
575 void add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
576
577 // Clear all items
578 void clear() {
579 items.clear();
581 }
582
583 // Remove processed items from the front
584 void remove_front(size_t count) { items.erase(items.begin(), items.begin() + count); }
585
586 bool empty() const { return items.empty(); }
587 size_t size() const { return items.size(); }
588 const BatchItem &operator[](size_t index) const { return items[index]; }
589 // Release excess capacity - only releases if items already empty
591 // Safe to call: batch is processed before release_buffer is called,
592 // and if any items remain (partial processing), we must not clear them.
593 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
594 if (items.empty()) {
595 std::vector<BatchItem>().swap(items);
596 }
597 }
598 };
599
600 // DeferredBatch here (16 bytes, 4-byte aligned)
602
603 // ConnectionState enum for type safety
604 enum class ConnectionState : uint8_t {
606 CONNECTED = 1,
607 AUTHENTICATED = 2,
608 };
609
610 // Group 5: Pack all small members together to minimize padding
611 // This group starts at a 4-byte boundary after DeferredBatch
612 struct APIFlags {
613 // Connection state only needs 2 bits (3 states)
614 uint8_t connection_state : 2;
615 // Log subscription needs 3 bits (log levels 0-7)
616 uint8_t log_subscription : 3;
617 // Boolean flags (1 bit each)
618 uint8_t remove : 1;
620 uint8_t sent_ping : 1;
621
623 uint8_t next_close : 1;
624 uint8_t batch_scheduled : 1;
625 uint8_t batch_first_message : 1; // For batch buffer allocation
626 uint8_t should_try_send_immediately : 1; // True after initial states are sent
627#ifdef HAS_PROTO_MESSAGE_DUMP
628 uint8_t log_only_mode : 1;
629#endif
630 } flags_{}; // 2 bytes total
631
632 // 2-byte types immediately after flags_ (no padding between them)
635 // 1-byte type to fill padding
637 // Total: 2 (flags) + 2 + 2 + 1 = 7 bytes, then 1 byte padding to next 4-byte boundary
638
639 uint32_t get_batch_delay_ms_() const;
640 // Message will use 8 more bytes than the minimum size, and typical
641 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
642 // If its IPv6 the header is 40 bytes, and if its IPv4
643 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
644 // available for the payload. But we also need to add the size of
645 // the protobuf overhead, which is 8 bytes.
646 //
647 // To be safe we pick 1390 bytes as the maximum size
648 // to send in one go. This is the maximum size of a single packet
649 // that can be sent over the network.
650 // This is to avoid fragmentation of the packet.
651 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
652
653 bool schedule_batch_();
654 void process_batch_();
656 this->deferred_batch_.clear();
657 this->flags_.batch_scheduled = false;
658 }
659
660#ifdef HAS_PROTO_MESSAGE_DUMP
661 // Helper to log a proto message from a MessageCreator object
662 void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type) {
663 this->flags_.log_only_mode = true;
664 creator(entity, this, MAX_BATCH_PACKET_SIZE, true, message_type);
665 this->flags_.log_only_mode = false;
666 }
667
669 // Use the helper to log the message
670 this->log_proto_message_(item.entity, item.creator, item.message_type);
671 }
672#endif
673
674 // Helper to check if a message type should bypass batching
675 // Returns true if:
676 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
677 // the main loop is blocked, e.g., during OTA updates)
678 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
679 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
680 // AND batch_delay = 0)
681 inline bool should_send_immediately_(uint8_t message_type) const {
682 return (
683#ifdef USE_UPDATE
684 message_type == UpdateStateResponse::MESSAGE_TYPE ||
685#endif
686#ifdef USE_EVENT
687 message_type == EventResponse::MESSAGE_TYPE ||
688#endif
689 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
690 }
691
692 // Helper method to send a message either immediately or via batching
693 // Tries immediate send if should_send_immediately_() returns true and buffer has space
694 // Falls back to batching if immediate send fails or isn't applicable
695 bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint8_t message_type,
696 uint8_t estimated_size) {
697 if (this->should_send_immediately_(message_type) && this->helper_->can_write_without_blocking()) {
698 // Now actually encode and send
699 if (creator(entity, this, MAX_BATCH_PACKET_SIZE, true) &&
700 this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, message_type)) {
701#ifdef HAS_PROTO_MESSAGE_DUMP
702 // Log the message in verbose mode
703 this->log_proto_message_(entity, MessageCreator(creator), message_type);
704#endif
705 return true;
706 }
707
708 // If immediate send failed, fall through to batching
709 }
710
711 // Fall back to scheduled batching
712 return this->schedule_message_(entity, creator, message_type, estimated_size);
713 }
714
715 // Overload for MessageCreator (used by events which need to capture event_type)
716 bool send_message_smart_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size) {
717 // Try to send immediately if message type should bypass batching and buffer has space
718 if (this->should_send_immediately_(message_type) && this->helper_->can_write_without_blocking()) {
719 // Now actually encode and send
720 if (creator(entity, this, MAX_BATCH_PACKET_SIZE, true, message_type) &&
721 this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, message_type)) {
722#ifdef HAS_PROTO_MESSAGE_DUMP
723 // Log the message in verbose mode
724 this->log_proto_message_(entity, creator, message_type);
725#endif
726 return true;
727 }
728
729 // If immediate send failed, fall through to batching
730 }
731
732 // Fall back to scheduled batching
733 return this->schedule_message_(entity, creator, message_type, estimated_size);
734 }
735
736 // Helper function to schedule a deferred message with known message type
737 bool schedule_message_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size) {
738 this->deferred_batch_.add_item(entity, creator, message_type, estimated_size);
739 return this->schedule_batch_();
740 }
741
742 // Overload for function pointers (for info messages and current state reads)
743 bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
744 uint8_t estimated_size) {
745 return schedule_message_(entity, MessageCreator(function_ptr), message_type, estimated_size);
746 }
747
748 // Helper function to schedule a high priority message at the front of the batch
749 bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
750 uint8_t estimated_size) {
751 this->deferred_batch_.add_item_front(entity, MessageCreator(function_ptr), message_type, estimated_size);
752 return this->schedule_batch_();
753 }
754
755 // Helper function to log API errors with errno
756 void log_warning_(const LogString *message, APIError err);
757 // Helper to handle fatal errors with logging
758 inline void fatal_error_with_log_(const LogString *message, APIError err) {
759 this->on_fatal_error();
760 this->log_warning_(message, err);
761 }
762};
763
764} // namespace esphome::api
765#endif
bool has_own_name() const
Definition entity_base.h:33
uint32_t get_object_id_hash()
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:85
uint32_t get_device_id() const
Definition entity_base.h:96
bool is_disabled_by_default() const
Definition entity_base.h:70
EntityCategory get_entity_category() const
Definition entity_base.h:74
StringRef get_object_id_to(std::span< char, OBJECT_ID_MAX_LEN > buf) const
Get object_id with zero heap allocation For static case: returns StringRef to internal storage (buffe...
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single, uint8_t message_type) const
static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_authenticate_response(const AuthenticationRequest &msg) override
struct esphome::api::APIConnection::APIFlags flags_
bool send_ping_response(const PingRequest &msg) override
bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint8_t message_type, uint8_t estimated_size)
void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override
static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void select_command(const SelectCommandRequest &msg) override
static uint16_t try_send_event_response(event::Event *event, const char *event_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_media_player_state(media_player::MediaPlayer *media_player)
void zwave_proxy_frame(const ZWaveProxyFrame &msg) override
static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_time_state(datetime::TimeEntity *time)
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void time_command(const TimeCommandRequest &msg) override
static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_water_heater_state(water_heater::WaterHeater *water_heater)
void update_command(const UpdateCommandRequest &msg) override
void on_ping_response(const PingResponse &value) override
static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) override
static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void prepare_first_message_buffer(std::vector< uint8_t > &shared_buf, size_t header_padding, size_t total_size)
bool send_subscribe_bluetooth_connections_free_response(const SubscribeBluetoothConnectionsFreeRequest &msg) override
ProtoWriteBuffer create_buffer(uint32_t reserve_size) override
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor)
bool send_fan_state(fan::Fan *fan)
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_scanner_set_mode(const BluetoothScannerSetModeRequest &msg) override
static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
void zwave_proxy_request(const ZWaveProxyRequest &msg) override
bool check_voice_assistant_api_connection_() const
void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type)
union esphome::api::APIConnection::IteratorUnion iterator_storage_
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void subscribe_logs(const SubscribeLogsRequest &msg) override
std::unique_ptr< APIFrameHelper > helper_
void date_command(const DateCommandRequest &msg) override
bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type, uint8_t estimated_size)
void set_camera_state(std::shared_ptr< camera::CameraImage > image)
const std::string & get_peername() const
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
uint32_t get_batch_delay_ms_() const
bool send_sensor_state(sensor::Sensor *sensor)
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
void datetime_command(const DateTimeCommandRequest &msg) override
uint16_t(*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single) MessageCreatorPtr
void voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) override
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor)
void process_iterator_batch_(Iterator &iterator)
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_valve_state(valve::Valve *valve)
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_select_state(select::Select *select)
void send_event(event::Event *event, const char *event_type)
bool send_switch_state(switch_::Switch *a_switch)
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void text_command(const TextCommandRequest &msg) override
bool should_send_immediately_(uint8_t message_type) const
bool send_lock_state(lock::Lock *a_lock)
bool send_hello_response(const HelloRequest &msg) override
static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_update_state(update::UpdateEntity *update)
void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override
static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool schedule_message_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override
static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_device_info_response(const DeviceInfoRequest &msg) override
bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type, uint8_t estimated_size)
void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override
void fatal_error_with_log_(const LogString *message, APIError err)
void number_command(const NumberCommandRequest &msg) override
const std::string & get_name() const
void log_warning_(const LogString *message, APIError err)
void list_entities(const ListEntitiesRequest &msg) override
void begin_iterator_(ActiveIterator type)
std::unique_ptr< camera::CameraImageReader > image_reader_
void media_player_command(const MediaPlayerCommandRequest &msg) override
void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override
void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) override
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
void subscribe_states(const SubscribeStatesRequest &msg) override
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override
bool send_date_state(datetime::DateEntity *date)
static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_read_descriptor(const BluetoothGATTReadDescriptorRequest &msg) override
void climate_command(const ClimateCommandRequest &msg) override
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_get_services(const BluetoothGATTGetServicesRequest &msg) override
bool is_connection_setup() override
bool send_number_state(number::Number *number)
void send_execute_service_response(uint32_t call_id, bool success, const std::string &error_message)
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) override
void fan_command(const FanCommandRequest &msg) override
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_noise_encryption_set_key_response(const NoiseEncryptionSetKeyRequest &msg) override
void send_homeassistant_action(const HomeassistantActionRequest &call)
bool send_light_state(light::LightState *light)
bool send_voice_assistant_get_configuration_response(const VoiceAssistantConfigurationRequest &msg) override
void valve_command(const ValveCommandRequest &msg) override
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
void cover_command(const CoverCommandRequest &msg) override
uint8_t get_log_subscription_level() const
void on_get_time_response(const GetTimeResponse &value) override
static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_water_heater_command_request(const WaterHeaterCommandRequest &msg) override
void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override
void on_disconnect_response(const DisconnectResponse &value) override
bool send_datetime_state(datetime::DateTimeEntity *datetime)
static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_disconnect_response(const DisconnectRequest &msg) override
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void camera_image(const CameraImageRequest &msg) override
void light_command(const LightCommandRequest &msg) override
bool send_text_state(text::Text *text)
void switch_command(const SwitchCommandRequest &msg) override
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_message_smart_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len)
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool client_supports_api_version(uint16_t major, uint16_t minor) const
void lock_command(const LockCommandRequest &msg) override
void bluetooth_device_request(const BluetoothDeviceRequest &msg) override
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override
void execute_service(const ExecuteServiceRequest &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)
void button_command(const ButtonCommandRequest &msg) override
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_unauthenticated_access() override
bool send_message(const ProtoMessage &msg, uint8_t message_type)
std::vector< uint8_t > & get_shared_buffer_ref()
Definition api_server.h:75
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2934
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:1245
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:1135
void set_object_id(const StringRef &ref)
Definition api_pb2.h:319
enums::EntityCategory entity_category
Definition api_pb2.h:328
void set_icon(const StringRef &ref)
Definition api_pb2.h:326
void set_name(const StringRef &ref)
Definition api_pb2.h:322
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:620
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:621
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:3077
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:181
Base class for all cover devices.
Definition cover.h:112
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
Base class for all locks.
Definition lock.h:111
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:30
Base-class for all sensors.
Definition sensor.h:43
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:24
Base class for all valve devices.
Definition valve.h:106
const char * message
Definition component.cpp:38
uint16_t type
Application App
Global storage of Application pointer - only one Application can exist.
BatchItem(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
const BatchItem & operator[](size_t index) const
void add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
void add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)