From 4b2a9e5e493d11fd1e89b9f904bb1e737860fcca Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 8 Jun 2019 16:44:25 +0200 Subject: [PATCH] Fix status binary sensor for MQTT (#628) Fixes https://github.com/esphome/issues/issues/417 --- esphome/components/mqtt/mqtt_binary_sensor.cpp | 13 ++++++++----- esphome/components/mqtt/mqtt_binary_sensor.h | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/esphome/components/mqtt/mqtt_binary_sensor.cpp b/esphome/components/mqtt/mqtt_binary_sensor.cpp index e63d6649b3..edabcb398c 100644 --- a/esphome/components/mqtt/mqtt_binary_sensor.cpp +++ b/esphome/components/mqtt/mqtt_binary_sensor.cpp @@ -19,15 +19,19 @@ void MQTTBinarySensorComponent::dump_config() { LOG_MQTT_COMPONENT(true, false) } MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor) - : MQTTComponent(), binary_sensor_(binary_sensor) {} + : MQTTComponent(), binary_sensor_(binary_sensor) { + if (this->binary_sensor_->is_status_binary_sensor()) { + this->set_custom_state_topic(mqtt::global_mqtt_client->get_availability().topic); + } +} std::string MQTTBinarySensorComponent::friendly_name() const { return this->binary_sensor_->get_name(); } void MQTTBinarySensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) { if (!this->binary_sensor_->get_device_class().empty()) root["device_class"] = this->binary_sensor_->get_device_class(); - if (this->is_status_) + if (this->binary_sensor_->is_status_binary_sensor()) root["payload_on"] = mqtt::global_mqtt_client->get_availability().payload_available; - if (this->is_status_) + if (this->binary_sensor_->is_status_binary_sensor()) root["payload_off"] = mqtt::global_mqtt_client->get_availability().payload_not_available; config.command_topic = false; } @@ -40,13 +44,12 @@ bool MQTTBinarySensorComponent::send_initial_state() { } bool MQTTBinarySensorComponent::is_internal() { return this->binary_sensor_->is_internal(); } bool MQTTBinarySensorComponent::publish_state(bool state) { - if (this->is_status_) + if (this->binary_sensor_->is_status_binary_sensor()) return true; const char *state_s = state ? "ON" : "OFF"; return this->publish(this->get_state_topic_(), state_s); } -void MQTTBinarySensorComponent::set_is_status(bool status) { this->is_status_ = status; } } // namespace mqtt } // namespace esphome diff --git a/esphome/components/mqtt/mqtt_binary_sensor.h b/esphome/components/mqtt/mqtt_binary_sensor.h index 7793caec08..1ca82a947e 100644 --- a/esphome/components/mqtt/mqtt_binary_sensor.h +++ b/esphome/components/mqtt/mqtt_binary_sensor.h @@ -35,7 +35,6 @@ class MQTTBinarySensorComponent : public mqtt::MQTTComponent { std::string component_type() const override; binary_sensor::BinarySensor *binary_sensor_; - bool is_status_{false}; }; } // namespace mqtt