add maintopic to entity name to get a better entity ID. For some undocumented reason, HA removes the maintopic again in the name, so it looks ok :)

This commit is contained in:
CaCO3
2022-10-29 11:50:19 +02:00
parent fc24db7d59
commit 56d8c65008

View File

@@ -26,7 +26,7 @@ extern const char* libfive_git_branch(void);
std::vector<NumberPost*>* NUMBERS; std::vector<NumberPost*>* NUMBERS;
void sendHomeAssistantDiscoveryTopic(std::string maintopic, std::string group, std::string field, void sendHomeAssistantDiscoveryTopic(std::string maintopic, std::string group, std::string field,
std::string userFriendlyName, std::string icon, std::string unit, std::string deviceClass, std::string stateClass) { std::string name, std::string icon, std::string unit, std::string deviceClass, std::string stateClass) {
std::string version = std::string(libfive_git_version()); std::string version = std::string(libfive_git_version());
if (version == "") { if (version == "") {
@@ -38,30 +38,29 @@ void sendHomeAssistantDiscoveryTopic(std::string maintopic, std::string group, s
std::string topicT; std::string topicT;
std::string payload; std::string payload;
std::string nl = "\n"; std::string nl = "\n";
std::string name;
if (group != "") { if (group == "") {
topic = group + "/" + field;
topicT = group + "_" + field;
}
else {
topic = field; topic = field;
topicT = field; topicT = field;
} }
else {
name = field; topic = group + "/" + field;
if (group != "") { topicT = group + "_" + field;
name = group + " " + name;
userFriendlyName = group + " " + userFriendlyName;
} }
/* The name is used as Friendly Name but also to generate the Entity ID! */
if (group != "") { // Prepend the group to the name
name = group + " " + name;
}
name = maintopic + " " + name; // Prepend device name to make the entities unique
topicFull = "homeassistant/sensor/" + maintopic + "/" + topicT + "/config"; topicFull = "homeassistant/sensor/" + maintopic + "/" + topicT + "/config";
/* See https://www.home-assistant.io/docs/mqtt/discovery/ */ /* See https://www.home-assistant.io/docs/mqtt/discovery/ */
payload = "{" + nl + payload = "{" + nl +
"\"~\": \"" + maintopic + "\"," + nl + "\"~\": \"" + maintopic + "\"," + nl +
"\"unique_id\": \"" + maintopic + "-" + topicT + "\"," + nl + "\"unique_id\": \"" + maintopic + "-" + topicT + "\"," + nl +
"\"name\": \"" + userFriendlyName + "\"," + nl + "\"name\": \"" + name + "\"," + nl +
"\"icon\": \"mdi:" + icon + "\"," + nl; "\"icon\": \"mdi:" + icon + "\"," + nl;
if (group != "") { if (group != "") {