[DISC] Fix Mqtt Device Trigger announcement message (#2091)

This commit is contained in:
Alessandro Staniscia
2024-10-25 20:24:10 +02:00
committed by GitHub
parent f86d89db85
commit f30960566c
2 changed files with 13 additions and 3 deletions

View File

@@ -130,7 +130,7 @@ void announceDeviceTrigger(bool use_gateway_info, char* topic, char* type, char*
strcat(state_topic, gateway_name);
strcat(state_topic, topic);
sensor["topic"] = state_topic;
sensor["info_topic"] = state_topic;
}
/* Set The Devices */
@@ -187,7 +187,7 @@ void announceDeviceTrigger(bool use_gateway_info, char* topic, char* type, char*
sensor["device"] = device; //device representing the board
/* Publish on the topic */
String topic_to_publish = String(discovery_prefix) + "/device_automation/" + String(unique_id) + "/config";
String topic_to_publish = String(discovery_prefix) + "/device_automation/" + String(Gateway_Short_Name) + "/" + String(unique_id) + "/config";
Log.trace(F("Announce Device Trigger %s" CR), topic_to_publish.c_str());
sensor["topic"] = topic_to_publish;
sensor["retain"] = true;

View File

@@ -585,7 +585,17 @@ bool pub(JsonObject& data) {
data.remove("origin");
} else if (data.containsKey("topic") && data["topic"].is<const char*>()) {
topic = data["topic"].as<const char*>();
data.remove("topic");
if (data.containsKey("info_topic") && data["info_topic"].is<const char*>()) {
// Sometimes it is necessary to provide information about the publishing topic, not just use it.
// This is the case, for example, for the RF2MQTT device trigger announcement message where the
// temporary variable info_topic provides information about the topic that will be used to publish the message,
// and it can be different of current message topic (This is a clever pun, I hope it's clear).
data["topic"].set(data["info_topic"]);
data.remove("info_topic");
} else {
data.remove("topic");
}
} else {
Log.error(F("No topic or origin in JSON, not published" CR));
gatewayState = GatewayState::ERROR;