Fix status binary sensor for MQTT (#628)

Fixes https://github.com/esphome/issues/issues/417
This commit is contained in:
Otto Winter 2019-06-08 16:44:25 +02:00
parent 1449c51d49
commit 4b2a9e5e49
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
2 changed files with 8 additions and 6 deletions

View file

@ -19,15 +19,19 @@ void MQTTBinarySensorComponent::dump_config() {
LOG_MQTT_COMPONENT(true, false) LOG_MQTT_COMPONENT(true, false)
} }
MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor) 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(); } std::string MQTTBinarySensorComponent::friendly_name() const { return this->binary_sensor_->get_name(); }
void MQTTBinarySensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) { void MQTTBinarySensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (!this->binary_sensor_->get_device_class().empty()) if (!this->binary_sensor_->get_device_class().empty())
root["device_class"] = this->binary_sensor_->get_device_class(); 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; 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; root["payload_off"] = mqtt::global_mqtt_client->get_availability().payload_not_available;
config.command_topic = false; 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::is_internal() { return this->binary_sensor_->is_internal(); }
bool MQTTBinarySensorComponent::publish_state(bool state) { bool MQTTBinarySensorComponent::publish_state(bool state) {
if (this->is_status_) if (this->binary_sensor_->is_status_binary_sensor())
return true; return true;
const char *state_s = state ? "ON" : "OFF"; const char *state_s = state ? "ON" : "OFF";
return this->publish(this->get_state_topic_(), state_s); return this->publish(this->get_state_topic_(), state_s);
} }
void MQTTBinarySensorComponent::set_is_status(bool status) { this->is_status_ = status; }
} // namespace mqtt } // namespace mqtt
} // namespace esphome } // namespace esphome

View file

@ -35,7 +35,6 @@ class MQTTBinarySensorComponent : public mqtt::MQTTComponent {
std::string component_type() const override; std::string component_type() const override;
binary_sensor::BinarySensor *binary_sensor_; binary_sensor::BinarySensor *binary_sensor_;
bool is_status_{false};
}; };
} // namespace mqtt } // namespace mqtt