ESPHome 2025.7.5
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 <vector>
14#include <functional>
15
16namespace esphome {
17namespace api {
18
19// Keepalive timeout in milliseconds
20static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
21// Maximum number of entities to process in a single batch during initial state/info sending
22static constexpr size_t MAX_INITIAL_PER_BATCH = 20;
23
25 public:
26 friend class APIServer;
28 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
29 virtual ~APIConnection();
30
31 void start();
32 void loop();
33
38#ifdef USE_BINARY_SENSOR
40#endif
41#ifdef USE_COVER
42 bool send_cover_state(cover::Cover *cover);
43 void cover_command(const CoverCommandRequest &msg) override;
44#endif
45#ifdef USE_FAN
46 bool send_fan_state(fan::Fan *fan);
47 void fan_command(const FanCommandRequest &msg) override;
48#endif
49#ifdef USE_LIGHT
51 void light_command(const LightCommandRequest &msg) override;
52#endif
53#ifdef USE_SENSOR
55#endif
56#ifdef USE_SWITCH
57 bool send_switch_state(switch_::Switch *a_switch);
58 void switch_command(const SwitchCommandRequest &msg) override;
59#endif
60#ifdef USE_TEXT_SENSOR
62#endif
63#ifdef USE_CAMERA
64 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
65 void camera_image(const CameraImageRequest &msg) override;
66#endif
67#ifdef USE_CLIMATE
69 void climate_command(const ClimateCommandRequest &msg) override;
70#endif
71#ifdef USE_NUMBER
73 void number_command(const NumberCommandRequest &msg) override;
74#endif
75#ifdef USE_DATETIME_DATE
77 void date_command(const DateCommandRequest &msg) override;
78#endif
79#ifdef USE_DATETIME_TIME
81 void time_command(const TimeCommandRequest &msg) override;
82#endif
83#ifdef USE_DATETIME_DATETIME
85 void datetime_command(const DateTimeCommandRequest &msg) override;
86#endif
87#ifdef USE_TEXT
88 bool send_text_state(text::Text *text);
89 void text_command(const TextCommandRequest &msg) override;
90#endif
91#ifdef USE_SELECT
93 void select_command(const SelectCommandRequest &msg) override;
94#endif
95#ifdef USE_BUTTON
96 void button_command(const ButtonCommandRequest &msg) override;
97#endif
98#ifdef USE_LOCK
99 bool send_lock_state(lock::Lock *a_lock);
100 void lock_command(const LockCommandRequest &msg) override;
101#endif
102#ifdef USE_VALVE
103 bool send_valve_state(valve::Valve *valve);
104 void valve_command(const ValveCommandRequest &msg) override;
105#endif
106#ifdef USE_MEDIA_PLAYER
108 void media_player_command(const MediaPlayerCommandRequest &msg) override;
109#endif
110 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
113 return;
114 this->send_message(call);
115 }
116#ifdef USE_BLUETOOTH_PROXY
120
121 void bluetooth_device_request(const BluetoothDeviceRequest &msg) override;
122 void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override;
123 void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override;
127 void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override;
129 const SubscribeBluetoothConnectionsFreeRequest &msg) override;
131
132#endif
133#ifdef USE_HOMEASSISTANT_TIME
135 GetTimeRequest req;
136 this->send_message(req);
137 }
138#endif
139
140#ifdef USE_VOICE_ASSISTANT
142 void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
144 void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override;
148 const VoiceAssistantConfigurationRequest &msg) override;
150#endif
151
152#ifdef USE_ALARM_CONTROL_PANEL
155#endif
156
157#ifdef USE_EVENT
158 void send_event(event::Event *event, const std::string &event_type);
159#endif
160
161#ifdef USE_UPDATE
163 void update_command(const UpdateCommandRequest &msg) override;
164#endif
165
166 void on_disconnect_response(const DisconnectResponse &value) override;
167 void on_ping_response(const PingResponse &value) override {
168 // we initiated ping
169 this->flags_.sent_ping = false;
170 }
172#ifdef USE_HOMEASSISTANT_TIME
173 void on_get_time_response(const GetTimeResponse &value) override;
174#endif
175 HelloResponse hello(const HelloRequest &msg) override;
176 ConnectResponse connect(const ConnectRequest &msg) override;
178 PingResponse ping(const PingRequest &msg) override { return {}; }
180 void list_entities(const ListEntitiesRequest &msg) override { this->list_entities_iterator_.begin(); }
181 void subscribe_states(const SubscribeStatesRequest &msg) override {
182 this->flags_.state_subscription = true;
184 }
185 void subscribe_logs(const SubscribeLogsRequest &msg) override {
186 this->flags_.log_subscription = msg.level;
187 if (msg.dump_config)
189 }
195 // TODO
196 return {};
197 }
198#ifdef USE_API_SERVICES
199 void execute_service(const ExecuteServiceRequest &msg) override;
200#endif
201#ifdef USE_API_NOISE
203#endif
204
205 bool is_authenticated() override {
207 }
208 bool is_connection_setup() override {
210 this->is_authenticated();
211 }
212 void on_fatal_error() override;
213 void on_unauthenticated_access() override;
214 void on_no_setup_connection() override;
215 ProtoWriteBuffer create_buffer(uint32_t reserve_size) override {
216 // FIXME: ensure no recursive writes can happen
217
218 // Get header padding size - used for both reserve and insert
219 uint8_t header_padding = this->helper_->frame_header_padding();
220
221 // Get shared buffer from parent server
222 std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
223 shared_buf.clear();
224 // Reserve space for header padding + message + footer
225 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
226 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
227 shared_buf.reserve(reserve_size + header_padding + this->helper_->frame_footer_size());
228 // Resize to add header padding so message encoding starts at the correct position
229 shared_buf.resize(header_padding);
230 return {&shared_buf};
231 }
232
233 // Prepare buffer for next message in batch
234 ProtoWriteBuffer prepare_message_buffer(uint16_t message_size, bool is_first_message) {
235 // Get reference to shared buffer (it maintains state between batch messages)
236 std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
237
238 if (is_first_message) {
239 shared_buf.clear();
240 }
241
242 size_t current_size = shared_buf.size();
243
244 // Calculate padding to add:
245 // - First message: just header padding
246 // - Subsequent messages: footer for previous message + header padding for this message
247 size_t padding_to_add = is_first_message
248 ? this->helper_->frame_header_padding()
249 : this->helper_->frame_header_padding() + this->helper_->frame_footer_size();
250
251 // Reserve space for padding + message
252 shared_buf.reserve(current_size + padding_to_add + message_size);
253
254 // Resize to add the padding bytes
255 shared_buf.resize(current_size + padding_to_add);
256
257 return {&shared_buf};
258 }
259
260 bool try_to_clear_buffer(bool log_out_of_space);
261 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
262
263 std::string get_client_combined_info() const {
264 if (this->client_info_ == this->client_peername_) {
265 // Before Hello message, both are the same (just IP:port)
266 return this->client_info_;
267 }
268 return this->client_info_ + " (" + this->client_peername_ + ")";
269 }
270
271 // Buffer allocator methods for batch processing
274
275 protected:
276 // Helper function to fill common entity info fields
278 // Set common fields that are shared by all entity types
279 response.key = entity->get_object_id_hash();
280 response.object_id = entity->get_object_id();
281
282 if (entity->has_own_name())
283 response.name = entity->get_name();
284
285 // Set common EntityBase properties
286 response.icon = entity->get_icon();
287 response.disabled_by_default = entity->is_disabled_by_default();
288 response.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
289#ifdef USE_DEVICES
290 response.device_id = entity->get_device_id();
291#endif
292 }
293
294 // Helper function to fill common entity state fields
296 response.key = entity->get_object_id_hash();
297#ifdef USE_DEVICES
298 response.device_id = entity->get_device_id();
299#endif
300 }
301
302 // Non-template helper to encode any ProtoMessage
303 static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn,
304 uint32_t remaining_size, bool is_single);
305
306#ifdef USE_VOICE_ASSISTANT
307 // Helper to check voice assistant validity and connection ownership
308 inline bool check_voice_assistant_api_connection_() const;
309#endif
310
311 // Helper method to process multiple entities from an iterator in a batch
312 template<typename Iterator> void process_iterator_batch_(Iterator &iterator) {
313 size_t initial_size = this->deferred_batch_.size();
314 while (!iterator.completed() && (this->deferred_batch_.size() - initial_size) < MAX_INITIAL_PER_BATCH) {
315 iterator.advance();
316 }
317
318 // If the batch is full, process it immediately
319 // Note: iterator.advance() already calls schedule_batch_() via schedule_message_()
320 if (this->deferred_batch_.size() >= MAX_INITIAL_PER_BATCH) {
321 this->process_batch_();
322 }
323 }
324
325#ifdef USE_BINARY_SENSOR
326 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
327 bool is_single);
328 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
329 bool is_single);
330#endif
331#ifdef USE_COVER
332 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
333 bool is_single);
334 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
335#endif
336#ifdef USE_FAN
337 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
338 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
339#endif
340#ifdef USE_LIGHT
341 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
342 bool is_single);
343 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
344#endif
345#ifdef USE_SENSOR
346 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
347 bool is_single);
348 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
349 bool is_single);
350#endif
351#ifdef USE_SWITCH
352 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
353 bool is_single);
354 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
355 bool is_single);
356#endif
357#ifdef USE_TEXT_SENSOR
358 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
359 bool is_single);
360 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
361 bool is_single);
362#endif
363#ifdef USE_CLIMATE
364 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
365 bool is_single);
366 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
367 bool is_single);
368#endif
369#ifdef USE_NUMBER
370 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
371 bool is_single);
372 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
373 bool is_single);
374#endif
375#ifdef USE_DATETIME_DATE
376 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
377 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
378#endif
379#ifdef USE_DATETIME_TIME
380 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
381 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
382#endif
383#ifdef USE_DATETIME_DATETIME
384 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
385 bool is_single);
386 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
387 bool is_single);
388#endif
389#ifdef USE_TEXT
390 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
391 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
392#endif
393#ifdef USE_SELECT
394 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
395 bool is_single);
396 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
397 bool is_single);
398#endif
399#ifdef USE_BUTTON
400 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
401 bool is_single);
402#endif
403#ifdef USE_LOCK
404 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
405 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
406#endif
407#ifdef USE_VALVE
408 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
409 bool is_single);
410 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
411#endif
412#ifdef USE_MEDIA_PLAYER
413 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
414 bool is_single);
415 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
416 bool is_single);
417#endif
418#ifdef USE_ALARM_CONTROL_PANEL
419 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
420 bool is_single);
421 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
422 bool is_single);
423#endif
424#ifdef USE_EVENT
425 static uint16_t try_send_event_response(event::Event *event, const std::string &event_type, APIConnection *conn,
426 uint32_t remaining_size, bool is_single);
427 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
428#endif
429#ifdef USE_UPDATE
430 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
431 bool is_single);
432 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
433 bool is_single);
434#endif
435#ifdef USE_CAMERA
436 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
437 bool is_single);
438#endif
439
440 // Method for ListEntitiesDone batching
441 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
442 bool is_single);
443
444 // Method for DisconnectRequest batching
445 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
446 bool is_single);
447
448 // Batch message method for ping requests
449 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
450 bool is_single);
451
452 // === Optimal member ordering for 32-bit systems ===
453
454 // Group 1: Pointers (4 bytes each on 32-bit)
455 std::unique_ptr<APIFrameHelper> helper_;
457
458 // Group 2: Larger objects (must be 4-byte aligned)
459 // These contain vectors/pointers internally, so putting them early ensures good alignment
462#ifdef USE_CAMERA
463 std::unique_ptr<camera::CameraImageReader> image_reader_;
464#endif
465
466 // Group 3: Strings (12 bytes each on 32-bit, 4-byte aligned)
467 std::string client_info_;
468 std::string client_peername_;
469
470 // Group 4: 4-byte types
473
474 // Function pointer type for message encoding
475 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single);
476
478 public:
479 // Constructor for function pointer
480 MessageCreator(MessageCreatorPtr ptr) { data_.function_ptr = ptr; }
481
482 // Constructor for string state capture
483 explicit MessageCreator(const std::string &str_value) { data_.string_ptr = new std::string(str_value); }
484
485 // No destructor - cleanup must be called explicitly with message_type
486
487 // Delete copy operations - MessageCreator should only be moved
488 MessageCreator(const MessageCreator &other) = delete;
489 MessageCreator &operator=(const MessageCreator &other) = delete;
490
491 // Move constructor
492 MessageCreator(MessageCreator &&other) noexcept : data_(other.data_) { other.data_.function_ptr = nullptr; }
493
494 // Move assignment
496 if (this != &other) {
497 // IMPORTANT: Caller must ensure cleanup() was called if this contains a string!
498 // In our usage, this happens in add_item() deduplication and vector::erase()
499 data_ = other.data_;
500 other.data_.function_ptr = nullptr;
501 }
502 return *this;
503 }
504
505 // Call operator - uses message_type to determine union type
506 uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single,
507 uint8_t message_type) const;
508
509 // Manual cleanup method - must be called before destruction for string types
510 void cleanup(uint8_t message_type) {
511#ifdef USE_EVENT
512 if (message_type == EventResponse::MESSAGE_TYPE && data_.string_ptr != nullptr) {
513 delete data_.string_ptr;
514 data_.string_ptr = nullptr;
515 }
516#endif
517 }
518
519 private:
520 union Data {
521 MessageCreatorPtr function_ptr;
522 std::string *string_ptr;
523 } data_; // 4 bytes on 32-bit, 8 bytes on 64-bit - same as before
524 };
525
526 // Generic batching mechanism for both state updates and entity info
528 struct BatchItem {
529 EntityBase *entity; // Entity pointer
530 MessageCreator creator; // Function that creates the message when needed
531 uint8_t message_type; // Message type for overhead calculation (max 255)
532 uint8_t estimated_size; // Estimated message size (max 255 bytes)
533
534 // Constructor for creating BatchItem
537 };
538
539 std::vector<BatchItem> items;
540 uint32_t batch_start_time{0};
541
542 private:
543 // Helper to cleanup items from the beginning
544 void cleanup_items_(size_t count) {
545 for (size_t i = 0; i < count; i++) {
546 items[i].creator.cleanup(items[i].message_type);
547 }
548 }
549
550 public:
552 // Pre-allocate capacity for typical batch sizes to avoid reallocation
553 items.reserve(8);
554 }
555
557 // Ensure cleanup of any remaining items
558 clear();
559 }
560
561 // Add item to the batch
562 void add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
563 // Add item to the front of the batch (for high priority messages like ping)
564 void add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
565
566 // Clear all items with proper cleanup
567 void clear() {
568 cleanup_items_(items.size());
569 items.clear();
571 }
572
573 // Remove processed items from the front with proper cleanup
574 void remove_front(size_t count) {
575 cleanup_items_(count);
576 items.erase(items.begin(), items.begin() + count);
577 }
578
579 bool empty() const { return items.empty(); }
580 size_t size() const { return items.size(); }
581 const BatchItem &operator[](size_t index) const { return items[index]; }
582 };
583
584 // DeferredBatch here (16 bytes, 4-byte aligned)
586
587 // ConnectionState enum for type safety
588 enum class ConnectionState : uint8_t {
590 CONNECTED = 1,
591 AUTHENTICATED = 2,
592 };
593
594 // Group 5: Pack all small members together to minimize padding
595 // This group starts at a 4-byte boundary after DeferredBatch
596 struct APIFlags {
597 // Connection state only needs 2 bits (3 states)
598 uint8_t connection_state : 2;
599 // Log subscription needs 3 bits (log levels 0-7)
600 uint8_t log_subscription : 3;
601 // Boolean flags (1 bit each)
602 uint8_t remove : 1;
604 uint8_t sent_ping : 1;
605
607 uint8_t next_close : 1;
608 uint8_t batch_scheduled : 1;
609 uint8_t batch_first_message : 1; // For batch buffer allocation
610 uint8_t should_try_send_immediately : 1; // True after initial states are sent
611#ifdef HAS_PROTO_MESSAGE_DUMP
612 uint8_t log_only_mode : 1;
613#endif
614 } flags_{}; // 2 bytes total
615
616 // 2-byte types immediately after flags_ (no padding between them)
619 // Total: 2 (flags) + 2 + 2 = 6 bytes, then 2 bytes padding to next 4-byte boundary
620
621 uint32_t get_batch_delay_ms_() const;
622 // Message will use 8 more bytes than the minimum size, and typical
623 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
624 // If its IPv6 the header is 40 bytes, and if its IPv4
625 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
626 // available for the payload. But we also need to add the size of
627 // the protobuf overhead, which is 8 bytes.
628 //
629 // To be safe we pick 1390 bytes as the maximum size
630 // to send in one go. This is the maximum size of a single packet
631 // that can be sent over the network.
632 // This is to avoid fragmentation of the packet.
633 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
634
635 bool schedule_batch_();
636 void process_batch_();
638 this->deferred_batch_.clear();
639 this->flags_.batch_scheduled = false;
640 }
641
642#ifdef HAS_PROTO_MESSAGE_DUMP
643 // Helper to log a proto message from a MessageCreator object
644 void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type) {
645 this->flags_.log_only_mode = true;
646 creator(entity, this, MAX_BATCH_PACKET_SIZE, true, message_type);
647 this->flags_.log_only_mode = false;
648 }
649
651 // Use the helper to log the message
652 this->log_proto_message_(item.entity, item.creator, item.message_type);
653 }
654#endif
655
656 // Helper method to send a message either immediately or via batching
657 bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint8_t message_type,
658 uint8_t estimated_size) {
659 // Try to send immediately if:
660 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
661 // the main loop is blocked, e.g., during OTA updates)
662 // 2. OR: We should try to send immediately (should_try_send_immediately = true)
663 // AND Batch delay is 0 (user has opted in to immediate sending)
664 // 3. AND: Buffer has space available
665 if ((
666#ifdef USE_UPDATE
667 message_type == UpdateStateResponse::MESSAGE_TYPE ||
668#endif
669 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0)) &&
670 this->helper_->can_write_without_blocking()) {
671 // Now actually encode and send
672 if (creator(entity, this, MAX_BATCH_PACKET_SIZE, true) &&
673 this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, message_type)) {
674#ifdef HAS_PROTO_MESSAGE_DUMP
675 // Log the message in verbose mode
676 this->log_proto_message_(entity, MessageCreator(creator), message_type);
677#endif
678 return true;
679 }
680
681 // If immediate send failed, fall through to batching
682 }
683
684 // Fall back to scheduled batching
685 return this->schedule_message_(entity, creator, message_type, estimated_size);
686 }
687
688 // Helper function to schedule a deferred message with known message type
689 bool schedule_message_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size) {
690 this->deferred_batch_.add_item(entity, std::move(creator), message_type, estimated_size);
691 return this->schedule_batch_();
692 }
693
694 // Overload for function pointers (for info messages and current state reads)
695 bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
696 uint8_t estimated_size) {
697 return schedule_message_(entity, MessageCreator(function_ptr), message_type, estimated_size);
698 }
699
700 // Helper function to schedule a high priority message at the front of the batch
701 bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
702 uint8_t estimated_size) {
703 this->deferred_batch_.add_item_front(entity, MessageCreator(function_ptr), message_type, estimated_size);
704 return this->schedule_batch_();
705 }
706};
707
708} // namespace api
709} // namespace esphome
710#endif
void begin(bool include_internal=false)
bool has_own_name() const
Definition entity_base.h:29
uint32_t get_object_id_hash()
const StringRef & get_name() const
std::string get_icon() const
uint32_t get_device_id() const
Definition entity_base.h:60
bool is_disabled_by_default() const
Definition entity_base.h:45
std::string get_object_id() const
EntityCategory get_entity_category() const
Definition entity_base.h:49
MessageCreator(MessageCreator &&other) noexcept
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single, uint8_t message_type) const
MessageCreator(const MessageCreator &other)=delete
MessageCreator(const std::string &str_value)
MessageCreator & operator=(const MessageCreator &other)=delete
MessageCreator & operator=(MessageCreator &&other) noexcept
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)
struct esphome::api::APIConnection::APIFlags flags_
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
BluetoothConnectionsFreeResponse subscribe_bluetooth_connections_free(const SubscribeBluetoothConnectionsFreeRequest &msg) override
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)
static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static void fill_entity_state_base(esphome::EntityBase *entity, StateResponseProtoMessage &response)
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)
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
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
bool check_voice_assistant_api_connection_() const
void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type)
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
InitialStateIterator initial_state_iterator_
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)
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
void send_event(event::Event *event, const std::string &event_type)
bool send_sensor_state(sensor::Sensor *sensor)
std::string get_client_combined_info() const
static constexpr size_t MAX_BATCH_PACKET_SIZE
void log_batch_item_(const DeferredBatch::BatchItem &item)
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)
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
VoiceAssistantConfigurationResponse voice_assistant_get_configuration(const VoiceAssistantConfigurationRequest &msg) override
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
DisconnectResponse disconnect(const DisconnectRequest &msg) override
void text_command(const TextCommandRequest &msg) override
bool send_lock_state(lock::Lock *a_lock)
HelloResponse hello(const HelloRequest &msg) override
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)
ProtoWriteBuffer allocate_batch_message_buffer(uint16_t size)
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
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)
static uint16_t try_send_event_response(event::Event *event, const std::string &event_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override
static void fill_entity_info_base(esphome::EntityBase *entity, InfoResponseProtoMessage &response)
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
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 number_command(const NumberCommandRequest &msg) override
void list_entities(const ListEntitiesRequest &msg) override
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
ProtoWriteBuffer allocate_single_message_buffer(uint16_t size)
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 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)
PingResponse ping(const PingRequest &msg) override
NoiseEncryptionSetKeyResponse noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) override
bool send_light_state(light::LightState *light)
DeviceInfoResponse device_info(const DeviceInfoRequest &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
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 subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &msg)
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void on_disconnect_response(const DisconnectResponse &value) override
bool send_datetime_state(datetime::DateTimeEntity *datetime)
ListEntitiesIterator list_entities_iterator_
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_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
GetTimeResponse get_time(const GetTimeRequest &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)
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)
ConnectResponse connect(const ConnectRequest &msg) override
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)
ProtoWriteBuffer prepare_message_buffer(uint16_t message_size, bool is_first_message)
void on_unauthenticated_access() override
std::vector< uint8_t > & get_shared_buffer_ref()
Definition api_server.h:51
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2689
enums::EntityCategory entity_category
Definition api_pb2.h:297
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:534
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:535
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2829
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:168
Base class for all cover devices.
Definition cover.h:111
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:66
Base class for all locks.
Definition lock.h:103
Base-class for all numbers.
Definition number.h:39
Base-class for all selects.
Definition select.h:31
Base-class for all sensors.
Definition sensor.h:62
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:105
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.
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)