test: add json stability and discovery payload coverage
This commit is contained in:
7
lib/dd3_transport_logic/include/ha_discovery_json.h
Normal file
7
lib/dd3_transport_logic/include/ha_discovery_json.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.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);
|
||||
37
lib/dd3_transport_logic/src/ha_discovery_json.cpp
Normal file
37
lib/dd3_transport_logic/src/ha_discovery_json.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user