ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
api_connection.cpp
Go to the documentation of this file.
1#include "api_connection.h"
2#ifdef USE_API
3#include "api_connection_buffer.h" // for encode_to_buffer / get_batch_delay_ms_ inlines
4#ifdef USE_API_NOISE
6#endif
7#ifdef USE_API_PLAINTEXT
9#endif
10#ifdef USE_API_USER_DEFINED_ACTIONS
11#include "user_services.h"
12#endif
13#include <cerrno>
14#include <cinttypes>
15#include <functional>
16#include <limits>
17#include <new>
18#include <utility>
19#ifdef USE_ESP8266
20#include <pgmspace.h>
21#endif
25#include "esphome/core/hal.h"
26#include "esphome/core/log.h"
28#ifdef USE_PROVISIONING
30#endif
31
32#ifdef USE_DEEP_SLEEP
34#endif
35#ifdef USE_HOMEASSISTANT_TIME
37#endif
38#ifdef USE_BLUETOOTH_PROXY
40#endif
41#ifdef USE_CLIMATE
43#endif
44#ifdef USE_VOICE_ASSISTANT
46#endif
47#ifdef USE_ZWAVE_PROXY
49#endif
50#ifdef USE_WATER_HEATER
52#endif
53#ifdef USE_INFRARED
55#endif
56#ifdef USE_RADIO_FREQUENCY
58#endif
59
60namespace esphome::api {
61
62// Maximum messages to read per loop iteration to prevent starving other components.
63// This is a balance between API responsiveness and allowing other components to run.
64// Since each message could contain multiple protobuf messages when using packet batching,
65// this limits the number of messages processed, not the number of TCP packets.
66static constexpr uint8_t MAX_MESSAGES_PER_LOOP = 10;
67static constexpr uint8_t MAX_PING_RETRIES = 60;
68static constexpr uint16_t PING_RETRY_INTERVAL = 1000;
69static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2;
70// Timeout for completing the handshake (Noise transport + HelloRequest).
71// A stalled handshake from a buggy client or network glitch holds a connection
72// slot, which can prevent legitimate clients from reconnecting. Also hardens
73// against the less likely case of intentional connection slot exhaustion.
74//
75// 60s is intentionally high: on ESP8266 with power_save_mode: LIGHT and weak
76// WiFi (-70 dBm+), TCP retransmissions push real-world handshake times to
77// 28-30s. See https://github.com/esphome/esphome/issues/14999
78static constexpr uint32_t HANDSHAKE_TIMEOUT_MS = 60000;
79
80static constexpr auto ESPHOME_VERSION_REF = StringRef::from_lit(ESPHOME_VERSION);
81
82// Cross-validate C++ constants against proto max_data_length annotations in api.proto
83static_assert(MAC_ADDRESS_PRETTY_BUFFER_SIZE - 1 == 17,
84 "Update max_data_length for mac_address/bluetooth_mac_address in api.proto");
85static_assert(Application::BUILD_TIME_STR_SIZE - 1 == 25, "Update max_data_length for compilation_time in api.proto");
86static_assert(sizeof(ESPHOME_VERSION) - 1 <= 32, "Update max_data_length for esphome_version in api.proto");
87static_assert(ESPHOME_DEVICE_NAME_MAX_LEN <= 31, "Update max_data_length for name in api.proto");
88static_assert(ESPHOME_FRIENDLY_NAME_MAX_LEN <= 120, "Update max_data_length for friendly_name in api.proto");
89
90static const char *const TAG = "api.connection";
91#ifdef USE_CAMERA
92static const int CAMERA_STOP_STREAM = 5000;
93#endif
94
95#ifdef USE_DEVICES
96// Helper macro for entity command handlers - gets entity by key and device_id, returns if not found, and creates call
97// object
98#define ENTITY_COMMAND_MAKE_CALL(entity_type, entity_var, getter_name) \
99 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key, msg.device_id); \
100 if ((entity_var) == nullptr) \
101 return; \
102 auto call = (entity_var)->make_call();
103
104// Helper macro for entity command handlers that don't use make_call() - gets entity by key and device_id and returns if
105// not found
106#define ENTITY_COMMAND_GET(entity_type, entity_var, getter_name) \
107 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key, msg.device_id); \
108 if ((entity_var) == nullptr) \
109 return;
110
111// Helper macro for multi-entity dispatch: looks up an entity by key and device_id without early return or make_call().
112// Use when multiple entity types must be checked in sequence (at most one will match).
113#define ENTITY_COMMAND_LOOKUP(entity_type, entity_var, getter_name) \
114 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key, msg.device_id)
115
116#else // No device support, use simpler macros
117// Helper macro for entity command handlers - gets entity by key, returns if not found, and creates call
118// object
119#define ENTITY_COMMAND_MAKE_CALL(entity_type, entity_var, getter_name) \
120 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key); \
121 if ((entity_var) == nullptr) \
122 return; \
123 auto call = (entity_var)->make_call();
124
125// Helper macro for entity command handlers that don't use make_call() - gets entity by key and returns if
126// not found
127#define ENTITY_COMMAND_GET(entity_type, entity_var, getter_name) \
128 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key); \
129 if ((entity_var) == nullptr) \
130 return;
131
132// Helper macro for multi-entity dispatch: looks up an entity by key without early return or make_call().
133// Use when multiple entity types must be checked in sequence (at most one will match).
134#define ENTITY_COMMAND_LOOKUP(entity_type, entity_var, getter_name) \
135 entity_type *entity_var = App.get_##getter_name##_by_key(msg.key)
136
137#endif // USE_DEVICES
138
139APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent) : parent_(parent) {
140#if defined(USE_API_PLAINTEXT) && defined(USE_API_NOISE)
141 auto &noise_ctx = parent->get_noise_ctx();
142 if (noise_ctx.has_psk()) {
143 this->helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), noise_ctx)};
144 } else {
145 this->helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
146 }
147#elif defined(USE_API_PLAINTEXT)
148 this->helper_ = std::unique_ptr<APIPlaintextFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
149#elif defined(USE_API_NOISE)
150 this->helper_ =
151 std::unique_ptr<APINoiseFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
152#else
153#error "No frame helper defined"
154#endif
155#ifdef USE_CAMERA
156 if (camera::Camera::instance() != nullptr) {
157 this->image_reader_ = std::unique_ptr<camera::CameraImageReader>{camera::Camera::instance()->create_image_reader()};
158 }
159#endif
160}
161
162void APIConnection::start() {
163 this->last_traffic_ = App.get_loop_component_start_time();
164
165 APIError err = this->helper_->init();
166 if (err != APIError::OK) {
167 this->fatal_error_with_log_(LOG_STR("Helper init failed"), err);
168 return;
169 }
170 // Initialize client name with peername (IP address) until Hello message provides actual name
171 char peername[socket::SOCKADDR_STR_LEN];
172 this->helper_->set_client_name(this->helper_->get_peername_to(peername), strlen(peername));
173}
174
175APIConnection::~APIConnection() {
176 this->destroy_active_iterator_();
177#ifdef USE_BLUETOOTH_PROXY
178 if (bluetooth_proxy::global_bluetooth_proxy->get_api_connection() == this) {
180 }
181#endif
182#ifdef USE_VOICE_ASSISTANT
183 if (voice_assistant::global_voice_assistant->get_api_connection() == this) {
185 }
186#endif
187#ifdef USE_ZWAVE_PROXY
188 if (zwave_proxy::global_zwave_proxy != nullptr && zwave_proxy::global_zwave_proxy->get_api_connection() == this) {
189 zwave_proxy::global_zwave_proxy->zwave_proxy_request(this, enums::ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE);
190 }
191#endif
192#ifdef USE_SERIAL_PROXY
193 for (auto *proxy : App.get_serial_proxies()) {
194 if (proxy->get_api_connection() == this) {
195 proxy->serial_proxy_request(this, enums::SERIAL_PROXY_REQUEST_TYPE_UNSUBSCRIBE);
196 }
197 }
198#endif
199}
200
201#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
202void APIConnection::upgrade_helper_to_noise_() {
203 // The client opened with a Noise hello while this device has no encryption
204 // key set. Replace the plaintext helper with a Noise helper so the key can
205 // be provisioned over an encrypted channel: the noise context PSK is all
206 // zeros when unprovisioned, and NNpsk0 still runs a fresh ephemeral X25519
207 // exchange, so a passive listener cannot read the session. A publicly known
208 // PSK authenticates nobody; this protects against sniffing only.
209 auto *plaintext = static_cast<APIPlaintextFrameHelper *>(this->helper_.get());
210 uint8_t header[3];
211 uint8_t header_len = plaintext->get_consumed_header(header);
212 auto *noise = new APINoiseFrameHelper(plaintext->release_socket_for_switch(), this->parent_->get_noise_ctx());
213 // Carry over the peername-based client name (Hello has not arrived yet)
214 const char *name = plaintext->get_client_name();
215 noise->set_client_name(name, strlen(name));
216 this->helper_.reset(noise); // destroys the plaintext helper
217 APIError err = noise->init_from_handoff(header, header_len);
218 if (err != APIError::OK) {
219 this->fatal_error_with_log_(LOG_STR("Noise handoff failed"), err);
220 }
221}
222#endif // USE_API_NOISE && USE_API_PLAINTEXT
223
224void APIConnection::destroy_active_iterator_() {
225 switch (this->active_iterator_) {
226 case ActiveIterator::LIST_ENTITIES:
227 this->iterator_storage_.list_entities.~ListEntitiesIterator();
228 break;
229 case ActiveIterator::INITIAL_STATE:
230 this->iterator_storage_.initial_state.~InitialStateIterator();
231 break;
232 case ActiveIterator::NONE:
233 break;
234 }
235 this->active_iterator_ = ActiveIterator::NONE;
236}
237
238void APIConnection::begin_iterator_(ActiveIterator type) {
239 this->destroy_active_iterator_();
240 this->active_iterator_ = type;
241 if (type == ActiveIterator::LIST_ENTITIES) {
242 new (&this->iterator_storage_.list_entities) ListEntitiesIterator(this);
243 this->iterator_storage_.list_entities.begin();
244 } else {
245 new (&this->iterator_storage_.initial_state) InitialStateIterator(this);
246 this->iterator_storage_.initial_state.begin();
247 }
248}
249
250void APIConnection::loop() {
251 if (this->flags_.next_close) {
252 // requested a disconnect - don't close socket here, let APIServer::loop() do it
253 // so getpeername() still works for the disconnect trigger
254 this->flags_.remove = true;
255 return;
256 }
257
258 APIError err = this->helper_->loop();
259 if (err != APIError::OK) {
260 this->fatal_error_with_log_(LOG_STR("Socket operation failed"), err);
261 return;
262 }
263
265 // Check if socket has data ready before attempting to read.
266 // Also try reading if we hit the message limit last time — LWIP's rcvevent
267 // (used by is_socket_ready) tracks pbuf dequeues, not bytes. When multiple
268 // messages share a TCP segment, the last message's data stays in LWIP's
269 // lastdata cache after rcvevent hits 0, making is_socket_ready() return false
270 // even though data remains.
271 if (this->helper_->is_socket_ready() || this->flags_.may_have_remaining_data) {
272 this->flags_.may_have_remaining_data = false;
273 // Read up to MAX_MESSAGES_PER_LOOP messages per loop to improve throughput
274 uint8_t message_count = 0;
275 for (; message_count < MAX_MESSAGES_PER_LOOP; message_count++) {
276 ReadPacketBuffer buffer;
277 err = this->helper_->read_packet(&buffer);
278 if (err == APIError::WOULD_BLOCK) {
279 // No more data available
280 break;
281 } else if (err != APIError::OK) {
282#if defined(USE_API_NOISE) && defined(USE_API_PLAINTEXT)
283 // Checked inside the error branch to keep the hot err == OK path
284 // free of it; this can only fire on the first bytes of a plaintext
285 // helper on an unprovisioned device
286 if (err == APIError::PROTOCOL_SWITCH_TO_NOISE) {
287 this->upgrade_helper_to_noise_();
288 return;
289 }
290#endif
291 this->fatal_error_with_log_(LOG_STR("Reading failed"), err);
292 return;
293 } else {
294 // Only update last_traffic_ after authentication to ensure the
295 // handshake timeout is an absolute deadline from connection start.
296 // Pre-auth messages (e.g. PingRequest) must not reset the timer.
297 if (this->is_authenticated()) {
298 this->last_traffic_ = now;
299 }
300 // read a packet
301 this->read_message_(buffer.data_len, buffer.type, buffer.data);
302 if (this->flags_.remove)
303 return;
304 }
305 }
306 // If we hit the limit, there may be more data remaining in LWIP's
307 // lastdata cache that rcvevent doesn't account for.
308 if (message_count == MAX_MESSAGES_PER_LOOP) {
309 this->flags_.may_have_remaining_data = true;
310 }
311 }
312
313 // Process deferred batch if scheduled and timer has expired
314 if (this->flags_.batch_scheduled && now - this->deferred_batch_.batch_start_time >= this->get_batch_delay_ms_()) {
315 this->process_batch_();
316 }
317
318 if (this->active_iterator_ != ActiveIterator::NONE) {
319 this->process_active_iterator_();
320 }
321
322 // Disconnect clients that haven't completed the handshake in time.
323 // Stale half-open connections from buggy clients or network issues can
324 // accumulate and block legitimate clients from reconnecting.
325 if (!this->is_authenticated() && now - this->last_traffic_ > HANDSHAKE_TIMEOUT_MS) {
326 this->on_fatal_error();
327 this->log_client_(ESPHOME_LOG_LEVEL_WARN, LOG_STR("handshake timeout; disconnecting"));
328 return;
329 }
330
331 // Keepalive: only call into the cold path when enough time has elapsed.
332 // When sent_ping is true, last_traffic_ hasn't been updated so this
333 // condition is already satisfied — covers both send-ping and disconnect cases.
334 if (now - this->last_traffic_ > KEEPALIVE_TIMEOUT_MS) {
335 this->check_keepalive_(now);
336 }
337
338#ifdef USE_API_HOMEASSISTANT_STATES
339 if (state_subs_at_ >= 0) {
340 this->process_state_subscriptions_();
341 }
342#endif
343
344#ifdef USE_CAMERA
345 // Process camera last - state updates are higher priority
346 // (missing a frame is fine, missing a state update is not)
347 this->try_send_camera_image_();
348#endif
349}
350
351void APIConnection::check_keepalive_(uint32_t now) {
352 // Caller guarantees: now - last_traffic_ > KEEPALIVE_TIMEOUT_MS
353 if (this->flags_.sent_ping) {
354 // Disconnect if not responded within 2.5*keepalive
355 if (now - this->last_traffic_ > KEEPALIVE_DISCONNECT_TIMEOUT) {
356 on_fatal_error();
357 this->log_client_(ESPHOME_LOG_LEVEL_WARN, LOG_STR("is unresponsive; disconnecting"));
358 }
359 } else if (!this->flags_.remove) {
360 // Only send ping if we're not disconnecting
361 ESP_LOGVV(TAG, "Sending keepalive PING");
362 PingRequest req;
363 this->flags_.sent_ping = this->send_message(req);
364 if (!this->flags_.sent_ping) {
365 // If we can't send the ping request directly (tx_buffer full),
366 // schedule it at the front of the batch so it will be sent with priority
367 ESP_LOGW(TAG, "Buffer full, ping queued");
368 this->schedule_message_front_(nullptr, PingRequest::MESSAGE_TYPE, PingRequest::ESTIMATED_SIZE);
369 this->flags_.sent_ping = true; // Mark as sent to avoid scheduling multiple pings
370 }
371 }
372}
373
374void APIConnection::process_active_iterator_() {
375 // Caller ensures active_iterator_ != NONE
376 if (this->active_iterator_ == ActiveIterator::LIST_ENTITIES) {
377 if (this->iterator_storage_.list_entities.completed()) {
378 this->destroy_active_iterator_();
379 if (this->flags_.state_subscription) {
380 this->begin_iterator_(ActiveIterator::INITIAL_STATE);
381 } else {
382 this->finalize_iterator_sync_();
383 }
384 } else {
385 this->process_iterator_batch_(this->iterator_storage_.list_entities);
386 }
387 } else { // INITIAL_STATE
388 if (this->iterator_storage_.initial_state.completed()) {
389 this->destroy_active_iterator_();
390 this->finalize_iterator_sync_();
391 } else {
392 this->process_iterator_batch_(this->iterator_storage_.initial_state);
393 }
394 }
395}
396
397void APIConnection::finalize_iterator_sync_() {
398 // Flush any remaining batched messages immediately so clients
399 // receive completion responses (e.g. ListEntitiesDoneResponse)
400 // without waiting for the batch timer.
401 if (!this->deferred_batch_.empty()) {
402 this->process_batch_();
403 }
404 // Enable immediate sending for future state changes
405 this->flags_.should_try_send_immediately = true;
406 // Release excess memory from buffers that grew during initial sync
407 this->deferred_batch_.release_buffer();
408 this->helper_->release_buffers();
409}
410
411void APIConnection::process_iterator_batch_(ComponentIterator &iterator) {
412 size_t initial_size = this->deferred_batch_.size();
413 size_t max_batch = MAX_INITIAL_PER_BATCH;
414 while (!iterator.completed() && (this->deferred_batch_.size() - initial_size) < max_batch) {
415 iterator.advance();
416 }
417
418 // If the batch is full, process it immediately
419 // Note: iterator.advance() already calls schedule_batch_() via schedule_message_()
420 if (this->deferred_batch_.size() >= max_batch) {
421 this->process_batch_();
422 }
423}
424
425bool APIConnection::send_disconnect_response_() {
426 // remote initiated disconnect_client
427 // don't close yet, we still need to send the disconnect response
428 // close will happen on next loop
429 this->log_client_(ESPHOME_LOG_LEVEL_DEBUG, LOG_STR("disconnected"));
430 this->flags_.next_close = true;
432 return this->send_message(resp);
433}
434void APIConnection::on_disconnect_response() {
435 // Don't close socket here, let APIServer::loop() do it
436 // so getpeername() still works for the disconnect trigger
437 this->flags_.remove = true;
438}
439
440uint16_t APIConnection::fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg,
441 CalculateSizeFn size_fn, MessageEncodeFn encode_fn,
442 APIConnection *conn, uint32_t remaining_size) {
443 msg.key = entity->get_object_id_hash();
444#ifdef USE_DEVICES
445 msg.device_id = entity->get_device_id();
446#endif
447 return encode_to_buffer(size_fn(&msg), encode_fn, &msg, conn, remaining_size);
448}
449
450uint16_t APIConnection::fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg,
451 CalculateSizeFn size_fn, MessageEncodeFn encode_fn,
452 APIConnection *conn, uint32_t remaining_size) {
453 // Set common fields that are shared by all entity types
454 msg.key = entity->get_object_id_hash();
455
456 if (entity->has_own_name()) {
457 msg.name = entity->get_name();
458 }
459
460 // Set common EntityBase properties
461#ifdef USE_ENTITY_ICON
462 char icon_buf[MAX_ICON_LENGTH];
463 msg.icon = StringRef(entity->get_icon_to(icon_buf));
464#endif
466 msg.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
467#ifdef USE_DEVICES
468 msg.device_id = entity->get_device_id();
469#endif
470 return encode_to_buffer_slow(size_fn(&msg), encode_fn, &msg, conn, remaining_size);
471}
472
473uint16_t APIConnection::fill_and_encode_entity_info_with_device_class(EntityBase *entity, InfoResponseProtoMessage &msg,
474 StringRef &device_class_field,
475 CalculateSizeFn size_fn,
476 MessageEncodeFn encode_fn, APIConnection *conn,
477 uint32_t remaining_size) {
478 char dc_buf[MAX_DEVICE_CLASS_LENGTH];
479 device_class_field = StringRef(entity->get_device_class_to(dc_buf));
480 return fill_and_encode_entity_info(entity, msg, size_fn, encode_fn, conn, remaining_size);
481}
482
483#ifdef USE_BINARY_SENSOR
484bool APIConnection::send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor) {
485 return this->send_message_smart_(binary_sensor, BinarySensorStateResponse::MESSAGE_TYPE,
486 BinarySensorStateResponse::ESTIMATED_SIZE);
487}
488
489uint16_t APIConnection::try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
490 auto *binary_sensor = static_cast<binary_sensor::BinarySensor *>(entity);
492 resp.state = binary_sensor->state;
493 resp.missing_state = !binary_sensor->has_state();
494 return fill_and_encode_entity_state(binary_sensor, resp, conn, remaining_size);
495}
496
497uint16_t APIConnection::try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
498 auto *binary_sensor = static_cast<binary_sensor::BinarySensor *>(entity);
500 msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
501 return fill_and_encode_entity_info_with_device_class(binary_sensor, msg, msg.device_class, conn, remaining_size);
502}
503#endif
504
505#ifdef USE_COVER
506bool APIConnection::send_cover_state(cover::Cover *cover) {
507 return this->send_message_smart_(cover, CoverStateResponse::MESSAGE_TYPE, CoverStateResponse::ESTIMATED_SIZE);
508}
509uint16_t APIConnection::try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
510 auto *cover = static_cast<cover::Cover *>(entity);
512 auto traits = cover->get_traits();
513 msg.position = cover->position;
514 if (traits.get_supports_tilt())
515 msg.tilt = cover->tilt;
516 msg.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
517 return fill_and_encode_entity_state(cover, msg, conn, remaining_size);
518}
519uint16_t APIConnection::try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
520 auto *cover = static_cast<cover::Cover *>(entity);
522 auto traits = cover->get_traits();
523 msg.assumed_state = traits.get_is_assumed_state();
524 msg.supports_position = traits.get_supports_position();
525 msg.supports_tilt = traits.get_supports_tilt();
526 msg.supports_stop = traits.get_supports_stop();
527 return fill_and_encode_entity_info_with_device_class(cover, msg, msg.device_class, conn, remaining_size);
528}
529void APIConnection::on_cover_command_request(const CoverCommandRequest &msg) {
530 ENTITY_COMMAND_MAKE_CALL(cover::Cover, cover, cover)
531 if (msg.has_position)
532 call.set_position(msg.position);
533 if (msg.has_tilt)
534 call.set_tilt(msg.tilt);
535 if (msg.stop)
536 call.set_command_stop();
537 call.perform();
538}
539#endif
540
541#ifdef USE_FAN
542bool APIConnection::send_fan_state(fan::Fan *fan) {
543 return this->send_message_smart_(fan, FanStateResponse::MESSAGE_TYPE, FanStateResponse::ESTIMATED_SIZE);
544}
545uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
546 auto *fan = static_cast<fan::Fan *>(entity);
548 auto traits = fan->get_traits();
549 msg.state = fan->state;
550 if (traits.supports_oscillation())
551 msg.oscillating = fan->oscillating;
552 if (traits.supports_speed()) {
553 msg.speed_level = fan->speed;
554 }
555 if (traits.supports_direction())
556 msg.direction = static_cast<enums::FanDirection>(fan->direction);
557 if (traits.supports_preset_modes() && fan->has_preset_mode())
558 msg.preset_mode = fan->get_preset_mode();
559 return fill_and_encode_entity_state(fan, msg, conn, remaining_size);
560}
561uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
562 auto *fan = static_cast<fan::Fan *>(entity);
564 auto traits = fan->get_traits();
565 msg.supports_oscillation = traits.supports_oscillation();
566 msg.supports_speed = traits.supports_speed();
567 msg.supports_direction = traits.supports_direction();
568 msg.supported_speed_count = traits.supported_speed_count();
569 msg.supported_preset_modes = &traits.supported_preset_modes();
570 return fill_and_encode_entity_info(fan, msg, conn, remaining_size);
571}
572void APIConnection::on_fan_command_request(const FanCommandRequest &msg) {
573 ENTITY_COMMAND_MAKE_CALL(fan::Fan, fan, fan)
574 if (msg.has_state)
575 call.set_state(msg.state);
576 if (msg.has_oscillating)
577 call.set_oscillating(msg.oscillating);
578 if (msg.has_speed_level) {
579 // Prefer level
580 call.set_speed(msg.speed_level);
581 }
582 if (msg.has_direction)
583 call.set_direction(static_cast<fan::FanDirection>(msg.direction));
584 if (msg.has_preset_mode)
585 call.set_preset_mode(msg.preset_mode.c_str(), msg.preset_mode.size());
586 call.perform();
587}
588#endif
589
590#ifdef USE_LIGHT
591bool APIConnection::send_light_state(light::LightState *light) {
592 return this->send_message_smart_(light, LightStateResponse::MESSAGE_TYPE, LightStateResponse::ESTIMATED_SIZE);
593}
594uint16_t APIConnection::try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
595 auto *light = static_cast<light::LightState *>(entity);
597 auto values = light->remote_values;
598 auto color_mode = values.get_color_mode();
599 resp.state = values.is_on();
600 resp.color_mode = static_cast<enums::ColorMode>(color_mode);
601 resp.brightness = values.get_brightness();
602 resp.color_brightness = values.get_color_brightness();
603 resp.red = values.get_red();
604 resp.green = values.get_green();
605 resp.blue = values.get_blue();
606 resp.white = values.get_white();
607 resp.color_temperature = values.get_color_temperature();
608 resp.cold_white = values.get_cold_white();
609 resp.warm_white = values.get_warm_white();
610 if (light->supports_effects()) {
611 resp.effect = light->get_effect_name();
612 }
613 return fill_and_encode_entity_state(light, resp, conn, remaining_size);
614}
615uint16_t APIConnection::try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
616 auto *light = static_cast<light::LightState *>(entity);
618 auto traits = light->get_traits();
619 auto supported_modes = traits.get_supported_color_modes();
620 // Pass pointer to ColorModeMask so the iterator can encode actual ColorMode enum values
621 msg.supported_color_modes = &supported_modes;
622 if (traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
623 traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE)) {
624 msg.min_mireds = traits.get_min_mireds();
625 msg.max_mireds = traits.get_max_mireds();
626 }
627 FixedVector<const char *> effects_list;
628 if (light->supports_effects()) {
629 auto &light_effects = light->get_effects();
630 effects_list.init(light_effects.size() + 1);
631 effects_list.push_back("None");
632 for (auto *effect : light_effects) {
633 // c_str() is safe as effect names are null-terminated strings from codegen
634 effects_list.push_back(effect->get_name().c_str());
635 }
636 }
637 msg.effects = &effects_list;
638 return fill_and_encode_entity_info(light, msg, conn, remaining_size);
639}
640void APIConnection::on_light_command_request(const LightCommandRequest &msg) {
641 ENTITY_COMMAND_MAKE_CALL(light::LightState, light, light)
642 if (msg.has_state)
643 call.set_state(msg.state);
644 if (msg.has_brightness)
645 call.set_brightness(msg.brightness);
646 if (msg.has_color_mode)
647 call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
648 if (msg.has_color_brightness)
649 call.set_color_brightness(msg.color_brightness);
650 if (msg.has_rgb) {
651 call.set_red(msg.red);
652 call.set_green(msg.green);
653 call.set_blue(msg.blue);
654 }
655 if (msg.has_white)
656 call.set_white(msg.white);
657 if (msg.has_color_temperature)
658 call.set_color_temperature(msg.color_temperature);
659 if (msg.has_cold_white)
660 call.set_cold_white(msg.cold_white);
661 if (msg.has_warm_white)
662 call.set_warm_white(msg.warm_white);
663 if (msg.has_transition_length)
664 call.set_transition_length(msg.transition_length);
665 if (msg.has_flash_length)
666 call.set_flash_length(msg.flash_length);
667 if (msg.has_effect)
668 call.set_effect(msg.effect.c_str(), msg.effect.size());
669 call.perform();
670}
671#endif
672
673#ifdef USE_SENSOR
674bool APIConnection::send_sensor_state(sensor::Sensor *sensor) {
675 return this->send_message_smart_(sensor, SensorStateResponse::MESSAGE_TYPE, SensorStateResponse::ESTIMATED_SIZE);
676}
677
678uint16_t APIConnection::try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
679 auto *sensor = static_cast<sensor::Sensor *>(entity);
681 resp.state = sensor->state;
682 resp.missing_state = !sensor->has_state();
683 return fill_and_encode_entity_state(sensor, resp, conn, remaining_size);
684}
685
686uint16_t APIConnection::try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
687 auto *sensor = static_cast<sensor::Sensor *>(entity);
689 msg.unit_of_measurement = sensor->get_unit_of_measurement_ref();
690 msg.accuracy_decimals = sensor->get_accuracy_decimals();
691 msg.force_update = sensor->get_force_update();
692 msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
693 return fill_and_encode_entity_info_with_device_class(sensor, msg, msg.device_class, conn, remaining_size);
694}
695#endif
696
697#ifdef USE_SWITCH
698bool APIConnection::send_switch_state(switch_::Switch *a_switch) {
699 return this->send_message_smart_(a_switch, SwitchStateResponse::MESSAGE_TYPE, SwitchStateResponse::ESTIMATED_SIZE);
700}
701
702uint16_t APIConnection::try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
703 auto *a_switch = static_cast<switch_::Switch *>(entity);
705 resp.state = a_switch->state;
706 return fill_and_encode_entity_state(a_switch, resp, conn, remaining_size);
707}
708
709uint16_t APIConnection::try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
710 auto *a_switch = static_cast<switch_::Switch *>(entity);
712 msg.assumed_state = a_switch->assumed_state();
713 return fill_and_encode_entity_info_with_device_class(a_switch, msg, msg.device_class, conn, remaining_size);
714}
715void APIConnection::on_switch_command_request(const SwitchCommandRequest &msg) {
716 ENTITY_COMMAND_GET(switch_::Switch, a_switch, switch)
717
718 if (msg.state) {
719 a_switch->turn_on();
720 } else {
721 a_switch->turn_off();
722 }
723}
724#endif
725
726#ifdef USE_TEXT_SENSOR
727bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor) {
728 return this->send_message_smart_(text_sensor, TextSensorStateResponse::MESSAGE_TYPE,
729 TextSensorStateResponse::ESTIMATED_SIZE);
730}
731
732uint16_t APIConnection::try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
733 auto *text_sensor = static_cast<text_sensor::TextSensor *>(entity);
735 resp.state = StringRef(text_sensor->state);
736 resp.missing_state = !text_sensor->has_state();
737 return fill_and_encode_entity_state(text_sensor, resp, conn, remaining_size);
738}
739uint16_t APIConnection::try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
740 auto *text_sensor = static_cast<text_sensor::TextSensor *>(entity);
742 return fill_and_encode_entity_info_with_device_class(text_sensor, msg, msg.device_class, conn, remaining_size);
743}
744#endif
745
746#ifdef USE_CLIMATE
747bool APIConnection::send_climate_state(climate::Climate *climate) {
748 return this->send_message_smart_(climate, ClimateStateResponse::MESSAGE_TYPE, ClimateStateResponse::ESTIMATED_SIZE);
749}
750uint16_t APIConnection::try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
751 auto *climate = static_cast<climate::Climate *>(entity);
753 auto traits = climate->get_traits();
754 resp.mode = static_cast<enums::ClimateMode>(climate->mode);
755 resp.action = static_cast<enums::ClimateAction>(climate->action);
756 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE))
757 resp.current_temperature = climate->current_temperature;
758 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE |
760 resp.target_temperature_low = climate->target_temperature_low;
761 resp.target_temperature_high = climate->target_temperature_high;
762 } else {
763 resp.target_temperature = climate->target_temperature;
764 }
765 if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
766 resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
767 if (!traits.get_supported_custom_fan_modes().empty() && climate->has_custom_fan_mode()) {
768 resp.custom_fan_mode = climate->get_custom_fan_mode();
769 }
770 if (traits.get_supports_presets() && climate->preset.has_value()) {
771 resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
772 }
773 if (!traits.get_supported_custom_presets().empty() && climate->has_custom_preset()) {
774 resp.custom_preset = climate->get_custom_preset();
775 }
776 if (traits.get_supports_swing_modes())
777 resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
778 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY))
779 resp.current_humidity = climate->current_humidity;
780 if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TARGET_HUMIDITY))
781 resp.target_humidity = climate->target_humidity;
782 return fill_and_encode_entity_state(climate, resp, conn, remaining_size);
783}
784uint16_t APIConnection::try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
785 auto *climate = static_cast<climate::Climate *>(entity);
787 auto traits = climate->get_traits();
788 // Flags set for backward compatibility, deprecated in 2025.11.0
791 msg.supports_two_point_target_temperature = traits.has_feature_flags(
794 msg.supports_action = traits.has_feature_flags(climate::CLIMATE_SUPPORTS_ACTION);
795 // Current feature flags and other supported parameters
796 msg.feature_flags = traits.get_feature_flags();
797 msg.temperature_unit = static_cast<enums::TemperatureUnit>(traits.get_temperature_unit());
798 msg.supported_modes = &traits.get_supported_modes();
799 msg.visual_min_temperature = traits.get_visual_min_temperature();
800 msg.visual_max_temperature = traits.get_visual_max_temperature();
801 msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
802 msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
803 msg.visual_min_humidity = traits.get_visual_min_humidity();
804 msg.visual_max_humidity = traits.get_visual_max_humidity();
805 msg.supported_fan_modes = &traits.get_supported_fan_modes();
806 msg.supported_custom_fan_modes = &traits.get_supported_custom_fan_modes();
807 msg.supported_presets = &traits.get_supported_presets();
808 msg.supported_custom_presets = &traits.get_supported_custom_presets();
809 msg.supported_swing_modes = &traits.get_supported_swing_modes();
810 return fill_and_encode_entity_info(climate, msg, conn, remaining_size);
811}
812void APIConnection::on_climate_command_request(const ClimateCommandRequest &msg) {
813 ENTITY_COMMAND_MAKE_CALL(climate::Climate, climate, climate)
814 if (msg.has_mode)
815 call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
817 call.set_target_temperature(msg.target_temperature);
819 call.set_target_temperature_low(msg.target_temperature_low);
821 call.set_target_temperature_high(msg.target_temperature_high);
822 if (msg.has_target_humidity)
823 call.set_target_humidity(msg.target_humidity);
824 if (msg.has_fan_mode)
825 call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
826 if (msg.has_custom_fan_mode)
827 call.set_fan_mode(msg.custom_fan_mode.c_str(), msg.custom_fan_mode.size());
828 if (msg.has_preset)
829 call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
830 if (msg.has_custom_preset)
831 call.set_preset(msg.custom_preset.c_str(), msg.custom_preset.size());
832 if (msg.has_swing_mode)
833 call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
834 call.perform();
835}
836#endif
837
838#ifdef USE_NUMBER
839bool APIConnection::send_number_state(number::Number *number) {
840 return this->send_message_smart_(number, NumberStateResponse::MESSAGE_TYPE, NumberStateResponse::ESTIMATED_SIZE);
841}
842
843uint16_t APIConnection::try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
844 auto *number = static_cast<number::Number *>(entity);
846 resp.state = number->state;
847 resp.missing_state = !number->has_state();
848 return fill_and_encode_entity_state(number, resp, conn, remaining_size);
849}
850
851uint16_t APIConnection::try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
852 auto *number = static_cast<number::Number *>(entity);
854 msg.unit_of_measurement = number->get_unit_of_measurement_ref();
855 msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
856 msg.min_value = number->traits.get_min_value();
857 msg.max_value = number->traits.get_max_value();
858 msg.step = number->traits.get_step();
859 return fill_and_encode_entity_info_with_device_class(number, msg, msg.device_class, conn, remaining_size);
860}
861void APIConnection::on_number_command_request(const NumberCommandRequest &msg) {
862 ENTITY_COMMAND_MAKE_CALL(number::Number, number, number)
863 call.set_value(msg.state);
864 call.perform();
865}
866#endif
867
868#ifdef USE_DATETIME_DATE
869bool APIConnection::send_date_state(datetime::DateEntity *date) {
870 return this->send_message_smart_(date, DateStateResponse::MESSAGE_TYPE, DateStateResponse::ESTIMATED_SIZE);
871}
872uint16_t APIConnection::try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
873 auto *date = static_cast<datetime::DateEntity *>(entity);
875 resp.missing_state = !date->has_state();
876 resp.year = date->year;
877 resp.month = date->month;
878 resp.day = date->day;
879 return fill_and_encode_entity_state(date, resp, conn, remaining_size);
880}
881uint16_t APIConnection::try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
882 auto *date = static_cast<datetime::DateEntity *>(entity);
884 return fill_and_encode_entity_info(date, msg, conn, remaining_size);
885}
886void APIConnection::on_date_command_request(const DateCommandRequest &msg) {
887 ENTITY_COMMAND_MAKE_CALL(datetime::DateEntity, date, date)
888 call.set_date(msg.year, msg.month, msg.day);
889 call.perform();
890}
891#endif
892
893#ifdef USE_DATETIME_TIME
894bool APIConnection::send_time_state(datetime::TimeEntity *time) {
895 return this->send_message_smart_(time, TimeStateResponse::MESSAGE_TYPE, TimeStateResponse::ESTIMATED_SIZE);
896}
897uint16_t APIConnection::try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
898 auto *time = static_cast<datetime::TimeEntity *>(entity);
900 resp.missing_state = !time->has_state();
901 resp.hour = time->hour;
902 resp.minute = time->minute;
903 resp.second = time->second;
904 return fill_and_encode_entity_state(time, resp, conn, remaining_size);
905}
906uint16_t APIConnection::try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
907 auto *time = static_cast<datetime::TimeEntity *>(entity);
909 return fill_and_encode_entity_info(time, msg, conn, remaining_size);
910}
911void APIConnection::on_time_command_request(const TimeCommandRequest &msg) {
912 ENTITY_COMMAND_MAKE_CALL(datetime::TimeEntity, time, time)
913 call.set_time(msg.hour, msg.minute, msg.second);
914 call.perform();
915}
916#endif
917
918#ifdef USE_DATETIME_DATETIME
919bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) {
920 return this->send_message_smart_(datetime, DateTimeStateResponse::MESSAGE_TYPE,
921 DateTimeStateResponse::ESTIMATED_SIZE);
922}
923uint16_t APIConnection::try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
924 auto *datetime = static_cast<datetime::DateTimeEntity *>(entity);
926 resp.missing_state = !datetime->has_state();
927 if (datetime->has_state()) {
928 ESPTime state = datetime->state_as_esptime();
929 resp.epoch_seconds = state.timestamp;
930 }
931 return fill_and_encode_entity_state(datetime, resp, conn, remaining_size);
932}
933uint16_t APIConnection::try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
934 auto *datetime = static_cast<datetime::DateTimeEntity *>(entity);
936 return fill_and_encode_entity_info(datetime, msg, conn, remaining_size);
937}
938void APIConnection::on_date_time_command_request(const DateTimeCommandRequest &msg) {
939 ENTITY_COMMAND_MAKE_CALL(datetime::DateTimeEntity, datetime, datetime)
940 call.set_datetime(msg.epoch_seconds);
941 call.perform();
942}
943#endif
944
945#ifdef USE_TEXT
946bool APIConnection::send_text_state(text::Text *text) {
947 return this->send_message_smart_(text, TextStateResponse::MESSAGE_TYPE, TextStateResponse::ESTIMATED_SIZE);
948}
949
950uint16_t APIConnection::try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
951 auto *text = static_cast<text::Text *>(entity);
953 resp.state = StringRef(text->state);
954 resp.missing_state = !text->has_state();
955 return fill_and_encode_entity_state(text, resp, conn, remaining_size);
956}
957
958uint16_t APIConnection::try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
959 auto *text = static_cast<text::Text *>(entity);
961 msg.mode = static_cast<enums::TextMode>(text->traits.get_mode());
962 msg.min_length = text->traits.get_min_length();
963 msg.max_length = text->traits.get_max_length();
964 msg.pattern = text->traits.get_pattern_ref();
965 return fill_and_encode_entity_info(text, msg, conn, remaining_size);
966}
967void APIConnection::on_text_command_request(const TextCommandRequest &msg) {
968 ENTITY_COMMAND_MAKE_CALL(text::Text, text, text)
969 call.set_value(msg.state.c_str(), msg.state.size());
970 call.perform();
971}
972#endif
973
974#ifdef USE_SELECT
975bool APIConnection::send_select_state(select::Select *select) {
976 return this->send_message_smart_(select, SelectStateResponse::MESSAGE_TYPE, SelectStateResponse::ESTIMATED_SIZE);
977}
978
979uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
980 auto *select = static_cast<select::Select *>(entity);
982 resp.state = select->current_option();
983 resp.missing_state = !select->has_state();
984 return fill_and_encode_entity_state(select, resp, conn, remaining_size);
985}
986
987uint16_t APIConnection::try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
988 auto *select = static_cast<select::Select *>(entity);
990 msg.options = &select->traits.get_options();
991 return fill_and_encode_entity_info(select, msg, conn, remaining_size);
992}
993void APIConnection::on_select_command_request(const SelectCommandRequest &msg) {
994 ENTITY_COMMAND_MAKE_CALL(select::Select, select, select)
995 call.set_option(msg.state.c_str(), msg.state.size());
996 call.perform();
997}
998#endif
999
1000#ifdef USE_BUTTON
1001uint16_t APIConnection::try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1002 auto *button = static_cast<button::Button *>(entity);
1004 return fill_and_encode_entity_info_with_device_class(button, msg, msg.device_class, conn, remaining_size);
1005}
1007 ENTITY_COMMAND_GET(button::Button, button, button)
1008 button->press();
1009}
1010#endif
1011
1012#ifdef USE_LOCK
1013bool APIConnection::send_lock_state(lock::Lock *a_lock) {
1014 return this->send_message_smart_(a_lock, LockStateResponse::MESSAGE_TYPE, LockStateResponse::ESTIMATED_SIZE);
1015}
1016
1017uint16_t APIConnection::try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1018 auto *a_lock = static_cast<lock::Lock *>(entity);
1019 LockStateResponse resp;
1020 resp.state = static_cast<enums::LockState>(a_lock->state);
1021 return fill_and_encode_entity_state(a_lock, resp, conn, remaining_size);
1022}
1023
1024uint16_t APIConnection::try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1025 auto *a_lock = static_cast<lock::Lock *>(entity);
1027 msg.assumed_state = a_lock->traits.get_assumed_state();
1028 msg.supports_open = a_lock->traits.get_supports_open();
1029 msg.requires_code = a_lock->traits.get_requires_code();
1030 return fill_and_encode_entity_info(a_lock, msg, conn, remaining_size);
1031}
1032void APIConnection::on_lock_command_request(const LockCommandRequest &msg) {
1033 ENTITY_COMMAND_GET(lock::Lock, a_lock, lock)
1034
1035 switch (msg.command) {
1036 case enums::LOCK_UNLOCK:
1037 a_lock->unlock();
1038 break;
1039 case enums::LOCK_LOCK:
1040 a_lock->lock();
1041 break;
1042 case enums::LOCK_OPEN:
1043 a_lock->open();
1044 break;
1045 }
1046}
1047#endif
1048
1049#ifdef USE_VALVE
1050bool APIConnection::send_valve_state(valve::Valve *valve) {
1051 return this->send_message_smart_(valve, ValveStateResponse::MESSAGE_TYPE, ValveStateResponse::ESTIMATED_SIZE);
1052}
1053uint16_t APIConnection::try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1054 auto *valve = static_cast<valve::Valve *>(entity);
1055 ValveStateResponse resp;
1056 resp.position = valve->position;
1057 resp.current_operation = static_cast<enums::ValveOperation>(valve->current_operation);
1058 return fill_and_encode_entity_state(valve, resp, conn, remaining_size);
1059}
1060uint16_t APIConnection::try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1061 auto *valve = static_cast<valve::Valve *>(entity);
1063 auto traits = valve->get_traits();
1064 msg.assumed_state = traits.get_is_assumed_state();
1065 msg.supports_position = traits.get_supports_position();
1066 msg.supports_stop = traits.get_supports_stop();
1067 return fill_and_encode_entity_info_with_device_class(valve, msg, msg.device_class, conn, remaining_size);
1068}
1069void APIConnection::on_valve_command_request(const ValveCommandRequest &msg) {
1070 ENTITY_COMMAND_MAKE_CALL(valve::Valve, valve, valve)
1071 if (msg.has_position)
1072 call.set_position(msg.position);
1073 if (msg.stop)
1074 call.set_command_stop();
1075 call.perform();
1076}
1077#endif
1078
1079#ifdef USE_MEDIA_PLAYER
1080bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) {
1081 return this->send_message_smart_(media_player, MediaPlayerStateResponse::MESSAGE_TYPE,
1082 MediaPlayerStateResponse::ESTIMATED_SIZE);
1083}
1084uint16_t APIConnection::try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1085 auto *media_player = static_cast<media_player::MediaPlayer *>(entity);
1089 : media_player->state;
1090 resp.state = static_cast<enums::MediaPlayerState>(report_state);
1091 resp.volume = media_player->volume;
1092 resp.muted = media_player->is_muted();
1093 return fill_and_encode_entity_state(media_player, resp, conn, remaining_size);
1094}
1095uint16_t APIConnection::try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1096 auto *media_player = static_cast<media_player::MediaPlayer *>(entity);
1098 auto traits = media_player->get_traits();
1099 msg.supports_pause = traits.get_supports_pause();
1100 msg.feature_flags = traits.get_feature_flags();
1101 for (auto &supported_format : traits.get_supported_formats()) {
1102 msg.supported_formats.emplace_back();
1103 auto &media_format = msg.supported_formats.back();
1104 media_format.format = StringRef(supported_format.format);
1105 media_format.sample_rate = supported_format.sample_rate;
1106 media_format.num_channels = supported_format.num_channels;
1107 media_format.purpose = static_cast<enums::MediaPlayerFormatPurpose>(supported_format.purpose);
1108 media_format.sample_bytes = supported_format.sample_bytes;
1109 }
1110 return fill_and_encode_entity_info(media_player, msg, conn, remaining_size);
1111}
1112void APIConnection::on_media_player_command_request(const MediaPlayerCommandRequest &msg) {
1113 ENTITY_COMMAND_MAKE_CALL(media_player::MediaPlayer, media_player, media_player)
1114 if (msg.has_command) {
1115 call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
1116 }
1117 if (msg.has_volume) {
1118 call.set_volume(msg.volume);
1119 }
1120 if (msg.has_media_url) {
1121 call.set_media_url(msg.media_url);
1122 }
1123 if (msg.has_announcement) {
1124 call.set_announcement(msg.announcement);
1125 }
1126 call.perform();
1127}
1128#endif
1129
1130#ifdef USE_CAMERA
1131void APIConnection::try_send_camera_image_() {
1132 if (!this->image_reader_)
1133 return;
1134
1135 // Send as many chunks as possible without blocking
1136 while (this->image_reader_->available()) {
1137 if (!this->helper_->can_write_without_blocking())
1138 return;
1139
1140 uint32_t to_send = std::min((size_t) MAX_BATCH_PACKET_SIZE, this->image_reader_->available());
1141 bool done = this->image_reader_->available() == to_send;
1142
1145 msg.set_data(this->image_reader_->peek_data_buffer(), to_send);
1146 msg.done = done;
1147#ifdef USE_DEVICES
1149#endif
1150
1151 if (!this->send_message(msg)) {
1152 return; // Send failed, try again later
1153 }
1154 this->image_reader_->consume_data(to_send);
1155 if (done) {
1156 this->image_reader_->return_image();
1157 return;
1158 }
1159 }
1160}
1161void APIConnection::set_camera_state(std::shared_ptr<camera::CameraImage> image) {
1162 if (!this->flags_.state_subscription)
1163 return;
1164 if (!this->image_reader_)
1165 return;
1166 if (this->image_reader_->available())
1167 return;
1168 if (image->was_requested_by(esphome::camera::API_REQUESTER) || image->was_requested_by(esphome::camera::IDLE)) {
1169 this->image_reader_->set_image(std::move(image));
1170 // Try to send immediately to reduce latency
1171 this->try_send_camera_image_();
1172 }
1173}
1174uint16_t APIConnection::try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1175 auto *camera = static_cast<camera::Camera *>(entity);
1177 return fill_and_encode_entity_info(camera, msg, conn, remaining_size);
1178}
1179void APIConnection::on_camera_image_request(const CameraImageRequest &msg) {
1180 if (camera::Camera::instance() == nullptr)
1181 return;
1182
1183 if (msg.single)
1185 if (msg.stream) {
1187
1188 App.scheduler.set_timeout(this->parent_, "api_camera_stop_stream", CAMERA_STOP_STREAM,
1190 }
1191}
1192#endif
1193
1194#ifdef USE_HOMEASSISTANT_TIME
1195void APIConnection::on_get_time_response(const GetTimeResponse &value) {
1198#if defined(USE_HOMEASSISTANT_TIMEZONE) && defined(USE_TIME_TIMEZONE)
1199 if (!value.timezone.empty()) {
1200 // Check if the sender provided pre-parsed timezone data.
1201 // If std_offset is non-zero or DST rules are present, the parsed data was populated.
1202 // For UTC (all zeros), string parsing produces the same result, so the fallback is equivalent.
1203 const auto &pt = value.parsed_timezone;
1204 if (pt.std_offset_seconds != 0 || pt.dst_start.type != enums::DST_RULE_TYPE_NONE) {
1206 tz.std_offset_seconds = pt.std_offset_seconds;
1207 tz.dst_offset_seconds = pt.dst_offset_seconds;
1208 tz.dst_start.time_seconds = pt.dst_start.time_seconds;
1209 tz.dst_start.day = static_cast<uint16_t>(pt.dst_start.day);
1210 tz.dst_start.type = static_cast<time::DSTRuleType>(pt.dst_start.type);
1211 tz.dst_start.month = static_cast<uint8_t>(pt.dst_start.month);
1212 tz.dst_start.week = static_cast<uint8_t>(pt.dst_start.week);
1213 tz.dst_start.day_of_week = static_cast<uint8_t>(pt.dst_start.day_of_week);
1214 tz.dst_end.time_seconds = pt.dst_end.time_seconds;
1215 tz.dst_end.day = static_cast<uint16_t>(pt.dst_end.day);
1216 tz.dst_end.type = static_cast<time::DSTRuleType>(pt.dst_end.type);
1217 tz.dst_end.month = static_cast<uint8_t>(pt.dst_end.month);
1218 tz.dst_end.week = static_cast<uint8_t>(pt.dst_end.week);
1219 tz.dst_end.day_of_week = static_cast<uint8_t>(pt.dst_end.day_of_week);
1221 } else {
1223 }
1224 }
1225#endif
1226 }
1227}
1228#endif
1229
1230#ifdef USE_BLUETOOTH_PROXY
1231void APIConnection::on_subscribe_bluetooth_le_advertisements_request(
1234}
1235void APIConnection::on_unsubscribe_bluetooth_le_advertisements_request() {
1237}
1238void APIConnection::on_bluetooth_device_request(const BluetoothDeviceRequest &msg) {
1240}
1241void APIConnection::on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg) {
1243}
1244void APIConnection::on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg) {
1246}
1247void APIConnection::on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg) {
1249}
1250void APIConnection::on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg) {
1252}
1253void APIConnection::on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg) {
1255}
1256
1257void APIConnection::on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg) {
1259}
1260
1261bool APIConnection::send_subscribe_bluetooth_connections_free_response_() {
1263 return true;
1264}
1265void APIConnection::on_subscribe_bluetooth_connections_free_request() {
1266 if (!this->send_subscribe_bluetooth_connections_free_response_()) {
1267 this->on_fatal_error();
1268 }
1269}
1270
1271void APIConnection::on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg) {
1273 msg.mode == enums::BluetoothScannerMode::BLUETOOTH_SCANNER_MODE_ACTIVE);
1274}
1275void APIConnection::on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg) {
1277}
1278#endif
1279
1280#ifdef USE_VOICE_ASSISTANT
1281bool APIConnection::check_voice_assistant_api_connection_() const {
1282 return voice_assistant::global_voice_assistant != nullptr &&
1284}
1285
1286void APIConnection::on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg) {
1289 }
1290}
1291void APIConnection::on_voice_assistant_response(const VoiceAssistantResponse &msg) {
1292 if (!this->check_voice_assistant_api_connection_()) {
1293 return;
1294 }
1295
1296 if (msg.error) {
1298 return;
1299 }
1300 if (msg.port == 0) {
1301 // Use API Audio
1303 } else {
1304 struct sockaddr_storage storage;
1305 socklen_t len = sizeof(storage);
1306 this->helper_->getpeername((struct sockaddr *) &storage, &len);
1308 }
1309};
1310void APIConnection::on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) {
1311 if (this->check_voice_assistant_api_connection_()) {
1313 }
1314}
1315void APIConnection::on_voice_assistant_audio(const VoiceAssistantAudio &msg) {
1316 if (this->check_voice_assistant_api_connection_()) {
1318 }
1319};
1320void APIConnection::on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) {
1321 if (this->check_voice_assistant_api_connection_()) {
1323 }
1324};
1325
1326void APIConnection::on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) {
1327 if (this->check_voice_assistant_api_connection_()) {
1329 }
1330}
1331
1332bool APIConnection::send_voice_assistant_get_configuration_response_(const VoiceAssistantConfigurationRequest &msg) {
1334 if (!this->check_voice_assistant_api_connection_()) {
1335 // send_message encodes synchronously, so this stack local outlives the encode
1336 const std::vector<std::string> empty_wake_words;
1337 resp.active_wake_words = &empty_wake_words;
1338 return this->send_message(resp);
1339 }
1340
1342 for (auto &wake_word : config.available_wake_words) {
1343 resp.available_wake_words.emplace_back();
1344 auto &resp_wake_word = resp.available_wake_words.back();
1345 resp_wake_word.id = StringRef(wake_word.id);
1346 resp_wake_word.wake_word = StringRef(wake_word.wake_word);
1347 for (const auto &lang : wake_word.trained_languages) {
1348 resp_wake_word.trained_languages.push_back(lang);
1349 }
1350 }
1351
1352 // Filter external wake words
1353 for (auto &wake_word : msg.external_wake_words) {
1354 if (wake_word.model_type != "micro") {
1355 // microWakeWord only
1356 continue;
1357 }
1358
1359 resp.available_wake_words.emplace_back();
1360 auto &resp_wake_word = resp.available_wake_words.back();
1361 resp_wake_word.id = StringRef(wake_word.id);
1362 resp_wake_word.wake_word = StringRef(wake_word.wake_word);
1363 for (const auto &lang : wake_word.trained_languages) {
1364 resp_wake_word.trained_languages.push_back(lang);
1365 }
1366 }
1367
1368 resp.active_wake_words = &config.active_wake_words;
1369 resp.max_active_wake_words = config.max_active_wake_words;
1370 return this->send_message(resp);
1371}
1372void APIConnection::on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) {
1373 if (!this->send_voice_assistant_get_configuration_response_(msg)) {
1374 this->on_fatal_error();
1375 }
1376}
1377
1378void APIConnection::on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) {
1379 if (this->check_voice_assistant_api_connection_()) {
1381 }
1382}
1383#endif
1384
1385#ifdef USE_ZWAVE_PROXY
1386void APIConnection::on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) {
1388}
1389
1390void APIConnection::on_z_wave_proxy_request(const ZWaveProxyRequest &msg) {
1392}
1393#endif
1394
1395#ifdef USE_ALARM_CONTROL_PANEL
1396bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) {
1397 return this->send_message_smart_(a_alarm_control_panel, AlarmControlPanelStateResponse::MESSAGE_TYPE,
1398 AlarmControlPanelStateResponse::ESTIMATED_SIZE);
1399}
1400uint16_t APIConnection::try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn,
1401 uint32_t remaining_size) {
1402 auto *a_alarm_control_panel = static_cast<alarm_control_panel::AlarmControlPanel *>(entity);
1404 resp.state = static_cast<enums::AlarmControlPanelState>(a_alarm_control_panel->get_state());
1405 return fill_and_encode_entity_state(a_alarm_control_panel, resp, conn, remaining_size);
1406}
1407uint16_t APIConnection::try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn,
1408 uint32_t remaining_size) {
1409 auto *a_alarm_control_panel = static_cast<alarm_control_panel::AlarmControlPanel *>(entity);
1411 msg.supported_features = a_alarm_control_panel->get_supported_features();
1412 msg.requires_code = a_alarm_control_panel->get_requires_code();
1413 msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm();
1414 return fill_and_encode_entity_info(a_alarm_control_panel, msg, conn, remaining_size);
1415}
1416void APIConnection::on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg) {
1417 ENTITY_COMMAND_MAKE_CALL(alarm_control_panel::AlarmControlPanel, a_alarm_control_panel, alarm_control_panel)
1418 switch (msg.command) {
1419 case enums::ALARM_CONTROL_PANEL_DISARM:
1420 call.disarm();
1421 break;
1422 case enums::ALARM_CONTROL_PANEL_ARM_AWAY:
1423 call.arm_away();
1424 break;
1425 case enums::ALARM_CONTROL_PANEL_ARM_HOME:
1426 call.arm_home();
1427 break;
1428 case enums::ALARM_CONTROL_PANEL_ARM_NIGHT:
1429 call.arm_night();
1430 break;
1431 case enums::ALARM_CONTROL_PANEL_ARM_VACATION:
1432 call.arm_vacation();
1433 break;
1434 case enums::ALARM_CONTROL_PANEL_ARM_CUSTOM_BYPASS:
1435 call.arm_custom_bypass();
1436 break;
1437 case enums::ALARM_CONTROL_PANEL_TRIGGER:
1438 call.pending();
1439 break;
1440 }
1441 call.set_code(msg.code.c_str(), msg.code.size());
1442 call.perform();
1443}
1444#endif
1445
1446#ifdef USE_WATER_HEATER
1447bool APIConnection::send_water_heater_state(water_heater::WaterHeater *water_heater) {
1448 return this->send_message_smart_(water_heater, WaterHeaterStateResponse::MESSAGE_TYPE,
1449 WaterHeaterStateResponse::ESTIMATED_SIZE);
1450}
1451uint16_t APIConnection::try_send_water_heater_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1452 auto *wh = static_cast<water_heater::WaterHeater *>(entity);
1454 resp.mode = static_cast<enums::WaterHeaterMode>(wh->get_mode());
1455 resp.current_temperature = wh->get_current_temperature();
1456 resp.target_temperature = wh->get_target_temperature();
1457 resp.target_temperature_low = wh->get_target_temperature_low();
1458 resp.target_temperature_high = wh->get_target_temperature_high();
1459 resp.state = wh->get_state();
1460
1461 return fill_and_encode_entity_state(wh, resp, conn, remaining_size);
1462}
1463uint16_t APIConnection::try_send_water_heater_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1464 auto *wh = static_cast<water_heater::WaterHeater *>(entity);
1466 auto traits = wh->get_traits();
1467 msg.min_temperature = traits.get_min_temperature();
1468 msg.max_temperature = traits.get_max_temperature();
1469 msg.target_temperature_step = traits.get_target_temperature_step();
1470 msg.supported_modes = &traits.get_supported_modes();
1471 msg.supported_features = traits.get_feature_flags();
1472 msg.temperature_unit = static_cast<enums::TemperatureUnit>(traits.get_temperature_unit());
1473 return fill_and_encode_entity_info(wh, msg, conn, remaining_size);
1474}
1475
1476void APIConnection::on_water_heater_command_request(const WaterHeaterCommandRequest &msg) {
1477 ENTITY_COMMAND_MAKE_CALL(water_heater::WaterHeater, water_heater, water_heater)
1478 if (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_MODE)
1479 call.set_mode(static_cast<water_heater::WaterHeaterMode>(msg.mode));
1480 if (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_TARGET_TEMPERATURE)
1481 call.set_target_temperature(msg.target_temperature);
1482 if (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_TARGET_TEMPERATURE_LOW)
1483 call.set_target_temperature_low(msg.target_temperature_low);
1484 if (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_TARGET_TEMPERATURE_HIGH)
1485 call.set_target_temperature_high(msg.target_temperature_high);
1486 if ((msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_AWAY_STATE) ||
1487 (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_STATE)) {
1488 call.set_away((msg.state & water_heater::WATER_HEATER_STATE_AWAY) != 0);
1489 }
1490 if ((msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_ON_STATE) ||
1491 (msg.has_fields & enums::WATER_HEATER_COMMAND_HAS_STATE)) {
1492 call.set_on((msg.state & water_heater::WATER_HEATER_STATE_ON) != 0);
1493 }
1494 call.perform();
1495}
1496#endif
1497
1498#ifdef USE_EVENT
1499// Event is a special case - unlike other entities with simple state fields,
1500// events store their state in a member accessed via obj->get_last_event_type()
1501void APIConnection::send_event(event::Event *event) {
1502 this->send_message_smart_(event, EventResponse::MESSAGE_TYPE, EventResponse::ESTIMATED_SIZE,
1503 event->get_last_event_type_index());
1504}
1505uint16_t APIConnection::try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
1506 uint32_t remaining_size) {
1507 EventResponse resp;
1508 resp.event_type = event_type;
1509 return fill_and_encode_entity_state(event, resp, conn, remaining_size);
1510}
1511
1512uint16_t APIConnection::try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1513 auto *event = static_cast<event::Event *>(entity);
1515 msg.event_types = &event->get_event_types();
1516 return fill_and_encode_entity_info_with_device_class(event, msg, msg.device_class, conn, remaining_size);
1517}
1518#endif
1519
1520#if defined(USE_IR_RF) || defined(USE_RADIO_FREQUENCY)
1521void APIConnection::on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg) {
1522 // Dispatch by key: infrared entities are checked first, then radio frequency entities.
1523 // The key is unique across all entity instances on a device, so at most one lookup will succeed.
1524#ifdef USE_INFRARED
1525 ENTITY_COMMAND_LOOKUP(infrared::Infrared, infrared, infrared);
1526 if (infrared != nullptr) {
1527 auto call = infrared->make_call();
1528 call.set_carrier_frequency(msg.carrier_frequency);
1529 call.set_raw_timings_packed(msg.timings_data_, msg.timings_length_, msg.timings_count_);
1530 call.set_repeat_count(msg.repeat_count);
1531 call.perform();
1532 return;
1533 }
1534#endif
1535#ifdef USE_RADIO_FREQUENCY
1536 ENTITY_COMMAND_LOOKUP(radio_frequency::RadioFrequency, radio_frequency, radio_frequency);
1537 if (radio_frequency != nullptr) {
1538 auto call = radio_frequency->make_call();
1539 call.set_frequency(msg.carrier_frequency);
1540 call.set_modulation(static_cast<radio_frequency::RadioFrequencyModulation>(msg.modulation));
1541 call.set_repeat_count(msg.repeat_count);
1542 call.set_raw_timings_packed(msg.timings_data_, msg.timings_length_, msg.timings_count_);
1543 call.perform();
1544 }
1545#endif
1546}
1547#endif
1548
1549#if defined(USE_IR_RF) || defined(USE_RADIO_FREQUENCY)
1550void APIConnection::send_infrared_rf_receive_event(const InfraredRFReceiveEvent &msg) { this->send_message(msg); }
1551#endif
1552
1553#ifdef USE_SERIAL_PROXY
1554void APIConnection::on_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg) {
1555 auto &proxies = App.get_serial_proxies();
1556 if (msg.instance >= proxies.size()) {
1557 ESP_LOGW(TAG, "Serial proxy instance %" PRIu32 " out of range (max %" PRIu32 ")", msg.instance,
1558 static_cast<uint32_t>(proxies.size()));
1559 return;
1560 }
1561 proxies[msg.instance]->configure(msg.baudrate, msg.flow_control, static_cast<uint8_t>(msg.parity), msg.stop_bits,
1562 msg.data_size);
1563}
1564
1565void APIConnection::on_serial_proxy_write_request(const SerialProxyWriteRequest &msg) {
1566 auto &proxies = App.get_serial_proxies();
1567 if (msg.instance >= proxies.size()) {
1568 ESP_LOGW(TAG, "Serial proxy instance %" PRIu32 " out of range", msg.instance);
1569 return;
1570 }
1571 proxies[msg.instance]->write_from_client(msg.data, msg.data_len);
1572}
1573
1574void APIConnection::on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg) {
1575 auto &proxies = App.get_serial_proxies();
1576 if (msg.instance >= proxies.size()) {
1577 ESP_LOGW(TAG, "Serial proxy instance %" PRIu32 " out of range", msg.instance);
1578 return;
1579 }
1580 proxies[msg.instance]->set_modem_pins(msg.line_states);
1581}
1582
1583void APIConnection::on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg) {
1584 auto &proxies = App.get_serial_proxies();
1585 if (msg.instance >= proxies.size()) {
1586 ESP_LOGW(TAG, "Serial proxy instance %" PRIu32 " out of range", msg.instance);
1587 return;
1588 }
1590 resp.instance = msg.instance;
1591 resp.line_states = proxies[msg.instance]->get_modem_pins();
1592 this->send_message(resp);
1593}
1594
1595void APIConnection::on_serial_proxy_request(const SerialProxyRequest &msg) {
1596 auto &proxies = App.get_serial_proxies();
1597 if (msg.instance >= proxies.size()) {
1598 ESP_LOGW(TAG, "Serial proxy instance %" PRIu32 " out of range", msg.instance);
1599 return;
1600 }
1601 switch (msg.type) {
1602 case enums::SERIAL_PROXY_REQUEST_TYPE_SUBSCRIBE:
1603 case enums::SERIAL_PROXY_REQUEST_TYPE_UNSUBSCRIBE:
1604 proxies[msg.instance]->serial_proxy_request(this, msg.type);
1605 break;
1606 case enums::SERIAL_PROXY_REQUEST_TYPE_FLUSH: {
1608 resp.instance = msg.instance;
1609 resp.type = enums::SERIAL_PROXY_REQUEST_TYPE_FLUSH;
1610 switch (proxies[msg.instance]->flush_port()) {
1612 resp.status = enums::SERIAL_PROXY_STATUS_OK;
1613 break;
1615 resp.status = enums::SERIAL_PROXY_STATUS_ASSUMED_SUCCESS;
1616 break;
1618 resp.status = enums::SERIAL_PROXY_STATUS_TIMEOUT;
1619 break;
1621 resp.status = enums::SERIAL_PROXY_STATUS_ERROR;
1622 break;
1623 }
1624 this->send_message(resp);
1625 break;
1626 }
1627 default:
1628 ESP_LOGW(TAG, "Unknown serial proxy request type: %" PRIu32, static_cast<uint32_t>(msg.type));
1629 break;
1630 }
1631}
1632
1633void APIConnection::send_serial_proxy_data(const SerialProxyDataReceived &msg) { this->send_message(msg); }
1634#endif
1635
1636#ifdef USE_INFRARED
1637uint16_t APIConnection::try_send_infrared_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1638 auto *infrared = static_cast<infrared::Infrared *>(entity);
1640 msg.capabilities = infrared->get_capability_flags();
1641 msg.receiver_frequency = infrared->get_traits().get_receiver_frequency_hz();
1642 return fill_and_encode_entity_info(infrared, msg, conn, remaining_size);
1643}
1644#endif
1645
1646#ifdef USE_RADIO_FREQUENCY
1647uint16_t APIConnection::try_send_radio_frequency_info(EntityBase *entity, APIConnection *conn,
1648 uint32_t remaining_size) {
1649 auto *rf = static_cast<radio_frequency::RadioFrequency *>(entity);
1651 msg.capabilities = rf->get_capability_flags();
1652 msg.frequency_min = rf->get_traits().get_frequency_min_hz();
1653 msg.frequency_max = rf->get_traits().get_frequency_max_hz();
1654 msg.supported_modulations = rf->get_traits().get_supported_modulations();
1655 return fill_and_encode_entity_info(rf, msg, conn, remaining_size);
1656}
1657#endif
1658
1659#ifdef USE_UPDATE
1660bool APIConnection::send_update_state(update::UpdateEntity *update) {
1661 return this->send_message_smart_(update, UpdateStateResponse::MESSAGE_TYPE, UpdateStateResponse::ESTIMATED_SIZE);
1662}
1663uint16_t APIConnection::try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1664 auto *update = static_cast<update::UpdateEntity *>(entity);
1666 resp.missing_state = !update->has_state();
1667 if (update->has_state()) {
1669 if (update->update_info.has_progress) {
1670 resp.has_progress = true;
1671 resp.progress = update->update_info.progress;
1672 }
1673 resp.current_version = StringRef(update->update_info.current_version);
1674 resp.latest_version = StringRef(update->update_info.latest_version);
1675 resp.title = StringRef(update->update_info.title);
1676 resp.release_summary = StringRef(update->update_info.summary);
1677 resp.release_url = StringRef(update->update_info.release_url);
1678 }
1679 return fill_and_encode_entity_state(update, resp, conn, remaining_size);
1680}
1681uint16_t APIConnection::try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
1682 auto *update = static_cast<update::UpdateEntity *>(entity);
1684 return fill_and_encode_entity_info_with_device_class(update, msg, msg.device_class, conn, remaining_size);
1685}
1686void APIConnection::on_update_command_request(const UpdateCommandRequest &msg) {
1687 ENTITY_COMMAND_GET(update::UpdateEntity, update, update)
1688
1689 switch (msg.command) {
1690 case enums::UPDATE_COMMAND_UPDATE:
1691 update->perform();
1692 break;
1693 case enums::UPDATE_COMMAND_CHECK:
1694 update->check();
1695 break;
1696 case enums::UPDATE_COMMAND_NONE:
1697 ESP_LOGE(TAG, "UPDATE_COMMAND_NONE not handled; confirm command is correct");
1698 break;
1699 default:
1700 ESP_LOGW(TAG, "Unknown update command: %" PRIu32, msg.command);
1701 break;
1702 }
1703}
1704#endif
1705
1706bool APIConnection::try_send_log_message(int level, const char *tag, const char *line, size_t message_len) {
1708 msg.level = static_cast<enums::LogLevel>(level);
1709 msg.set_message(reinterpret_cast<const uint8_t *>(line), message_len);
1710 return this->send_message(msg);
1711}
1712
1713void APIConnection::complete_authentication_() {
1714 // Early return if already authenticated
1715 if (this->flags_.connection_state == static_cast<uint8_t>(ConnectionState::AUTHENTICATED)) {
1716 return;
1717 }
1718
1719 this->flags_.connection_state = static_cast<uint8_t>(ConnectionState::AUTHENTICATED);
1720 // Reset traffic timer so keepalive starts from authentication, not connection start
1721 this->last_traffic_ = App.get_loop_component_start_time();
1722 this->log_client_(ESPHOME_LOG_LEVEL_DEBUG, LOG_STR("connected"));
1723#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
1724 {
1725 char peername[socket::SOCKADDR_STR_LEN];
1726 this->parent_->get_client_connected_trigger()->trigger(std::string(this->helper_->get_client_name()),
1727 std::string(this->helper_->get_peername_to(peername)));
1728 }
1729#endif
1730#ifdef USE_HOMEASSISTANT_TIME
1732 this->send_time_request();
1733 }
1734#endif
1735#ifdef USE_ZWAVE_PROXY
1736 if (zwave_proxy::global_zwave_proxy != nullptr) {
1738 }
1739#endif
1740}
1741
1742bool APIConnection::send_hello_response_(const HelloRequest &msg) {
1743 // Copy client name with truncation if needed (set_client_name handles truncation)
1744 this->helper_->set_client_name(msg.client_info.c_str(), msg.client_info.size());
1745 this->client_api_version_major_ = msg.api_version_major;
1746 this->client_api_version_minor_ = msg.api_version_minor;
1747 char peername[socket::SOCKADDR_STR_LEN];
1748 ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu16 ".%" PRIu16, this->helper_->get_client_name(),
1749 this->helper_->get_peername_to(peername), this->client_api_version_major_, this->client_api_version_minor_);
1750
1751 HelloResponse resp;
1752 resp.api_version_major = 1;
1753 resp.api_version_minor = 14;
1754 // Send only the version string - the client only logs this for debugging and doesn't use it otherwise
1755 resp.server_info = ESPHOME_VERSION_REF;
1756 resp.name = StringRef(App.get_name());
1757
1758#ifdef USE_PROVISIONING
1760 // The provisioning window has closed without the device being provisioned.
1761 // Acknowledge the hello so the client can read the server name, then request
1762 // disconnect with the reason. Authentication is intentionally not completed.
1763 this->log_client_(ESPHOME_LOG_LEVEL_WARN, LOG_STR("Provisioning closed; rejecting connection"));
1764 this->send_message(resp);
1766 req.reason = enums::DISCONNECT_REASON_PROVISIONING_CLOSED;
1767 return this->send_message(req);
1768 }
1769#endif
1770
1771 // Auto-authenticate - password auth was removed in ESPHome 2026.1.0
1772 this->complete_authentication_();
1773
1774 return this->send_message(resp);
1775}
1776
1777bool APIConnection::send_ping_response_() {
1778 PingResponse resp;
1779 return this->send_message(resp);
1780}
1781
1782bool APIConnection::send_device_info_response_() {
1783 DeviceInfoResponse resp;
1784 resp.name = StringRef(App.get_name());
1786#ifdef USE_AREAS
1788#endif
1789 // Stack buffer for MAC address (XX:XX:XX:XX:XX:XX\0 = 18 bytes)
1790 char mac_address[18];
1791 uint8_t mac[6];
1793 format_mac_addr_upper(mac, mac_address);
1794 resp.mac_address = StringRef(mac_address);
1795
1796 resp.esphome_version = ESPHOME_VERSION_REF;
1797
1798 // Stack buffer for build time string
1799 char build_time_str[Application::BUILD_TIME_STR_SIZE];
1800 App.get_build_time_string(build_time_str);
1801 resp.compilation_time = StringRef(build_time_str);
1802
1803 // Manufacturer string - define once, handle ESP8266 PROGMEM separately
1804#if defined(USE_ESP8266) || defined(USE_ESP32)
1805#define ESPHOME_MANUFACTURER "Espressif"
1806#elif defined(USE_RP2)
1807#define ESPHOME_MANUFACTURER "Raspberry Pi"
1808#elif defined(USE_BK72XX)
1809#define ESPHOME_MANUFACTURER "Beken"
1810#elif defined(USE_LN882X)
1811#define ESPHOME_MANUFACTURER "Lightning"
1812#elif defined(USE_NRF52)
1813#define ESPHOME_MANUFACTURER "Nordic Semiconductor"
1814#elif defined(USE_RTL87XX)
1815#define ESPHOME_MANUFACTURER "Realtek"
1816#elif defined(USE_HOST)
1817#define ESPHOME_MANUFACTURER "Host"
1818#endif
1819
1820#ifdef USE_ESP8266
1821 // ESP8266 requires PROGMEM for flash storage, copy to stack for memcpy compatibility
1822 static const char MANUFACTURER_PROGMEM[] PROGMEM = ESPHOME_MANUFACTURER;
1823 char manufacturer_buf[sizeof(MANUFACTURER_PROGMEM)];
1824 memcpy_P(manufacturer_buf, MANUFACTURER_PROGMEM, sizeof(MANUFACTURER_PROGMEM));
1825 resp.manufacturer = StringRef(manufacturer_buf, sizeof(MANUFACTURER_PROGMEM) - 1);
1826#else
1827 static constexpr auto MANUFACTURER = StringRef::from_lit(ESPHOME_MANUFACTURER);
1828 resp.manufacturer = MANUFACTURER;
1829#endif
1830 static_assert(sizeof(ESPHOME_MANUFACTURER) - 1 <= 20, "Update max_data_length for manufacturer in api.proto");
1831#undef ESPHOME_MANUFACTURER
1832
1833#ifdef USE_ESP8266
1834 static const char MODEL_PROGMEM[] PROGMEM = ESPHOME_BOARD;
1835 char model_buf[sizeof(MODEL_PROGMEM)];
1836 memcpy_P(model_buf, MODEL_PROGMEM, sizeof(MODEL_PROGMEM));
1837 resp.model = StringRef(model_buf, sizeof(MODEL_PROGMEM) - 1);
1838#else
1839 static constexpr auto MODEL = StringRef::from_lit(ESPHOME_BOARD);
1840 resp.model = MODEL;
1841#endif
1842#ifdef USE_DEEP_SLEEP
1844#endif
1845#ifdef ESPHOME_PROJECT_NAME
1846#ifdef USE_ESP8266
1847 static const char PROJECT_NAME_PROGMEM[] PROGMEM = ESPHOME_PROJECT_NAME;
1848 static const char PROJECT_VERSION_PROGMEM[] PROGMEM = ESPHOME_PROJECT_VERSION;
1849 char project_name_buf[sizeof(PROJECT_NAME_PROGMEM)];
1850 char project_version_buf[sizeof(PROJECT_VERSION_PROGMEM)];
1851 memcpy_P(project_name_buf, PROJECT_NAME_PROGMEM, sizeof(PROJECT_NAME_PROGMEM));
1852 memcpy_P(project_version_buf, PROJECT_VERSION_PROGMEM, sizeof(PROJECT_VERSION_PROGMEM));
1853 resp.project_name = StringRef(project_name_buf, sizeof(PROJECT_NAME_PROGMEM) - 1);
1854 resp.project_version = StringRef(project_version_buf, sizeof(PROJECT_VERSION_PROGMEM) - 1);
1855#else
1856 static constexpr auto PROJECT_NAME = StringRef::from_lit(ESPHOME_PROJECT_NAME);
1857 static constexpr auto PROJECT_VERSION = StringRef::from_lit(ESPHOME_PROJECT_VERSION);
1858 resp.project_name = PROJECT_NAME;
1859 resp.project_version = PROJECT_VERSION;
1860#endif
1861#endif
1862#ifdef USE_WEBSERVER
1863 resp.webserver_port = USE_WEBSERVER_PORT;
1864#endif
1865#ifdef USE_BLUETOOTH_PROXY
1867 // Stack buffer for Bluetooth MAC address (XX:XX:XX:XX:XX:XX\0 = 18 bytes)
1868 char bluetooth_mac[18];
1870 resp.bluetooth_mac_address = StringRef(bluetooth_mac);
1871#endif
1872#ifdef USE_VOICE_ASSISTANT
1874#endif
1875#ifdef USE_ZWAVE_PROXY
1878#endif
1879#ifdef USE_SERIAL_PROXY
1880 size_t serial_proxy_index = 0;
1881 for (auto const &proxy : App.get_serial_proxies()) {
1882 if (serial_proxy_index >= SERIAL_PROXY_COUNT)
1883 break;
1884 auto &info = resp.serial_proxies[serial_proxy_index++];
1885 info.name = StringRef(proxy->get_name());
1886 info.port_type = proxy->get_port_type();
1887 }
1888#endif
1889#ifdef USE_API_NOISE
1890 resp.api_encryption_supported = true;
1891#ifndef USE_API_NOISE_PSK_FROM_YAML
1892 // No key from YAML: while no key is set, the key can be provisioned over a
1893 // zero-PSK Noise connection. Gated on the YAML define (not the plaintext
1894 // one) so this advertisement survives the plaintext removal in 2027.2.0.
1895 resp.api_encryption_provisionable = !this->parent_->get_noise_ctx().has_psk();
1896#endif
1897#endif
1898#ifdef USE_DEVICES
1899 size_t device_index = 0;
1900 for (auto const &device : App.get_devices()) {
1901 if (device_index >= ESPHOME_DEVICE_COUNT)
1902 break;
1903 auto &device_info = resp.devices[device_index++];
1904 device_info.device_id = device->get_device_id();
1905 device_info.name = StringRef(device->get_name());
1906 device_info.area_id = device->get_area_id();
1907 }
1908#endif
1909#ifdef USE_AREAS
1910 size_t area_index = 0;
1911 for (auto const &area : App.get_areas()) {
1912 if (area_index >= ESPHOME_AREA_COUNT)
1913 break;
1914 auto &area_info = resp.areas[area_index++];
1915 area_info.area_id = area->get_area_id();
1916 area_info.name = StringRef(area->get_name());
1917 }
1918#endif
1919
1920 return this->send_message(resp);
1921}
1922void APIConnection::on_hello_request(const HelloRequest &msg) {
1923 if (!this->send_hello_response_(msg)) {
1924 this->on_fatal_error();
1925 }
1926}
1927void APIConnection::on_disconnect_request(const DisconnectRequest & /*msg*/) {
1928 // The reason is informational when a client disconnects us; we always ack and close.
1929 if (!this->send_disconnect_response_()) {
1930 this->on_fatal_error();
1931 }
1932}
1933void APIConnection::on_ping_request() {
1934 if (!this->send_ping_response_()) {
1935 this->on_fatal_error();
1936 }
1937}
1938void APIConnection::on_device_info_request() {
1939 if (!this->send_device_info_response_()) {
1940 this->on_fatal_error();
1941 }
1942}
1943
1944#ifdef USE_API_HOMEASSISTANT_STATES
1945void APIConnection::on_home_assistant_state_response(const HomeAssistantStateResponse &msg) {
1946 // Skip if entity_id is empty (invalid message)
1947 if (msg.entity_id.empty()) {
1948 return;
1949 }
1950
1951 // Null-terminate state in-place for safe c_str() usage (e.g., parse_number in callbacks).
1952 // Safe: decode is complete, byte after string data was already consumed during parse,
1953 // and frame helpers reserve RX_BUF_NULL_TERMINATOR extra byte in rx_buf_.
1954 // const_cast is safe: msg references mutable rx_buf_ data; the const& handler
1955 // signature is a generated protobuf pattern, not a true immutability contract.
1956 if (!msg.state.empty()) {
1957 const_cast<char *>(msg.state.c_str())[msg.state.size()] = '\0';
1958 }
1959
1960 for (auto &it : this->parent_->get_state_subs()) {
1961 if (msg.entity_id != it.entity_id) {
1962 continue;
1963 }
1964
1965 // If subscriber has attribute filter (non-null), message attribute must match it;
1966 // if subscriber has no filter (nullptr), message must have no attribute.
1967 if (it.attribute != nullptr ? msg.attribute != it.attribute : !msg.attribute.empty()) {
1968 continue;
1969 }
1970
1971 it.callback(msg.state);
1972 }
1973}
1974#endif
1975#ifdef USE_API_USER_DEFINED_ACTIONS
1976void APIConnection::on_execute_service_request(const ExecuteServiceRequest &msg) {
1977 // Null-terminate string args in-place for safe c_str() usage in YAML service triggers.
1978 // Safe: full ExecuteServiceRequest decode is complete, all bytes in rx_buf_ consumed,
1979 // and frame helpers reserve RX_BUF_NULL_TERMINATOR extra byte for the last field.
1980 // const_cast is safe: msg references mutable rx_buf_ data; the const& handler
1981 // signature is a generated protobuf pattern, not a true immutability contract.
1982 for (auto &arg : const_cast<ExecuteServiceRequest &>(msg).args) {
1983 if (!arg.string_.empty()) {
1984 const_cast<char *>(arg.string_.c_str())[arg.string_.size()] = '\0';
1985 }
1986 }
1987 bool found = false;
1988#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
1989 // Register the call and get a unique server-generated action_call_id
1990 // This avoids collisions when multiple clients use the same call_id
1991 uint32_t action_call_id = 0;
1992 if (msg.call_id != 0) {
1993 action_call_id = this->parent_->register_active_action_call(msg.call_id, this);
1994 }
1995 // Use the overload that passes action_call_id separately (avoids copying msg)
1996 for (auto *service : this->parent_->get_user_services()) {
1997 if (service->execute_service(msg, action_call_id)) {
1998 found = true;
1999 }
2000 }
2001#else
2002 for (auto *service : this->parent_->get_user_services()) {
2003 if (service->execute_service(msg)) {
2004 found = true;
2005 }
2006 }
2007#endif
2008 if (!found) {
2009 ESP_LOGV(TAG, "Could not find service");
2010 }
2011 // Note: For services with supports_response != none, the call is unregistered
2012 // by an automatically appended APIUnregisterServiceCallAction at the end of
2013 // the action list. This ensures async actions (delays, waits) complete first.
2014}
2015#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
2016void APIConnection::send_execute_service_response(uint32_t call_id, bool success, StringRef error_message) {
2018 resp.call_id = call_id;
2019 resp.success = success;
2020 resp.error_message = error_message;
2021 this->send_message(resp);
2022}
2023#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
2024void APIConnection::send_execute_service_response(uint32_t call_id, bool success, StringRef error_message,
2025 const uint8_t *response_data, size_t response_data_len) {
2027 resp.call_id = call_id;
2028 resp.success = success;
2029 resp.error_message = error_message;
2030 resp.response_data = response_data;
2031 resp.response_data_len = response_data_len;
2032 this->send_message(resp);
2033}
2034#endif // USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
2035#endif // USE_API_USER_DEFINED_ACTION_RESPONSES
2036#endif
2037
2038#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
2039void APIConnection::on_homeassistant_action_response(const HomeassistantActionResponse &msg) {
2040#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
2041 if (msg.response_data_len > 0) {
2042 this->parent_->handle_action_response(msg.call_id, msg.success, msg.error_message, msg.response_data,
2043 msg.response_data_len);
2044 } else
2045#endif
2046 {
2047 this->parent_->handle_action_response(msg.call_id, msg.success, msg.error_message);
2048 }
2049};
2050#endif
2051#ifdef USE_API_NOISE
2052bool APIConnection::send_noise_encryption_set_key_response_(const NoiseEncryptionSetKeyRequest &msg) {
2054 resp.success = false;
2055
2056#ifdef USE_PROVISIONING
2057 // Refuse to set a key once the provisioning window has closed (defense in depth;
2058 // such connections are already rejected at hello).
2060 ESP_LOGW(TAG, "Provisioning closed; rejecting key set");
2061 return this->send_message(resp);
2062 }
2063#endif
2064
2065 psk_t psk{};
2066 if (msg.key_len == 0) {
2067 if (this->parent_->clear_noise_psk(true)) {
2068 resp.success = true;
2069 } else {
2070 ESP_LOGW(TAG, "Failed to clear encryption key");
2071 }
2072 } else if (base64_decode(msg.key, msg.key_len, psk.data(), psk.size()) != psk.size()) {
2073 ESP_LOGW(TAG, "Invalid encryption key length");
2074 } else if (APINoiseContext::is_all_zeros(psk)) {
2075 // Accepting the reserved provisioning PSK would report success without
2076 // enabling encryption (or silently clear an existing key)
2077 ESP_LOGW(TAG, "Rejecting all-zero encryption key");
2078 } else if (!this->parent_->save_noise_psk(psk, true)) {
2079 ESP_LOGW(TAG, "Failed to save encryption key");
2080 } else {
2081 resp.success = true;
2082#ifdef USE_API_PLAINTEXT
2083 if (this->helper_->frame_footer_size() == 0) {
2084 // Plaintext transport has no frame footer; Noise always has the MAC footer.
2085 // Remove after 2027.2.0 together with plaintext support on keyless devices.
2086 ESP_LOGW(TAG, "Key received over plaintext; deprecated, will be removed in 2027.2.0");
2087 }
2088#endif
2089 }
2090
2091 return this->send_message(resp);
2092}
2093void APIConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) {
2094 if (!this->send_noise_encryption_set_key_response_(msg)) {
2095 this->on_fatal_error();
2096 }
2097}
2098#endif
2099#ifdef USE_API_HOMEASSISTANT_STATES
2100void APIConnection::on_subscribe_home_assistant_states_request() { state_subs_at_ = 0; }
2101#endif
2102bool APIConnection::try_to_clear_buffer_slow_(bool log_out_of_space) {
2103 delay(0);
2104 APIError err = this->helper_->loop();
2105 if (err != APIError::OK) {
2106 this->fatal_error_with_log_(LOG_STR("Socket operation failed"), err);
2107 return false;
2108 }
2109 if (this->helper_->can_write_without_blocking())
2110 return true;
2111 if (log_out_of_space) {
2112 ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
2113 }
2114 return false;
2115}
2116bool APIConnection::send_message_(uint32_t payload_size, uint8_t message_type, MessageEncodeFn encode_fn,
2117 const void *msg) {
2118#ifdef HAS_PROTO_MESSAGE_DUMP
2119 // Skip dump for log messages (recursive logging risk) and camera frames (high-frequency noise)
2120 if (message_type != SubscribeLogsResponse::MESSAGE_TYPE
2121#ifdef USE_CAMERA
2122 && message_type != CameraImageResponse::MESSAGE_TYPE
2123#endif
2124 ) {
2125 auto *proto_msg = static_cast<const ProtoMessage *>(msg);
2126 DumpBuffer dump_buf;
2127 this->log_send_message_(proto_msg->message_name(), proto_msg->dump_to(dump_buf));
2128 }
2129#endif
2130 auto &shared_buf = this->parent_->get_shared_buffer_ref();
2131 this->prepare_first_message_buffer(shared_buf, payload_size);
2132 size_t write_start = shared_buf.size();
2133 shared_buf.resize(write_start + payload_size);
2134 ProtoWriteBuffer buffer{&shared_buf, write_start};
2135 encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
2136 return this->send_buffer(ProtoWriteBuffer{&shared_buf}, message_type);
2137}
2138// encode_to_buffer is defined inline in api_connection.h (ESPHOME_ALWAYS_INLINE)
2139
2140// Noinline version for cold paths — single shared copy
2141uint16_t APIConnection::encode_to_buffer_slow(uint32_t calculated_size, MessageEncodeFn encode_fn, const void *msg,
2142 APIConnection *conn, uint32_t remaining_size) {
2143 return encode_to_buffer(calculated_size, encode_fn, msg, conn, remaining_size);
2144}
2145bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) {
2146 const bool is_log_message = (message_type == SubscribeLogsResponse::MESSAGE_TYPE);
2147
2148 if (!this->try_to_clear_buffer(!is_log_message)) {
2149 return false;
2150 }
2151
2152 // Set TCP_NODELAY based on message type - see set_nodelay_for_message() for details
2153 this->helper_->set_nodelay_for_message(is_log_message);
2154
2155 APIError err = this->helper_->write_protobuf_packet(message_type, buffer);
2156 if (err == APIError::WOULD_BLOCK)
2157 return false;
2158 if (err != APIError::OK) {
2159 this->fatal_error_with_log_(LOG_STR("Packet write failed"), err);
2160 return false;
2161 }
2162 // Do not set last_traffic_ on send
2163 return true;
2164}
2165void APIConnection::on_no_setup_connection() {
2166 this->on_fatal_error();
2167 this->log_client_(ESPHOME_LOG_LEVEL_DEBUG, LOG_STR("no connection setup"));
2168}
2169void APIConnection::on_fatal_error() {
2170 // Don't close socket here - keep it open so getpeername() works for logging
2171 // Socket will be closed when client is removed from the list in APIServer::loop()
2172 this->flags_.remove = true;
2173}
2174
2175bool APIConnection::schedule_message_front_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size) {
2176 this->deferred_batch_.add_item_front(entity, message_type, estimated_size);
2177 return this->schedule_batch_();
2178}
2179
2180bool APIConnection::send_message_smart_(EntityBase *entity, uint8_t message_type, uint8_t estimated_size,
2181 uint8_t aux_data_index) {
2182 if (this->should_send_immediately_(message_type) && this->helper_->can_write_without_blocking()) {
2183 auto &shared_buf = this->parent_->get_shared_buffer_ref();
2184 this->prepare_first_message_buffer(shared_buf, estimated_size);
2185 DeferredBatch::BatchItem item{entity, message_type, estimated_size, aux_data_index};
2186 if (this->dispatch_message_(item, MAX_BATCH_PACKET_SIZE, true) &&
2187 this->send_buffer(ProtoWriteBuffer{&shared_buf}, message_type)) {
2188#ifdef HAS_PROTO_MESSAGE_DUMP
2189 this->log_batch_item_(item);
2190#endif
2191 return true;
2192 }
2193 }
2194 return this->schedule_message_(entity, message_type, estimated_size, aux_data_index);
2195}
2196
2197bool APIConnection::schedule_batch_() {
2198 if (!this->flags_.batch_scheduled) {
2199 this->flags_.batch_scheduled = true;
2200 this->deferred_batch_.batch_start_time = App.get_loop_component_start_time();
2201 }
2202 return true;
2203}
2204
2205void APIConnection::process_batch_() {
2206 if (this->deferred_batch_.empty()) {
2207 this->flags_.batch_scheduled = false;
2208 return;
2209 }
2210
2211 // Ensure TCP_NODELAY is on before draining overflow and writing batch data.
2212 // Log messages enable Nagle (NODELAY off) to coalesce small packets.
2213 // If Nagle is still on when we try to drain, LWIP holds data in the
2214 // Nagle buffer, the TCP send buffer stays full, and the overflow
2215 // buffer can never drain — blocking the batch write indefinitely.
2216 this->helper_->set_nodelay_for_message(false);
2217
2218 // Try to clear buffer first
2219 if (!this->try_to_clear_buffer(true)) {
2220 // Can't write now, we'll try again later
2221 return;
2222 }
2223
2224 // Get shared buffer reference once to avoid multiple calls
2225 auto &shared_buf = this->parent_->get_shared_buffer_ref();
2226 size_t num_items = this->deferred_batch_.size();
2227
2228 // Cache these values to avoid repeated virtual calls
2229 const uint8_t header_padding = this->helper_->frame_header_padding();
2230 const uint8_t footer_size = this->helper_->frame_footer_size();
2231
2232 // Pre-calculate exact buffer size needed based on message types
2233 uint32_t total_estimated_size = num_items * (header_padding + footer_size);
2234 for (size_t i = 0; i < num_items; i++) {
2235 total_estimated_size += this->deferred_batch_[i].estimated_size;
2236 }
2237 // Clamp to MAX_BATCH_PACKET_SIZE — we won't send more than that per batch
2238 if (total_estimated_size > MAX_BATCH_PACKET_SIZE) {
2239 total_estimated_size = MAX_BATCH_PACKET_SIZE;
2240 }
2241
2242 this->prepare_first_message_buffer(shared_buf, header_padding, total_estimated_size);
2243
2244 // Fast path for single message - buffer already allocated above
2245 if (num_items == 1) {
2246 const auto &item = this->deferred_batch_[0];
2247 // Let dispatch_message_ calculate size and encode if it fits
2248 uint16_t payload_size = this->dispatch_message_(item, std::numeric_limits<uint16_t>::max(), true);
2249
2250 if (payload_size > 0 && this->send_buffer(ProtoWriteBuffer{&shared_buf}, item.message_type)) {
2251#ifdef HAS_PROTO_MESSAGE_DUMP
2252 // Log message after send attempt for VV debugging
2253 this->log_batch_item_(item);
2254#endif
2255 this->clear_batch_();
2256 } else if (payload_size == 0) {
2257 // Message too large to fit in available space
2258 ESP_LOGW(TAG, "Message too large to send: type=%u", item.message_type);
2259 this->clear_batch_();
2260 }
2261 return;
2262 }
2263
2264 // Multi-message path — heavy stack frame isolated in separate noinline function
2265 this->process_batch_multi_(shared_buf, num_items, header_padding, footer_size);
2266}
2267
2268// Separated from process_batch_() so the single-message fast path gets a minimal
2269// stack frame without the MAX_MESSAGES_PER_BATCH * sizeof(MessageInfo) array.
2270void APIConnection::process_batch_multi_(APIBuffer &shared_buf, size_t num_items, uint8_t header_padding,
2271 uint8_t footer_size) {
2272 // Ensure MessageInfo remains trivially destructible for our placement new approach
2273 static_assert(std::is_trivially_destructible<MessageInfo>::value,
2274 "MessageInfo must remain trivially destructible with this placement-new approach");
2275
2276 const size_t messages_to_process = std::min(num_items, MAX_MESSAGES_PER_BATCH);
2277
2278 // Stack-allocated array for message info
2279 alignas(MessageInfo) char message_info_storage[MAX_MESSAGES_PER_BATCH * sizeof(MessageInfo)];
2280 MessageInfo *message_info = reinterpret_cast<MessageInfo *>(message_info_storage);
2281 size_t items_processed = 0;
2282 uint16_t remaining_size = std::numeric_limits<uint16_t>::max();
2283 // Track where each message's header begins in the buffer
2284 // First message: offset 0 (max padding, may have unused leading bytes)
2285 // Subsequent messages: offset points to exact header start (no gaps)
2286 uint32_t current_offset = 0;
2287
2288 // Process items and encode directly to buffer (up to our limit)
2289 for (size_t i = 0; i < messages_to_process; i++) {
2290 const auto &item = this->deferred_batch_[i];
2291 // Try to encode message via dispatch
2292 // The dispatch function calculates overhead to determine if the message fits
2293 uint16_t payload_size = this->dispatch_message_(item, remaining_size, i == 0);
2294
2295 if (payload_size == 0) {
2296 // Message won't fit, stop processing
2297 break;
2298 }
2299
2300 // Message was encoded successfully
2301 // payload_size = header_size + proto_payload_size + footer_size
2302 uint16_t proto_payload_size = payload_size - this->batch_header_size_ - footer_size;
2303 // Use placement new to construct MessageInfo in pre-allocated stack array
2304 // This avoids default-constructing all MAX_MESSAGES_PER_BATCH elements
2305 // Explicit destruction is not needed because MessageInfo is trivially destructible,
2306 // as ensured by the static_assert in its definition.
2307 new (&message_info[items_processed++])
2308 MessageInfo(item.message_type, current_offset, proto_payload_size, this->batch_header_size_);
2309 // After first message, set remaining size to MAX_BATCH_PACKET_SIZE to avoid fragmentation
2310 if (items_processed == 1) {
2311 remaining_size = MAX_BATCH_PACKET_SIZE;
2312 }
2313 remaining_size -= payload_size;
2314 // Calculate where the next message's header padding will start
2315 // Current buffer size + footer space for this message
2316 current_offset = shared_buf.size() + footer_size;
2317 }
2318
2319 if (items_processed > 0) {
2320 // Add footer space for the last message (for Noise protocol MAC)
2321 if (footer_size > 0) {
2322 shared_buf.resize(shared_buf.size() + footer_size);
2323 }
2324
2325 // Send all collected messages
2326 APIError err = this->helper_->write_protobuf_messages(ProtoWriteBuffer{&shared_buf},
2327 std::span<const MessageInfo>(message_info, items_processed));
2328 if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
2329 this->fatal_error_with_log_(LOG_STR("Batch write failed"), err);
2330 }
2331
2332#ifdef HAS_PROTO_MESSAGE_DUMP
2333 // Log messages after send attempt for VV debugging
2334 // It's safe to use the buffer for logging at this point regardless of send result
2335 for (size_t i = 0; i < items_processed; i++) {
2336 const auto &item = this->deferred_batch_[i];
2337 this->log_batch_item_(item);
2338 }
2339#endif
2340
2341 // Partial batch — remove processed items and reschedule
2342 if (items_processed < this->deferred_batch_.size()) {
2343 this->deferred_batch_.remove_front(items_processed);
2344 this->schedule_batch_();
2345 return;
2346 }
2347 }
2348
2349 // All items processed (or none could be processed)
2350 this->clear_batch_();
2351}
2352
2353// Dispatch message encoding based on message_type
2354// Switch assigns function pointer, single call site for smaller code size
2355uint16_t APIConnection::dispatch_message_(const DeferredBatch::BatchItem &item, uint32_t remaining_size,
2356 bool batch_first) {
2357 this->flags_.batch_first_message = batch_first;
2358 this->batch_message_type_ = item.message_type;
2359#ifdef USE_EVENT
2360 // Events need aux_data_index to look up event type from entity
2361 if (item.message_type == EventResponse::MESSAGE_TYPE) {
2362 // Skip if aux_data_index is invalid (should never happen in normal operation)
2363 if (item.aux_data_index == DeferredBatch::AUX_DATA_UNUSED)
2364 return 0;
2365 auto *event = static_cast<event::Event *>(item.entity);
2366 return try_send_event_response(event, StringRef::from_maybe_nullptr(event->get_event_type(item.aux_data_index)),
2367 this, remaining_size);
2368 }
2369#endif
2370
2371 // All other message types use function pointer lookup via switch
2372 MessageCreatorPtr func = nullptr;
2373
2374// Macros to reduce repetitive switch cases
2375#define CASE_STATE_INFO(entity_name, StateResp, InfoResp) \
2376 case StateResp::MESSAGE_TYPE: \
2377 func = &try_send_##entity_name##_state; \
2378 break; \
2379 case InfoResp::MESSAGE_TYPE: \
2380 func = &try_send_##entity_name##_info; \
2381 break;
2382#define CASE_INFO_ONLY(entity_name, InfoResp) \
2383 case InfoResp::MESSAGE_TYPE: \
2384 func = &try_send_##entity_name##_info; \
2385 break;
2386
2387 switch (item.message_type) {
2388#ifdef USE_BINARY_SENSOR
2389 CASE_STATE_INFO(binary_sensor, BinarySensorStateResponse, ListEntitiesBinarySensorResponse)
2390#endif
2391#ifdef USE_COVER
2392 CASE_STATE_INFO(cover, CoverStateResponse, ListEntitiesCoverResponse)
2393#endif
2394#ifdef USE_FAN
2395 CASE_STATE_INFO(fan, FanStateResponse, ListEntitiesFanResponse)
2396#endif
2397#ifdef USE_LIGHT
2398 CASE_STATE_INFO(light, LightStateResponse, ListEntitiesLightResponse)
2399#endif
2400#ifdef USE_SENSOR
2401 CASE_STATE_INFO(sensor, SensorStateResponse, ListEntitiesSensorResponse)
2402#endif
2403#ifdef USE_SWITCH
2404 CASE_STATE_INFO(switch, SwitchStateResponse, ListEntitiesSwitchResponse)
2405#endif
2406#ifdef USE_BUTTON
2407 CASE_INFO_ONLY(button, ListEntitiesButtonResponse)
2408#endif
2409#ifdef USE_TEXT_SENSOR
2410 CASE_STATE_INFO(text_sensor, TextSensorStateResponse, ListEntitiesTextSensorResponse)
2411#endif
2412#ifdef USE_CLIMATE
2413 CASE_STATE_INFO(climate, ClimateStateResponse, ListEntitiesClimateResponse)
2414#endif
2415#ifdef USE_NUMBER
2416 CASE_STATE_INFO(number, NumberStateResponse, ListEntitiesNumberResponse)
2417#endif
2418#ifdef USE_DATETIME_DATE
2419 CASE_STATE_INFO(date, DateStateResponse, ListEntitiesDateResponse)
2420#endif
2421#ifdef USE_DATETIME_TIME
2422 CASE_STATE_INFO(time, TimeStateResponse, ListEntitiesTimeResponse)
2423#endif
2424#ifdef USE_DATETIME_DATETIME
2425 CASE_STATE_INFO(datetime, DateTimeStateResponse, ListEntitiesDateTimeResponse)
2426#endif
2427#ifdef USE_TEXT
2428 CASE_STATE_INFO(text, TextStateResponse, ListEntitiesTextResponse)
2429#endif
2430#ifdef USE_SELECT
2431 CASE_STATE_INFO(select, SelectStateResponse, ListEntitiesSelectResponse)
2432#endif
2433#ifdef USE_LOCK
2435#endif
2436#ifdef USE_VALVE
2437 CASE_STATE_INFO(valve, ValveStateResponse, ListEntitiesValveResponse)
2438#endif
2439#ifdef USE_MEDIA_PLAYER
2440 CASE_STATE_INFO(media_player, MediaPlayerStateResponse, ListEntitiesMediaPlayerResponse)
2441#endif
2442#ifdef USE_ALARM_CONTROL_PANEL
2443 CASE_STATE_INFO(alarm_control_panel, AlarmControlPanelStateResponse, ListEntitiesAlarmControlPanelResponse)
2444#endif
2445#ifdef USE_WATER_HEATER
2446 CASE_STATE_INFO(water_heater, WaterHeaterStateResponse, ListEntitiesWaterHeaterResponse)
2447#endif
2448#ifdef USE_CAMERA
2449 CASE_INFO_ONLY(camera, ListEntitiesCameraResponse)
2450#endif
2451#ifdef USE_INFRARED
2452 CASE_INFO_ONLY(infrared, ListEntitiesInfraredResponse)
2453#endif
2454#ifdef USE_RADIO_FREQUENCY
2455 CASE_INFO_ONLY(radio_frequency, ListEntitiesRadioFrequencyResponse)
2456#endif
2457#ifdef USE_EVENT
2458 CASE_INFO_ONLY(event, ListEntitiesEventResponse)
2459#endif
2460#ifdef USE_UPDATE
2461 CASE_STATE_INFO(update, UpdateStateResponse, ListEntitiesUpdateResponse)
2462#endif
2463 // Special messages (not entity state/info)
2464 case ListEntitiesDoneResponse::MESSAGE_TYPE:
2465 func = &try_send_list_info_done;
2466 break;
2467 case DisconnectRequest::MESSAGE_TYPE:
2468 func = &try_send_disconnect_request;
2469 break;
2470 case PingRequest::MESSAGE_TYPE:
2471 func = &try_send_ping_request;
2472 break;
2473 default:
2474 return 0;
2475 }
2476
2477#undef CASE_STATE_INFO
2478#undef CASE_INFO_ONLY
2479
2480 return func(item.entity, this, remaining_size);
2481}
2482
2483uint16_t APIConnection::try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
2485 return encode_message_to_buffer(resp, conn, remaining_size);
2486}
2487
2488uint16_t APIConnection::try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
2490 return encode_message_to_buffer(req, conn, remaining_size);
2491}
2492
2493uint16_t APIConnection::try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
2494 PingRequest req;
2495 return encode_message_to_buffer(req, conn, remaining_size);
2496}
2497
2498#ifdef USE_API_HOMEASSISTANT_STATES
2499void APIConnection::process_state_subscriptions_() {
2500 const auto &subs = this->parent_->get_state_subs();
2501 if (this->state_subs_at_ >= static_cast<int>(subs.size())) {
2502 this->state_subs_at_ = -1;
2503 return;
2504 }
2505
2506 const auto &it = subs[this->state_subs_at_];
2508 resp.entity_id = StringRef(it.entity_id);
2509
2510 // Avoid string copy by using the const char* pointer if it exists
2511 resp.attribute = it.attribute != nullptr ? StringRef(it.attribute) : StringRef("");
2512
2513 resp.once = it.once;
2514 if (this->send_message(resp)) {
2515 this->state_subs_at_++;
2516 }
2517}
2518#endif // USE_API_HOMEASSISTANT_STATES
2519
2520void APIConnection::log_client_(int level, const LogString *message) {
2521 char peername[socket::SOCKADDR_STR_LEN];
2522 esp_log_printf_(level, TAG, __LINE__, ESPHOME_LOG_FORMAT("%s (%s): %s"), this->helper_->get_client_name(),
2523 this->helper_->get_peername_to(peername), LOG_STR_ARG(message));
2524}
2525
2526void APIConnection::log_warning_(const LogString *message, APIError err) {
2527 char peername[socket::SOCKADDR_STR_LEN];
2528 ESP_LOGW(TAG, "%s (%s): %s %s errno=%d", this->helper_->get_client_name(), this->helper_->get_peername_to(peername),
2529 LOG_STR_ARG(message), LOG_STR_ARG(api_error_to_logstr(err)), errno);
2530}
2531
2532} // namespace esphome::api
2533#endif
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const auto & get_areas()
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
void get_build_time_string(std::span< char, BUILD_TIME_STR_SIZE > buffer)
Copy the build time string into the provided buffer Buffer must be BUILD_TIME_STR_SIZE bytes (compile...
const char * get_area() const
Get the area of this Application set by pre_setup().
const auto & get_devices()
auto & get_serial_proxies() const
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void begin(bool include_internal=false)
const char * get_device_class_to(std::span< char, MAX_DEVICE_CLASS_LENGTH > buffer) const
bool has_own_name() const
Definition entity_base.h:74
const StringRef & get_name() const
Definition entity_base.h:71
ESPDEPRECATED("Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be " "removed in ESPHome 2026.9.0", "2026.3.0") std const char * get_icon_to(std::span< char, MAX_ICON_LENGTH > buffer) const
Get the unit of measurement as std::string (deprecated, prefer get_unit_of_measurement_ref())
uint32_t get_object_id_hash() const
Definition entity_base.h:77
ESPDEPRECATED("Use get_icon_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") std uint32_t get_device_id() const
bool is_disabled_by_default() const
EntityCategory get_entity_category() const
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:534
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:651
void init(size_t n)
Definition helpers.h:624
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr bool empty() const
Definition string_ref.h:76
constexpr size_type size() const
Definition string_ref.h:74
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:50
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:53
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:36
size_t size() const
Definition api_buffer.h:55
void resize(size_t n) ESPHOME_ALWAYS_INLINE
Definition api_buffer.h:43
void on_button_command_request(const ButtonCommandRequest &msg)
uint8_t *(*)(const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM) MessageEncodeFn
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
uint16_t(*)(EntityBase *, APIConnection *, uint32_t remaining_size) MessageCreatorPtr
uint32_t(*)(const void *) CalculateSizeFn
uint8_t get_consumed_header(uint8_t out[3]) const
APINoiseContext & get_noise_ctx()
Definition api_server.h:79
enums::AlarmControlPanelStateCommand command
Definition api_pb2.h:2646
enums::AlarmControlPanelState state
Definition api_pb2.h:2630
enums::BluetoothScannerMode mode
Definition api_pb2.h:2344
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:1359
enums::ClimateSwingMode swing_mode
Definition api_pb2.h:1470
enums::ClimateFanMode fan_mode
Definition api_pb2.h:1468
enums::ClimatePreset preset
Definition api_pb2.h:1474
enums::ClimateFanMode fan_mode
Definition api_pb2.h:1437
enums::ClimateSwingMode swing_mode
Definition api_pb2.h:1438
enums::ClimateAction action
Definition api_pb2.h:1436
enums::ClimatePreset preset
Definition api_pb2.h:1440
enums::CoverOperation current_operation
Definition api_pb2.h:682
std::array< AreaInfo, ESPHOME_AREA_COUNT > areas
Definition api_pb2.h:578
std::array< SerialProxyInfo, SERIAL_PROXY_COUNT > serial_proxies
Definition api_pb2.h:590
std::array< DeviceInfo, ESPHOME_DEVICE_COUNT > devices
Definition api_pb2.h:575
enums::DisconnectReason reason
Definition api_pb2.h:441
Fixed-size buffer for message dumps - avoids heap allocation.
Definition proto.h:543
enums::FanDirection direction
Definition api_pb2.h:765
enums::FanDirection direction
Definition api_pb2.h:742
ParsedTimezone parsed_timezone
Definition api_pb2.h:1222
enums::EntityCategory entity_category
Definition api_pb2.h:367
enums::ColorMode color_mode
Definition api_pb2.h:809
const std::vector< const char * > * supported_custom_presets
Definition api_pb2.h:1408
const climate::ClimateSwingModeMask * supported_swing_modes
Definition api_pb2.h:1405
enums::TemperatureUnit temperature_unit
Definition api_pb2.h:1415
const std::vector< const char * > * supported_custom_fan_modes
Definition api_pb2.h:1406
const climate::ClimatePresetMask * supported_presets
Definition api_pb2.h:1407
const climate::ClimateFanModeMask * supported_fan_modes
Definition api_pb2.h:1404
const climate::ClimateModeMask * supported_modes
Definition api_pb2.h:1399
const FixedVector< const char * > * event_types
Definition api_pb2.h:2830
const std::vector< const char * > * supported_preset_modes
Definition api_pb2.h:724
const FixedVector< const char * > * effects
Definition api_pb2.h:791
const light::ColorModeMask * supported_color_modes
Definition api_pb2.h:788
std::vector< MediaPlayerSupportedFormat > supported_formats
Definition api_pb2.h:1835
const FixedVector< const char * > * options
Definition api_pb2.h:1618
enums::SensorStateClass state_class
Definition api_pb2.h:882
const water_heater::WaterHeaterModeMask * supported_modes
Definition api_pb2.h:1500
enums::LockCommand command
Definition api_pb2.h:1765
enums::MediaPlayerCommand command
Definition api_pb2.h:1871
enums::MediaPlayerState state
Definition api_pb2.h:1852
enums::SerialProxyParity parity
Definition api_pb2.h:3159
enums::SerialProxyRequestType type
Definition api_pb2.h:3265
void set_message(const uint8_t *data, size_t len)
Definition api_pb2.h:1021
enums::UpdateCommand command
Definition api_pb2.h:3010
enums::ValveOperation current_operation
Definition api_pb2.h:2884
std::vector< VoiceAssistantExternalWakeWord > external_wake_words
Definition api_pb2.h:2562
std::vector< VoiceAssistantWakeWord > available_wake_words
Definition api_pb2.h:2577
const std::vector< std::string > * active_wake_words
Definition api_pb2.h:2578
std::vector< std::string > active_wake_words
Definition api_pb2.h:2595
enums::ZWaveProxyRequestType type
Definition api_pb2.h:3046
Base class for all binary_sensor-type classes.
void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg)
void bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg)
void bluetooth_device_request(const api::BluetoothDeviceRequest &msg)
void bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg)
void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags)
void unsubscribe_api_connection(api::APIConnection *api_connection)
void get_bluetooth_mac_address_pretty(std::span< char, 18 > output)
void bluetooth_set_connection_params(const api::BluetoothSetConnectionParamsRequest &msg)
void bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg)
void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg)
void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg)
Base class for all buttons.
Definition button.h:25
Abstract camera base class.
Definition camera.h:114
virtual CameraImageReader * create_image_reader()=0
Returns a new camera image reader that keeps track of the JPEG data in the camera image.
virtual void start_stream(CameraRequester requester)=0
virtual void stop_stream(CameraRequester requester)=0
virtual void request_image(CameraRequester requester)=0
static Camera * instance()
The singleton instance of the camera implementation.
Definition camera.cpp:18
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:187
Base class for all cover devices.
Definition cover.h:110
uint8_t get_last_event_type_index() const
Return index of last triggered event type, or max uint8_t if no event triggered yet.
Definition event.h:53
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:114
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
RadioFrequency - Base class for radio frequency implementations.
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
void set_timezone(const char *tz)
Set the time zone from a POSIX TZ string.
Base class for all valve devices.
Definition valve.h:103
void on_timer_event(const api::VoiceAssistantTimerEventResponse &msg)
void on_audio(const api::VoiceAssistantAudio &msg)
void client_subscription(api::APIConnection *client, bool subscribe)
void on_event(const api::VoiceAssistantEventResponse &msg)
void on_announce(const api::VoiceAssistantAnnounceRequest &msg)
api::APIConnection * get_api_connection() const
void on_set_configuration(const std::vector< std::string > &active_wake_words)
void zwave_proxy_request(api::APIConnection *api_connection, api::enums::ZWaveProxyRequestType type)
uint32_t get_feature_flags() const
Definition zwave_proxy.h:66
void send_frame(api::APIConnection *api_connection, const uint8_t *data, size_t length)
void api_connection_authenticated(api::APIConnection *conn)
const LogString * message
Definition component.cpp:35
uint16_t type
bool state
Definition fan.h:2
uint32_t socklen_t
Definition headers.h:99
const LogString * api_error_to_logstr(APIError err)
std::array< uint8_t, 32 > psk_t
BluetoothProxy * global_bluetooth_proxy
@ CLIMATE_SUPPORTS_CURRENT_HUMIDITY
@ CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE
ClimatePreset
Enum for all preset modes NOTE: If adding values, update ClimatePresetMask in climate_traits....
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
ClimateMode
Enum for all modes a climate device can be in.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:20
HomeassistantTime * global_homeassistant_time
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:49
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
ProvisioningManager * global_provisioning_manager
RadioFrequencyModulation
Modulation types supported by radio frequency implementations.
void set_global_tz(const ParsedTimezone &tz)
Set the global timezone used by epoch_to_local_tm() when called without a timezone.
Definition posix_tz.cpp:15
DSTRuleType
Type of DST transition rule.
Definition posix_tz.h:11
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_FAILED
Confirmed: driver or hardware error.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
VoiceAssistant * global_voice_assistant
@ WATER_HEATER_STATE_ON
Water heater is on (not in standby)
@ WATER_HEATER_STATE_AWAY
Away/vacation mode is currently active.
ZWaveProxy * global_zwave_proxy
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
const char int const __FlashStringHelper va_list args
Definition log.h:74
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:21
const void size_t len
Definition hal.h:64
std::vector< uint8_t > base64_decode(const std::string &encoded_string)
Decode a base64 string to a byte vector.
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:74
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1467
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint16_t day
Day of year (for JULIAN_NO_LEAP and DAY_OF_YEAR)
Definition posix_tz.h:21
DSTRuleType type
Type of rule.
Definition posix_tz.h:22
uint8_t week
Week 1-5, 5 = last (for MONTH_WEEK_DAY)
Definition posix_tz.h:24
int32_t time_seconds
Seconds after midnight (default 7200 = 2:00 AM)
Definition posix_tz.h:20
uint8_t day_of_week
Day 0-6, 0 = Sunday (for MONTH_WEEK_DAY)
Definition posix_tz.h:25
uint8_t month
Month 1-12 (for MONTH_WEEK_DAY)
Definition posix_tz.h:23
Parsed POSIX timezone information (packed for 32-bit: 32 bytes)
Definition posix_tz.h:29
DSTRule dst_end
When DST ends.
Definition posix_tz.h:33
DSTRule dst_start
When DST starts.
Definition posix_tz.h:32
int32_t dst_offset_seconds
DST offset from UTC in seconds.
Definition posix_tz.h:31
int32_t std_offset_seconds
Standard time offset from UTC in seconds (positive = west)
Definition posix_tz.h:30
uint32_t payload_size()
SemaphoreHandle_t lock
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:28