Skip to content

Commit

Permalink
[DISC] Fix Mqtt Device Trigger announcement message (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
Odyno authored Oct 25, 2024
1 parent f86d89d commit f309605
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f309605

Please sign in to comment.