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