38 lines
1.3 KiB
C++
38 lines
1.3 KiB
C++
#include "ha_discovery_json.h"
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
bool ha_build_discovery_sensor_payload(const char *device_id, const char *key, const char *name, const char *unit,
|
|
const char *device_class, const char *state_topic, const char *value_template,
|
|
const char *manufacturer, String &out_payload) {
|
|
if (!device_id || !key || !name || !state_topic || !value_template || !manufacturer) {
|
|
return false;
|
|
}
|
|
|
|
StaticJsonDocument<256> doc;
|
|
String unique_id = String(device_id) + "_" + key;
|
|
String sensor_name = String(device_id) + " " + name;
|
|
|
|
doc["name"] = sensor_name;
|
|
doc["state_topic"] = state_topic;
|
|
doc["unique_id"] = unique_id;
|
|
if (unit && unit[0] != '\0') {
|
|
doc["unit_of_measurement"] = unit;
|
|
}
|
|
if (device_class && device_class[0] != '\0') {
|
|
doc["device_class"] = device_class;
|
|
}
|
|
doc["value_template"] = value_template;
|
|
|
|
JsonObject device = doc.createNestedObject("device");
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add(String(device_id));
|
|
device["name"] = String(device_id);
|
|
device["model"] = "DD3-LoRa-Bridge";
|
|
device["manufacturer"] = manufacturer;
|
|
|
|
out_payload = "";
|
|
size_t len = serializeJson(doc, out_payload);
|
|
return len > 0;
|
|
}
|