test: add json stability and discovery payload coverage
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "ha_discovery_json.h"
|
||||
#include "config.h"
|
||||
#include "json_codec.h"
|
||||
|
||||
@@ -10,6 +11,13 @@ static PubSubClient mqtt_client(wifi_client);
|
||||
static WifiMqttConfig g_cfg;
|
||||
static String g_client_id;
|
||||
|
||||
static const char *ha_manufacturer_anchor() {
|
||||
StaticJsonDocument<32> doc;
|
||||
JsonObject device = doc.createNestedObject("device");
|
||||
device["manufacturer"] = HA_MANUFACTURER;
|
||||
return HA_MANUFACTURER;
|
||||
}
|
||||
|
||||
static const char *fault_text(FaultType fault) {
|
||||
switch (fault) {
|
||||
case FaultType::MeterRead:
|
||||
@@ -94,31 +102,9 @@ bool mqtt_publish_faults(const char *device_id, const FaultCounters &counters, F
|
||||
|
||||
static bool publish_discovery_sensor(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) {
|
||||
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"] = HA_MANUFACTURER;
|
||||
|
||||
String payload;
|
||||
size_t len = serializeJson(doc, payload);
|
||||
if (len == 0) {
|
||||
if (!ha_build_discovery_sensor_payload(device_id, key, name, unit, device_class, state_topic, value_template,
|
||||
ha_manufacturer_anchor(), payload)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user