From f30960566c8aa008808661aef9e251dd328fc1ab Mon Sep 17 00:00:00 2001 From: Alessandro Staniscia Date: Fri, 25 Oct 2024 20:24:10 +0200 Subject: [PATCH] [DISC] Fix Mqtt Device Trigger announcement message (#2091) --- main/ZmqttDiscovery.ino | 4 ++-- main/main.ino | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 5dfb901139..a48765f0e8 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -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; diff --git a/main/main.ino b/main/main.ino index a5d6d67aa4..e650a9edb8 100644 --- a/main/main.ino +++ b/main/main.ino @@ -585,7 +585,17 @@ bool pub(JsonObject& data) { data.remove("origin"); } else if (data.containsKey("topic") && data["topic"].is()) { topic = data["topic"].as(); - data.remove("topic"); + if (data.containsKey("info_topic") && data["info_topic"].is()) { + // 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;