From 72218171b3e7d572491bb587d33aba12eee4761c Mon Sep 17 00:00:00 2001 From: Brandon Davidson Date: Mon, 27 May 2019 08:21:01 -0700 Subject: [PATCH] Don't fall back to the global availability topic if it is empty (#553) Fixes esphome/issues#320 --- esphome/components/mqtt/mqtt_component.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index 13b48630fa..042e362f75 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -72,11 +72,13 @@ bool MQTTComponent::send_discovery_() { root["command_topic"] = this->get_command_topic_(); if (this->availability_ == nullptr) { - root["availability_topic"] = global_mqtt_client->get_availability().topic; - if (global_mqtt_client->get_availability().payload_available != "online") - root["payload_available"] = global_mqtt_client->get_availability().payload_available; - if (global_mqtt_client->get_availability().payload_not_available != "offline") - root["payload_not_available"] = global_mqtt_client->get_availability().payload_not_available; + if (!global_mqtt_client->get_availability().topic.empty()) { + root["availability_topic"] = global_mqtt_client->get_availability().topic; + if (global_mqtt_client->get_availability().payload_available != "online") + root["payload_available"] = global_mqtt_client->get_availability().payload_available; + if (global_mqtt_client->get_availability().payload_not_available != "offline") + root["payload_not_available"] = global_mqtt_client->get_availability().payload_not_available; + } } else if (!this->availability_->topic.empty()) { root["availability_topic"] = this->availability_->topic; if (this->availability_->payload_available != "online")