mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Reduce memory usage with StringRef in MQTT Components (#5719)
This commit is contained in:
parent
222bb9b495
commit
74281b93c4
3 changed files with 15 additions and 12 deletions
|
@ -25,7 +25,7 @@ void MQTTBinarySensorComponent::dump_config() {
|
|||
MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor)
|
||||
: binary_sensor_(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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,13 +34,13 @@ std::string MQTTComponent::get_default_topic_for_(const std::string &suffix) con
|
|||
|
||||
std::string MQTTComponent::get_state_topic_() const {
|
||||
if (this->has_custom_state_topic_)
|
||||
return this->custom_state_topic_;
|
||||
return this->custom_state_topic_.str();
|
||||
return this->get_default_topic_for_("state");
|
||||
}
|
||||
|
||||
std::string MQTTComponent::get_command_topic_() const {
|
||||
if (this->has_custom_command_topic_)
|
||||
return this->custom_command_topic_;
|
||||
return this->custom_command_topic_.str();
|
||||
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; }
|
||||
void MQTTComponent::disable_discovery() { this->discovery_enabled_ = false; }
|
||||
void MQTTComponent::set_custom_state_topic(const std::string &custom_state_topic) {
|
||||
this->custom_state_topic_ = custom_state_topic;
|
||||
void MQTTComponent::set_custom_state_topic(const char *custom_state_topic) {
|
||||
this->custom_state_topic_ = StringRef(custom_state_topic);
|
||||
this->has_custom_state_topic_ = true;
|
||||
}
|
||||
void MQTTComponent::set_custom_command_topic(const std::string &custom_command_topic) {
|
||||
this->custom_command_topic_ = custom_command_topic;
|
||||
void MQTTComponent::set_custom_command_topic(const char *custom_command_topic) {
|
||||
this->custom_command_topic_ = StringRef(custom_command_topic);
|
||||
this->has_custom_command_topic_ = true;
|
||||
}
|
||||
void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "mqtt_client.h"
|
||||
|
||||
namespace esphome {
|
||||
|
@ -88,9 +89,9 @@ class MQTTComponent : public Component {
|
|||
virtual std::string component_type() const = 0;
|
||||
|
||||
/// 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.
|
||||
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.
|
||||
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.
|
||||
std::string get_default_object_id_() const;
|
||||
|
||||
std::string custom_state_topic_{};
|
||||
std::string custom_command_topic_{};
|
||||
StringRef custom_state_topic_{};
|
||||
StringRef custom_command_topic_{};
|
||||
|
||||
std::unique_ptr<Availability> availability_;
|
||||
|
||||
bool has_custom_state_topic_{false};
|
||||
bool has_custom_command_topic_{false};
|
||||
|
||||
bool command_retain_{false};
|
||||
bool retain_{true};
|
||||
bool discovery_enabled_{true};
|
||||
std::unique_ptr<Availability> availability_;
|
||||
bool resend_state_{false};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue