Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions esphome/components/mqtt/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,11 @@ void MQTTClientComponent::set_discovery_info(std::string &&prefix, MQTTDiscovery
this->discovery_info_.object_id_generator = object_id_generator;
this->discovery_info_.retain = retain;
this->discovery_info_.clean = clean;
this->discovery_prefix_configured_ = this->discovery_info_.prefix;
}

void MQTTClientComponent::set_discovery_enabled(bool enabled) {
this->discovery_info_.prefix = enabled ? this->discovery_prefix_configured_ : "";
}

void MQTTClientComponent::disable_last_will() {
Expand All @@ -739,6 +744,7 @@ void MQTTClientComponent::disable_discovery() {
.unique_id_generator = MQTT_LEGACY_UNIQUE_ID_GENERATOR,
.object_id_generator = MQTT_NONE_OBJECT_ID_GENERATOR,
};
this->discovery_prefix_configured_ = "";
}
void MQTTClientComponent::on_shutdown() {
if (!this->shutdown_message_.topic.empty()) {
Expand Down
4 changes: 4 additions & 0 deletions esphome/components/mqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class MQTTClientComponent : public Component {
const MQTTDiscoveryInfo &get_discovery_info() const;
/// Globally disable Home Assistant discovery.
void disable_discovery();
/// Flip discovery at run time; off keeps the configured prefix so a later on can restore it.
void set_discovery_enabled(bool enabled);
bool is_discovery_enabled() const;
bool is_discovery_ip_enabled() const;

Expand Down Expand Up @@ -314,6 +316,8 @@ class MQTTClientComponent : public Component {
.unique_id_generator = MQTT_LEGACY_UNIQUE_ID_GENERATOR,
.object_id_generator = MQTT_NONE_OBJECT_ID_GENERATOR,
};
/// What set_discovery_enabled(true) restores; empty once discovery is off at build time.
std::string discovery_prefix_configured_{"homeassistant"};
std::string topic_prefix_{};
bool topic_prefix_auto_{true};
bool birth_message_auto_{true};
Expand Down
Loading