Skip to content

Commit

Permalink
derive correct node_id for homeassistant service discovery in nested …
Browse files Browse the repository at this point in the history
…topics (fixes jomjol#1792)
  • Loading branch information
henrythasler committed May 29, 2024
1 parent 79543df commit 986b024
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions code/components/jomjol_mqtt/server_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ bool sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
name = group + " " + name;
}

/**
* homeassistant needs the MQTT discovery topic according to the following structure:
* <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
* if the main topic is embedded in a nested structure, we just use the last part as node_id
* This means a maintopic "home/test/watermeter" is transformed to the discovery topic "homeassistant/sensor/watermeter/..."
*/
auto splitPos = maintopic.find_last_of('/');
string node_id = ((splitPos == string::npos) ? maintopic : maintopic.substr(splitPos + 1));

if (field == "problem") { // Special binary sensor which is based on error topic
topicFull = "homeassistant/binary_sensor/" + maintopic + "/" + configTopic + "/config";
topicFull = "homeassistant/binary_sensor/" + node_id + "/" + configTopic + "/config";
}
else {
topicFull = "homeassistant/sensor/" + maintopic + "/" + configTopic + "/config";
topicFull = "homeassistant/sensor/" + node_id + "/" + configTopic + "/config";
}

/* See https://www.home-assistant.io/docs/mqtt/discovery/ */
Expand Down

0 comments on commit 986b024

Please sign in to comment.