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