Remove excessive whitespaces and newlines in json (#1904)

* .

* .

* .

* .

* .

* remove newlines in json as it is not needed

* .

Co-authored-by: CaCO3 <caco@ruinelli.ch>
This commit is contained in:
CaCO3
2023-01-23 19:08:24 +01:00
committed by GitHub
parent 90d7c2e641
commit 5dc111e7a5
2 changed files with 40 additions and 44 deletions

View File

@@ -57,7 +57,6 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
std::string topicFull;
std::string configTopic;
std::string payload;
std::string nl = "\n";
configTopic = field;
@@ -74,63 +73,62 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
}
/* See https://www.home-assistant.io/docs/mqtt/discovery/ */
payload = "{" + nl +
"\"~\": \"" + maintopic + "\"," + nl +
"\"unique_id\": \"" + maintopic + "-" + configTopic + "\"," + nl +
"\"object_id\": \"" + maintopic + "_" + configTopic + "\"," + nl + // This used to generate the Entity ID
"\"name\": \"" + name + "\"," + nl +
"\"icon\": \"mdi:" + icon + "\"," + nl;
payload = string("{") +
"\"~\": \"" + maintopic + "\"," +
"\"unique_id\": \"" + maintopic + "-" + configTopic + "\"," +
"\"object_id\": \"" + maintopic + "_" + configTopic + "\"," + // This used to generate the Entity ID
"\"name\": \"" + name + "\"," +
"\"icon\": \"mdi:" + icon + "\",";
if (group != "") {
if (field == "problem") { // Special binary sensor which is based on error topic
payload += "\"state_topic\": \"~/" + group + "/error\"," + nl;
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\"," + nl;
payload += "\"state_topic\": \"~/" + group + "/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
else {
payload += "\"state_topic\": \"~/" + group + "/" + field + "\"," + nl;
payload += "\"state_topic\": \"~/" + group + "/" + field + "\",";
}
}
else {
if (field == "problem") { // Special binary sensor which is based on error topic
payload += "\"state_topic\": \"~/error\"," + nl;
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\"," + nl;
payload += "\"state_topic\": \"~/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
else {
payload += "\"state_topic\": \"~/" + field + "\"," + nl;
payload += "\"state_topic\": \"~/" + field + "\",";
}
}
if (unit != "") {
payload += "\"unit_of_meas\": \"" + unit + "\"," + nl;
payload += "\"unit_of_meas\": \"" + unit + "\",";
}
if (deviceClass != "") {
payload += "\"device_class\": \"" + deviceClass + "\"," + nl;
payload += "\"device_class\": \"" + deviceClass + "\",";
}
if (stateClass != "") {
payload += "\"state_class\": \"" + stateClass + "\"," + nl;
payload += "\"state_class\": \"" + stateClass + "\",";
}
if (entityCategory != "") {
payload += "\"entity_category\": \"" + entityCategory + "\"," + nl;
payload += "\"entity_category\": \"" + entityCategory + "\",";
}
payload +=
"\"availability_topic\": \"~/" + std::string(LWT_TOPIC) + "\"," + nl +
"\"payload_available\": \"" + LWT_CONNECTED + "\"," + nl +
"\"payload_not_available\": \"" + LWT_DISCONNECTED + "\"," + nl;
"\"availability_topic\": \"~/" + std::string(LWT_TOPIC) + "\"," +
"\"payload_available\": \"" + LWT_CONNECTED + "\"," +
"\"payload_not_available\": \"" + LWT_DISCONNECTED + "\",";
payload +=
"\"device\": {" + nl +
"\"identifiers\": [\"" + maintopic + "\"]," + nl +
"\"name\": \"" + maintopic + "\"," + nl +
"\"model\": \"Meter Digitizer\"," + nl +
"\"manufacturer\": \"AI on the Edge Device\"," + nl +
"\"sw_version\": \"" + version + "\"," + nl +
"\"configuration_url\": \"http://" + *getIPAddress() + "\"" + nl +
"}" + nl +
"}" + nl;
payload += string("\"device\": {") +
"\"identifiers\": [\"" + maintopic + "\"]," +
"\"name\": \"" + maintopic + "\"," +
"\"model\": \"Meter Digitizer\"," +
"\"manufacturer\": \"AI on the Edge Device\"," +
"\"sw_version\": \"" + version + "\"," +
"\"configuration_url\": \"http://" + *getIPAddress() + "\"" +
"}" +
"}";
MQTTPublish(topicFull, payload, true);
}

View File

@@ -356,20 +356,18 @@ esp_err_t sysinfo_handler(httpd_req_t *req)
const char *hostname;
ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
zw = "[\
{\
\"firmware\" : \"" + gitversion + "\",\
\"buildtime\" : \"" + buildtime + "\",\
\"gitbranch\" : \"" + gitbranch + "\",\
\"gittag\" : \"" + gittag + "\",\
\"gitrevision\" : \"" + gitrevision + "\",\
\"html\" : \"" + htmlversion + "\",\
\"cputemp\" : \"" + cputemp + "\",\
\"hostname\" : \"" + hostname + "\",\
\"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\",\
\"freeHeapMem\" : \"" + freeheapmem + "\"\
}\
]";
zw = string("[{") +
"\"firmware\": \"" + gitversion + "\"," +
"\"buildtime\": \"" + buildtime + "\"," +
"\"gitbranch\": \"" + gitbranch + "\"," +
"\"gittag\": \"" + gittag + "\"," +
"\"gitrevision\": \"" + gitrevision + "\"," +
"\"html\": \"" + htmlversion + "\"," +
"\"cputemp\": \"" + cputemp + "\"," +
"\"hostname\": \"" + hostname + "\"," +
"\"IPv4\": \"" + ip4addr_ntoa(&ip_info.ip) + "\"," +
"\"freeHeapMem\": \"" + freeheapmem + "\"" +
"}]";
resp_str = zw.c_str();