ESPHome 2026.6.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"
18#ifdef USE_ESP32_CRASH_HANDLER
20#endif
21#ifdef USE_RP2040_CRASH_HANDLER
23#endif
24#ifdef USE_ESP8266_CRASH_HANDLER
26#endif
29
30#include <functional>
31#include <limits>
32#include <vector>
33
34namespace esphome {
35class ComponentIterator;
36} // namespace esphome
37
38namespace esphome::api {
39
40// Forward-declared to break the api_server.h cycle; full-type inlines are in api_connection_buffer.h.
41class APIServer;
42
43// Keepalive timeout in milliseconds
44static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
45// Maximum number of entities to process in a single batch during initial state/info sending
46// API 1.14+ clients compute object_id client-side, so messages are smaller and we can fit more per batch
47// TODO: Remove MAX_INITIAL_PER_BATCH_LEGACY before 2026.7.0 - all clients should support API 1.14 by then
48static constexpr size_t MAX_INITIAL_PER_BATCH_LEGACY = 24; // For clients < API 1.14 (includes object_id)
49static constexpr size_t MAX_INITIAL_PER_BATCH = 34; // For clients >= API 1.14 (no object_id)
50// Verify MAX_MESSAGES_PER_BATCH (defined in api_frame_helper.h) can hold the initial batch
51static_assert(MAX_MESSAGES_PER_BATCH >= MAX_INITIAL_PER_BATCH,
52 "MAX_MESSAGES_PER_BATCH must be >= MAX_INITIAL_PER_BATCH");
53
54#ifdef USE_BENCHMARK
55class APIConnection;
59#endif
60
62 public:
63 friend class APIServer;
65#ifdef USE_BENCHMARK
67 friend void bench_clear_batch(APIConnection *conn);
69#endif
70 APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
72
73 void start();
74 void loop();
75
76 protected:
77 // read_message_ is defined here (instead of in APIServerConnectionBase) so the
78 // compiler can devirtualize and inline on_* handler calls within this final class.
79 void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data);
80
81 // Auth helpers defined here (not in ProtoService) so the compiler can
82 // devirtualize is_connection_setup()/on_no_setup_connection() calls
83 // within this final class.
85 if (!this->is_connection_setup()) {
87 return false;
88 }
89 return true;
90 }
91 inline bool check_authenticated_() { return this->check_connection_setup_(); }
92
93 public:
98#ifdef USE_BINARY_SENSOR
100#endif
101#ifdef USE_COVER
102 bool send_cover_state(cover::Cover *cover);
104#endif
105#ifdef USE_FAN
106 bool send_fan_state(fan::Fan *fan);
108#endif
109#ifdef USE_LIGHT
112#endif
113#ifdef USE_SENSOR
114 bool send_sensor_state(sensor::Sensor *sensor);
115#endif
116#ifdef USE_SWITCH
117 bool send_switch_state(switch_::Switch *a_switch);
119#endif
120#ifdef USE_TEXT_SENSOR
122#endif
123#ifdef USE_CAMERA
124 void set_camera_state(std::shared_ptr<camera::CameraImage> image);
126#endif
127#ifdef USE_CLIMATE
130#endif
131#ifdef USE_NUMBER
132 bool send_number_state(number::Number *number);
134#endif
135#ifdef USE_DATETIME_DATE
138#endif
139#ifdef USE_DATETIME_TIME
142#endif
143#ifdef USE_DATETIME_DATETIME
146#endif
147#ifdef USE_TEXT
148 bool send_text_state(text::Text *text);
150#endif
151#ifdef USE_SELECT
152 bool send_select_state(select::Select *select);
154#endif
155#ifdef USE_BUTTON
157#endif
158#ifdef USE_LOCK
159 bool send_lock_state(lock::Lock *a_lock);
161#endif
162#ifdef USE_VALVE
163 bool send_valve_state(valve::Valve *valve);
165#endif
166#ifdef USE_MEDIA_PLAYER
169#endif
170 bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
171#ifdef USE_API_HOMEASSISTANT_SERVICES
174 return;
175 this->send_message(call);
176 }
177#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
179#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
180#endif // USE_API_HOMEASSISTANT_SERVICES
181#ifdef USE_BLUETOOTH_PROXY
184
195
196#endif
197#ifdef USE_HOMEASSISTANT_TIME
199 GetTimeRequest req;
200 this->send_message(req);
201 }
202#endif
203
204#ifdef USE_VOICE_ASSISTANT
213#endif
214
215#ifdef USE_ZWAVE_PROXY
216 void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg);
218#endif
219
220#ifdef USE_ALARM_CONTROL_PANEL
223#endif
224
225#ifdef USE_WATER_HEATER
228#endif
229
230#if defined(USE_IR_RF) || defined(USE_RADIO_FREQUENCY)
233#endif
234
235#ifdef USE_SERIAL_PROXY
242#endif
243
244#ifdef USE_EVENT
245 void send_event(event::Event *event);
246#endif
247
248#ifdef USE_UPDATE
251#endif
252
255 // we initiated ping
256 this->flags_.sent_ping = false;
257 }
258#ifdef USE_API_HOMEASSISTANT_STATES
260#endif
261#ifdef USE_HOMEASSISTANT_TIME
262 void on_get_time_response(const GetTimeResponse &value);
263#endif
264 void on_hello_request(const HelloRequest &msg);
266 void on_ping_request();
270 this->flags_.state_subscription = true;
271 // Start initial state iterator only if no iterator is active
272 // If list_entities is running, we'll start initial_state when it completes
275 }
276 }
278 this->flags_.log_subscription = msg.level;
279 if (msg.dump_config)
281#ifdef USE_ESP32_CRASH_HANDLER
284#endif
285#ifdef USE_RP2040_CRASH_HANDLER
287#endif
288#ifdef USE_ESP8266_CRASH_HANDLER
290#endif
291 }
292#ifdef USE_API_HOMEASSISTANT_SERVICES
294#endif
295#ifdef USE_API_HOMEASSISTANT_STATES
297#endif
298#ifdef USE_API_USER_DEFINED_ACTIONS
300#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
301 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
302#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
303 void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
304 const uint8_t *response_data, size_t response_data_len);
305#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
306#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
307#endif
308#ifdef USE_API_NOISE
310#endif
311
317 this->is_authenticated();
318 }
319 bool is_marked_for_removal() const { return this->flags_.remove; }
320 uint8_t get_log_subscription_level() const { return this->flags_.log_subscription; }
321
322 // Get client API version for feature detection
323 bool client_supports_api_version(uint16_t major, uint16_t minor) const {
324 return this->client_api_version_major_ > major ||
325 (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
326 }
327
328 void on_fatal_error();
330
331 // Function pointer type for type-erased message encoding
332 using MessageEncodeFn = uint8_t *(*) (const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM);
333 // Function pointer type for type-erased size calculation
334 using CalculateSizeFn = uint32_t (*)(const void *);
335
336 template<typename T> bool send_message(const T &msg) {
337 if constexpr (T::ESTIMATED_SIZE == 0) {
338 return this->send_message_(0, T::MESSAGE_TYPE, &encode_msg_noop, &msg);
339 } else {
340 return this->send_message_(msg.calculate_size(), T::MESSAGE_TYPE, &proto_encode_msg<T>, &msg);
341 }
342 }
343
344 void prepare_first_message_buffer(APIBuffer &shared_buf, size_t header_padding, size_t total_size) {
345 shared_buf.clear();
346 // Reserve space for header padding + message + footer
347 // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
348 // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
349 // Reserve full size but only set initial size to header padding
350 // so message encoding starts at the correct position
351 shared_buf.reserve_and_resize(total_size, header_padding);
352 }
353
354 // Convenience overload - computes frame overhead internally
356 const uint8_t header_padding = this->helper_->frame_header_padding();
357 const uint8_t footer_size = this->helper_->frame_footer_size();
358 this->prepare_first_message_buffer(shared_buf, header_padding, payload_size + header_padding + footer_size);
359 }
360
361 bool try_to_clear_buffer(bool log_out_of_space) {
362 if (this->flags_.remove)
363 return false;
364 if (this->helper_->can_write_without_blocking())
365 return true;
366 return this->try_to_clear_buffer_slow_(log_out_of_space);
367 }
368 bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type);
369
370 const char *get_name() const { return this->helper_->get_client_name(); }
372 const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
373 return this->helper_->get_peername_to(buf);
374 }
375
376 protected:
377 bool try_to_clear_buffer_slow_(bool log_out_of_space);
378
379 // Helper function to handle authentication completion
381
382 // Pattern B helpers: send response and return success/failure
383 bool send_hello_response_(const HelloRequest &msg);
385 bool send_ping_response_();
387#ifdef USE_API_NOISE
389#endif
390#ifdef USE_BLUETOOTH_PROXY
392#endif
393#ifdef USE_VOICE_ASSISTANT
395#endif
396
397#ifdef USE_CAMERA
399#endif
400
401#ifdef USE_API_HOMEASSISTANT_STATES
403#endif
404
405 // Size thunk — converts void* back to concrete type for direct calculate_size() call
406 template<typename T> static uint32_t calc_size(const void *msg) {
407 return static_cast<const T *>(msg)->calculate_size();
408 }
409
410 // Shared no-op encode thunk for empty messages (ESTIMATED_SIZE == 0)
411 static uint8_t *encode_msg_noop(const void *, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM) {
412 return buf.get_pos();
413 }
414
415 // Non-template buffer management for send_message
416 bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *msg);
417
418 // Core batch encoding logic. ALWAYS_INLINE so encode_fn devirtualizes at hot call sites.
419 // Defined in api_connection_buffer.h (needs APIServer complete).
420 static uint16_t ESPHOME_ALWAYS_INLINE encode_to_buffer(uint32_t calculated_size, MessageEncodeFn encode_fn,
421 const void *msg, APIConnection *conn, uint32_t remaining_size);
422
423 // Noinline version of encode_to_buffer for cold paths (entity info, zero-payload messages).
424 // All cold callers share this single copy instead of each getting an ALWAYS_INLINE expansion.
425 static uint16_t encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg,
426 APIConnection *conn, uint32_t remaining_size);
427
428 // Thin template wrapper — uses noinline encode_to_buffer_slow since
429 // encode_message_to_buffer callers are cold paths (zero-payload control messages).
430 // Hot paths (state/info) go through fill_and_encode_entity_state/info instead.
431 // batch_message_type_ is already set by dispatch_message_ before reaching here.
432 template<typename T> static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size) {
433 if constexpr (T::ESTIMATED_SIZE == 0) {
434 return encode_to_buffer_slow(0, &encode_msg_noop, &msg, conn, remaining_size);
435 } else {
436 return encode_to_buffer_slow(msg.calculate_size(), &proto_encode_msg<T>, &msg, conn, remaining_size);
437 }
438 }
439
440 // Non-template core — fills state fields and encodes
442 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
443 uint32_t remaining_size);
444
445 // Thin template wrapper
446 template<typename T>
447 static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn,
448 uint32_t remaining_size) {
449 return fill_and_encode_entity_state(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
450 }
451
452 // Non-template core — fills info fields, allocates buffers, and encodes
454 CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn,
455 uint32_t remaining_size);
456
457 // Thin template wrapper
458 template<typename T>
459 static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn,
460 uint32_t remaining_size) {
461 return fill_and_encode_entity_info(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
462 }
463
464 // Non-template core — fills device_class, then delegates to fill_and_encode_entity_info
466 StringRef &device_class_field, CalculateSizeFn size_fn,
467 MessageEncodeFn encode_fn, APIConnection *conn,
468 uint32_t remaining_size);
469
470 // Thin template wrapper
471 template<typename T>
473 StringRef &device_class_field, APIConnection *conn,
474 uint32_t remaining_size) {
475 return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &calc_size<T>,
476 &proto_encode_msg<T>, conn, remaining_size);
477 }
478
479#ifdef USE_VOICE_ASSISTANT
480 // Helper to check voice assistant validity and connection ownership
481 inline bool check_voice_assistant_api_connection_() const;
482#endif
483
484 // Get the max batch size based on client API version
485 // API 1.14+ clients don't receive object_id, so messages are smaller and more fit per batch
486 // TODO: Remove this method before 2026.7.0 and use MAX_INITIAL_PER_BATCH directly
487 size_t get_max_batch_size_() const {
488 return this->client_supports_api_version(1, 14) ? MAX_INITIAL_PER_BATCH : MAX_INITIAL_PER_BATCH_LEGACY;
489 }
490
491 // Send keepalive ping or disconnect unresponsive client.
492 // Cold path — extracted from loop() to reduce instruction cache pressure.
493 void __attribute__((noinline)) check_keepalive_(uint32_t now);
494
495 // Process active iterator (list_entities/initial_state) during connection setup.
496 // Extracted from loop() — only runs during initial handshake, NONE in steady state.
497 void __attribute__((noinline)) process_active_iterator_();
498
499 // Helper method to process multiple entities from an iterator in a batch.
500 // Takes ComponentIterator base class reference to avoid duplicate template instantiations.
502
503#ifdef USE_BINARY_SENSOR
504 static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
505 static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
506#endif
507#ifdef USE_COVER
508 static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
509 static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
510#endif
511#ifdef USE_FAN
512 static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
513 static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
514#endif
515#ifdef USE_LIGHT
516 static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
517 static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
518#endif
519#ifdef USE_SENSOR
520 static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
521 static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
522#endif
523#ifdef USE_SWITCH
524 static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
525 static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
526#endif
527#ifdef USE_TEXT_SENSOR
528 static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
529 static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
530#endif
531#ifdef USE_CLIMATE
532 static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
533 static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
534#endif
535#ifdef USE_NUMBER
536 static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
537 static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
538#endif
539#ifdef USE_DATETIME_DATE
540 static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
541 static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
542#endif
543#ifdef USE_DATETIME_TIME
544 static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
545 static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
546#endif
547#ifdef USE_DATETIME_DATETIME
548 static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
549 static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
550#endif
551#ifdef USE_TEXT
552 static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
553 static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
554#endif
555#ifdef USE_SELECT
556 static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
557 static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
558#endif
559#ifdef USE_BUTTON
560 static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
561#endif
562#ifdef USE_LOCK
563 static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
564 static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
565#endif
566#ifdef USE_VALVE
567 static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
568 static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
569#endif
570#ifdef USE_MEDIA_PLAYER
571 static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
572 static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
573#endif
574#ifdef USE_ALARM_CONTROL_PANEL
575 static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
576 static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
577#endif
578#ifdef USE_WATER_HEATER
579 static uint16_t try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
580 static uint16_t try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
581#endif
582#ifdef USE_INFRARED
583 static uint16_t try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
584#endif
585#ifdef USE_RADIO_FREQUENCY
586 static uint16_t try_send_radio_frequency_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
587#endif
588#ifdef USE_EVENT
589 static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
590 uint32_t remaining_size);
591 static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
592#endif
593#ifdef USE_UPDATE
594 static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
595 static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
596#endif
597#ifdef USE_CAMERA
598 static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
599#endif
600
601 // Method for ListEntitiesDone batching
602 static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
603
604 // Method for DisconnectRequest batching
605 static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
606
607 // Batch message method for ping requests
608 static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size);
609
610 // === Optimal member ordering for 32-bit systems ===
611
612 // Group 1: Pointers (4 bytes each on 32-bit)
613#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
614 std::unique_ptr<APIFrameHelper> helper_;
615#elif defined(USE_API_NOISE)
616 std::unique_ptr<APINoiseFrameHelper> helper_;
617#elif defined(USE_API_PLAINTEXT)
618 std::unique_ptr<APIPlaintextFrameHelper> helper_;
619#endif
621
622 // Group 2: Iterator union (saves ~16 bytes vs separate iterators)
623 // These iterators are never active simultaneously - list_entities runs to completion
624 // before initial_state begins, so we use a union with explicit construction/destruction.
625 enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
626
630 // Constructor/destructor do nothing - use placement new/explicit destructor
634
635 // Helper methods for iterator lifecycle management
639#ifdef USE_CAMERA
640 std::unique_ptr<camera::CameraImageReader> image_reader_;
641#endif
642
643 // Group 3: 4-byte types
645#ifdef USE_API_HOMEASSISTANT_STATES
647#endif
648
649 // Function pointer type for message encoding
650 using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size);
651
652 // Generic batching mechanism for both state updates and entity info
654 // Sentinel value for unused aux_data_index
655 static constexpr uint8_t AUX_DATA_UNUSED = std::numeric_limits<uint8_t>::max();
656
657 struct BatchItem {
658 EntityBase *entity; // 4 bytes - Entity pointer
659 uint8_t message_type; // 1 byte - Message type for protocol and dispatch
660 uint8_t estimated_size; // 1 byte - Estimated message size (max 255 bytes)
661 uint8_t aux_data_index{AUX_DATA_UNUSED}; // 1 byte - For events: index into entity's event_types
662 // 1 byte padding
663 };
664
665 std::vector<BatchItem> items;
667
668 // No pre-allocation - log connections never use batching, and for
669 // connections that do, buffers are released after initial sync anyway
670
671 // Add item to the batch (with deduplication)
672 void add_item(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
673 uint8_t aux_data_index = AUX_DATA_UNUSED) {
674 // Dedup: O(n) scan but optimized for RAM over performance
675 // Skip deduplication for events - they are edge-triggered, every occurrence matters
676#ifdef USE_EVENT
677 if (message_type != EventResponse::MESSAGE_TYPE)
678#endif
679 {
680 for (const auto &item : this->items) {
681 if (item.entity == entity && item.message_type == message_type)
682 return; // Already queued
683 }
684 }
685 this->items.push_back({entity, message_type, estimated_size, aux_data_index});
686 }
687 // Add item to the front of the batch (for high priority messages like ping)
688 void add_item_front(EntityBase *entity, uint8_t message_type, uint8_t estimated_size) {
689 // Swap to front avoids expensive vector::insert which shifts all elements
690 this->items.push_back({entity, message_type, estimated_size, AUX_DATA_UNUSED});
691 if (this->items.size() > 1) {
692 std::swap(this->items.front(), this->items.back());
693 }
694 }
695
696 // Clear all items
697 void clear() {
698 items.clear();
700 }
701
702 // Remove processed items from the front — noinline to keep memmove out of warm callers
703 void remove_front(size_t count) __attribute__((noinline)) { items.erase(items.begin(), items.begin() + count); }
704
705 bool empty() const { return items.empty(); }
706 size_t size() const { return items.size(); }
707 const BatchItem &operator[](size_t index) const { return items[index]; }
708
709 // Release excess capacity - only releases if items already empty
711 // Safe to call: batch is processed before release_buffer is called,
712 // and if any items remain (partial processing), we must not clear them.
713 // Use swap trick since shrink_to_fit() is non-binding and may be ignored.
714 if (items.empty()) {
715 std::vector<BatchItem>().swap(items);
716 }
717 }
718 };
719
720 // DeferredBatch here (16 bytes, 4-byte aligned)
722
723 // ConnectionState enum for type safety
724 enum class ConnectionState : uint8_t {
726 CONNECTED = 1,
727 AUTHENTICATED = 2,
728 };
729
730 // Group 5: Pack all small members together to minimize padding
731 // This group starts at a 4-byte boundary after DeferredBatch
732 struct APIFlags {
733 // Connection state only needs 2 bits (3 states)
734 uint8_t connection_state : 2;
735 // Log subscription needs 3 bits (log levels 0-7)
736 uint8_t log_subscription : 3;
737 // Boolean flags (1 bit each)
738 uint8_t remove : 1;
740 uint8_t sent_ping : 1;
741
743 uint8_t next_close : 1;
744 uint8_t batch_scheduled : 1;
745 uint8_t batch_first_message : 1; // For batch buffer allocation
746 uint8_t should_try_send_immediately : 1; // True after initial states are sent
747 uint8_t may_have_remaining_data : 1; // Read loop hit limit, retry without ready check
748#ifdef HAS_PROTO_MESSAGE_DUMP
749 uint8_t log_only_mode : 1;
750#endif
751 } flags_{}; // 2 bytes total
752
753 // 2-byte types immediately after flags_ (no padding between them)
756 // 1-byte types to fill remaining space before next 4-byte boundary
758 uint8_t batch_message_type_{0}; // Current message type during batch encoding
759 // Total: 2 (flags) + 2 + 2 + 1 + 1 = 8 bytes, aligned to 4-byte boundary
760
761 // Actual header size used by encode_to_buffer for the current message.
762 // Read by process_batch_multi_ to pass into MessageInfo.
764
765 // Defined in api_connection_buffer.h (needs APIServer complete).
767 // Message will use 8 more bytes than the minimum size, and typical
768 // MTU is 1500. Sometimes users will see as low as 1460 MTU.
769 // If its IPv6 the header is 40 bytes, and if its IPv4
770 // the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
771 // available for the payload. But we also need to add the size of
772 // the protobuf overhead, which is 8 bytes.
773 //
774 // To be safe we pick 1390 bytes as the maximum size
775 // to send in one go. This is the maximum size of a single packet
776 // that can be sent over the network.
777 // This is to avoid fragmentation of the packet.
778 static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390; // MTU
779
780 bool schedule_batch_();
781 void process_batch_();
782 void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size)
783 __attribute__((noinline));
785 this->deferred_batch_.clear();
786 this->flags_.batch_scheduled = false;
787 }
788
789 // Dispatch message encoding based on message_type - replaces function pointer storage
790 // Switch assigns pointer, single call site for smaller code size
791 uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first);
792
793#ifdef HAS_PROTO_MESSAGE_DUMP
795 this->flags_.log_only_mode = true;
796 this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true);
797 this->flags_.log_only_mode = false;
798 }
799#endif
800
801 // Helper to check if a message type should bypass batching
802 // Returns true if:
803 // 1. It's an UpdateStateResponse (always send immediately to handle cases where
804 // the main loop is blocked, e.g., during OTA updates)
805 // 2. It's an EventResponse (events are edge-triggered - every occurrence matters)
806 // 3. OR: User has opted into immediate sending (should_try_send_immediately = true
807 // AND batch_delay = 0)
808 inline bool should_send_immediately_(uint8_t message_type) const {
809 return (
810#ifdef USE_UPDATE
811 message_type == UpdateStateResponse::MESSAGE_TYPE ||
812#endif
813#ifdef USE_EVENT
814 message_type == EventResponse::MESSAGE_TYPE ||
815#endif
816 (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0));
817 }
818
819 // Helper method to send a message either immediately or via batching
820 // Tries immediate send if should_send_immediately_() returns true and buffer has space
821 // Falls back to batching if immediate send fails or isn't applicable
822 bool send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
823 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED);
824
825 // Helper function to schedule a deferred message with known message type
826 bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
827 uint8_t aux_data_index = DeferredBatch::AUX_DATA_UNUSED) {
828 this->deferred_batch_.add_item(entity, message_type, estimated_size, aux_data_index);
829 return this->schedule_batch_();
830 }
831
832 // Helper function to schedule a high priority message at the front of the batch
833 // Out-of-line: callers (on_shutdown, check_keepalive_) are cold paths
834 bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size);
835
836 // Helper function to log client messages with name and peername
837 void log_client_(int level, const LogString *message);
838 // Helper function to log API errors with errno
839 void log_warning_(const LogString *message, APIError err);
840 // Helper to handle fatal errors with logging
841 inline void fatal_error_with_log_(const LogString *message, APIError err) {
842 this->on_fatal_error();
843 this->log_warning_(message, err);
844 }
845};
846
847} // namespace esphome::api
848#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:36
void reserve_and_resize(size_t reserve_size, size_t new_size) ESPHOME_ALWAYS_INLINE
Reserve capacity for max(reserve_size, new_size) bytes, then set size to new_size.
Definition api_buffer.h:49
struct esphome::api::APIConnection::APIFlags flags_
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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)
std::unique_ptr< APIPlaintextFrameHelper > helper_
bool send_time_state(datetime::TimeEntity *time)
bool schedule_message_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size, uint8_t aux_data_index=DeferredBatch::AUX_DATA_UNUSED)
void on_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)
void on_valve_command_request(const ValveCommandRequest &msg)
void on_bluetooth_device_request(const BluetoothDeviceRequest &msg)
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
uint16_t dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size, bool batch_first)
static uint32_t calc_size(const void *msg)
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)
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 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)
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)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, T &msg, StringRef &device_class_field, APIConnection *conn, uint32_t remaining_size)
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)
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
friend void bench_clear_batch(APIConnection *conn)
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)
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, InfoResponseProtoMessage &msg, StringRef &device_class_field, CalculateSizeFn size_fn, MessageEncodeFn encode_fn, APIConnection *conn, uint32_t remaining_size)
void on_date_command_request(const DateCommandRequest &msg)
void log_client_(int level, const LogString *message)
void on_subscribe_logs_request(const SubscribeLogsRequest &msg)
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)
uint8_t *(*)(const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM) MessageEncodeFn
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)
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg)
void on_select_command_request(const SelectCommandRequest &msg)
bool send_message(const T &msg)
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void 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)
void prepare_first_message_buffer(APIBuffer &shared_buf, size_t payload_size)
void on_switch_command_request(const SwitchCommandRequest &msg)
bool send_update_state(update::UpdateEntity *update)
static uint16_t try_send_datetime_state(EntityBase *entity, 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)
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)
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)
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_
bool schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size)
static uint16_t try_send_radio_frequency_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
bool send_hello_response_(const HelloRequest &msg)
void send_homeassistant_action(const HomeassistantActionRequest &call)
bool send_light_state(light::LightState *light)
static uint8_t * encode_msg_noop(const void *, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM)
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)
bool send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn, const void *msg)
void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg)
bool send_subscribe_bluetooth_connections_free_response_()
void prepare_first_message_buffer(APIBuffer &shared_buf, size_t header_padding, size_t total_size)
void on_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)
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
void on_unsubscribe_bluetooth_le_advertisements_request()
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type)
bool client_supports_api_version(uint16_t major, uint16_t minor) const
void process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding, uint8_t footer_size) __attribute__((noinline))
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size)
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 uint8_t MESSAGE_TYPE
Definition api_pb2.h:2830
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:594
static constexpr uint8_t ESTIMATED_SIZE
Definition api_pb2.h:595
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2970
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:187
Base class for all cover devices.
Definition cover.h:110
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
Base class for all locks.
Definition lock.h:112
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:29
Base-class for all sensors.
Definition sensor.h:47
Base class for all switches.
Definition switch.h:38
Base-class for all text inputs.
Definition text.h:21
Base class for all valve devices.
Definition valve.h:103
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
const LogString * message
Definition component.cpp:35
uint16_t type
uint8_t * proto_encode_msg(const void *msg, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM)
Definition proto.h:885
void bench_process_batch(APIConnection *conn)
void bench_enable_immediate_send(APIConnection *conn)
void bench_clear_batch(APIConnection *conn)
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_clear()
Clear the magic marker and mark crash data as consumed.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
void 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()