1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 15:12:21 +01:00

Correctly allow mqtt topics to be none so ESPHome does not sub/pub to them (#5213)

This commit is contained in:
Jesse Hills
2023-10-26 13:37:50 +13:00
committed by GitHub
parent 4774200f6b
commit 8b9a7608f0
4 changed files with 30 additions and 8 deletions

View File

@@ -2,9 +2,9 @@
#ifdef USE_MQTT
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/version.h"
#include "mqtt_const.h"
@@ -28,15 +28,15 @@ std::string MQTTComponent::get_default_topic_for_(const std::string &suffix) con
}
std::string MQTTComponent::get_state_topic_() const {
if (this->custom_state_topic_.empty())
return this->get_default_topic_for_("state");
return this->custom_state_topic_;
if (this->has_custom_state_topic_)
return this->custom_state_topic_;
return this->get_default_topic_for_("state");
}
std::string MQTTComponent::get_command_topic_() const {
if (this->custom_command_topic_.empty())
return this->get_default_topic_for_("command");
return this->custom_command_topic_;
if (this->has_custom_command_topic_)
return this->custom_command_topic_;
return this->get_default_topic_for_("command");
}
bool MQTTComponent::publish(const std::string &topic, const std::string &payload) {
@@ -171,9 +171,11 @@ float MQTTComponent::get_setup_priority() const { return setup_priority::AFTER_C
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;
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;
this->has_custom_command_topic_ = true;
}
void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }
@@ -240,7 +242,20 @@ bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connec
std::string MQTTComponent::friendly_name() const { return this->get_entity()->get_name(); }
std::string MQTTComponent::get_icon() const { return this->get_entity()->get_icon(); }
bool MQTTComponent::is_disabled_by_default() const { return this->get_entity()->is_disabled_by_default(); }
bool MQTTComponent::is_internal() { return this->get_entity()->is_internal(); }
bool MQTTComponent::is_internal() {
if ((this->get_state_topic_().empty()) || (this->get_command_topic_().empty())) {
// If both state_topic and command_topic are empty, then the entity is internal to mqtt
return true;
}
if (this->has_custom_state_topic_ || this->has_custom_command_topic_) {
// If a custom state_topic or command_topic is set, then the entity is not internal to mqtt
return false;
}
// Use ESPHome's entity internal state
return this->get_entity()->is_internal();
}
} // namespace mqtt
} // namespace esphome

View File

@@ -190,6 +190,9 @@ class MQTTComponent : public Component {
std::string custom_state_topic_{};
std::string custom_command_topic_{};
bool has_custom_state_topic_{false};
bool has_custom_command_topic_{false};
bool command_retain_{false};
bool retain_{true};
bool discovery_enabled_{true};