mqtt_discovery: Add state_class, entity_category

Populating these options helps Home Assistant display the sensors better
in the UI.

The `publish_client` function has too many parameters as it is, making
it difficult to keep track of which value is passed as which argument.
Using a structure and designated initializers makes this a lot cleaner.
master
Dustin 2022-05-16 20:29:55 -05:00
parent 87c4833a10
commit e628508ff5
1 changed files with 71 additions and 25 deletions

View File

@ -4,32 +4,47 @@
#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;
};
static bool publish_config(PubSubClient* mqtt, const char* topic,
const char* name, const char* unique_id,
const char* value_template, const char* identifier,
const char* device_class, const char* unit) {
StaticJsonDocument<256> doc;
doc["unique_id"] = unique_id;
doc["name"] = name;
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"] = value_template;
if (device_class != NULL) {
doc["device_class"] = device_class;
doc["value_template"] = config->value_template;
if (config->device_class != NULL) {
doc["device_class"] = config->device_class;
}
if (unit != NULL) {
doc["unit_of_measurement"] = unit;
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;
}
auto device = doc.createNestedObject("device");
device["manufacturer"] = DEVICE_MANUFACTURER;
device["name"] = DEVICE_NAME;
device["model"] = DEVICE_MODEL;
auto ident = device.createNestedArray("identifiers");
ident.add(identifier);
ident.add(config->identifier);
String msg;
serializeJson(doc, msg);
auto payload = msg.c_str();
Serial.printf("Publishing configuration for %s (%zd bytes) to %s ... ",
unique_id, strlen(payload), topic);
config->unique_id, strlen(payload), topic);
auto r = mqtt->publish(topic, payload, true);
Serial.println(r ? "OK" : "FAILED");
return r;
@ -37,18 +52,49 @@ static bool publish_config(PubSubClient* mqtt, const char* topic,
bool publish_all_config(PubSubClient* mqtt, const char* ident) {
bool ret = true;
publish_config(mqtt, TOPIC_CFG_MOISTURE, "Garden Sensor Moisture",
"sensor.garden_sensor_moisture",
"{{ value_json.moisture }}", ident, NULL, NULL);
publish_config(mqtt, TOPIC_CFG_TEMPERATURE, "Garden Sensor Temperature",
"sensor.garden_sensor_temperature",
"{{ value_json.temperature }}", ident, "temperature", "°C");
publish_config(mqtt, TOPIC_CFG_BATTERY, "Garden Sensor Battery Level",
"sensor.garden_sensor_battery_level",
"{{ value_json.battery_level }}", ident, "voltage", "V");
publish_config(mqtt, TOPIC_CFG_RSSI, "Garden Sensor Signal Strength",
"sensor.garden_sensor_rssi", "{{ value_json.rssi }}", ident,
"signal_strength", "dBm");
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};
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 Level",
.unique_id = "sensor.garden_sensor_battery_level",
.value_template = "{{ value_json.battery_level }}",
.identifier = ident,
.device_class = "voltage",
.unit = "V",
.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;
}