ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
api_connection.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_API
5#include "api_frame_helper.h"
6#ifdef USE_API_NOISE
8#endif
9#ifdef USE_API_PLAINTEXT
11#endif
12#include "api_pb2.h"
13#include "api_pb2_service.h"
14#include "list_entities.h"
15#include "subscribe_state.h"
19#include "esphome/core/log.h"
21
22#include <functional>
23#include <limits>
24#include <vector>
25
26namespace esphome {
27class ComponentIterator;
28} // namespace esphome
29
30namespace esphome::api {
31
32// Forward-declared to break the api_server.h cycle; full-type inlines are in api_connection_buffer.h.
33class APIServer;
34
35// One shared flash string for every refused-frame warning: send_message()
36// fails as soon as the TCP buffer is full, and each caller only pays for its
37// short name. The guard drops the helper and its arguments below WARN.
38#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
39void log_dropped_message(const char *tag, int line, const LogString *what);
40#define API_LOG_MSG_DROPPED(tag, what) esphome::api::log_dropped_message(tag, __LINE__, LOG_STR(what))
41#else
42#define API_LOG_MSG_DROPPED(tag, what)
43#endif
44
45// Keepalive timeout in milliseconds
46static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
47// Deferred batch size cap during initial state/info sync
48static constexpr size_t MAX_INITIAL_BATCH_SIZE = 34;
49// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch
50static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_BATCH_SIZE,
51 "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_BATCH_SIZE");
52
53#ifdef USE_BENCHMARK
54class APIConnection;
58#endif
59
61 public:
62 friend class APIServer;
64#ifdef USE_BENCHMARK
66 friend void bench_clear_batch(APIConnection *conn);
68#endif
69 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
71
72 void start();
73 void loop();
74
75 protected:
76 // read_message_ is defined here (instead of in APIServerConnectionBase) so the
77 // compiler can devirtualize and inline on_* handler calls within this final class.
78 void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data);
79
80 // Auth helpers defined here (not in ProtoService) so the compiler can
81 // devirtualize is_connection_setup()/on_no_setup_connection() calls
82 // within this final class.
84 if (!this->is_connection_setup()) {
86 return false;
87 }
88 return true;
89 }
90 inline bool check_authenticated_() { return this->check_connection_setup_(); }
91
92 public:
97#ifdef USE_BINARY_SENSOR
99#endif
100#ifdef USE_COVER
101 bool send_cover_state(cover::Cover *cover);
103#endif
104#ifdef USE_FAN
105 bool send_fan_state(fan::Fan *fan);
107#endif
108#ifdef USE_LIGHT
111#endif
112#ifdef USE_SENSOR
113 bool send_sensor_state(sensor::Sensor *sensor);
114#endif
115#ifdef USE_SWITCH
116 bool send_switch_state(switch_::Switch *a_switch);
118#endif
119#ifdef USE_TEXT_SENSOR
121#endif
122#ifdef USE_CAMERA
123 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
125#endif
126#ifdef USE_CLIMATE
129#endif
130#ifdef USE_NUMBER
131 bool send_number_state(number::Number *number);
133#endif
134#ifdef USE_DATETIME_DATE
137#endif
138#ifdef USE_DATETIME_TIME
141#endif
142#ifdef USE_DATETIME_DATETIME
145#endif
146#ifdef USE_TEXT
147 bool send_text_state(text::Text *text);
149#endif
150#ifdef USE_SELECT
151 bool send_select_state(select::Select *select);
153#endif
154#ifdef USE_BUTTON
156#endif
157#ifdef USE_LOCK
158 bool send_lock_state(lock::Lock *a_lock);
160#endif
161#ifdef USE_VALVE
162 bool send_valve_state(valve::Valve *valve);
164#endif
165#ifdef USE_MEDIA_PLAYER
168#endif
169 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
170#ifdef USE_API_HOMEASSISTANT_SERVICES
171 // Returns whether this client has subscribed to Home Assistant actions; the message
172 // is only handed to the send path when subscribed. A true return does not guarantee
173 // delivery - it lets the caller warn when no connected client has the subscription.
175#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
177#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
178#endif // USE_API_HOMEASSISTANT_SERVICES
179#ifdef USE_BLUETOOTH_PROXY
182
183#ifdef USE_BLUETOOTH_PROXY_CONNECTIONS
193#endif
195
196#endif
197#ifdef USE_HOMEASSISTANT_TIME
198 void send_time_request();
199#endif
200
201#ifdef USE_VOICE_ASSISTANT
210#endif
211
212#ifdef USE_ZWAVE_PROXY
213 void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg);
215#endif
216
217#if defined(USE_SENDSPIN) && defined(USE_ESP_IDF)
219#endif
220
221#ifdef USE_ALARM_CONTROL_PANEL
224#endif
225
226#ifdef USE_WATER_HEATER
229#endif
230
231#ifdef USE_IR_RF
234 // Reply to an InfraredRFTransmitRawTimingsRequest (API 1.18+); false when the TCP buffer is
235 // full, the entity that owns the reply retries it then
236 [[nodiscard]] bool send_infrared_rf_transmit_complete(uint32_t device_id, uint32_t key, bool success);
237#endif
238
239#ifdef USE_SERIAL_PROXY
250#endif
251
252#ifdef USE_EVENT
253 void send_event(event::Event *event);
254#endif
255
256#ifdef USE_UPDATE
259#endif
260
263 // we initiated ping
264 this->flags_.sent_ping = false;
265 }
266#ifdef USE_API_HOMEASSISTANT_STATES
268#endif
269#ifdef USE_HOMEASSISTANT_TIME
270 void on_get_time_response(const GetTimeResponse &value);
271#endif
272 void on_hello_request(const HelloRequest &msg);
274 void on_ping_request();
277#ifdef USE_API_WIZARD
279#endif
280#ifdef USE_API_WIZARD_INPUTS
282#endif
285 this->flags_.state_subscription = true;
286 // Start initial state iterator only if no iterator is active
287 // If list_entities is running, we'll start initial_state when it completes
290 }
291 }
293#ifdef USE_API_HOMEASSISTANT_SERVICES
295#endif
296#ifdef USE_API_HOMEASSISTANT_STATES
298#ifdef USE_API_WIZARD_LINKED_INPUTS
300 void resend_state_subscriptions(const char *entity_id);
301#endif
302#endif
303#ifdef USE_API_USER_DEFINED_ACTIONS
305#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
306 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
307#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
308 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
309 const uint8_t *response_data, size_t response_data_len);
310#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
311#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
312#endif
313#ifdef USE_API_NOISE
315#endif
316
317 // How long a new connection holds off the spare ephemeral refill
318 static constexpr uint32_t CONNECT_GRACE_MS = 1000;
322 // An older unauthenticated connection is a stale half open client and does not count
324 return !this->is_authenticated() && now - this->last_traffic_ < CONNECT_GRACE_MS;
325 }
328 this->is_authenticated();
329 }
330 bool is_marked_for_removal() const { return this->flags_.remove; }
331 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
332
333 // Get client API version for feature detection.
334 // Stored versions saturate at 255 (see send_hello_response_), so requesting
335 // a minimum above that can never match.
336 bool client_supports_api_version(uint8_t major, uint8_t minor) const {
337 return this->client_api_version_major_ > major ||
338 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
339 }
340
341 void on_fatal_error();
343
344 // Function pointer type for type-erased message encoding
346 // Function pointer type for type-erased size calculation
347 using CalculateSizeFn = uint32_t (*)(const void *);
348
351 template<typename T> [[nodiscard]] bool send_message(const T &msg) {
352 return this->send_message_(T::calc_size_msg(&msg), T::MESSAGE_TYPE, &T::encode_msg, &msg);
353 }
354
358 [[nodiscard]] bool prepare_first_message_buffer(size_t header_padding, size_t total_size);
359
360 // Convenience overload - computes frame overhead internally
361 [[nodiscard]] bool prepare_first_message_buffer(size_t payload_size);
362
363 bool try_to_clear_buffer(bool log_out_of_space) {
364 if (this->flags_.remove)
365 return false;
366 if (this->helper_->can_write_without_blocking())
367 return true;
368 return this->try_to_clear_buffer_slow_(log_out_of_space);
369 }
370 bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type);
371
372 const char *get_name() const { return this->helper_->get_client_name(); }
374 const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
375 return this->helper_->get_peername_to(buf);
376 }
377
378#ifdef USE_API_OUTGOING_CONNECTION
380 int getpeername(struct sockaddr *addr, socklen_t *addrlen) const { return this->helper_->getpeername(addr, addrlen); }
385 if (this->flags_.remove) {
386 return; // start() failed; the connection is already being torn down
387 }
388 APIError err = static_cast<APINoiseFrameHelper *>(this->helper_.get())->send_server_hello_first();
389 if (err != APIError::OK) {
390 this->fatal_error_with_log_(LOG_STR("Server hello failed"), err);
391 }
392 }
393#endif
394
395 protected:
396 bool try_to_clear_buffer_slow_(bool log_out_of_space);
397
398 // Helper function to handle authentication completion
400
401 // Pattern B helpers: send response and return success/failure
402 bool send_hello_response_(const HelloRequest &msg);
404 bool send_ping_response_();
407#ifdef USE_API_NOISE
409#endif
410#ifdef USE_BLUETOOTH_PROXY_CONNECTIONS
412#endif
413#ifdef USE_VOICE_ASSISTANT
415#endif
416#if defined(USE_SENDSPIN) && defined(USE_ESP_IDF)
418#endif
419
420#ifdef USE_CAMERA
422#endif
423
424#ifdef USE_API_HOMEASSISTANT_STATES
426#endif
427
428 // Non-template buffer management for send_message
429 bool send_message_(uint32_t payload_size, uint16_t message_type, MessageEncodeFn encode_fn, const void *msg);
430
431 // Core batch encoding logic. ALWAYS_INLINE so encode_fn devirtualizes at hot call sites.
432 // Defined in api_connection_buffer.h (needs APIServer complete).
433 static uint16_t ESPHOME_ALWAYS_INLINE encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn,
434 const void *msg, APIConnection *conn, uint32_t remaining_size);
435
436 // Noinline version of encode_to_buffer for cold paths (entity info, zero-payload messages).
437 // All cold callers share this single copy instead of each getting an ALWAYS_INLINE expansion.
438 static uint16_t encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg,
439 APIConnection *conn, uint32_t remaining_size);
440
441 // Thin template wrapper — uses noinline encode_to_buffer_slow since
442 // encode_message_to_buffer callers are cold paths (zero-payload control messages).
443 // Hot paths (state/info) go through fill_and_encode_entity_state/info instead.
444 // batch_message_type_ is already set by dispatch_message_ before reaching here.
445 template<typename T> static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size) {
446 return encode_to_buffer_slow(T::calc_size_msg(&msg), &T::encode_msg, &msg, conn, remaining_size);
447 }
448
449 // Non-template core — fills state fields and encodes
451 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
452 uint32_t remaining_size);
453
454 // Thin template wrapper
455 template<typename T>
456 static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn,
457 uint32_t remaining_size) {
458 return fill_and_encode_entity_state(entity, msg, &T::calc_size_msg, &T::encode_msg, conn, remaining_size);
459 }
460
461 // Non-template core — fills info fields, allocates buffers, and encodes
463 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
464 uint32_t remaining_size);
465
466 // Thin template wrapper
467 template<typename T>
468 static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn,
469 uint32_t remaining_size) {
470 return fill_and_encode_entity_info(entity, msg, &T::calc_size_msg, &T::encode_msg, conn, remaining_size);
471 }
472
473 // Non-template core — fills device_class, then delegates to fill_and_encode_entity_info
475 ProgmemStringRef &device_class_field,
476 CalculateSizeFn size_fn, MessageEncodeFn encode_fn,
477 APIConnection *conn, uint32_t remaining_size);
478
479 // Thin template wrapper
480 template<typename T>
482 ProgmemStringRef &device_class_field,
483 APIConnection *conn, uint32_t remaining_size) {
484 return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &T::calc_size_msg,
485 &T::encode_msg, conn, remaining_size);
486 }
487
488#ifdef USE_VOICE_ASSISTANT
489 // Helper to check voice assistant validity and connection ownership
490 inline bool check_voice_assistant_api_connection_() const;
491#endif
492
493 // Send keepalive ping or disconnect unresponsive client.
494 // Cold path — extracted from loop() to reduce instruction cache pressure.
495 void __attribute__((noinline)) check_keepalive_(uint32_t now);
496
497 // Process active iterator (list_entities/initial_state) during connection setup.
498 // Extracted from loop() — only runs during initial handshake, NONE in steady state.
499 void __attribute__((noinline)) process_active_iterator_();
500
501 // Helper method to process multiple entities from an iterator in a batch.
502 // Takes ComponentIterator base class reference to avoid duplicate template instantiations.
504
505#ifdef USE_BINARY_SENSOR
506 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
507 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
508#endif
509#ifdef USE_COVER
510 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
511 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
512#endif
513#ifdef USE_FAN
514 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
515 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
516#endif
517#ifdef USE_LIGHT
518 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
519 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
520#endif
521#ifdef USE_SENSOR
522 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
523 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
524#endif
525#ifdef USE_SWITCH
526 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
527 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
528#endif
529#ifdef USE_TEXT_SENSOR
530 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
531 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
532#endif
533#ifdef USE_CLIMATE
534 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
535 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
536#endif
537#ifdef USE_NUMBER
538 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
539 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
540#endif
541#ifdef USE_DATETIME_DATE
542 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
543 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
544#endif
545#ifdef USE_DATETIME_TIME
546 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
547 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
548#endif
549#ifdef USE_DATETIME_DATETIME
550 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
551 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
552#endif
553#ifdef USE_TEXT
554 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
555 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
556#endif
557#ifdef USE_SELECT
558 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
559 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
560#endif
561#ifdef USE_BUTTON
562 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
563#endif
564#ifdef USE_LOCK
565 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
566 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
567#endif
568#ifdef USE_VALVE
569 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
570 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
571#endif
572#ifdef USE_MEDIA_PLAYER
573 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
574 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
575#endif
576#ifdef USE_ALARM_CONTROL_PANEL
577 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
578 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
579#endif
580#ifdef USE_WATER_HEATER
581 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
582 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
583#endif
584#ifdef USE_INFRARED
585 static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
586#endif
587#ifdef USE_RADIO_FREQUENCY
588 static uint16_t try_send_radio_frequency_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
589#endif
590#ifdef USE_EVENT
591 static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
592 uint32_t remaining_size);
593 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
594#endif
595#ifdef USE_UPDATE
596 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
597 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
598#endif
599#ifdef USE_CAMERA
600 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
601#endif
602
603 // Method for ListEntitiesDone batching
604 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
605
606 // Method for DisconnectRequest batching
607 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
608
609 // Batch message method for ping requests
610 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
611
612 // === Optimal member ordering for 32-bit systems ===
613
614 // Group 1: Pointers (4 bytes each on 32-bit)
615#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
616 std::unique_ptr<APIFrameHelper> helper_;
617#elif defined(USE_API_NOISE)
618 std::unique_ptr<APINoiseFrameHelper> helper_;
619#elif defined(USE_API_PLAINTEXT)
620 std::unique_ptr<APIPlaintextFrameHelper> helper_;
621#endif
623
624 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
625 // These iterators are never active simultaneously - list_entities runs to completion
626 // before initial_state begins, so we use a union with explicit construction/destruction.
627 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
628
632 // Constructor/destructor do nothing - use placement new/explicit destructor
636
637 // Helper methods for iterator lifecycle management
641#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
642 // Swap the plaintext helper for a Noise helper after the client opened
643 // with a Noise hello on an unprovisioned device (zero-PSK provisioning).
645#endif
646#ifdef USE_CAMERA
647 std::unique_ptr<camera::CameraImageReader> image_reader_;
648#endif
649
650 // Group 3: 4-byte types
652#ifdef USE_API_HOMEASSISTANT_STATES
654#endif
655
656 // Function pointer type for message encoding
657 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size);
658
659 // Generic batching mechanism for both state updates and entity info
661 // Sentinel value for unused aux_data_index
662 static constexpr uint8_t AUX_DATA_UNUSED = std::numeric_limits<uint8_t>::max();
663
664 struct BatchItem {
665 EntityBase *entity; // 4 bytes - Entity pointer
666 uint16_t message_type; // 2 bytes - Message type for protocol and dispatch
667 uint8_t estimated_size; // 1 byte - Estimated message size (max 255 bytes)
668 uint8_t aux_data_index{AUX_DATA_UNUSED}; // 1 byte - For events: index into entity's event_types
669 };
670
671 std::vector<BatchItem> items;
673
674 // No pre-allocation - log connections never use batching, and for
675 // connections that do, buffers are released after initial sync anyway
676
677 // Add item to the batch (with deduplication)
678 void add_item(EntityBase *entity, uint16_t message_type, uint8_t estimated_size,
679 uint8_t aux_data_index = AUX_DATA_UNUSED) {
680 // Dedup: O(n) scan but optimized for RAM over performance
681 // Skip deduplication for events - they are edge-triggered, every occurrence matters
682#ifdef USE_EVENT
683 if (message_type != EventResponse::MESSAGE_TYPE)
684#endif
685 {
686 for (const auto &item : this->items) {
687 if (item.entity == entity && item.message_type == message_type)
688 return; // Already queued
689 }
690 }
691 this->items.push_back({entity, message_type, estimated_size, aux_data_index});
692 }
693 // Add item to the front of the batch (for high priority messages like ping)
694 void add_item_front(EntityBase *entity, uint16_t message_type, uint8_t estimated_size) {
695 // Swap to front avoids expensive vector::insert which shifts all elements
696 this->items.push_back({entity, message_type, estimated_size, AUX_DATA_UNUSED});
697 if (this->items.size() > 1) {
698 std::swap(this->items.front(), this->items.back());
699 }
700 }
701
702 // Clear all items
703 void clear() {
704 items.clear();
706 }
707
708 // Remove processed items from the front — noinline to keep memmove out of warm callers
709 void remove_front(size_t count) __attribute__((noinline)) { items.erase(items.begin(), items.begin() + count); }
710
711 bool empty() const { return items.empty(); }
712 size_t size() const { return items.size(); }
713 const BatchItem &operator[](size_t index) const { return items[index]; }
714
715 // Release excess capacity - only releases if items already empty
717 // Safe to call: batch is processed before release_buffer is called,
718 // and if any items remain (partial processing), we must not clear them.
719 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
720 if (items.empty()) {
721 std::vector<BatchItem>().swap(items);
722 }
723 }
724 };
725
726 // DeferredBatch here (16 bytes, 4-byte aligned)
728
729 // ConnectionState enum for type safety
730 enum class ConnectionState : uint8_t {
732 CONNECTED = 1,
733 AUTHENTICATED = 2,
734 };
735
736 // Group 5: Pack all small members together to minimize padding
737 // This group starts at a 4-byte boundary after DeferredBatch
738 struct APIFlags {
739 // Connection state only needs 2 bits (3 states)
740 uint8_t connection_state : 2;
741 // Log subscription needs 3 bits (log levels 0-7)
742 uint8_t log_subscription : 3;
743 // Boolean flags (1 bit each)
744 uint8_t remove : 1;
746 uint8_t sent_ping : 1;
747
749 uint8_t next_close : 1;
750 uint8_t batch_scheduled : 1;
751 uint8_t batch_first_message : 1; // For batch buffer allocation
752 uint8_t should_try_send_immediately : 1; // True after initial states are sent
753 uint8_t may_have_remaining_data : 1; // Read loop hit limit, retry without ready check
754#ifdef USE_API_WIZARD_LINKED_INPUTS
755 uint8_t home_assistant_states : 1; // Client subscribed to Home Assistant states
756#endif
757#ifdef USE_API_OUTGOING_CONNECTION
758 uint8_t outgoing_connection_target : 1; // Client declared itself a dial-back target in its hello
759#endif
760#ifdef HAS_PROTO_MESSAGE_DUMP
761 uint8_t log_only_mode : 1;
762#endif
763#ifdef USE_SERIAL_PROXY_USB_IDENTITY
765#endif
766 } flags_{}; // 2 bytes; 3 with HAS_PROTO_MESSAGE_DUMP + USE_API_OUTGOING_CONNECTION + USE_SERIAL_PROXY_USB_IDENTITY
767
768 // 2-byte type immediately after flags_ (one padding byte when flags_ is 3 bytes)
769 uint16_t batch_message_type_{0}; // Current message type during batch encoding
770 // 1-byte types to fill remaining space before next 4-byte boundary
771 // Client API versions are clamped to 255 on receive (see send_hello_response_)
775 // Total: 2 (flags) + 2 + 1 + 1 + 1 + 1 (batch_header_size_ below) = 8 bytes,
776 // aligned to 4-byte boundary
777
778 // Actual header size used by encode_to_buffer for the current message.
779 // Read by process_batch_multi_ to pass into MessageInfo.
781
782 // Defined in api_connection_buffer.h (needs APIServer complete).
784 // Message will use 8 more bytes than the minimum size, and typical
785 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
786 // If its IPv6 the header is 40 bytes, and if its IPv4
787 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
788 // available for the payload. But we also need to add the size of
789 // the protobuf overhead, which is 8 bytes.
790 //
791 // To be safe we pick 1390 bytes as the maximum size
792 // to send in one go. This is the maximum size of a single packet
793 // that can be sent over the network.
794 // This is to avoid fragmentation of the packet.
795 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
796
797 bool schedule_batch_();
798 void process_batch_();
799 void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size)
800 __attribute__((noinline));
802 this->deferred_batch_.clear();
803 this->flags_.batch_scheduled = false;
804 }
805
806 // Dispatch message encoding based on message_type - replaces function pointer storage
807 // Switch assigns pointer, single call site for smaller code size
808 uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first);
809
810#ifdef HAS_PROTO_MESSAGE_DUMP
812 this->flags_.log_only_mode = true;
813 this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true);
814 this->flags_.log_only_mode = false;
815 }
816#endif
817
818 // Helper to check if a message type should bypass batching
819 // Returns true if:
820 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
821 // the main loop is blocked, e.g., during OTA updates)
822 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
823 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
824 // AND batch_delay = 0)
825 inline bool should_send_immediately_(uint16_t message_type) const {
826 return (
827#ifdef USE_UPDATE
828 message_type == UpdateStateResponse::MESSAGE_TYPE ||
829#endif
830#ifdef USE_EVENT
831 message_type == EventResponse::MESSAGE_TYPE ||
832#endif
833 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
834 }
835
836 // Helper method to send a message either immediately or via batching
837 // Tries immediate send if should_send_immediately_() returns true and buffer has space
838 // Falls back to batching if immediate send fails or isn't applicable
839 bool send_message_smart_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size,
840 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED);
841
842 // Helper function to schedule a deferred message with known message type
843 bool schedule_message_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size,
844 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED) {
845 this->deferred_batch_.add_item(entity, message_type, estimated_size, aux_data_index);
846 return this->schedule_batch_();
847 }
848
849 // Helper function to schedule a high priority message at the front of the batch
850 // Out-of-line: callers (on_shutdown, check_keepalive_) are cold paths
851 bool schedule_message_front_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size);
852
853 // Helper function to log client messages with name and peername
854 void log_client_(int level, const LogString *message);
855 // Helper function to log API errors with errno
856 void log_warning_(const LogString *message, APIError err);
857 // Helper to handle fatal errors with logging
858 inline void fatal_error_with_log_(const LogString *message, APIError err) {
859 this->on_fatal_error();
860 this->log_warning_(message, err);
861 }
862 // Shared cold path for buffer allocation failures — noinline keeps the
863 // OOM handling out of the hot send paths
864 void __attribute__((noinline)) fatal_out_of_memory_();
865};
866
867} // namespace esphome::api
868#endif
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:230
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:26
struct esphome::api::APIConnection::APIFlags flags_
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static constexpr uint32_t CONNECT_GRACE_MS
void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg)
void on_camera_image_request(const CameraImageRequest &msg)
void on_media_player_command_request(const MediaPlayerCommandRequest &msg)
static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_text_command_request(const TextCommandRequest &msg)
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_media_player_state(media_player::MediaPlayer *media_player)
void on_update_command_request(const UpdateCommandRequest &msg)
bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type)
int getpeername(struct sockaddr *addr, socklen_t *addrlen) const
Get the peer address itself, for remembering a dial-back target.
std::unique_ptr< APIPlaintextFrameHelper > helper_
bool send_time_state(datetime::TimeEntity *time)
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg)
static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn, uint32_t remaining_size)
void __attribute__((noinline)) process_active_iterator_()
bool send_water_heater_state(water_heater::WaterHeater *water_heater)
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg)
void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message)
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_button_command_request(const ButtonCommandRequest &msg)
void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg)
void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg)
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool prepare_first_message_buffer(size_t header_padding, size_t total_size)
Clear the shared write buffer and reserve space for the first message.
void on_valve_command_request(const ValveCommandRequest &msg)
void on_bluetooth_device_request(const BluetoothDeviceRequest &msg)
bool client_supports_api_version(uint8_t major, uint8_t minor) const
void __attribute__((noinline)) fatal_out_of_memory_()
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool should_send_immediately_(uint16_t message_type) const
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)
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor)
static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg)
bool send_fan_state(fan::Fan *fan)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, T &msg, ProgmemStringRef &device_class_field, APIConnection *conn, uint32_t remaining_size)
void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg)
bool check_voice_assistant_api_connection_() const
void send_serial_proxy_data(const SerialProxyDataReceived &msg)
bool schedule_message_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size, uint8_t aux_data_index=DeferredBatch::AUX_DATA_UNUSED)
static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
union esphome::api::APIConnection::IteratorUnion iterator_storage_
static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_cover_command_request(const CoverCommandRequest &msg)
std::unique_ptr< APIFrameHelper > helper_
static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void set_camera_state(std::shared_ptr< camera::CameraImage > image)
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg)
bool send_sensor_state(sensor::Sensor *sensor)
const char * get_peername_to(std::span< char, socket::SOCKADDR_STR_LEN > buf) const
Get peer name (IP address) into caller-provided buffer, returns buf for convenience.
static constexpr size_t MAX_BATCH_PACKET_SIZE
void log_batch_item_(const DeferredBatch::BatchItem &item)
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_fan_command_request(const FanCommandRequest &msg)
void mark_outgoing()
Outgoing connection: send our server hello immediately so the peer can pick the matching key.
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_wizard_input_set_request(const WizardInputSetRequest &msg)
bool send_message_smart_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size, uint8_t aux_data_index=DeferredBatch::AUX_DATA_UNUSED)
static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
const char * get_name() const
friend void bench_clear_batch(APIConnection *conn)
void on_disconnect_request(const DisconnectRequest &msg)
void on_climate_command_request(const ClimateCommandRequest &msg)
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor)
void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg)
bool send_valve_state(valve::Valve *valve)
bool send_voice_assistant_get_configuration_response_(const VoiceAssistantConfigurationRequest &msg)
bool send_select_state(select::Select *select)
void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg)
bool send_switch_state(switch_::Switch *a_switch)
void send_event(event::Event *event)
static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_z_wave_proxy_request(const ZWaveProxyRequest &msg)
static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_noise_encryption_set_key_response_(const NoiseEncryptionSetKeyRequest &msg)
void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg)
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
void on_date_command_request(const DateCommandRequest &msg)
void log_client_(int level, const LogString *message)
void on_subscribe_logs_request(const SubscribeLogsRequest &msg)
void resend_state_subscriptions(const char *entity_id)
Tell this client about the subscriptions whose entity id is stored in the given buffer,...
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg)
void send_infrared_rf_receive_event(const InfraredRFReceiveEvent &msg)
bool send_lock_state(lock::Lock *a_lock)
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg)
void on_select_command_request(const SelectCommandRequest &msg)
bool send_message(const T &msg)
Returns false as soon as the TCP buffer is full.
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg)
void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg)
void on_homeassistant_action_response(const HomeassistantActionResponse &msg)
bool is_still_connecting(uint32_t now)
void on_switch_command_request(const SwitchCommandRequest &msg)
bool send_update_state(update::UpdateEntity *update)
bool send_homeassistant_action(const HomeassistantActionRequest &call)
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, InfoResponseProtoMessage &msg, ProgmemStringRef &device_class_field, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
void on_get_time_response(const GetTimeResponse &value)
void fatal_error_with_log_(const LogString *message, APIError err)
static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg)
bool send_infrared_rf_transmit_complete(uint32_t device_id, uint32_t key, bool success)
void log_warning_(const LogString *message, APIError err)
void on_serial_proxy_request(const SerialProxyRequest &msg)
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_lock_command_request(const LockCommandRequest &msg)
void begin_iterator_(ActiveIterator type)
void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg)
std::unique_ptr< camera::CameraImageReader > image_reader_
void on_execute_service_request(const ExecuteServiceRequest &msg)
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
bool schedule_message_front_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size)
static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_date_state(datetime::DateEntity *date)
friend void bench_enable_immediate_send(APIConnection *conn)
bool send_message_(uint32_t payload_size, uint16_t message_type, MessageEncodeFn encode_fn, const void *msg)
void on_number_command_request(const NumberCommandRequest &msg)
bool send_number_state(number::Number *number)
static uint16_t encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg, APIConnection *conn, uint32_t remaining_size)
std::unique_ptr< APINoiseFrameHelper > helper_
static uint16_t try_send_radio_frequency_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_hello_response_(const HelloRequest &msg)
void on_serial_proxy_set_mode_request(const SerialProxySetModeRequest &msg)
bool send_light_state(light::LightState *light)
friend void bench_process_batch(APIConnection *conn)
static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
uint16_t(*)(EntityBase *, APIConnection *, uint32_t remaining_size) MessageCreatorPtr
static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg)
void on_time_command_request(const TimeCommandRequest &msg)
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_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg)
bool send_subscribe_bluetooth_connections_free_response_()
void on_hello_request(const HelloRequest &msg)
void on_voice_assistant_audio(const VoiceAssistantAudio &msg)
bool send_datetime_state(datetime::DateTimeEntity *datetime)
void on_serial_proxy_write_request(const SerialProxyWriteRequest &msg)
void on_light_command_request(const LightCommandRequest &msg)
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg)
bool send_text_state(text::Text *text)
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn, uint32_t remaining_size)
void on_date_time_command_request(const DateTimeCommandRequest &msg)
void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data)
static uint16_t ESPHOME_ALWAYS_INLINE encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg)
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len)
void process_iterator_batch_(ComponentIterator &iterator)
void send_serial_proxy_identity(const SerialProxyIdentity &msg)
Send a port identity to this client.
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_unsubscribe_bluetooth_le_advertisements_request()
void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size) __attribute__((noinline))
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_water_heater_command_request(const WaterHeaterCommandRequest &msg)
bool send_climate_state(climate::Climate *climate)
bool try_to_clear_buffer(bool log_out_of_space)
bool send_cover_state(cover::Cover *cover)
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool try_to_clear_buffer_slow_(bool log_out_of_space)
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg)
void __attribute__((noinline)) check_keepalive_(uint32_t now)
uint32_t(*)(const void *) CalculateSizeFn
static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_voice_assistant_response(const VoiceAssistantResponse &msg)
static constexpr uint16_t MESSAGE_TYPE
Definition api_pb2.h:3495
static constexpr uint16_t MESSAGE_TYPE
Definition api_pb2.h:855
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:856
static constexpr uint16_t MESSAGE_TYPE
Definition api_pb2.h:3663
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:192
Base class for all cover devices.
Definition cover.h:115
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
Base class for all locks.
Definition lock.h:112
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:28
Base-class for all sensors.
Definition sensor.h:47
Base class for all switches.
Definition switch.h:43
Base-class for all text inputs.
Definition text.h:20
Base class for all valve devices.
Definition valve.h:102
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
const LogString * message
Definition component.cpp:35
uint8_t type
const char * key
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
uint32_t socklen_t
Definition headers.h:109
uint8_t *(*)(const void *, uint8_t *PROTO_ENCODE_DEBUG_PARAM) ProtoEncodeFn
Generated encode body: writes the fields at pos, returns the cursor past them.
Definition proto.h:304
void bench_process_batch(APIConnection *conn)
void log_dropped_message(const char *tag, int line, const LogString *what)
void bench_enable_immediate_send(APIConnection *conn)
void bench_clear_batch(APIConnection *conn)
static void uint32_t
void remove_front(size_t count) __attribute__((noinline))
const BatchItem & operator[](size_t index) const
void add_item(EntityBase *entity, uint16_t message_type, uint8_t estimated_size, uint8_t aux_data_index=AUX_DATA_UNUSED)
void add_item_front(EntityBase *entity, uint16_t message_type, uint8_t estimated_size)
uint32_t payload_size()