From 1f64cb446d6164e447221a40d0e030501cec3dd2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Jun 2025 23:03:57 +0100 Subject: [PATCH] tweaks --- esphome/components/api/api_connection.cpp | 8 ++++---- esphome/components/api/api_connection.h | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index a0723c7170..1454407ded 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -568,7 +568,7 @@ void APIConnection::switch_command(const SwitchCommandRequest &msg) { #endif #ifdef USE_TEXT_SENSOR -bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state) { +bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor, const std::string &state) { return this->schedule_message_(text_sensor, MessageCreator(state, TextSensorStateResponse::MESSAGE_TYPE), TextSensorStateResponse::MESSAGE_TYPE); } @@ -839,7 +839,7 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { #endif #ifdef USE_TEXT -bool APIConnection::send_text_state(text::Text *text, std::string state) { +bool APIConnection::send_text_state(text::Text *text, const std::string &state) { return this->schedule_message_(text, MessageCreator(state, TextStateResponse::MESSAGE_TYPE), TextStateResponse::MESSAGE_TYPE); } @@ -870,7 +870,7 @@ void APIConnection::text_command(const TextCommandRequest &msg) { #endif #ifdef USE_SELECT -bool APIConnection::send_select_state(select::Select *select, std::string state) { +bool APIConnection::send_select_state(select::Select *select, const std::string &state) { return this->schedule_message_(select, MessageCreator(state, SelectStateResponse::MESSAGE_TYPE), SelectStateResponse::MESSAGE_TYPE); } @@ -1336,7 +1336,7 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe #endif #ifdef USE_EVENT -void APIConnection::send_event(event::Event *event, std::string event_type) { +void APIConnection::send_event(event::Event *event, const std::string &event_type) { this->schedule_message_(event, MessageCreator(event_type, EventResponse::MESSAGE_TYPE), EventResponse::MESSAGE_TYPE); } void APIConnection::send_event_info(event::Event *event) { diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 73c565a05c..d3ee38dfce 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -61,7 +61,7 @@ class APIConnection : public APIServerConnection { void switch_command(const SwitchCommandRequest &msg) override; #endif #ifdef USE_TEXT_SENSOR - bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state); + bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, const std::string &state); void send_text_sensor_info(text_sensor::TextSensor *text_sensor); #endif #ifdef USE_ESP32_CAMERA @@ -95,12 +95,12 @@ class APIConnection : public APIServerConnection { void datetime_command(const DateTimeCommandRequest &msg) override; #endif #ifdef USE_TEXT - bool send_text_state(text::Text *text, std::string state); + bool send_text_state(text::Text *text, const std::string &state); void send_text_info(text::Text *text); void text_command(const TextCommandRequest &msg) override; #endif #ifdef USE_SELECT - bool send_select_state(select::Select *select, std::string state); + bool send_select_state(select::Select *select, const std::string &state); void send_select_info(select::Select *select); void select_command(const SelectCommandRequest &msg) override; #endif @@ -172,7 +172,7 @@ class APIConnection : public APIServerConnection { #endif #ifdef USE_EVENT - void send_event(event::Event *event, std::string event_type); + void send_event(event::Event *event, const std::string &event_type); void send_event_info(event::Event *event); #endif @@ -506,7 +506,7 @@ class APIConnection : public APIServerConnection { // Destructor ~MessageCreator() { // Clean up string data for string-based message types - if (uses_string_data()) { + if (uses_string_data_()) { delete data_.string_ptr; } } @@ -515,7 +515,7 @@ class APIConnection : public APIServerConnection { MessageCreator(const MessageCreator &other) : message_type_(other.message_type_) { if (message_type_ == 0) { data_.ptr = other.data_.ptr; - } else if (uses_string_data()) { + } else if (uses_string_data_()) { data_.string_ptr = new std::string(*other.data_.string_ptr); } else { data_ = other.data_; // For POD types @@ -523,7 +523,7 @@ class APIConnection : public APIServerConnection { } // Move constructor - MessageCreator(MessageCreator &&other) noexcept : message_type_(other.message_type_), data_(other.data_) { + MessageCreator(MessageCreator &&other) noexcept : data_(other.data_), message_type_(other.message_type_) { other.message_type_ = 0; // Reset other to function pointer type other.data_.ptr = nullptr; } @@ -532,14 +532,14 @@ class APIConnection : public APIServerConnection { MessageCreator &operator=(const MessageCreator &other) { if (this != &other) { // Clean up current string data if needed - if (uses_string_data()) { + if (uses_string_data_()) { delete data_.string_ptr; } // Copy new data message_type_ = other.message_type_; if (other.message_type_ == 0) { data_.ptr = other.data_.ptr; - } else if (other.uses_string_data()) { + } else if (other.uses_string_data_()) { data_.string_ptr = new std::string(*other.data_.string_ptr); } else { data_ = other.data_; @@ -551,7 +551,7 @@ class APIConnection : public APIServerConnection { MessageCreator &operator=(MessageCreator &&other) noexcept { if (this != &other) { // Clean up current string data if needed - if (uses_string_data()) { + if (uses_string_data_()) { delete data_.string_ptr; } // Move data @@ -569,7 +569,7 @@ class APIConnection : public APIServerConnection { private: // Helper to check if this message type uses heap-allocated strings - bool uses_string_data() const { + bool uses_string_data_() const { return message_type_ == TextSensorStateResponse::MESSAGE_TYPE || message_type_ == SelectStateResponse::MESSAGE_TYPE || message_type_ == TextStateResponse::MESSAGE_TYPE || message_type_ == EventResponse::MESSAGE_TYPE;