107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#include "mqtt_discovery.hpp"
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include "constants.h"
|
|
|
|
struct sensor_config {
|
|
const char* name;
|
|
const char* unique_id;
|
|
const char* value_template;
|
|
const char* identifier;
|
|
const char* device_class;
|
|
const char* unit;
|
|
const char* state_class;
|
|
const char* entity_category;
|
|
const char* icon;
|
|
};
|
|
|
|
static bool publish_config(PubSubClient* mqtt, const char* topic,
|
|
const struct sensor_config* config) {
|
|
StaticJsonDocument<512> doc;
|
|
doc["unique_id"] = config->unique_id;
|
|
doc["name"] = config->name;
|
|
doc["state_topic"] = TOPIC_STATE;
|
|
doc["value_template"] = config->value_template;
|
|
if (config->device_class != NULL) {
|
|
doc["device_class"] = config->device_class;
|
|
}
|
|
if (config->unit != NULL) {
|
|
doc["unit_of_measurement"] = config->unit;
|
|
}
|
|
if (config->state_class != NULL) {
|
|
doc["state_class"] = config->state_class;
|
|
}
|
|
if (config->entity_category != NULL) {
|
|
doc["entity_category"] = config->entity_category;
|
|
}
|
|
if (config->icon != NULL) {
|
|
doc["icon"] = config->icon;
|
|
}
|
|
auto device = doc.createNestedObject("device");
|
|
device["manufacturer"] = DEVICE_MANUFACTURER;
|
|
device["name"] = DEVICE_NAME;
|
|
device["model"] = DEVICE_MODEL;
|
|
device["sw_version"] = VERSION;
|
|
auto ident = device.createNestedArray("identifiers");
|
|
ident.add(config->identifier);
|
|
String msg;
|
|
serializeJson(doc, msg);
|
|
auto payload = msg.c_str();
|
|
Serial.printf("Publishing configuration for %s (%zd bytes) to %s ... ",
|
|
config->unique_id, strlen(payload), topic);
|
|
auto r = mqtt->publish(topic, payload, true);
|
|
Serial.println(r ? "OK" : "FAILED");
|
|
return r;
|
|
}
|
|
|
|
bool publish_all_config(PubSubClient* mqtt, const char* ident) {
|
|
bool ret = true;
|
|
struct sensor_config moisture = {
|
|
.name = "Garden Sensor Moisture",
|
|
.unique_id = "sensor.garden_sensor_moisture",
|
|
.value_template = "{{ value_json.moisture }}",
|
|
.identifier = ident,
|
|
.device_class = NULL,
|
|
.unit = NULL,
|
|
.state_class = "measurement",
|
|
.entity_category = NULL,
|
|
.icon = "mdi:water",
|
|
};
|
|
publish_config(mqtt, TOPIC_CFG_MOISTURE, &moisture);
|
|
struct sensor_config temperature = {
|
|
.name = "Garden Sensor Temperature",
|
|
.unique_id = "sensor.garden_sensor_temperature",
|
|
.value_template = "{{ value_json.temperature }}",
|
|
.identifier = ident,
|
|
.device_class = "temperature",
|
|
.unit = "°C",
|
|
.state_class = NULL,
|
|
.entity_category = NULL,
|
|
};
|
|
publish_config(mqtt, TOPIC_CFG_TEMPERATURE, &temperature);
|
|
struct sensor_config battery = {
|
|
.name = "Garden Sensor Battery",
|
|
.unique_id = "sensor.garden_sensor_battery",
|
|
.value_template = "{{ value_json.battery }}",
|
|
.identifier = ident,
|
|
.device_class = "battery",
|
|
.unit = "%",
|
|
.state_class = NULL,
|
|
.entity_category = "diagnostic",
|
|
};
|
|
publish_config(mqtt, TOPIC_CFG_BATTERY, &battery);
|
|
struct sensor_config signal = {
|
|
.name = "Garden Sensor Signal Strength",
|
|
.unique_id = "sensor.garden_sensor_rssi",
|
|
.value_template = "{{ value_json.rssi }}",
|
|
.identifier = ident,
|
|
.device_class = "signal_strength",
|
|
.unit = "dBm",
|
|
.state_class = NULL,
|
|
.entity_category = "diagnostic",
|
|
};
|
|
publish_config(mqtt, TOPIC_CFG_RSSI, &signal);
|
|
|
|
return ret;
|
|
} |