Reduce memory usage with StringRef in MQTT Components (#5719)

This commit is contained in:
kahrendt 2023-12-21 02:19:15 -05:00 committed by GitHub
parent 222bb9b495
commit 74281b93c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

View file

@ -25,7 +25,7 @@ void MQTTBinarySensorComponent::dump_config() {
MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor) MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor)
: binary_sensor_(binary_sensor) { : binary_sensor_(binary_sensor) {
if (this->binary_sensor_->is_status_binary_sensor()) { if (this->binary_sensor_->is_status_binary_sensor()) {
this->set_custom_state_topic(mqtt::global_mqtt_client->get_availability().topic); this->set_custom_state_topic(mqtt::global_mqtt_client->get_availability().topic.c_str());
} }
} }

View file

@ -34,13 +34,13 @@ std::string MQTTComponent::get_default_topic_for_(const std::string &suffix) con
std::string MQTTComponent::get_state_topic_() const { std::string MQTTComponent::get_state_topic_() const {
if (this->has_custom_state_topic_) if (this->has_custom_state_topic_)
return this->custom_state_topic_; return this->custom_state_topic_.str();
return this->get_default_topic_for_("state"); return this->get_default_topic_for_("state");
} }
std::string MQTTComponent::get_command_topic_() const { std::string MQTTComponent::get_command_topic_() const {
if (this->has_custom_command_topic_) if (this->has_custom_command_topic_)
return this->custom_command_topic_; return this->custom_command_topic_.str();
return this->get_default_topic_for_("command"); return this->get_default_topic_for_("command");
} }
@ -180,12 +180,12 @@ MQTTComponent::MQTTComponent() = default;
float MQTTComponent::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; } float MQTTComponent::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
void MQTTComponent::disable_discovery() { this->discovery_enabled_ = false; } void MQTTComponent::disable_discovery() { this->discovery_enabled_ = false; }
void MQTTComponent::set_custom_state_topic(const std::string &custom_state_topic) { void MQTTComponent::set_custom_state_topic(const char *custom_state_topic) {
this->custom_state_topic_ = custom_state_topic; this->custom_state_topic_ = StringRef(custom_state_topic);
this->has_custom_state_topic_ = true; this->has_custom_state_topic_ = true;
} }
void MQTTComponent::set_custom_command_topic(const std::string &custom_command_topic) { void MQTTComponent::set_custom_command_topic(const char *custom_command_topic) {
this->custom_command_topic_ = custom_command_topic; this->custom_command_topic_ = StringRef(custom_command_topic);
this->has_custom_command_topic_ = true; this->has_custom_command_topic_ = true;
} }
void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; } void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }

View file

@ -8,6 +8,7 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/entity_base.h" #include "esphome/core/entity_base.h"
#include "esphome/core/string_ref.h"
#include "mqtt_client.h" #include "mqtt_client.h"
namespace esphome { namespace esphome {
@ -88,9 +89,9 @@ class MQTTComponent : public Component {
virtual std::string component_type() const = 0; virtual std::string component_type() const = 0;
/// Set a custom state topic. Set to "" for default behavior. /// Set a custom state topic. Set to "" for default behavior.
void set_custom_state_topic(const std::string &custom_state_topic); void set_custom_state_topic(const char *custom_state_topic);
/// Set a custom command topic. Set to "" for default behavior. /// Set a custom command topic. Set to "" for default behavior.
void set_custom_command_topic(const std::string &custom_command_topic); void set_custom_command_topic(const char *custom_command_topic);
/// Set whether command message should be retained. /// Set whether command message should be retained.
void set_command_retain(bool command_retain); void set_command_retain(bool command_retain);
@ -188,15 +189,17 @@ class MQTTComponent : public Component {
/// Generate the Home Assistant MQTT discovery object id by automatically transforming the friendly name. /// Generate the Home Assistant MQTT discovery object id by automatically transforming the friendly name.
std::string get_default_object_id_() const; std::string get_default_object_id_() const;
std::string custom_state_topic_{}; StringRef custom_state_topic_{};
std::string custom_command_topic_{}; StringRef custom_command_topic_{};
std::unique_ptr<Availability> availability_;
bool has_custom_state_topic_{false}; bool has_custom_state_topic_{false};
bool has_custom_command_topic_{false}; bool has_custom_command_topic_{false};
bool command_retain_{false}; bool command_retain_{false};
bool retain_{true}; bool retain_{true};
bool discovery_enabled_{true}; bool discovery_enabled_{true};
std::unique_ptr<Availability> availability_;
bool resend_state_{false}; bool resend_state_{false};
}; };