ESPHome 2026.2.1
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"
13
14#include <functional>
15#include <limits>
16#include <vector>
17
18namespace esphome {
19class ComponentIterator;
20} // namespace esphome
21
22namespace esphome::api {
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// API 1.14+ clients compute object_id client-side, so messages are smaller and we can fit more per batch
28// TODO: Remove MAX_INITIAL_PER_BATCH_LEGACY before 2026.7.0 - all clients should support API 1.14 by then
29static constexpr size_t MAX_INITIAL_PER_BATCH_LEGACY = 24; // For clients < API 1.14 (includes object_id)
30static constexpr size_t MAX_INITIAL_PER_BATCH = 34; // For clients >= API 1.14 (no object_id)
31// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch
32static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_PER_BATCH,
33 "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_PER_BATCH");
34
36 public:
37 friend class APIServer;
39 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
40 virtual ~APIConnection();
41
42 void start();
43 void loop();
44
49#ifdef USE_BINARY_SENSOR
51#endif
52#ifdef USE_COVER
53 bool send_cover_state(cover::Cover *cover);
54 void on_cover_command_request(const CoverCommandRequest &msg) override;
55#endif
56#ifdef USE_FAN
57 bool send_fan_state(fan::Fan *fan);
58 void on_fan_command_request(const FanCommandRequest &msg) override;
59#endif
60#ifdef USE_LIGHT
62 void on_light_command_request(const LightCommandRequest &msg) override;
63#endif
64#ifdef USE_SENSOR
66#endif
67#ifdef USE_SWITCH
68 bool send_switch_state(switch_::Switch *a_switch);
69 void on_switch_command_request(const SwitchCommandRequest &msg) override;
70#endif
71#ifdef USE_TEXT_SENSOR
73#endif
74#ifdef USE_CAMERA
75 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
76 void on_camera_image_request(const CameraImageRequest &msg) override;
77#endif
78#ifdef USE_CLIMATE
80 void on_climate_command_request(const ClimateCommandRequest &msg) override;
81#endif
82#ifdef USE_NUMBER
84 void on_number_command_request(const NumberCommandRequest &msg) override;
85#endif
86#ifdef USE_DATETIME_DATE
88 void on_date_command_request(const DateCommandRequest &msg) override;
89#endif
90#ifdef USE_DATETIME_TIME
92 void on_time_command_request(const TimeCommandRequest &msg) override;
93#endif
94#ifdef USE_DATETIME_DATETIME
97#endif
98#ifdef USE_TEXT
99 bool send_text_state(text::Text *text);
100 void on_text_command_request(const TextCommandRequest &msg) override;
101#endif
102#ifdef USE_SELECT
103 bool send_select_state(select::Select *select);
104 void on_select_command_request(const SelectCommandRequest &msg) override;
105#endif
106#ifdef USE_BUTTON
107 void on_button_command_request(const ButtonCommandRequest &msg) override;
108#endif
109#ifdef USE_LOCK
110 bool send_lock_state(lock::Lock *a_lock);
111 void on_lock_command_request(const LockCommandRequest &msg) override;
112#endif
113#ifdef USE_VALVE
114 bool send_valve_state(valve::Valve *valve);
115 void on_valve_command_request(const ValveCommandRequest &msg) override;
116#endif
117#ifdef USE_MEDIA_PLAYER
120#endif
121 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
122#ifdef USE_API_HOMEASSISTANT_SERVICES
128#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
130#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
131#endif // USE_API_HOMEASSISTANT_SERVICES
132#ifdef USE_BLUETOOTH_PROXY
135
136 void on_bluetooth_device_request(const BluetoothDeviceRequest &msg) override;
145
146#endif
147#ifdef USE_HOMEASSISTANT_TIME
152#endif
153
154#ifdef USE_VOICE_ASSISTANT
156 void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
158 void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override;
163#endif
164
165#ifdef USE_ZWAVE_PROXY
166 void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) override;
167 void on_z_wave_proxy_request(const ZWaveProxyRequest &msg) override;
168#endif
169
170#ifdef USE_ALARM_CONTROL_PANEL
173#endif
174
175#ifdef USE_WATER_HEATER
178#endif
179
180#ifdef USE_IR_RF
183#endif
184
185#ifdef USE_EVENT
186 void send_event(event::Event *event);
187#endif
188
189#ifdef USE_UPDATE
191 void on_update_command_request(const UpdateCommandRequest &msg) override;
192#endif
193
194 void on_disconnect_response() override;
195 void on_ping_response() override {
196 // we initiated ping
197 this->flags_.sent_ping = false;
198 }
199#ifdef USE_API_HOMEASSISTANT_STATES
201#endif
202#ifdef USE_HOMEASSISTANT_TIME
203 void on_get_time_response(const GetTimeResponse &value) override;
204#endif
205 void on_hello_request(const HelloRequest &msg) override;
206 void on_disconnect_request() override;
207 void on_ping_request() override;
208 void on_device_info_request() override;
211 this->flags_.state_subscription = true;
212 // Start initial state iterator only if no iterator is active
213 // If list_entities is running, we'll start initial_state when it completes
216 }
217 }
219 this->flags_.log_subscription = msg.level;
220 if (msg.dump_config)
222 }
223#ifdef USE_API_HOMEASSISTANT_SERVICES
225#endif
226#ifdef USE_API_HOMEASSISTANT_STATES
228#endif
229#ifdef USE_API_USER_DEFINED_ACTIONS
230 void on_execute_service_request(const ExecuteServiceRequest &msg) override;
231#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
232 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
233#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
234 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
235 const uint8_t *response_data, size_t response_data_len);
236#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
237#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
238#endif
239#ifdef USE_API_NOISE
241#endif
242
243 bool is_authenticated() override {
245 }
246 bool is_connection_setup() override {
248 this->is_authenticated();
249 }
250 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
251
252 // Get client API version for feature detection
253 bool client_supports_api_version(uint16_t major, uint16_t minor) const {
254 return this->client_api_version_major_ > major ||
255 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
256 }
257
258 void on_fatal_error() override;
259 void on_no_setup_connection() override;
260 bool send_message_impl(const ProtoMessage &msg, uint8_t message_type) override;
261
262 void prepare_first_message_buffer(std::vector<uint8_t> &shared_buf, size_t header_padding, size_t total_size) {
263 shared_buf.clear();
264 // Reserve space for header padding + message + footer
265 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
266 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
267 shared_buf.reserve(total_size);
268 // Resize to add header padding so message encoding starts at the correct position
269 shared_buf.resize(header_padding);
270 }
271
272 // Convenience overload - computes frame overhead internally
273 void prepare_first_message_buffer(std::vector<uint8_t> &shared_buf, size_t payload_size) {
274 const uint8_t header_padding = this->helper_->frame_header_padding();
275 const uint8_t footer_size = this->helper_->frame_footer_size();
276 this->prepare_first_message_buffer(shared_buf, header_padding, payload_size + header_padding + footer_size);
277 }
278
279 bool try_to_clear_buffer(bool log_out_of_space);
280 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
281
282 const char *get_name() const { return this->helper_->get_client_name(); }
284 const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
285 return this->helper_->get_peername_to(buf);
286 }
287
288 protected:
289 // Helper function to handle authentication completion
291
292 // Pattern B helpers: send response and return success/failure
293 bool send_hello_response_(const HelloRequest &msg);
295 bool send_ping_response_();
297#ifdef USE_API_NOISE
299#endif
300#ifdef USE_BLUETOOTH_PROXY
302#endif
303#ifdef USE_VOICE_ASSISTANT
305#endif
306
307#ifdef USE_CAMERA
309#endif
310
311#ifdef USE_API_HOMEASSISTANT_STATES
313#endif
314
315 // Non-template helper to encode any ProtoMessage
316 static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn,
317 uint32_t remaining_size);
318
319 // Helper to fill entity state base and encode message
320 static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, uint8_t message_type,
321 APIConnection *conn, uint32_t remaining_size) {
322 msg.key = entity->get_object_id_hash();
323#ifdef USE_DEVICES
324 msg.device_id = entity->get_device_id();
325#endif
326 return encode_message_to_buffer(msg, message_type, conn, remaining_size);
327 }
328
329 // Helper to fill entity info base and encode message
330 static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, uint8_t message_type,
331 APIConnection *conn, uint32_t remaining_size) {
332 // Set common fields that are shared by all entity types
333 msg.key = entity->get_object_id_hash();
334
335 // API 1.14+ clients compute object_id client-side from the entity name
336 // For older clients, we must send object_id for backward compatibility
337 // See: https://github.com/esphome/backlog/issues/76
338 // TODO: Remove this backward compat code before 2026.7.0 - all clients should support API 1.14 by then
339 // Buffer must remain in scope until encode_message_to_buffer is called
340 char object_id_buf[OBJECT_ID_MAX_LEN];
341 if (!conn->client_supports_api_version(1, 14)) {
342 msg.object_id = entity->get_object_id_to(object_id_buf);
343 }
344
345 if (entity->has_own_name()) {
346 msg.name = entity->get_name();
347 }
348
349 // Set common EntityBase properties
350#ifdef USE_ENTITY_ICON
351 msg.icon = entity->get_icon_ref();
352#endif
354 msg.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
355#ifdef USE_DEVICES
356 msg.device_id = entity->get_device_id();
357#endif
358 return encode_message_to_buffer(msg, message_type, conn, remaining_size);
359 }
360
361#ifdef USE_VOICE_ASSISTANT
362 // Helper to check voice assistant validity and connection ownership
363 inline bool check_voice_assistant_api_connection_() const;
364#endif
365
366 // Get the max batch size based on client API version
367 // API 1.14+ clients don't receive object_id, so messages are smaller and more fit per batch
368 // TODO: Remove this method before 2026.7.0 and use MAX_INITIAL_PER_BATCH directly
369 size_t get_max_batch_size_() const {
370 return this->client_supports_api_version(1, 14) ? MAX_INITIAL_PER_BATCH : MAX_INITIAL_PER_BATCH_LEGACY;
371 }
372
373 // Process active iterator (list_entities/initial_state) during connection setup.
374 // Extracted from loop() — only runs during initial handshake, NONE in steady state.
375 void __attribute__((noinline)) process_active_iterator_();
376
377 // Helper method to process multiple entities from an iterator in a batch.
378 // Takes ComponentIterator base class reference to avoid duplicate template instantiations.
380
381#ifdef USE_BINARY_SENSOR
382 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
383 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
384#endif
385#ifdef USE_COVER
386 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
387 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
388#endif
389#ifdef USE_FAN
390 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
391 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
392#endif
393#ifdef USE_LIGHT
394 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
395 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
396#endif
397#ifdef USE_SENSOR
398 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
399 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
400#endif
401#ifdef USE_SWITCH
402 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
403 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
404#endif
405#ifdef USE_TEXT_SENSOR
406 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
407 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
408#endif
409#ifdef USE_CLIMATE
410 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
411 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
412#endif
413#ifdef USE_NUMBER
414 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
415 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
416#endif
417#ifdef USE_DATETIME_DATE
418 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
419 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
420#endif
421#ifdef USE_DATETIME_TIME
422 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
423 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
424#endif
425#ifdef USE_DATETIME_DATETIME
426 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
427 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
428#endif
429#ifdef USE_TEXT
430 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
431 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
432#endif
433#ifdef USE_SELECT
434 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
435 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
436#endif
437#ifdef USE_BUTTON
438 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
439#endif
440#ifdef USE_LOCK
441 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
442 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
443#endif
444#ifdef USE_VALVE
445 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
446 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
447#endif
448#ifdef USE_MEDIA_PLAYER
449 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
450 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
451#endif
452#ifdef USE_ALARM_CONTROL_PANEL
453 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
454 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
455#endif
456#ifdef USE_WATER_HEATER
457 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
458 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
459#endif
460#ifdef USE_INFRARED
461 static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
462#endif
463#ifdef USE_EVENT
464 static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
465 uint32_t remaining_size);
466 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
467#endif
468#ifdef USE_UPDATE
469 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
470 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
471#endif
472#ifdef USE_CAMERA
473 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
474#endif
475
476 // Method for ListEntitiesDone batching
477 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
478
479 // Method for DisconnectRequest batching
480 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
481
482 // Batch message method for ping requests
483 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
484
485 // === Optimal member ordering for 32-bit systems ===
486
487 // Group 1: Pointers (4 bytes each on 32-bit)
488 std::unique_ptr<APIFrameHelper> helper_;
490
491 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
492 // These iterators are never active simultaneously - list_entities runs to completion
493 // before initial_state begins, so we use a union with explicit construction/destruction.
494 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
495
499 // Constructor/destructor do nothing - use placement new/explicit destructor
503
504 // Helper methods for iterator lifecycle management
507#ifdef USE_CAMERA
508 std::unique_ptr<camera::CameraImageReader> image_reader_;
509#endif
510
511 // Group 3: 4-byte types
513#ifdef USE_API_HOMEASSISTANT_STATES
515#endif
516
517 // Function pointer type for message encoding
518 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size);
519
520 // Generic batching mechanism for both state updates and entity info
522 // Sentinel value for unused aux_data_index
523 static constexpr uint8_t AUX_DATA_UNUSED = std::numeric_limits<uint8_t>::max();
524
525 struct BatchItem {
526 EntityBase *entity; // 4 bytes - Entity pointer
527 uint8_t message_type; // 1 byte - Message type for protocol and dispatch
528 uint8_t estimated_size; // 1 byte - Estimated message size (max 255 bytes)
529 uint8_t aux_data_index{AUX_DATA_UNUSED}; // 1 byte - For events: index into entity's event_types
530 // 1 byte padding
531 };
532
533 std::vector<BatchItem> items;
534 uint32_t batch_start_time{0};
535
536 // No pre-allocation - log connections never use batching, and for
537 // connections that do, buffers are released after initial sync anyway
538
539 // Add item to the batch (with deduplication)
540 void add_item(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
541 uint8_t aux_data_index = AUX_DATA_UNUSED);
542 // Add item to the front of the batch (for high priority messages like ping)
543 void add_item_front(EntityBase *entity, uint8_t message_type, uint8_t estimated_size);
544
545 // Clear all items
546 void clear() {
547 items.clear();
549 }
550
551 // Remove processed items from the front — noinline to keep memmove out of warm callers
552 void remove_front(size_t count) __attribute__((noinline)) { items.erase(items.begin(), items.begin() + count); }
553
554 bool empty() const { return items.empty(); }
555 size_t size() const { return items.size(); }
556 const BatchItem &operator[](size_t index) const { return items[index]; }
557
558 // Release excess capacity - only releases if items already empty
560 // Safe to call: batch is processed before release_buffer is called,
561 // and if any items remain (partial processing), we must not clear them.
562 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
563 if (items.empty()) {
564 std::vector<BatchItem>().swap(items);
565 }
566 }
567 };
568
569 // DeferredBatch here (16 bytes, 4-byte aligned)
571
572 // ConnectionState enum for type safety
573 enum class ConnectionState : uint8_t {
575 CONNECTED = 1,
576 AUTHENTICATED = 2,
577 };
578
579 // Group 5: Pack all small members together to minimize padding
580 // This group starts at a 4-byte boundary after DeferredBatch
581 struct APIFlags {
582 // Connection state only needs 2 bits (3 states)
583 uint8_t connection_state : 2;
584 // Log subscription needs 3 bits (log levels 0-7)
585 uint8_t log_subscription : 3;
586 // Boolean flags (1 bit each)
587 uint8_t remove : 1;
589 uint8_t sent_ping : 1;
590
592 uint8_t next_close : 1;
593 uint8_t batch_scheduled : 1;
594 uint8_t batch_first_message : 1; // For batch buffer allocation
595 uint8_t should_try_send_immediately : 1; // True after initial states are sent
596#ifdef HAS_PROTO_MESSAGE_DUMP
597 uint8_t log_only_mode : 1;
598#endif
599 } flags_{}; // 2 bytes total
600
601 // 2-byte types immediately after flags_ (no padding between them)
604 // 1-byte type to fill padding
606 // Total: 2 (flags) + 2 + 2 + 1 = 7 bytes, then 1 byte padding to next 4-byte boundary
607
608 uint32_t get_batch_delay_ms_() const;
609 // Message will use 8 more bytes than the minimum size, and typical
610 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
611 // If its IPv6 the header is 40 bytes, and if its IPv4
612 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
613 // available for the payload. But we also need to add the size of
614 // the protobuf overhead, which is 8 bytes.
615 //
616 // To be safe we pick 1390 bytes as the maximum size
617 // to send in one go. This is the maximum size of a single packet
618 // that can be sent over the network.
619 // This is to avoid fragmentation of the packet.
620 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
621
622 bool schedule_batch_();
623 void process_batch_();
624 void process_batch_multi_(std::vector<uint8_t> &shared_buf, size_t num_items, uint8_t header_padding,
625 uint8_t footer_size) __attribute__((noinline));
627 this->deferred_batch_.clear();
628 this->flags_.batch_scheduled = false;
629 }
630
631 // Dispatch message encoding based on message_type - replaces function pointer storage
632 // Switch assigns pointer, single call site for smaller code size
633 uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first);
634
635#ifdef HAS_PROTO_MESSAGE_DUMP
637 this->flags_.log_only_mode = true;
638 this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true);
639 this->flags_.log_only_mode = false;
640 }
641#endif
642
643 // Helper to check if a message type should bypass batching
644 // Returns true if:
645 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
646 // the main loop is blocked, e.g., during OTA updates)
647 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
648 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
649 // AND batch_delay = 0)
650 inline bool should_send_immediately_(uint8_t message_type) const {
651 return (
652#ifdef USE_UPDATE
653 message_type == UpdateStateResponse::MESSAGE_TYPE ||
654#endif
655#ifdef USE_EVENT
656 message_type == EventResponse::MESSAGE_TYPE ||
657#endif
658 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
659 }
660
661 // Helper method to send a message either immediately or via batching
662 // Tries immediate send if should_send_immediately_() returns true and buffer has space
663 // Falls back to batching if immediate send fails or isn't applicable
664 bool send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
665 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED);
666
667 // Helper function to schedule a deferred message with known message type
668 bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
669 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED) {
670 this->deferred_batch_.add_item(entity, message_type, estimated_size, aux_data_index);
671 return this->schedule_batch_();
672 }
673
674 // Helper function to schedule a high priority message at the front of the batch
675 bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size) {
676 this->deferred_batch_.add_item_front(entity, message_type, estimated_size);
677 return this->schedule_batch_();
678 }
679
680 // Helper function to log client messages with name and peername
681 void log_client_(int level, const LogString *message);
682 // Helper function to log API errors with errno
683 void log_warning_(const LogString *message, APIError err);
684 // Helper to handle fatal errors with logging
685 inline void fatal_error_with_log_(const LogString *message, APIError err) {
686 this->on_fatal_error();
687 this->log_warning_(message, err);
688 }
689};
690
691} // namespace esphome::api
692#endif
bool has_own_name() const
Definition entity_base.h:50
ESPDEPRECATED("object_id mangles names and all object_id methods are planned for removal " "(see https://github.com/esphome/backlog/issues/76). " "Now is the time to stop using object_id. If still needed, use get_object_id_to() " "which will remain available longer. get_object_id() will be removed in 2026.7.0", "2025.12.0") std uint32_t get_object_id_hash()
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:98
uint32_t get_device_id() const
bool is_disabled_by_default() const
Definition entity_base.h:83
EntityCategory get_entity_category() const
Definition entity_base.h:87
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...
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
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)
void process_batch_multi_(std::vector< uint8_t > &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size) __attribute__((noinline))
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)
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
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
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
void prepare_first_message_buffer(std::vector< uint8_t > &shared_buf, size_t header_padding, size_t total_size)
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)
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
bool send_fan_state(fan::Fan *fan)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
bool check_voice_assistant_api_connection_() const
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_
bool send_message_impl(const ProtoMessage &msg, uint8_t message_type) override
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)
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)
void prepare_first_message_buffer(std::vector< uint8_t > &shared_buf, size_t payload_size)
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)
void log_client_(int level, const LogString *message)
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)
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)
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)
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_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
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)
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_subscribe_bluetooth_connections_free_response_()
void on_subscribe_states_request() 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 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 try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, 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
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 fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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)
bool send_message(const ProtoMessage &msg, uint8_t message_type)
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2744
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:1108
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:1017
enums::EntityCategory entity_category
Definition api_pb2.h:327
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:540
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:541
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2884
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:182
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:91
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:43
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:22
Base class for all valve devices.
Definition valve.h:104
struct @65::@66 __attribute__
const char * message
Definition component.cpp:38
uint16_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.
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()