[api] Make api a buildable cpp unit test target

Three fixes so 'script/cpp_unit_test.py api' actually compiles instead
of crashing in build setup:

1. script/build_helpers.py: when adding transitive component
   dependencies to the post-validation config, use {} (dict) instead
   of [] (list) for non-MULTI_CONF components. socket's
   FILTER_SOURCE_FILES (and any other code that subscripts
   CORE.config[component] with a string key) was crashing because
   socket got config = [] from setdefault.

2. esphome/components/api/api_pb2_service.cpp + the codegen in
   script/api_protobuf/api_protobuf.py: wrap the generated
   APIConnection::read_message_ definition in #ifdef USE_API. The
   class itself is only declared inside #ifdef USE_API in
   api_connection.h, so without the guard the .cpp fails to compile
   in any build that pulls in the api source files without setting
   USE_API (e.g. cpp unit tests of api dependencies).
This commit is contained in:
J. Nick Koston
2026-04-25 04:41:17 -05:00
parent 68ffd3b221
commit 45525c8a82
4 changed files with 17 additions and 11 deletions
+7 -1
View File
@@ -3577,8 +3577,13 @@ static const char *const TAG = "api.service";
# 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 this method in api_connection.h.
# Guard with #ifdef USE_API since APIConnection itself is only defined when
# USE_API is set; without this, builds that compile this .cpp without
# USE_API (e.g. C++ unit tests for api dependencies) fail to find the
# class declaration.
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"
@@ -3623,6 +3628,7 @@ static const char *const TAG = "api.service";
out += " break;\n"
out += " }\n"
out += "}\n"
out += "#endif // USE_API\n"
cpp += out
hpp += "};\n"