From c0c0a423626b66abc5e35dbe6c02e116c566328d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 Aug 2025 16:37:47 -1000 Subject: [PATCH] [api] Use static allocation for areas and devices in DeviceInfoResponse (#10038) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/api/api.proto | 4 ++-- esphome/components/api/api_connection.cpp | 12 ++++++++---- esphome/components/api/api_pb2.cpp | 12 ++++++++---- esphome/components/api/api_pb2.h | 6 +++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index e0b2c19a21..67e91cc8e3 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -250,8 +250,8 @@ message DeviceInfoResponse { // Supports receiving and saving api encryption key bool api_encryption_supported = 19 [(field_ifdef) = "USE_API_NOISE"]; - repeated DeviceInfo devices = 20 [(field_ifdef) = "USE_DEVICES"]; - repeated AreaInfo areas = 21 [(field_ifdef) = "USE_AREAS"]; + repeated DeviceInfo devices = 20 [(field_ifdef) = "USE_DEVICES", (fixed_array_size_define) = "ESPHOME_DEVICE_COUNT"]; + repeated AreaInfo areas = 21 [(field_ifdef) = "USE_AREAS", (fixed_array_size_define) = "ESPHOME_AREA_COUNT"]; // Top-level area info to phase out suggested_area AreaInfo area = 22 [(field_ifdef) = "USE_AREAS"]; diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 5fff270c99..cdeabb5cac 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1462,18 +1462,22 @@ bool APIConnection::send_device_info_response(const DeviceInfoRequest &msg) { resp.api_encryption_supported = true; #endif #ifdef USE_DEVICES + size_t device_index = 0; for (auto const &device : App.get_devices()) { - resp.devices.emplace_back(); - auto &device_info = resp.devices.back(); + if (device_index >= ESPHOME_DEVICE_COUNT) + break; + auto &device_info = resp.devices[device_index++]; device_info.device_id = device->get_device_id(); device_info.set_name(StringRef(device->get_name())); device_info.area_id = device->get_area_id(); } #endif #ifdef USE_AREAS + size_t area_index = 0; for (auto const &area : App.get_areas()) { - resp.areas.emplace_back(); - auto &area_info = resp.areas.back(); + if (area_index >= ESPHOME_AREA_COUNT) + break; + auto &area_info = resp.areas[area_index++]; area_info.area_id = area->get_area_id(); area_info.set_name(StringRef(area->get_name())); } diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 8c14153155..5dddc79b49 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -115,12 +115,12 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(19, this->api_encryption_supported); #endif #ifdef USE_DEVICES - for (auto &it : this->devices) { + for (const auto &it : this->devices) { buffer.encode_message(20, it, true); } #endif #ifdef USE_AREAS - for (auto &it : this->areas) { + for (const auto &it : this->areas) { buffer.encode_message(21, it, true); } #endif @@ -167,10 +167,14 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { size.add_bool(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES - size.add_repeated_message(2, this->devices); + for (const auto &it : this->devices) { + size.add_message_object_force(2, it); + } #endif #ifdef USE_AREAS - size.add_repeated_message(2, this->areas); + for (const auto &it : this->areas) { + size.add_message_object_force(2, it); + } #endif #ifdef USE_AREAS size.add_message_object(2, this->area); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 0bc75ef00b..d43d3c61b7 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -490,7 +490,7 @@ class DeviceInfo : public ProtoMessage { class DeviceInfoResponse : public ProtoMessage { public: static constexpr uint8_t MESSAGE_TYPE = 10; - static constexpr uint8_t ESTIMATED_SIZE = 211; + static constexpr uint8_t ESTIMATED_SIZE = 247; #ifdef HAS_PROTO_MESSAGE_DUMP const char *message_name() const override { return "device_info_response"; } #endif @@ -543,10 +543,10 @@ class DeviceInfoResponse : public ProtoMessage { bool api_encryption_supported{false}; #endif #ifdef USE_DEVICES - std::vector devices{}; + std::array devices{}; #endif #ifdef USE_AREAS - std::vector areas{}; + std::array areas{}; #endif #ifdef USE_AREAS AreaInfo area{};