add utility method to publish ha autodiscovery topic
This commit is contained in:
398
Network.cpp
398
Network.cpp
@@ -572,7 +572,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
json["stat_unlocked"] = unlockedState;
|
json["stat_unlocked"] = unlockedState;
|
||||||
json["opt"] = "false";
|
json["opt"] = "false";
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
serializeJson(json, reinterpret_cast<char(&)[JSON_BUFFER_SIZE]>(*jsonOut));
|
||||||
|
|
||||||
String path = discoveryTopic;
|
String path = discoveryTopic;
|
||||||
path.concat("/lock/");
|
path.concat("/lock/");
|
||||||
@@ -583,113 +583,75 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
|
|
||||||
|
|
||||||
// Battery critical
|
// Battery critical
|
||||||
json.clear();
|
publishHassTopic("binary_sensor",
|
||||||
dev = json.createNestedObject("dev");
|
"battery_low",
|
||||||
ids = dev.createNestedArray("ids");
|
uidString,
|
||||||
ids.add(String("nuki_") + uidString);
|
"_battery_low",
|
||||||
json["dev"]["mf"] = "Nuki";
|
"battery low",
|
||||||
json["dev"]["mdl"] = deviceType;
|
name,
|
||||||
json["dev"]["name"] = name;
|
baseTopic,
|
||||||
json["~"] = baseTopic;
|
mqtt_topic_battery_critical,
|
||||||
json["name"] = name + String(" battery low");
|
deviceType,
|
||||||
json["unique_id"] = String(uidString) + "_battery_low";
|
"battery",
|
||||||
json["dev_cla"] = "battery";
|
"",
|
||||||
json["ent_cat"] = "diagnostic";
|
"diagnostic",
|
||||||
json["plf_off"] = "0";
|
"",
|
||||||
json["plf_on"] = "1";
|
"1",
|
||||||
json["stat_t"] = String("~") + mqtt_topic_battery_critical;
|
"0");
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
path = discoveryTopic;
|
|
||||||
path.concat("/binary_sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/battery_low/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
if(hasKeypad)
|
if(hasKeypad)
|
||||||
{
|
{
|
||||||
// Keypad battery critical
|
// Keypad battery critical
|
||||||
json.clear();
|
publishHassTopic("binary_sensor",
|
||||||
dev = json.createNestedObject("dev");
|
"keypad_battery_low",
|
||||||
ids = dev.createNestedArray("ids");
|
uidString,
|
||||||
ids.add(String("nuki_") + uidString);
|
"_keypad_battery_low",
|
||||||
json["dev"]["mf"] = "Nuki";
|
"keypad battery low",
|
||||||
json["dev"]["mdl"] = deviceType;
|
name,
|
||||||
json["dev"]["name"] = name;
|
baseTopic,
|
||||||
json["~"] = baseTopic;
|
mqtt_topic_battery_keypad_critical,
|
||||||
json["name"] = name + String(" keypad battery low");
|
deviceType,
|
||||||
json["unique_id"] = String(uidString) + "_keypad_battery_low";
|
"battery",
|
||||||
json["dev_cla"] = "battery";
|
"",
|
||||||
json["ent_cat"] = "diagnostic";
|
"diagnostic",
|
||||||
json["plf_off"] = "0";
|
"",
|
||||||
json["plf_on"] = "1";
|
"1",
|
||||||
json["stat_t"] = String("~") + mqtt_topic_battery_keypad_critical;
|
"0");
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
path = discoveryTopic;
|
|
||||||
path.concat("/binary_sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/keypad_battery_low/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Battery voltage
|
// Battery voltage
|
||||||
json.clear();
|
publishHassTopic("sensor",
|
||||||
dev = json.createNestedObject("dev");
|
"battery_voltage",
|
||||||
ids = dev.createNestedArray("ids");
|
uidString,
|
||||||
ids.add(String("nuki_") + uidString);
|
"_battery_voltage",
|
||||||
json["dev"]["mf"] = "Nuki";
|
"battery voltage",
|
||||||
json["dev"]["mdl"] = deviceType;
|
name,
|
||||||
json["dev"]["name"] = name;
|
baseTopic,
|
||||||
json["~"] = baseTopic;
|
mqtt_topic_battery_voltage,
|
||||||
json["name"] = name + String(" battery voltage");
|
deviceType,
|
||||||
json["unique_id"] = String(uidString) + "_battery_voltage";
|
"battery",
|
||||||
json["dev_cla"] = "voltage";
|
"measurement",
|
||||||
json["stat_cla"] = "measurement";
|
"diagnostic",
|
||||||
json["unit_of_meas"] = "V";
|
"V");
|
||||||
json["ent_cat"] = "diagnostic";
|
|
||||||
json["plf_off"] = "0";
|
|
||||||
json["plf_on"] = "1";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_battery_voltage;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
path = discoveryTopic;
|
|
||||||
path.concat("/sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/battery_voltage/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
// Trigger
|
// Trigger
|
||||||
json.clear();
|
publishHassTopic("sensor",
|
||||||
dev = json.createNestedObject("dev");
|
"trigger",
|
||||||
ids = dev.createNestedArray("ids");
|
uidString,
|
||||||
ids.add(String("nuki_") + uidString);
|
"_trigger",
|
||||||
json["dev"]["mf"] = "Nuki";
|
"trigger",
|
||||||
json["dev"]["mdl"] = deviceType;
|
name,
|
||||||
json["dev"]["name"] = name;
|
baseTopic,
|
||||||
json["~"] = baseTopic;
|
mqtt_topic_lock_trigger,
|
||||||
json["name"] = name + String(" trigger");
|
deviceType,
|
||||||
json["unique_id"] = String(uidString) + "_trigger";
|
"",
|
||||||
json["ent_cat"] = "diagnostic";
|
"",
|
||||||
json["enabled_by_default"] = "true";
|
"diagnostic",
|
||||||
json["stat_t"] = String("~") + mqtt_topic_lock_trigger;
|
"",
|
||||||
|
"",
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
"",
|
||||||
|
"",
|
||||||
path = discoveryTopic;
|
"true");
|
||||||
path.concat("/sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/trigger/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -701,36 +663,19 @@ void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic,
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char* jsonOut = new char[JSON_BUFFER_SIZE];
|
publishHassTopic("sensor",
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
"battery_level",
|
||||||
|
uidString,
|
||||||
// Battery level
|
"_battery_level",
|
||||||
json.clear();
|
"battery level",
|
||||||
auto dev = json.createNestedObject("dev");
|
name,
|
||||||
auto ids = dev.createNestedArray("ids");
|
baseTopic,
|
||||||
ids.add(String("nuki_") + uidString);
|
mqtt_topic_battery_level,
|
||||||
json["dev"]["mf"] = "Nuki";
|
deviceType,
|
||||||
json["dev"]["mdl"] = deviceType;
|
"battery",
|
||||||
json["dev"]["name"] = name;
|
"measurement",
|
||||||
json["~"] = baseTopic;
|
"diagnostic",
|
||||||
json["name"] = name + String(" battery level");
|
"%");
|
||||||
json["unique_id"] = String(uidString) + "_battery_level";
|
|
||||||
json["dev_cla"] = "battery";
|
|
||||||
json["stat_cla"] = "measurement";
|
|
||||||
json["unit_of_meas"] = "%";
|
|
||||||
json["ent_cat"] = "diagnostic";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_battery_level;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
String path = discoveryTopic;
|
|
||||||
path.concat("/sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/battery_voltage/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -742,36 +687,22 @@ void Network::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopi
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char* jsonOut = new char[JSON_BUFFER_SIZE];
|
publishHassTopic("binary_sensor",
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
"door_sensor",
|
||||||
|
uidString,
|
||||||
// Battery level
|
"_door_sensor",
|
||||||
json.clear();
|
"door sensor",
|
||||||
auto dev = json.createNestedObject("dev");
|
name,
|
||||||
auto ids = dev.createNestedArray("ids");
|
baseTopic,
|
||||||
ids.add(String("nuki_") + uidString);
|
mqtt_topic_lock_door_sensor_state,
|
||||||
json["dev"]["mf"] = "Nuki";
|
deviceType,
|
||||||
json["dev"]["mdl"] = deviceType;
|
"door",
|
||||||
json["dev"]["name"] = name;
|
"",
|
||||||
json["~"] = baseTopic;
|
"",
|
||||||
json["name"] = name + String(" door sensor");
|
"",
|
||||||
json["unique_id"] = String(uidString) + "_door_sensor";
|
"doorOpened",
|
||||||
json["dev_cla"] = "door";
|
"doorClosed",
|
||||||
json["plf_off"] = "doorClosed";
|
"unavailable");
|
||||||
json["plf_on"] = "doorOpened";
|
|
||||||
json["pl_not_avail"] = "unavailable";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_lock_door_sensor_state;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
String path = discoveryTopic;
|
|
||||||
path.concat("/binary_sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/door_sensor/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -783,35 +714,16 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char* jsonOut = new char[JSON_BUFFER_SIZE];
|
publishHassTopic("sensor",
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
"ring",
|
||||||
|
uidString,
|
||||||
// Battery level
|
"_ring_detect",
|
||||||
json.clear();
|
"ring detect",
|
||||||
auto dev = json.createNestedObject("dev");
|
name,
|
||||||
auto ids = dev.createNestedArray("ids");
|
baseTopic,
|
||||||
ids.add(String("nuki_") + uidString);
|
mqtt_topic_lock_door_sensor_state,
|
||||||
json["dev"]["mf"] = "Nuki";
|
deviceType,
|
||||||
json["dev"]["mdl"] = deviceType;
|
"sound");
|
||||||
json["dev"]["name"] = name;
|
|
||||||
json["~"] = baseTopic;
|
|
||||||
json["name"] = name + String(" door sensor");
|
|
||||||
json["unique_id"] = String(uidString) + "_door_sensor";
|
|
||||||
json["dev_cla"] = "sound";
|
|
||||||
json["plf_off"] = "locked";
|
|
||||||
json["plf_on"] = "ring";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_lock_door_sensor_state;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
String path = discoveryTopic;
|
|
||||||
path.concat("/binary_sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/ring/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -826,36 +738,7 @@ void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic,
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char* jsonOut = new char[JSON_BUFFER_SIZE];
|
publishHassTopic("sensor", "wifi_signal_strength", uidString, "_wifi_signal_strength", "wifi signal strength", name, baseTopic, mqtt_topic_wifi_rssi, deviceType, "signal_strength", "measurement", "diagnostic", "dBm");
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
|
||||||
|
|
||||||
// Battery level
|
|
||||||
json.clear();
|
|
||||||
auto dev = json.createNestedObject("dev");
|
|
||||||
auto ids = dev.createNestedArray("ids");
|
|
||||||
ids.add(String("nuki_") + uidString);
|
|
||||||
json["dev"]["mf"] = "Nuki";
|
|
||||||
json["dev"]["mdl"] = deviceType;
|
|
||||||
json["dev"]["name"] = name;
|
|
||||||
json["~"] = baseTopic;
|
|
||||||
json["name"] = name + String(" WiFi signal strength");
|
|
||||||
json["unique_id"] = String(uidString) + "_wifi_signal_strength";
|
|
||||||
json["dev_cla"] = "signal_strength";
|
|
||||||
json["stat_cla"] = "measurement";
|
|
||||||
json["unit_of_meas"] = "dBm";
|
|
||||||
json["ent_cat"] = "diagnostic";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_wifi_rssi;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
|
||||||
|
|
||||||
String path = discoveryTopic;
|
|
||||||
path.concat("/sensor/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/wifi_signal_strength/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -863,6 +746,33 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic,
|
|||||||
{
|
{
|
||||||
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
|
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
|
||||||
|
|
||||||
|
if (discoveryTopic != "")
|
||||||
|
{
|
||||||
|
publishHassTopic("sensor", "bluetooth_signal_strength", uidString, "_bluetooth_signal_strength", "bluetooth signal strength", name, baseTopic, mqtt_topic_lock_rssi, deviceType, "signal_strength", "measurement", "diagnostic", "dBm");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Network::publishHassTopic(const String& mqttDeviceType,
|
||||||
|
const String& mattDeviceName,
|
||||||
|
const String& uidString,
|
||||||
|
const String& uidStringPostfix,
|
||||||
|
const String& displayName,
|
||||||
|
const String& name,
|
||||||
|
const String& baseTopic,
|
||||||
|
const String& stateTopic,
|
||||||
|
const String& deviceType,
|
||||||
|
const String& deviceClass,
|
||||||
|
const String& stateClass,
|
||||||
|
const String& entityCat,
|
||||||
|
const String& unitOfMeasurement,
|
||||||
|
const String& plfOn,
|
||||||
|
const String& plfOff,
|
||||||
|
const String& plfNotAvailable,
|
||||||
|
const String& enabledByDefault
|
||||||
|
)
|
||||||
|
{
|
||||||
|
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
|
||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char *jsonOut = new char[JSON_BUFFER_SIZE];
|
char *jsonOut = new char[JSON_BUFFER_SIZE];
|
||||||
@@ -877,20 +787,52 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic,
|
|||||||
json["dev"]["mdl"] = deviceType;
|
json["dev"]["mdl"] = deviceType;
|
||||||
json["dev"]["name"] = name;
|
json["dev"]["name"] = name;
|
||||||
json["~"] = baseTopic;
|
json["~"] = baseTopic;
|
||||||
json["name"] = name + String(" bluetooth signal strength");
|
json["name"] = name + String(" " + displayName);
|
||||||
json["unique_id"] = String(uidString) + "_bluetooth_signal_strength";
|
json["unique_id"] = String(uidString) + uidStringPostfix;
|
||||||
json["dev_cla"] = "signal_strength";
|
json["dev_cla"] = deviceClass;
|
||||||
json["stat_cla"] = "measurement";
|
json["stat_t"] = String("~") + stateTopic;
|
||||||
json["unit_of_meas"] = "dBm";
|
|
||||||
json["ent_cat"] = "diagnostic";
|
|
||||||
json["stat_t"] = String("~") + mqtt_topic_lock_rssi;
|
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[2048]>(*jsonOut));
|
if(stateClass != "")
|
||||||
|
{
|
||||||
|
json["stat_cla"] = stateClass;
|
||||||
|
}
|
||||||
|
if(entityCat != "")
|
||||||
|
{
|
||||||
|
json["ent_cat"] = entityCat;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unitOfMeasurement != "")
|
||||||
|
{
|
||||||
|
json["unit_of_meas"] = unitOfMeasurement;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plfOff != "")
|
||||||
|
{
|
||||||
|
json["plf_off"] = plfOff;
|
||||||
|
}
|
||||||
|
if (plfOn != "")
|
||||||
|
{
|
||||||
|
json["plf_on"] = plfOn;
|
||||||
|
}
|
||||||
|
if (plfNotAvailable != "")
|
||||||
|
{
|
||||||
|
json["pl_not_avail"] = plfNotAvailable;
|
||||||
|
}
|
||||||
|
if (enabledByDefault != "")
|
||||||
|
{
|
||||||
|
json["enabled_by_default"] = "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
serializeJson(json, reinterpret_cast<char (&)[JSON_BUFFER_SIZE]>(*jsonOut));
|
||||||
|
|
||||||
String path = discoveryTopic;
|
String path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/");
|
||||||
|
path.concat(mqttDeviceType);
|
||||||
|
path.concat("/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/bluetooth_signal_strength/config");
|
path.concat("/");
|
||||||
|
path.concat(mattDeviceName);
|
||||||
|
path.concat("/config");
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
||||||
|
|
||||||
|
|||||||
19
Network.h
19
Network.h
@@ -62,6 +62,25 @@ private:
|
|||||||
void setupDevice();
|
void setupDevice();
|
||||||
bool reconnect();
|
bool reconnect();
|
||||||
|
|
||||||
|
void publishHassTopic(const String& mqttDeviceType,
|
||||||
|
const String& mattDeviceName,
|
||||||
|
const String& uidString,
|
||||||
|
const String& uidStringPostfix,
|
||||||
|
const String& displayName,
|
||||||
|
const String& name,
|
||||||
|
const String& baseTopic,
|
||||||
|
const String& stateTopic,
|
||||||
|
const String& deviceType,
|
||||||
|
const String& deviceClass,
|
||||||
|
const String& stateClass = "",
|
||||||
|
const String& entityCat = "",
|
||||||
|
const String& unitOfMeasurement = "",
|
||||||
|
const String& plfOn = "",
|
||||||
|
const String& plfOff = "",
|
||||||
|
const String& plfNotAvailable = "",
|
||||||
|
const String& enabledByDefault = ""
|
||||||
|
);
|
||||||
|
|
||||||
void onMqttConnect(const bool& sessionPresent);
|
void onMqttConnect(const bool& sessionPresent);
|
||||||
void onMqttDisconnect(const espMqttClientTypes::DisconnectReason& reason);
|
void onMqttDisconnect(const espMqttClientTypes::DisconnectReason& reason);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user