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

View File

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