From c74f12be989739c6229c27c24a8148913bcf9c62 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 23 Jul 2025 21:15:42 -1000 Subject: [PATCH] [api] Use C++17 nested namespace syntax (#9856) --- esphome/components/api/api_connection.cpp | 6 ++-- esphome/components/api/api_connection.h | 6 ++-- esphome/components/api/api_frame_helper.cpp | 6 ++-- esphome/components/api/api_frame_helper.h | 6 ++-- .../components/api/api_frame_helper_noise.cpp | 6 ++-- .../components/api/api_frame_helper_noise.h | 6 ++-- .../api/api_frame_helper_plaintext.cpp | 6 ++-- .../api/api_frame_helper_plaintext.h | 6 ++-- esphome/components/api/api_noise_context.h | 6 ++-- esphome/components/api/api_pb2.cpp | 6 ++-- esphome/components/api/api_pb2.h | 6 ++-- esphome/components/api/api_pb2_dump.cpp | 6 ++-- esphome/components/api/api_pb2_service.cpp | 6 ++-- esphome/components/api/api_pb2_service.h | 6 ++-- esphome/components/api/api_server.cpp | 6 ++-- esphome/components/api/api_server.h | 6 ++-- esphome/components/api/custom_api_device.h | 6 ++-- .../components/api/homeassistant_service.h | 6 ++-- esphome/components/api/list_entities.cpp | 6 ++-- esphome/components/api/list_entities.h | 6 ++-- esphome/components/api/proto.cpp | 6 ++-- esphome/components/api/proto.h | 6 ++-- esphome/components/api/subscribe_state.cpp | 6 ++-- esphome/components/api/subscribe_state.h | 6 ++-- esphome/components/api/user_services.cpp | 6 ++-- esphome/components/api/user_services.h | 6 ++-- script/api_protobuf/api_protobuf.py | 30 +++++++------------ 27 files changed, 62 insertions(+), 124 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 60dc0a113d..76713c54c8 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -31,8 +31,7 @@ #include "esphome/components/voice_assistant/voice_assistant.h" #endif -namespace esphome { -namespace api { +namespace esphome::api { // Read a maximum of 5 messages per loop iteration to prevent starving other components. // This is a balance between API responsiveness and allowing other components to run. @@ -1837,6 +1836,5 @@ uint16_t APIConnection::try_send_ping_request(EntityBase *entity, APIConnection return encode_message_to_buffer(req, PingRequest::MESSAGE_TYPE, conn, remaining_size, is_single); } -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 5365a48292..6214d5ba82 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -13,8 +13,7 @@ #include #include -namespace esphome { -namespace api { +namespace esphome::api { // Client information structure struct ClientInfo { @@ -723,6 +722,5 @@ class APIConnection : public APIServerConnection { } }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index b1c9478e59..6ca38e80ed 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -9,8 +9,7 @@ #include #include -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.frame_helper"; @@ -247,6 +246,5 @@ APIError APIFrameHelper::handle_socket_read_result_(ssize_t received) { return APIError::OK; } -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 231a3366ce..76dfe1366c 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -12,8 +12,7 @@ #include "esphome/core/application.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { // uncomment to log raw packets //#define HELPER_LOG_PACKETS @@ -184,7 +183,6 @@ class APIFrameHelper { APIError handle_socket_read_result_(ssize_t received); }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API diff --git a/esphome/components/api/api_frame_helper_noise.cpp b/esphome/components/api/api_frame_helper_noise.cpp index dcb9de9c93..35d1715931 100644 --- a/esphome/components/api/api_frame_helper_noise.cpp +++ b/esphome/components/api/api_frame_helper_noise.cpp @@ -10,8 +10,7 @@ #include #include -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.noise"; static const char *const PROLOGUE_INIT = "NoiseAPIInit"; @@ -579,7 +578,6 @@ void noise_rand_bytes(void *output, size_t len) { } } -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API_NOISE #endif // USE_API diff --git a/esphome/components/api/api_frame_helper_noise.h b/esphome/components/api/api_frame_helper_noise.h index ed5141d625..e82e5daadb 100644 --- a/esphome/components/api/api_frame_helper_noise.h +++ b/esphome/components/api/api_frame_helper_noise.h @@ -5,8 +5,7 @@ #include "noise/protocol.h" #include "api_noise_context.h" -namespace esphome { -namespace api { +namespace esphome::api { class APINoiseFrameHelper : public APIFrameHelper { public: @@ -64,7 +63,6 @@ class APINoiseFrameHelper : public APIFrameHelper { // 4 bytes total, no padding }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API_NOISE #endif // USE_API diff --git a/esphome/components/api/api_frame_helper_plaintext.cpp b/esphome/components/api/api_frame_helper_plaintext.cpp index d0bc631e1b..fdaacbd94e 100644 --- a/esphome/components/api/api_frame_helper_plaintext.cpp +++ b/esphome/components/api/api_frame_helper_plaintext.cpp @@ -10,8 +10,7 @@ #include #include -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.plaintext"; @@ -286,7 +285,6 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer return write_raw_(this->reusable_iovs_.data(), this->reusable_iovs_.size(), total_write_len); } -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API_PLAINTEXT #endif // USE_API diff --git a/esphome/components/api/api_frame_helper_plaintext.h b/esphome/components/api/api_frame_helper_plaintext.h index 465ceae827..b50902dd75 100644 --- a/esphome/components/api/api_frame_helper_plaintext.h +++ b/esphome/components/api/api_frame_helper_plaintext.h @@ -3,8 +3,7 @@ #ifdef USE_API #ifdef USE_API_PLAINTEXT -namespace esphome { -namespace api { +namespace esphome::api { class APIPlaintextFrameHelper : public APIFrameHelper { public: @@ -49,7 +48,6 @@ class APIPlaintextFrameHelper : public APIFrameHelper { // 8 bytes total, no padding needed }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API_PLAINTEXT #endif // USE_API diff --git a/esphome/components/api/api_noise_context.h b/esphome/components/api/api_noise_context.h index fa4435e570..b5f7016689 100644 --- a/esphome/components/api/api_noise_context.h +++ b/esphome/components/api/api_noise_context.h @@ -3,8 +3,7 @@ #include #include "esphome/core/defines.h" -namespace esphome { -namespace api { +namespace esphome::api { #ifdef USE_API_NOISE using psk_t = std::array; @@ -28,5 +27,4 @@ class APINoiseContext { }; #endif // USE_API_NOISE -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index d51f641746..6d2e17dc27 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -5,8 +5,7 @@ #include "esphome/core/helpers.h" #include -namespace esphome { -namespace api { +namespace esphome::api { bool HelloRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2981,5 +2980,4 @@ bool UpdateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { } #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 9bf50fd18b..91a285fc6c 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -7,8 +7,7 @@ #include "proto.h" -namespace esphome { -namespace api { +namespace esphome::api { namespace enums { @@ -2891,5 +2890,4 @@ class UpdateCommandRequest : public CommandProtoMessage { }; #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index a2e69255e1..5db9b79cfa 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -7,8 +7,7 @@ #ifdef HAS_PROTO_MESSAGE_DUMP -namespace esphome { -namespace api { +namespace esphome::api { // Helper function to append a quoted string, handling empty StringRef static inline void append_quoted_string(std::string &out, const StringRef &ref) { @@ -2067,7 +2066,6 @@ void UpdateCommandRequest::dump_to(std::string &out) const { } #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index d8ff4fcd24..d7d302a238 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -3,8 +3,7 @@ #include "api_pb2_service.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.service"; @@ -901,5 +900,4 @@ void APIServerConnection::on_alarm_control_panel_command_request(const AlarmCont } #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index f06ebdf9d5..38008197fa 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -6,8 +6,7 @@ #include "api_pb2.h" -namespace esphome { -namespace api { +namespace esphome::api { class APIServerConnectionBase : public ProtoService { public: @@ -444,5 +443,4 @@ class APIServerConnection : public APIServerConnectionBase { #endif }; -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 88966089cc..6d1729e611 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -16,8 +16,7 @@ #include -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api"; @@ -483,6 +482,5 @@ bool APIServer::teardown() { return this->clients_.empty(); } -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index edbd289421..54663a013f 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -18,8 +18,7 @@ #include -namespace esphome { -namespace api { +namespace esphome::api { #ifdef USE_API_NOISE struct SavedNoisePsk { @@ -196,6 +195,5 @@ template class APIConnectedCondition : public Condition { bool check(Ts... x) override { return global_api_server->is_connected(); } }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/custom_api_device.h b/esphome/components/api/custom_api_device.h index 6375dbfb9f..73c7804ff3 100644 --- a/esphome/components/api/custom_api_device.h +++ b/esphome/components/api/custom_api_device.h @@ -6,8 +6,7 @@ #ifdef USE_API_SERVICES #include "user_services.h" #endif -namespace esphome { -namespace api { +namespace esphome::api { #ifdef USE_API_SERVICES template class CustomAPIDeviceService : public UserServiceBase { @@ -222,6 +221,5 @@ class CustomAPIDevice { } }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index d91c1ab287..212b3b22d6 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -7,8 +7,7 @@ #include "esphome/core/helpers.h" #include -namespace esphome { -namespace api { +namespace esphome::api { template class TemplatableStringValue : public TemplatableValue { private: @@ -99,6 +98,5 @@ template class HomeAssistantServiceCallAction : public Action> variables_; }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/list_entities.cpp b/esphome/components/api/list_entities.cpp index 809c658803..da4800a45e 100644 --- a/esphome/components/api/list_entities.cpp +++ b/esphome/components/api/list_entities.cpp @@ -6,8 +6,7 @@ #include "esphome/core/log.h" #include "esphome/core/util.h" -namespace esphome { -namespace api { +namespace esphome::api { // Generate entity handler implementations using macros #ifdef USE_BINARY_SENSOR @@ -90,6 +89,5 @@ bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) { } #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/list_entities.h b/esphome/components/api/list_entities.h index b4cbf6c489..769d7b9b6e 100644 --- a/esphome/components/api/list_entities.h +++ b/esphome/components/api/list_entities.h @@ -4,8 +4,7 @@ #ifdef USE_API #include "esphome/core/component.h" #include "esphome/core/component_iterator.h" -namespace esphome { -namespace api { +namespace esphome::api { class APIConnection; @@ -96,6 +95,5 @@ class ListEntitiesIterator : public ComponentIterator { APIConnection *client_; }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/proto.cpp b/esphome/components/api/proto.cpp index bf64d5f723..cb6c07ec3c 100644 --- a/esphome/components/api/proto.cpp +++ b/esphome/components/api/proto.cpp @@ -3,8 +3,7 @@ #include "esphome/core/helpers.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.proto"; @@ -89,5 +88,4 @@ std::string ProtoMessage::dump() const { } #endif -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 3e59ee1541..44f9716516 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -13,8 +13,7 @@ #define HAS_PROTO_MESSAGE_DUMP #endif -namespace esphome { -namespace api { +namespace esphome::api { /* * StringRef Ownership Model for API Protocol Messages @@ -910,5 +909,4 @@ class ProtoService { } }; -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/subscribe_state.cpp b/esphome/components/api/subscribe_state.cpp index 12accf4613..3a563f2221 100644 --- a/esphome/components/api/subscribe_state.cpp +++ b/esphome/components/api/subscribe_state.cpp @@ -3,8 +3,7 @@ #include "api_connection.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { // Generate entity handler implementations using macros #ifdef USE_BINARY_SENSOR @@ -69,6 +68,5 @@ INITIAL_STATE_HANDLER(update, update::UpdateEntity) InitialStateIterator::InitialStateIterator(APIConnection *client) : client_(client) {} -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/subscribe_state.h b/esphome/components/api/subscribe_state.h index 2b7b508056..2c22c322ec 100644 --- a/esphome/components/api/subscribe_state.h +++ b/esphome/components/api/subscribe_state.h @@ -5,8 +5,7 @@ #include "esphome/core/component.h" #include "esphome/core/component_iterator.h" #include "esphome/core/controller.h" -namespace esphome { -namespace api { +namespace esphome::api { class APIConnection; @@ -89,6 +88,5 @@ class InitialStateIterator : public ComponentIterator { APIConnection *client_; }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif diff --git a/esphome/components/api/user_services.cpp b/esphome/components/api/user_services.cpp index 7e73722a92..27b30eb332 100644 --- a/esphome/components/api/user_services.cpp +++ b/esphome/components/api/user_services.cpp @@ -1,8 +1,7 @@ #include "user_services.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { template<> bool get_execute_arg_value(const ExecuteServiceArgument &arg) { return arg.bool_; } template<> int32_t get_execute_arg_value(const ExecuteServiceArgument &arg) { @@ -40,5 +39,4 @@ template<> enums::ServiceArgType to_service_arg_type>() return enums::SERVICE_ARG_TYPE_STRING_ARRAY; } -} // namespace api -} // namespace esphome +} // namespace esphome::api diff --git a/esphome/components/api/user_services.h b/esphome/components/api/user_services.h index deec636cae..5f040e8433 100644 --- a/esphome/components/api/user_services.h +++ b/esphome/components/api/user_services.h @@ -8,8 +8,7 @@ #include "api_pb2.h" #ifdef USE_API_SERVICES -namespace esphome { -namespace api { +namespace esphome::api { class UserServiceDescriptor { public: @@ -74,6 +73,5 @@ template class UserServiceTrigger : public UserServiceBasetrigger(x...); } // NOLINT }; -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // USE_API_SERVICES diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 635291371e..424565a893 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2045,8 +2045,7 @@ def main() -> None: #include "proto.h" -namespace esphome { -namespace api { +namespace esphome::api { """ @@ -2057,8 +2056,7 @@ namespace api { #include "esphome/core/helpers.h" #include -namespace esphome { -namespace api { +namespace esphome::api { """ @@ -2072,8 +2070,7 @@ namespace api { #ifdef HAS_PROTO_MESSAGE_DUMP -namespace esphome { -namespace api { +namespace esphome::api { // Helper function to append a quoted string, handling empty StringRef static inline void append_quoted_string(std::string &out, const StringRef &ref) { @@ -2265,19 +2262,16 @@ static void dump_field(std::string &out, const char *field_name, T value, int in content += """\ -} // namespace api -} // namespace esphome +} // namespace esphome::api """ cpp += """\ -} // namespace api -} // namespace esphome +} // namespace esphome::api """ dump_cpp += """\ -} // namespace api -} // namespace esphome +} // namespace esphome::api #endif // HAS_PROTO_MESSAGE_DUMP """ @@ -2299,8 +2293,7 @@ static void dump_field(std::string &out, const char *field_name, T value, int in #include "api_pb2.h" -namespace esphome { -namespace api { +namespace esphome::api { """ @@ -2309,8 +2302,7 @@ namespace api { #include "api_pb2_service.h" #include "esphome/core/log.h" -namespace esphome { -namespace api { +namespace esphome::api { static const char *const TAG = "api.service"; @@ -2451,13 +2443,11 @@ static const char *const TAG = "api.service"; hpp += """\ -} // namespace api -} // namespace esphome +} // namespace esphome::api """ cpp += """\ -} // namespace api -} // namespace esphome +} // namespace esphome::api """ with open(root / "api_pb2_service.h", "w", encoding="utf-8") as f: