mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[api] Use static allocation for areas and devices in DeviceInfoResponse
This commit is contained in:
		| @@ -250,8 +250,8 @@ message DeviceInfoResponse { | |||||||
|   // Supports receiving and saving api encryption key |   // Supports receiving and saving api encryption key | ||||||
|   bool api_encryption_supported = 19 [(field_ifdef) = "USE_API_NOISE"]; |   bool api_encryption_supported = 19 [(field_ifdef) = "USE_API_NOISE"]; | ||||||
|  |  | ||||||
|   repeated DeviceInfo devices = 20 [(field_ifdef) = "USE_DEVICES"]; |   repeated DeviceInfo devices = 20 [(field_ifdef) = "USE_DEVICES", (fixed_array_size_define) = "ESPHOME_DEVICE_COUNT"]; | ||||||
|   repeated AreaInfo areas = 21 [(field_ifdef) = "USE_AREAS"]; |   repeated AreaInfo areas = 21 [(field_ifdef) = "USE_AREAS", (fixed_array_size_define) = "ESPHOME_AREA_COUNT"]; | ||||||
|  |  | ||||||
|   // Top-level area info to phase out suggested_area |   // Top-level area info to phase out suggested_area | ||||||
|   AreaInfo area = 22 [(field_ifdef) = "USE_AREAS"]; |   AreaInfo area = 22 [(field_ifdef) = "USE_AREAS"]; | ||||||
|   | |||||||
| @@ -1462,18 +1462,22 @@ bool APIConnection::send_device_info_response(const DeviceInfoRequest &msg) { | |||||||
|   resp.api_encryption_supported = true; |   resp.api_encryption_supported = true; | ||||||
| #endif | #endif | ||||||
| #ifdef USE_DEVICES | #ifdef USE_DEVICES | ||||||
|  |   size_t device_index = 0; | ||||||
|   for (auto const &device : App.get_devices()) { |   for (auto const &device : App.get_devices()) { | ||||||
|     resp.devices.emplace_back(); |     if (device_index >= ESPHOME_DEVICE_COUNT) | ||||||
|     auto &device_info = resp.devices.back(); |       break; | ||||||
|  |     auto &device_info = resp.devices[device_index++]; | ||||||
|     device_info.device_id = device->get_device_id(); |     device_info.device_id = device->get_device_id(); | ||||||
|     device_info.set_name(StringRef(device->get_name())); |     device_info.set_name(StringRef(device->get_name())); | ||||||
|     device_info.area_id = device->get_area_id(); |     device_info.area_id = device->get_area_id(); | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|  |   size_t area_index = 0; | ||||||
|   for (auto const &area : App.get_areas()) { |   for (auto const &area : App.get_areas()) { | ||||||
|     resp.areas.emplace_back(); |     if (area_index >= ESPHOME_AREA_COUNT) | ||||||
|     auto &area_info = resp.areas.back(); |       break; | ||||||
|  |     auto &area_info = resp.areas[area_index++]; | ||||||
|     area_info.area_id = area->get_area_id(); |     area_info.area_id = area->get_area_id(); | ||||||
|     area_info.set_name(StringRef(area->get_name())); |     area_info.set_name(StringRef(area->get_name())); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -115,12 +115,12 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { | |||||||
|   buffer.encode_bool(19, this->api_encryption_supported); |   buffer.encode_bool(19, this->api_encryption_supported); | ||||||
| #endif | #endif | ||||||
| #ifdef USE_DEVICES | #ifdef USE_DEVICES | ||||||
|   for (auto &it : this->devices) { |   for (const auto &it : this->devices) { | ||||||
|     buffer.encode_message(20, it, true); |     buffer.encode_message(20, it, true); | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|   for (auto &it : this->areas) { |   for (const auto &it : this->areas) { | ||||||
|     buffer.encode_message(21, it, true); |     buffer.encode_message(21, it, true); | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
| @@ -167,10 +167,14 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { | |||||||
|   size.add_bool(2, this->api_encryption_supported); |   size.add_bool(2, this->api_encryption_supported); | ||||||
| #endif | #endif | ||||||
| #ifdef USE_DEVICES | #ifdef USE_DEVICES | ||||||
|   size.add_repeated_message(2, this->devices); |   for (const auto &it : this->devices) { | ||||||
|  |     size.add_message_object_force(2, it); | ||||||
|  |   } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|   size.add_repeated_message(2, this->areas); |   for (const auto &it : this->areas) { | ||||||
|  |     size.add_message_object_force(2, it); | ||||||
|  |   } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|   size.add_message_object(2, this->area); |   size.add_message_object(2, this->area); | ||||||
|   | |||||||
| @@ -490,7 +490,7 @@ class DeviceInfo : public ProtoMessage { | |||||||
| class DeviceInfoResponse : public ProtoMessage { | class DeviceInfoResponse : public ProtoMessage { | ||||||
|  public: |  public: | ||||||
|   static constexpr uint8_t MESSAGE_TYPE = 10; |   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 | #ifdef HAS_PROTO_MESSAGE_DUMP | ||||||
|   const char *message_name() const override { return "device_info_response"; } |   const char *message_name() const override { return "device_info_response"; } | ||||||
| #endif | #endif | ||||||
| @@ -543,10 +543,10 @@ class DeviceInfoResponse : public ProtoMessage { | |||||||
|   bool api_encryption_supported{false}; |   bool api_encryption_supported{false}; | ||||||
| #endif | #endif | ||||||
| #ifdef USE_DEVICES | #ifdef USE_DEVICES | ||||||
|   std::vector<DeviceInfo> devices{}; |   std::array<DeviceInfo, ESPHOME_DEVICE_COUNT> devices{}; | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|   std::vector<AreaInfo> areas{}; |   std::array<AreaInfo, ESPHOME_AREA_COUNT> areas{}; | ||||||
| #endif | #endif | ||||||
| #ifdef USE_AREAS | #ifdef USE_AREAS | ||||||
|   AreaInfo area{}; |   AreaInfo area{}; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user