diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index ee88a99ca9..d7f784b50f 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -747,7 +747,10 @@ void HelloResponse::dump_to(std::string &out) const { dump_field(out, "name", this->name); } #ifdef USE_API_PASSWORD -void AuthenticationRequest::dump_to(std::string &out) const { dump_field(out, "password", this->password); } +void AuthenticationRequest::dump_to(std::string &out) const { + MessageDumpHelper helper(out, "AuthenticationRequest"); + dump_field(out, "password", this->password); +} void AuthenticationResponse::dump_to(std::string &out) const { MessageDumpHelper helper(out, "AuthenticationResponse"); dump_field(out, "invalid_password", this->invalid_password); @@ -1148,7 +1151,10 @@ void NoiseEncryptionSetKeyRequest::dump_to(std::string &out) const { out.append(format_hex_pretty(this->key, this->key_len)); out.append("\n"); } -void NoiseEncryptionSetKeyResponse::dump_to(std::string &out) const { dump_field(out, "success", this->success); } +void NoiseEncryptionSetKeyResponse::dump_to(std::string &out) const { + MessageDumpHelper helper(out, "NoiseEncryptionSetKeyResponse"); + dump_field(out, "success", this->success); +} #endif #ifdef USE_API_HOMEASSISTANT_SERVICES void SubscribeHomeassistantServicesRequest::dump_to(std::string &out) const { @@ -1738,7 +1744,10 @@ void BluetoothDeviceConnectionResponse::dump_to(std::string &out) const { dump_field(out, "mtu", this->mtu); dump_field(out, "error", this->error); } -void BluetoothGATTGetServicesRequest::dump_to(std::string &out) const { dump_field(out, "address", this->address); } +void BluetoothGATTGetServicesRequest::dump_to(std::string &out) const { + MessageDumpHelper helper(out, "BluetoothGATTGetServicesRequest"); + dump_field(out, "address", this->address); +} void BluetoothGATTDescriptor::dump_to(std::string &out) const { MessageDumpHelper helper(out, "BluetoothGATTDescriptor"); for (const auto &it : this->uuid) { @@ -1959,7 +1968,10 @@ void VoiceAssistantAnnounceRequest::dump_to(std::string &out) const { dump_field(out, "preannounce_media_id", this->preannounce_media_id); dump_field(out, "start_conversation", this->start_conversation); } -void VoiceAssistantAnnounceFinished::dump_to(std::string &out) const { dump_field(out, "success", this->success); } +void VoiceAssistantAnnounceFinished::dump_to(std::string &out) const { + MessageDumpHelper helper(out, "VoiceAssistantAnnounceFinished"); + dump_field(out, "success", this->success); +} void VoiceAssistantWakeWord::dump_to(std::string &out) const { MessageDumpHelper helper(out, "VoiceAssistantWakeWord"); dump_field(out, "id", this->id); diff --git a/esphome/components/api/custom_api_device.h b/esphome/components/api/custom_api_device.h index 7ff02512dc..d4a52a6923 100644 --- a/esphome/components/api/custom_api_device.h +++ b/esphome/components/api/custom_api_device.h @@ -220,7 +220,7 @@ class CustomAPIDevice { for (auto &it : data) { auto &kv = resp.data.emplace_back(); kv.key = StringRef(it.first); - kv.value = StringRef(it.second); + kv.value = it.second; // value is std::string (no_zero_copy), assign directly } global_api_server->send_homeassistant_action(resp); } @@ -263,7 +263,7 @@ class CustomAPIDevice { for (auto &it : data) { auto &kv = resp.data.emplace_back(); kv.key = StringRef(it.first); - kv.value = StringRef(it.second); + kv.value = it.second; // value is std::string (no_zero_copy), assign directly } global_api_server->send_homeassistant_action(resp); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index e5a245f94e..274a672c7c 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2151,12 +2151,10 @@ def build_message_type( # dump_to implementation will go in dump_cpp dump_impl = f"void {desc.name}::dump_to(std::string &out) const {{" if dump: - if len(dump) == 1 and len(dump[0]) + len(dump_impl) + 3 < 120: - dump_impl += f" {dump[0]} " - else: - dump_impl += "\n" - dump_impl += f' MessageDumpHelper helper(out, "{desc.name}");\n' - dump_impl += indent("\n".join(dump)) + "\n" + # Always use MessageDumpHelper for consistent output formatting + dump_impl += "\n" + dump_impl += f' MessageDumpHelper helper(out, "{desc.name}");\n' + dump_impl += indent("\n".join(dump)) + "\n" else: o2 = f'out.append("{desc.name} {{}}");' if len(dump_impl) + len(o2) + 3 < 120: