From bad165c804b4b8acd35b8f779f54d2448908acd4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Jun 2025 08:14:03 +0100 Subject: [PATCH] fix gen --- esphome/components/api/api_pb2_service.cpp | 6 ++++++ esphome/components/api/api_pb2_service.h | 9 ++++++++- script/api_protobuf/api_protobuf.py | 16 +++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index 7245bc2ef6..1eea8a9c9e 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -8,6 +8,12 @@ namespace api { static const char *const TAG = "api.service"; +#ifdef HAS_PROTO_MESSAGE_DUMP +void APIServerConnectionBase::log_send_message_(const char *name, const std::string &dump) { + ESP_LOGVV(TAG, "send_message %s: %s", name, dump.c_str()); +} +#endif + #ifdef USE_COVER #endif #ifdef USE_FAN diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index fe38bff31a..c927cfa9b7 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -10,9 +10,16 @@ namespace api { class APIServerConnectionBase : public ProtoService { public: +#ifdef HAS_PROTO_MESSAGE_DUMP + protected: + void log_send_message_(const char *name, const std::string &dump); + + public: +#endif + template bool send_message(const T &msg) { #ifdef HAS_PROTO_MESSAGE_DUMP - ESP_LOGVV(TAG, "send_message %s: %s", T::message_name(), msg.dump().c_str()); + this->log_send_message_(T::message_name(), msg.dump()); #endif return this->send_message_(msg, T::message_type); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 57279c4aee..f40ed7b80c 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1094,15 +1094,29 @@ def main() -> None: hpp += f"class {class_name} : public ProtoService {{\n" hpp += " public:\n" + # Add logging helper method declaration + hpp += "#ifdef HAS_PROTO_MESSAGE_DUMP\n" + hpp += " protected:\n" + hpp += " void log_send_message_(const char *name, const std::string &dump);\n" + hpp += " public:\n" + hpp += "#endif\n\n" + # Add generic send_message method hpp += " template\n" hpp += " bool send_message(const T &msg) {\n" hpp += "#ifdef HAS_PROTO_MESSAGE_DUMP\n" - hpp += ' ESP_LOGVV(TAG, "send_message %s: %s", T::message_name(), msg.dump().c_str());\n' + hpp += " this->log_send_message_(T::message_name(), msg.dump());\n" hpp += "#endif\n" hpp += " return this->send_message_(msg, T::message_type);\n" hpp += " }\n\n" + # Add logging helper method implementation to cpp + cpp += "#ifdef HAS_PROTO_MESSAGE_DUMP\n" + cpp += f"void {class_name}::log_send_message_(const char *name, const std::string &dump) {{\n" + cpp += ' ESP_LOGVV(TAG, "send_message %s: %s", name, dump.c_str());\n' + cpp += "}\n" + cpp += "#endif\n\n" + for mt in file.message_type: obj = build_service_message_type(mt) if obj is None: