diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 5036899bf6..3d8563b1ae 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -55,8 +55,8 @@ class APIConnection final : public APIServerConnectionBase { void loop(); protected: - // Override read_message here (instead of in APIServerConnectionBase) so the - // compiler can devirtualize and inline on_* handler calls within this class. + // read_message_ is defined here (instead of in APIServerConnectionBase) so the + // compiler can devirtualize and inline on_* handler calls within this final class. void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data); // Auth helpers defined here (not in ProtoService) so the compiler can diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index d86cf912db..37d9e7d3cf 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -1,7 +1,9 @@ // This file was automatically generated with a tool. // See script/api_protobuf/api_protobuf.py #include "api_pb2_service.h" +#ifdef USE_API #include "api_connection.h" +#endif #include "esphome/core/log.h" namespace esphome::api { @@ -21,6 +23,7 @@ void APIServerConnectionBase::log_receive_message_(const LogString *name) { } #endif +#ifdef USE_API void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) { // Check authentication/connection requirements switch (msg_type) { @@ -706,5 +709,6 @@ void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const ui break; } } +#endif // USE_API } // namespace esphome::api diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 845714c833..cc1d1f1549 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -697,7 +697,7 @@ inline void ProtoLengthDelimited::decode_to_message(ProtoDecodableMessage &msg) template const char *proto_enum_to_string(T value); -// ProtoService removed — all methods moved to APIServerConnectionBase. -// APIConnection is the only concrete class; virtual dispatch was unnecessary. +// ProtoService removed — its methods were inlined into APIConnection. +// APIConnection is the concrete server-side implementation; the extra virtual layer was unnecessary. } // namespace esphome::api diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index a8c6c58da6..e4e0632542 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2947,7 +2947,9 @@ namespace esphome::api { cpp = FILE_HEADER cpp += """\ #include "api_pb2_service.h" +#ifdef USE_API #include "api_connection.h" +#endif #include "esphome/core/log.h" namespace esphome::api { @@ -3051,11 +3053,12 @@ static const char *const TAG = "api.service"; result += "#endif\n" return result - # Generate read_message as APIConnection method (not base class) so the compiler + # Generate read_message_ as APIConnection method (not base class) so the compiler # can devirtualize and inline the on_* handler calls within the same class. - # APIConnection declares the override in api_connection.h. + # APIConnection declares this method in api_connection.h. - out = "void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) {\n" + out = "#ifdef USE_API\n" + out += "void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) {\n" # Auth check block before dispatch switch out += " // Check authentication/connection requirements\n" @@ -3100,6 +3103,7 @@ static const char *const TAG = "api.service"; out += " break;\n" out += " }\n" out += "}\n" + out += "#endif // USE_API\n" cpp += out hpp += "};\n"