From 72fd984d4b16b23d29edbd938cce5ce121ed0989 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 21 Jul 2025 19:39:23 -1000 Subject: [PATCH] preen --- esphome/components/api/api_connection.cpp | 1 - esphome/components/api/api_pb2_dump.cpp | 747 ++++------------------ script/api_protobuf/api_protobuf.py | 17 +- 3 files changed, 142 insertions(+), 623 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 3e9005d5ec..5cab911d31 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1377,7 +1377,6 @@ void APIConnection::complete_authentication_() { } bool APIConnection::send_hello_response(const HelloRequest &msg) { - // Process the request first this->client_info_.name = msg.client_info; this->client_info_.peername = this->helper_->getpeername(); this->client_api_version_major_ = msg.api_version_major; diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index dd1c02b469..a6937dcf56 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -10,6 +10,15 @@ namespace esphome { namespace api { +// Helper function to append a quoted string, handling empty StringRef +static inline void append_quoted_string(std::string &out, const StringRef &ref) { + out.append("'"); + if (!ref.empty()) { + out.append(ref.c_str()); + } + out.append("'"); +} + template<> const char *proto_enum_to_string(enums::EntityCategory value) { switch (value) { case enums::ENTITY_CATEGORY_NONE: @@ -580,19 +589,11 @@ void HelloResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" server_info: "); - if (!this->server_info_ref_.empty()) { - out.append("'").append(this->server_info_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->server_info_ref_); out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append("}"); } @@ -627,11 +628,7 @@ void AreaInfo::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append("}"); } @@ -646,11 +643,7 @@ void DeviceInfo::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" area_id: "); @@ -670,43 +663,23 @@ void DeviceInfoResponse::dump_to(std::string &out) const { #endif out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" mac_address: "); - if (!this->mac_address_ref_.empty()) { - out.append("'").append(this->mac_address_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->mac_address_ref_); out.append("\n"); out.append(" esphome_version: "); - if (!this->esphome_version_ref_.empty()) { - out.append("'").append(this->esphome_version_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->esphome_version_ref_); out.append("\n"); out.append(" compilation_time: "); - if (!this->compilation_time_ref_.empty()) { - out.append("'").append(this->compilation_time_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->compilation_time_ref_); out.append("\n"); out.append(" model: "); - if (!this->model_ref_.empty()) { - out.append("'").append(this->model_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->model_ref_); out.append("\n"); #ifdef USE_DEEP_SLEEP @@ -717,21 +690,13 @@ void DeviceInfoResponse::dump_to(std::string &out) const { #endif #ifdef ESPHOME_PROJECT_NAME out.append(" project_name: "); - if (!this->project_name_ref_.empty()) { - out.append("'").append(this->project_name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->project_name_ref_); out.append("\n"); #endif #ifdef ESPHOME_PROJECT_NAME out.append(" project_version: "); - if (!this->project_version_ref_.empty()) { - out.append("'").append(this->project_version_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->project_version_ref_); out.append("\n"); #endif @@ -750,19 +715,11 @@ void DeviceInfoResponse::dump_to(std::string &out) const { #endif out.append(" manufacturer: "); - if (!this->manufacturer_ref_.empty()) { - out.append("'").append(this->manufacturer_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->manufacturer_ref_); out.append("\n"); out.append(" friendly_name: "); - if (!this->friendly_name_ref_.empty()) { - out.append("'").append(this->friendly_name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->friendly_name_ref_); out.append("\n"); #ifdef USE_VOICE_ASSISTANT @@ -774,21 +731,13 @@ void DeviceInfoResponse::dump_to(std::string &out) const { #endif #ifdef USE_AREAS out.append(" suggested_area: "); - if (!this->suggested_area_ref_.empty()) { - out.append("'").append(this->suggested_area_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->suggested_area_ref_); out.append("\n"); #endif #ifdef USE_BLUETOOTH_PROXY out.append(" bluetooth_mac_address: "); - if (!this->bluetooth_mac_address_ref_.empty()) { - out.append("'").append(this->bluetooth_mac_address_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->bluetooth_mac_address_ref_); out.append("\n"); #endif @@ -830,11 +779,7 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesBinarySensorResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -843,19 +788,11 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); out.append(" is_status_binary_sensor: "); @@ -868,11 +805,7 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -920,11 +853,7 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesCoverResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -933,11 +862,7 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" assumed_state: "); @@ -953,11 +878,7 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); out.append(" disabled_by_default: "); @@ -966,11 +887,7 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -1067,11 +984,7 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesFanResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -1080,11 +993,7 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" supports_oscillation: "); @@ -1110,11 +1019,7 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -1167,11 +1072,7 @@ void FanStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" preset_mode: "); - if (!this->preset_mode_ref_.empty()) { - out.append("'").append(this->preset_mode_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->preset_mode_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -1247,11 +1148,7 @@ void ListEntitiesLightResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesLightResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -1260,11 +1157,7 @@ void ListEntitiesLightResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); for (const auto &it : this->supported_color_modes) { @@ -1299,11 +1192,7 @@ void ListEntitiesLightResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -1382,11 +1271,7 @@ void LightStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" effect: "); - if (!this->effect_ref_.empty()) { - out.append("'").append(this->effect_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->effect_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -1536,11 +1421,7 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesSensorResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -1549,29 +1430,17 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif out.append(" unit_of_measurement: "); - if (!this->unit_of_measurement_ref_.empty()) { - out.append("'").append(this->unit_of_measurement_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->unit_of_measurement_ref_); out.append("\n"); out.append(" accuracy_decimals: "); @@ -1584,11 +1453,7 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); out.append(" state_class: "); @@ -1644,11 +1509,7 @@ void ListEntitiesSwitchResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesSwitchResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -1657,20 +1518,12 @@ void ListEntitiesSwitchResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -1687,11 +1540,7 @@ void ListEntitiesSwitchResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -1751,11 +1600,7 @@ void ListEntitiesTextSensorResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesTextSensorResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -1764,20 +1609,12 @@ void ListEntitiesTextSensorResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -1790,11 +1627,7 @@ void ListEntitiesTextSensorResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -1815,11 +1648,7 @@ void TextSensorStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" state: "); - if (!this->state_ref_.empty()) { - out.append("'").append(this->state_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->state_ref_); out.append("\n"); out.append(" missing_state: "); @@ -1885,19 +1714,11 @@ void HomeassistantServiceMap::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("HomeassistantServiceMap {\n"); out.append(" key: "); - if (!this->key_ref_.empty()) { - out.append("'").append(this->key_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->key_ref_); out.append("\n"); out.append(" value: "); - if (!this->value_ref_.empty()) { - out.append("'").append(this->value_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->value_ref_); out.append("\n"); out.append("}"); } @@ -1905,11 +1726,7 @@ void HomeassistantServiceResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("HomeassistantServiceResponse {\n"); out.append(" service: "); - if (!this->service_ref_.empty()) { - out.append("'").append(this->service_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->service_ref_); out.append("\n"); for (const auto &it : this->data) { @@ -1942,19 +1759,11 @@ void SubscribeHomeAssistantStateResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("SubscribeHomeAssistantStateResponse {\n"); out.append(" entity_id: "); - if (!this->entity_id_ref_.empty()) { - out.append("'").append(this->entity_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->entity_id_ref_); out.append("\n"); out.append(" attribute: "); - if (!this->attribute_ref_.empty()) { - out.append("'").append(this->attribute_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->attribute_ref_); out.append("\n"); out.append(" once: "); @@ -1993,11 +1802,7 @@ void ListEntitiesServicesArgument::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesServicesArgument {\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" type: "); @@ -2009,11 +1814,7 @@ void ListEntitiesServicesResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesServicesResponse {\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" key: "); @@ -2106,11 +1907,7 @@ void ListEntitiesCameraResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesCameraResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2119,11 +1916,7 @@ void ListEntitiesCameraResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" disabled_by_default: "); @@ -2132,11 +1925,7 @@ void ListEntitiesCameraResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2196,11 +1985,7 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesClimateResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2209,11 +1994,7 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); out.append(" supports_current_temperature: "); @@ -2293,11 +2074,7 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const { #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2382,11 +2159,7 @@ void ClimateStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" custom_fan_mode: "); - if (!this->custom_fan_mode_ref_.empty()) { - out.append("'").append(this->custom_fan_mode_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->custom_fan_mode_ref_); out.append("\n"); out.append(" preset: "); @@ -2394,11 +2167,7 @@ void ClimateStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" custom_preset: "); - if (!this->custom_preset_ref_.empty()) { - out.append("'").append(this->custom_preset_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->custom_preset_ref_); out.append("\n"); out.append(" current_humidity: "); @@ -2527,11 +2296,7 @@ void ListEntitiesNumberResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesNumberResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2540,20 +2305,12 @@ void ListEntitiesNumberResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2581,11 +2338,7 @@ void ListEntitiesNumberResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" unit_of_measurement: "); - if (!this->unit_of_measurement_ref_.empty()) { - out.append("'").append(this->unit_of_measurement_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->unit_of_measurement_ref_); out.append("\n"); out.append(" mode: "); @@ -2593,11 +2346,7 @@ void ListEntitiesNumberResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -2663,11 +2412,7 @@ void ListEntitiesSelectResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesSelectResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2676,20 +2421,12 @@ void ListEntitiesSelectResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2729,11 +2466,7 @@ void SelectStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" state: "); - if (!this->state_ref_.empty()) { - out.append("'").append(this->state_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->state_ref_); out.append("\n"); out.append(" missing_state: "); @@ -2776,11 +2509,7 @@ void ListEntitiesSirenResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesSirenResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2789,20 +2518,12 @@ void ListEntitiesSirenResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2919,11 +2640,7 @@ void ListEntitiesLockResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesLockResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -2932,20 +2649,12 @@ void ListEntitiesLockResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -2970,11 +2679,7 @@ void ListEntitiesLockResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" code_format: "); - if (!this->code_format_ref_.empty()) { - out.append("'").append(this->code_format_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->code_format_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -3042,11 +2747,7 @@ void ListEntitiesButtonResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesButtonResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -3055,20 +2756,12 @@ void ListEntitiesButtonResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -3081,11 +2774,7 @@ void ListEntitiesButtonResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -3120,11 +2809,7 @@ void MediaPlayerSupportedFormat::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("MediaPlayerSupportedFormat {\n"); out.append(" format: "); - if (!this->format_ref_.empty()) { - out.append("'").append(this->format_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->format_ref_); out.append("\n"); out.append(" sample_rate: "); @@ -3151,11 +2836,7 @@ void ListEntitiesMediaPlayerResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesMediaPlayerResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -3164,20 +2845,12 @@ void ListEntitiesMediaPlayerResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -3787,11 +3460,7 @@ void VoiceAssistantRequest::dump_to(std::string &out) const { out.append("\n"); out.append(" conversation_id: "); - if (!this->conversation_id_ref_.empty()) { - out.append("'").append(this->conversation_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->conversation_id_ref_); out.append("\n"); out.append(" flags: "); @@ -3804,11 +3473,7 @@ void VoiceAssistantRequest::dump_to(std::string &out) const { out.append("\n"); out.append(" wake_word_phrase: "); - if (!this->wake_word_phrase_ref_.empty()) { - out.append("'").append(this->wake_word_phrase_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->wake_word_phrase_ref_); out.append("\n"); out.append("}"); } @@ -3929,19 +3594,11 @@ void VoiceAssistantWakeWord::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("VoiceAssistantWakeWord {\n"); out.append(" id: "); - if (!this->id_ref_.empty()) { - out.append("'").append(this->id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->id_ref_); out.append("\n"); out.append(" wake_word: "); - if (!this->wake_word_ref_.empty()) { - out.append("'").append(this->wake_word_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->wake_word_ref_); out.append("\n"); for (const auto &it : this->trained_languages) { @@ -4003,11 +3660,7 @@ void ListEntitiesAlarmControlPanelResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesAlarmControlPanelResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4016,20 +3669,12 @@ void ListEntitiesAlarmControlPanelResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4115,11 +3760,7 @@ void ListEntitiesTextResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesTextResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4128,20 +3769,12 @@ void ListEntitiesTextResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4164,11 +3797,7 @@ void ListEntitiesTextResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" pattern: "); - if (!this->pattern_ref_.empty()) { - out.append("'").append(this->pattern_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->pattern_ref_); out.append("\n"); out.append(" mode: "); @@ -4193,11 +3822,7 @@ void TextStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" state: "); - if (!this->state_ref_.empty()) { - out.append("'").append(this->state_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->state_ref_); out.append("\n"); out.append(" missing_state: "); @@ -4240,11 +3865,7 @@ void ListEntitiesDateResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesDateResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4253,20 +3874,12 @@ void ListEntitiesDateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4361,11 +3974,7 @@ void ListEntitiesTimeResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesTimeResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4374,20 +3983,12 @@ void ListEntitiesTimeResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4482,11 +4083,7 @@ void ListEntitiesEventResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesEventResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4495,20 +4092,12 @@ void ListEntitiesEventResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4521,11 +4110,7 @@ void ListEntitiesEventResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); for (const auto &it : this->event_types) { @@ -4556,11 +4141,7 @@ void EventResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" event_type: "); - if (!this->event_type_ref_.empty()) { - out.append("'").append(this->event_type_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->event_type_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -4578,11 +4159,7 @@ void ListEntitiesValveResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesValveResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4591,20 +4168,12 @@ void ListEntitiesValveResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4617,11 +4186,7 @@ void ListEntitiesValveResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); out.append(" assumed_state: "); @@ -4707,11 +4272,7 @@ void ListEntitiesDateTimeResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesDateTimeResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4720,20 +4281,12 @@ void ListEntitiesDateTimeResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4808,11 +4361,7 @@ void ListEntitiesUpdateResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesUpdateResponse {\n"); out.append(" object_id: "); - if (!this->object_id_ref_.empty()) { - out.append("'").append(this->object_id_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->object_id_ref_); out.append("\n"); out.append(" key: "); @@ -4821,20 +4370,12 @@ void ListEntitiesUpdateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - if (!this->name_ref_.empty()) { - out.append("'").append(this->name_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->name_ref_); out.append("\n"); #ifdef USE_ENTITY_ICON out.append(" icon: "); - if (!this->icon_ref_.empty()) { - out.append("'").append(this->icon_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->icon_ref_); out.append("\n"); #endif @@ -4847,11 +4388,7 @@ void ListEntitiesUpdateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" device_class: "); - if (!this->device_class_ref_.empty()) { - out.append("'").append(this->device_class_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->device_class_ref_); out.append("\n"); #ifdef USE_DEVICES @@ -4889,43 +4426,23 @@ void UpdateStateResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" current_version: "); - if (!this->current_version_ref_.empty()) { - out.append("'").append(this->current_version_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->current_version_ref_); out.append("\n"); out.append(" latest_version: "); - if (!this->latest_version_ref_.empty()) { - out.append("'").append(this->latest_version_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->latest_version_ref_); out.append("\n"); out.append(" title: "); - if (!this->title_ref_.empty()) { - out.append("'").append(this->title_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->title_ref_); out.append("\n"); out.append(" release_summary: "); - if (!this->release_summary_ref_.empty()) { - out.append("'").append(this->release_summary_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->release_summary_ref_); out.append("\n"); out.append(" release_url: "); - if (!this->release_url_ref_.empty()) { - out.append("'").append(this->release_url_ref_.c_str()).append("'"); - } else { - out.append("'").append("").append("'"); - } + append_quoted_string(out, this->release_url_ref_); out.append("\n"); #ifdef USE_DEVICES diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index d6f6159050..6e459eb723 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -575,13 +575,7 @@ class StringType(TypeInfo): # For SOURCE_SERVER, always use StringRef if not self._needs_decode: - return ( - f"if (!this->{self.field_name}_ref_.empty()) {{" - f' out.append("\'").append(this->{self.field_name}_ref_.c_str()).append("\'");' - f"}} else {{" - f' out.append("\'").append("").append("\'");' - f"}}" - ) + return f"append_quoted_string(out, this->{self.field_name}_ref_);" # For SOURCE_BOTH, check if StringRef is set (sending) or use string (received) return ( @@ -1868,6 +1862,15 @@ namespace api { namespace esphome { namespace api { +// Helper function to append a quoted string, handling empty StringRef +static inline void append_quoted_string(std::string &out, const StringRef &ref) { + out.append("'"); + if (!ref.empty()) { + out.append(ref.c_str()); + } + out.append("'"); +} + """ content += "namespace enums {\n\n"