Fixes
This commit is contained in:
193
Network.cpp
193
Network.cpp
@@ -1105,32 +1105,12 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi
|
|||||||
{{"pl_on", "ring"},
|
{{"pl_on", "ring"},
|
||||||
{"pl_off", "locked"}});
|
{"pl_off", "locked"}});
|
||||||
|
|
||||||
DynamicJsonDocument ringjson(_bufferSize);
|
DynamicJsonDocument json(_bufferSize);
|
||||||
ringjson.clear();
|
json = createHassJson(uidString, "_ring_event", "Ring", name, baseTopic, String("~") + mqtt_topic_lock_state, deviceType, "doorbell", "", "", "", {{"value_template", "{ \"event_type\": \"{{ value }}\" }"}});
|
||||||
auto ringdev = ringjson.createNestedObject("dev");
|
json["event_types"][0] = "ring";
|
||||||
auto ringids = ringdev.createNestedArray("ids");
|
serializeJson(json, _buffer, _bufferSize);
|
||||||
ringids.add(String("nuki_") + uidString);
|
String path = createHassTopicPath("event", "ring", uidString);
|
||||||
ringjson["dev"]["mf"] = "Nuki";
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
||||||
ringjson["dev"]["mdl"] = deviceType;
|
|
||||||
ringjson["dev"]["name"] = name;
|
|
||||||
ringjson["~"] = baseTopic;
|
|
||||||
ringjson["name"] = "Ring";
|
|
||||||
ringjson["unique_id"] = String(uidString) + "_ring_event";
|
|
||||||
ringjson["dev_cla"] = "doorbell";
|
|
||||||
ringjson["stat_t"] = String("~") + mqtt_topic_lock_state;
|
|
||||||
ringjson["avty"]["t"] = _lockPath + mqtt_topic_mqtt_connection_state;
|
|
||||||
ringjson["event_types"][0] = "ring";
|
|
||||||
ringjson["value_template"] = "{ \"event_type\": \"{{ value }}\" }";
|
|
||||||
|
|
||||||
serializeJson(ringjson, _buffer, _bufferSize);
|
|
||||||
|
|
||||||
String ringpath = discoveryTopic;
|
|
||||||
ringpath.concat("/event/");
|
|
||||||
ringpath.concat(uidString);
|
|
||||||
ringpath.concat("/ring/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(ringpath.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1288,73 +1268,27 @@ void Network::publishHassTopic(const String& mqttDeviceType,
|
|||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
DynamicJsonDocument json(_bufferSize);
|
DynamicJsonDocument json(_bufferSize);
|
||||||
|
json = createHassJson(uidString, uidStringPostfix, displayName, name, baseTopic, stateTopic, deviceType, deviceClass, stateClass, entityCat, commandTopic, additionalEntries);
|
||||||
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"] = displayName;
|
|
||||||
json["unique_id"] = String(uidString) + uidStringPostfix;
|
|
||||||
if(deviceClass != "")
|
|
||||||
{
|
|
||||||
json["dev_cla"] = deviceClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stateTopic != "")
|
|
||||||
{
|
|
||||||
json["stat_t"] = stateTopic;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stateClass != "")
|
|
||||||
{
|
|
||||||
json["stat_cla"] = stateClass;
|
|
||||||
}
|
|
||||||
if(entityCat != "")
|
|
||||||
{
|
|
||||||
json["ent_cat"] = entityCat;
|
|
||||||
}
|
|
||||||
if(commandTopic != "")
|
|
||||||
{
|
|
||||||
json["cmd_t"] = commandTopic;
|
|
||||||
}
|
|
||||||
|
|
||||||
json["avty"]["t"] = _lockPath + mqtt_topic_mqtt_connection_state;
|
|
||||||
|
|
||||||
for(const auto& entry : additionalEntries)
|
|
||||||
{
|
|
||||||
if(strcmp(entry.second, "true") == 0)
|
|
||||||
{
|
|
||||||
json[entry.first] = true;
|
|
||||||
}
|
|
||||||
else if(strcmp(entry.second, "false") == 0)
|
|
||||||
{
|
|
||||||
json[entry.first] = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
json[entry.first] = entry.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serializeJson(json, _buffer, _bufferSize);
|
serializeJson(json, _buffer, _bufferSize);
|
||||||
|
String path = createHassTopicPath(mqttDeviceType, mqttDeviceName, uidString);
|
||||||
String path = discoveryTopic;
|
|
||||||
path.concat("/");
|
|
||||||
path.concat(mqttDeviceType);
|
|
||||||
path.concat("/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/");
|
|
||||||
path.concat(mqttDeviceName);
|
|
||||||
path.concat("/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String Network::createHassTopicPath(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString)
|
||||||
|
{
|
||||||
|
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
|
||||||
|
String path = discoveryTopic;
|
||||||
|
path.concat("/");
|
||||||
|
path.concat(mqttDeviceType);
|
||||||
|
path.concat("/");
|
||||||
|
path.concat(uidString);
|
||||||
|
path.concat("/");
|
||||||
|
path.concat(mqttDeviceName);
|
||||||
|
path.concat("/config");
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
void Network::removeHassTopic(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString)
|
void Network::removeHassTopic(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString)
|
||||||
{
|
{
|
||||||
@@ -1362,15 +1296,7 @@ void Network::removeHassTopic(const String& mqttDeviceType, const String& mqttDe
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
String path = discoveryTopic;
|
String path = createHassTopicPath(mqttDeviceType, mqttDeviceName, uidString);
|
||||||
path.concat("/");
|
|
||||||
path.concat(mqttDeviceType);
|
|
||||||
path.concat("/");
|
|
||||||
path.concat(uidString);
|
|
||||||
path.concat("/");
|
|
||||||
path.concat(mqttDeviceName);
|
|
||||||
path.concat("/config");
|
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1479,6 +1405,79 @@ void Network::removeHASSConfigTopic(char *deviceType, char *name, char *uidStrin
|
|||||||
removeHassTopic(deviceType, name, uidString);
|
removeHassTopic(deviceType, name, uidString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DynamicJsonDocument Network::createHassJson(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& commandTopic,
|
||||||
|
std::vector<std::pair<char*, char*>> additionalEntries
|
||||||
|
)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument json(_bufferSize);
|
||||||
|
|
||||||
|
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"] = displayName;
|
||||||
|
json["unique_id"] = String(uidString) + uidStringPostfix;
|
||||||
|
|
||||||
|
if(deviceClass != "")
|
||||||
|
{
|
||||||
|
json["dev_cla"] = deviceClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(stateTopic != "")
|
||||||
|
{
|
||||||
|
json["stat_t"] = stateTopic;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(stateClass != "")
|
||||||
|
{
|
||||||
|
json["stat_cla"] = stateClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entityCat != "")
|
||||||
|
{
|
||||||
|
json["ent_cat"] = entityCat;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(commandTopic != "")
|
||||||
|
{
|
||||||
|
json["cmd_t"] = commandTopic;
|
||||||
|
}
|
||||||
|
|
||||||
|
json["avty"]["t"] = _lockPath + mqtt_topic_mqtt_connection_state;
|
||||||
|
|
||||||
|
for(const auto& entry : additionalEntries)
|
||||||
|
{
|
||||||
|
if(strcmp(entry.second, "true") == 0)
|
||||||
|
{
|
||||||
|
json[entry.first] = true;
|
||||||
|
}
|
||||||
|
else if(strcmp(entry.second, "false") == 0)
|
||||||
|
{
|
||||||
|
json[entry.first] = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
json[entry.first] = entry.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
void Network::publishPresenceDetection(char *csv)
|
void Network::publishPresenceDetection(char *csv)
|
||||||
{
|
{
|
||||||
_presenceCsv = csv;
|
_presenceCsv = csv;
|
||||||
|
|||||||
19
Network.h
19
Network.h
@@ -8,6 +8,7 @@
|
|||||||
#include "networkDevices/IPConfiguration.h"
|
#include "networkDevices/IPConfiguration.h"
|
||||||
#include "MqttTopics.h"
|
#include "MqttTopics.h"
|
||||||
#include "Gpio.h"
|
#include "Gpio.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
enum class NetworkDeviceType
|
enum class NetworkDeviceType
|
||||||
{
|
{
|
||||||
@@ -97,9 +98,23 @@ private:
|
|||||||
const String& commandTopic = "",
|
const String& commandTopic = "",
|
||||||
std::vector<std::pair<char*, char*>> additionalEntries = {}
|
std::vector<std::pair<char*, char*>> additionalEntries = {}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
String createHassTopicPath(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString);
|
||||||
void removeHassTopic(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString);
|
void removeHassTopic(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString);
|
||||||
|
DynamicJsonDocument createHassJson(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& commandTopic = "",
|
||||||
|
std::vector<std::pair<char*, char*>> additionalEntries = {}
|
||||||
|
);
|
||||||
|
|
||||||
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