mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Correctly allow mqtt topics to be none so ESPHome does not sub/pub to them (#5213)
This commit is contained in:
parent
4774200f6b
commit
8b9a7608f0
4 changed files with 30 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -1047,6 +1047,8 @@ def ipv4(value):
|
|||
|
||||
def _valid_topic(value):
|
||||
"""Validate that this is a valid topic name/filter."""
|
||||
if value is None: # Used to disable publishing and subscribing
|
||||
return ""
|
||||
if isinstance(value, dict):
|
||||
raise Invalid("Can't use dictionary with topic")
|
||||
value = string(value)
|
||||
|
|
|
@ -64,6 +64,7 @@ mqtt:
|
|||
discovery: true
|
||||
discovery_prefix: homeassistant
|
||||
idf_send_async: false
|
||||
log_topic:
|
||||
on_message:
|
||||
topic: testing/sensor/testing_sensor/state
|
||||
qos: 0
|
||||
|
@ -403,6 +404,7 @@ sensor:
|
|||
update_interval: 1s
|
||||
- platform: internal_temperature
|
||||
name: Internal Temperature
|
||||
state_topic:
|
||||
- platform: selec_meter
|
||||
total_active_energy:
|
||||
name: SelecEM2M Total Active Energy
|
||||
|
|
Loading…
Reference in a new issue