[api] Use C++17 nested namespace syntax (#9856)

This commit is contained in:
J. Nick Koston 2025-07-23 21:15:42 -10:00 committed by GitHub
parent 705ea4ebaa
commit c74f12be98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 62 additions and 124 deletions

View File

@ -31,8 +31,7 @@
#include "esphome/components/voice_assistant/voice_assistant.h" #include "esphome/components/voice_assistant/voice_assistant.h"
#endif #endif
namespace esphome { namespace esphome::api {
namespace api {
// Read a maximum of 5 messages per loop iteration to prevent starving other components. // 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. // 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); return encode_message_to_buffer(req, PingRequest::MESSAGE_TYPE, conn, remaining_size, is_single);
} }
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -13,8 +13,7 @@
#include <vector> #include <vector>
#include <functional> #include <functional>
namespace esphome { namespace esphome::api {
namespace api {
// Client information structure // Client information structure
struct ClientInfo { struct ClientInfo {
@ -723,6 +722,5 @@ class APIConnection : public APIServerConnection {
} }
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -9,8 +9,7 @@
#include <cstring> #include <cstring>
#include <cinttypes> #include <cinttypes>
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.frame_helper"; static const char *const TAG = "api.frame_helper";
@ -247,6 +246,5 @@ APIError APIFrameHelper::handle_socket_read_result_(ssize_t received) {
return APIError::OK; return APIError::OK;
} }
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -12,8 +12,7 @@
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
// uncomment to log raw packets // uncomment to log raw packets
//#define HELPER_LOG_PACKETS //#define HELPER_LOG_PACKETS
@ -184,7 +183,6 @@ class APIFrameHelper {
APIError handle_socket_read_result_(ssize_t received); APIError handle_socket_read_result_(ssize_t received);
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API #endif // USE_API

View File

@ -10,8 +10,7 @@
#include <cstring> #include <cstring>
#include <cinttypes> #include <cinttypes>
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.noise"; static const char *const TAG = "api.noise";
static const char *const PROLOGUE_INIT = "NoiseAPIInit"; static const char *const PROLOGUE_INIT = "NoiseAPIInit";
@ -579,7 +578,6 @@ void noise_rand_bytes(void *output, size_t len) {
} }
} }
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API_NOISE #endif // USE_API_NOISE
#endif // USE_API #endif // USE_API

View File

@ -5,8 +5,7 @@
#include "noise/protocol.h" #include "noise/protocol.h"
#include "api_noise_context.h" #include "api_noise_context.h"
namespace esphome { namespace esphome::api {
namespace api {
class APINoiseFrameHelper : public APIFrameHelper { class APINoiseFrameHelper : public APIFrameHelper {
public: public:
@ -64,7 +63,6 @@ class APINoiseFrameHelper : public APIFrameHelper {
// 4 bytes total, no padding // 4 bytes total, no padding
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API_NOISE #endif // USE_API_NOISE
#endif // USE_API #endif // USE_API

View File

@ -10,8 +10,7 @@
#include <cstring> #include <cstring>
#include <cinttypes> #include <cinttypes>
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.plaintext"; 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); return write_raw_(this->reusable_iovs_.data(), this->reusable_iovs_.size(), total_write_len);
} }
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API_PLAINTEXT #endif // USE_API_PLAINTEXT
#endif // USE_API #endif // USE_API

View File

@ -3,8 +3,7 @@
#ifdef USE_API #ifdef USE_API
#ifdef USE_API_PLAINTEXT #ifdef USE_API_PLAINTEXT
namespace esphome { namespace esphome::api {
namespace api {
class APIPlaintextFrameHelper : public APIFrameHelper { class APIPlaintextFrameHelper : public APIFrameHelper {
public: public:
@ -49,7 +48,6 @@ class APIPlaintextFrameHelper : public APIFrameHelper {
// 8 bytes total, no padding needed // 8 bytes total, no padding needed
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API_PLAINTEXT #endif // USE_API_PLAINTEXT
#endif // USE_API #endif // USE_API

View File

@ -3,8 +3,7 @@
#include <cstdint> #include <cstdint>
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
namespace esphome { namespace esphome::api {
namespace api {
#ifdef USE_API_NOISE #ifdef USE_API_NOISE
using psk_t = std::array<uint8_t, 32>; using psk_t = std::array<uint8_t, 32>;
@ -28,5 +27,4 @@ class APINoiseContext {
}; };
#endif // USE_API_NOISE #endif // USE_API_NOISE
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -5,8 +5,7 @@
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include <cstring> #include <cstring>
namespace esphome { namespace esphome::api {
namespace api {
bool HelloRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { bool HelloRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
switch (field_id) { switch (field_id) {
@ -2981,5 +2980,4 @@ bool UpdateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
} }
#endif #endif
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -7,8 +7,7 @@
#include "proto.h" #include "proto.h"
namespace esphome { namespace esphome::api {
namespace api {
namespace enums { namespace enums {
@ -2891,5 +2890,4 @@ class UpdateCommandRequest : public CommandProtoMessage {
}; };
#endif #endif
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -7,8 +7,7 @@
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
namespace esphome { namespace esphome::api {
namespace api {
// Helper function to append a quoted string, handling empty StringRef // Helper function to append a quoted string, handling empty StringRef
static inline void append_quoted_string(std::string &out, const StringRef &ref) { 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 #endif
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // HAS_PROTO_MESSAGE_DUMP #endif // HAS_PROTO_MESSAGE_DUMP

View File

@ -3,8 +3,7 @@
#include "api_pb2_service.h" #include "api_pb2_service.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.service"; static const char *const TAG = "api.service";
@ -901,5 +900,4 @@ void APIServerConnection::on_alarm_control_panel_command_request(const AlarmCont
} }
#endif #endif
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -6,8 +6,7 @@
#include "api_pb2.h" #include "api_pb2.h"
namespace esphome { namespace esphome::api {
namespace api {
class APIServerConnectionBase : public ProtoService { class APIServerConnectionBase : public ProtoService {
public: public:
@ -444,5 +443,4 @@ class APIServerConnection : public APIServerConnectionBase {
#endif #endif
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -16,8 +16,7 @@
#include <algorithm> #include <algorithm>
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api"; static const char *const TAG = "api";
@ -483,6 +482,5 @@ bool APIServer::teardown() {
return this->clients_.empty(); return this->clients_.empty();
} }
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -18,8 +18,7 @@
#include <vector> #include <vector>
namespace esphome { namespace esphome::api {
namespace api {
#ifdef USE_API_NOISE #ifdef USE_API_NOISE
struct SavedNoisePsk { struct SavedNoisePsk {
@ -196,6 +195,5 @@ template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
bool check(Ts... x) override { return global_api_server->is_connected(); } bool check(Ts... x) override { return global_api_server->is_connected(); }
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -6,8 +6,7 @@
#ifdef USE_API_SERVICES #ifdef USE_API_SERVICES
#include "user_services.h" #include "user_services.h"
#endif #endif
namespace esphome { namespace esphome::api {
namespace api {
#ifdef USE_API_SERVICES #ifdef USE_API_SERVICES
template<typename T, typename... Ts> class CustomAPIDeviceService : public UserServiceBase<Ts...> { template<typename T, typename... Ts> class CustomAPIDeviceService : public UserServiceBase<Ts...> {
@ -222,6 +221,5 @@ class CustomAPIDevice {
} }
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -7,8 +7,7 @@
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include <vector> #include <vector>
namespace esphome { namespace esphome::api {
namespace api {
template<typename... X> class TemplatableStringValue : public TemplatableValue<std::string, X...> { template<typename... X> class TemplatableStringValue : public TemplatableValue<std::string, X...> {
private: private:
@ -99,6 +98,5 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
std::vector<TemplatableKeyValuePair<Ts...>> variables_; std::vector<TemplatableKeyValuePair<Ts...>> variables_;
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -6,8 +6,7 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/util.h" #include "esphome/core/util.h"
namespace esphome { namespace esphome::api {
namespace api {
// Generate entity handler implementations using macros // Generate entity handler implementations using macros
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
@ -90,6 +89,5 @@ bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) {
} }
#endif #endif
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -4,8 +4,7 @@
#ifdef USE_API #ifdef USE_API
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/component_iterator.h" #include "esphome/core/component_iterator.h"
namespace esphome { namespace esphome::api {
namespace api {
class APIConnection; class APIConnection;
@ -96,6 +95,5 @@ class ListEntitiesIterator : public ComponentIterator {
APIConnection *client_; APIConnection *client_;
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -3,8 +3,7 @@
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.proto"; static const char *const TAG = "api.proto";
@ -89,5 +88,4 @@ std::string ProtoMessage::dump() const {
} }
#endif #endif
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -13,8 +13,7 @@
#define HAS_PROTO_MESSAGE_DUMP #define HAS_PROTO_MESSAGE_DUMP
#endif #endif
namespace esphome { namespace esphome::api {
namespace api {
/* /*
* StringRef Ownership Model for API Protocol Messages * StringRef Ownership Model for API Protocol Messages
@ -910,5 +909,4 @@ class ProtoService {
} }
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -3,8 +3,7 @@
#include "api_connection.h" #include "api_connection.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
// Generate entity handler implementations using macros // Generate entity handler implementations using macros
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
@ -69,6 +68,5 @@ INITIAL_STATE_HANDLER(update, update::UpdateEntity)
InitialStateIterator::InitialStateIterator(APIConnection *client) : client_(client) {} InitialStateIterator::InitialStateIterator(APIConnection *client) : client_(client) {}
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -5,8 +5,7 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/component_iterator.h" #include "esphome/core/component_iterator.h"
#include "esphome/core/controller.h" #include "esphome/core/controller.h"
namespace esphome { namespace esphome::api {
namespace api {
class APIConnection; class APIConnection;
@ -89,6 +88,5 @@ class InitialStateIterator : public ComponentIterator {
APIConnection *client_; APIConnection *client_;
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif #endif

View File

@ -1,8 +1,7 @@
#include "user_services.h" #include "user_services.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
template<> bool get_execute_arg_value<bool>(const ExecuteServiceArgument &arg) { return arg.bool_; } template<> bool get_execute_arg_value<bool>(const ExecuteServiceArgument &arg) { return arg.bool_; }
template<> int32_t get_execute_arg_value<int32_t>(const ExecuteServiceArgument &arg) { template<> int32_t get_execute_arg_value<int32_t>(const ExecuteServiceArgument &arg) {
@ -40,5 +39,4 @@ template<> enums::ServiceArgType to_service_arg_type<std::vector<std::string>>()
return enums::SERVICE_ARG_TYPE_STRING_ARRAY; return enums::SERVICE_ARG_TYPE_STRING_ARRAY;
} }
} // namespace api } // namespace esphome::api
} // namespace esphome

View File

@ -8,8 +8,7 @@
#include "api_pb2.h" #include "api_pb2.h"
#ifdef USE_API_SERVICES #ifdef USE_API_SERVICES
namespace esphome { namespace esphome::api {
namespace api {
class UserServiceDescriptor { class UserServiceDescriptor {
public: public:
@ -74,6 +73,5 @@ template<typename... Ts> class UserServiceTrigger : public UserServiceBase<Ts...
void execute(Ts... x) override { this->trigger(x...); } // NOLINT void execute(Ts... x) override { this->trigger(x...); } // NOLINT
}; };
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // USE_API_SERVICES #endif // USE_API_SERVICES

View File

@ -2045,8 +2045,7 @@ def main() -> None:
#include "proto.h" #include "proto.h"
namespace esphome { namespace esphome::api {
namespace api {
""" """
@ -2057,8 +2056,7 @@ namespace api {
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include <cstring> #include <cstring>
namespace esphome { namespace esphome::api {
namespace api {
""" """
@ -2072,8 +2070,7 @@ namespace api {
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
namespace esphome { namespace esphome::api {
namespace api {
// Helper function to append a quoted string, handling empty StringRef // Helper function to append a quoted string, handling empty StringRef
static inline void append_quoted_string(std::string &out, const StringRef &ref) { 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 += """\ content += """\
} // namespace api } // namespace esphome::api
} // namespace esphome
""" """
cpp += """\ cpp += """\
} // namespace api } // namespace esphome::api
} // namespace esphome
""" """
dump_cpp += """\ dump_cpp += """\
} // namespace api } // namespace esphome::api
} // namespace esphome
#endif // HAS_PROTO_MESSAGE_DUMP #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" #include "api_pb2.h"
namespace esphome { namespace esphome::api {
namespace api {
""" """
@ -2309,8 +2302,7 @@ namespace api {
#include "api_pb2_service.h" #include "api_pb2_service.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome::api {
namespace api {
static const char *const TAG = "api.service"; static const char *const TAG = "api.service";
@ -2451,13 +2443,11 @@ static const char *const TAG = "api.service";
hpp += """\ hpp += """\
} // namespace api } // namespace esphome::api
} // namespace esphome
""" """
cpp += """\ cpp += """\
} // namespace api } // namespace esphome::api
} // namespace esphome
""" """
with open(root / "api_pb2_service.h", "w", encoding="utf-8") as f: with open(root / "api_pb2_service.h", "w", encoding="utf-8") as f: