From 5d254d463b5c07e7bd71dd1aed06acad82a29851 Mon Sep 17 00:00:00 2001 From: rodriguezst <2828844+rodriguezst@users.noreply.github.com> Date: Tue, 21 Jun 2022 12:01:31 +0000 Subject: [PATCH] Add input form for HASS discovery topic --- Network.cpp | 111 ++++++++++++++++++++++++---------------------- PreferencesKeys.h | 1 + WebCfgServer.cpp | 6 +++ 3 files changed, 66 insertions(+), 52 deletions(-) diff --git a/Network.cpp b/Network.cpp index dd5452a..46a328b 100644 --- a/Network.cpp +++ b/Network.cpp @@ -345,64 +345,71 @@ void Network::publishPresenceDetection(char *csv) void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState) { - String configJSON = "{\"dev\":{\"ids\":[\"nuki_"; - configJSON.concat(uidString); - configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\""); - configJSON.concat(deviceType); - configJSON.concat("\",\"name\":\""); - configJSON.concat(name); - configJSON.concat("\"},\"~\":\""); - configJSON.concat(baseTopic); - configJSON.concat("\",\"name\":\""); - configJSON.concat(name); - configJSON.concat("\",\"unique_id\":\""); - configJSON.concat(uidString); - configJSON.concat("_lock\",\"cmd_t\":\"~"); - configJSON.concat(mqtt_topic_lock_action); - configJSON.concat("\",\"pl_lock\":\""); - configJSON.concat(lockAction); - configJSON.concat("\",\"pl_unlk\":\""); - configJSON.concat(unlockAction); - configJSON.concat("\",\"pl_open\":\""); - configJSON.concat(openAction); - configJSON.concat("\",\"stat_t\":\"~"); - configJSON.concat(mqtt_topic_lock_state); - configJSON.concat("\",\"stat_locked\":\""); - configJSON.concat(lockedState); - configJSON.concat("\",\"stat_unlocked\":\""); - configJSON.concat(unlockedState); - configJSON.concat("\",\"opt\":\"false\"}"); + String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery); - String path = "homeassistant/lock/"; - path.concat(uidString); - path.concat("/smartlock/config"); + if(discoveryTopic != "") + { + String configJSON = "{\"dev\":{\"ids\":[\"nuki_"; + configJSON.concat(uidString); + configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\""); + configJSON.concat(deviceType); + configJSON.concat("\",\"name\":\""); + configJSON.concat(name); + configJSON.concat("\"},\"~\":\""); + configJSON.concat(baseTopic); + configJSON.concat("\",\"name\":\""); + configJSON.concat(name); + configJSON.concat("\",\"unique_id\":\""); + configJSON.concat(uidString); + configJSON.concat("_lock\",\"cmd_t\":\"~"); + configJSON.concat(mqtt_topic_lock_action); + configJSON.concat("\",\"pl_lock\":\""); + configJSON.concat(lockAction); + configJSON.concat("\",\"pl_unlk\":\""); + configJSON.concat(unlockAction); + configJSON.concat("\",\"pl_open\":\""); + configJSON.concat(openAction); + configJSON.concat("\",\"stat_t\":\"~"); + configJSON.concat(mqtt_topic_lock_state); + configJSON.concat("\",\"stat_locked\":\""); + configJSON.concat(lockedState); + configJSON.concat("\",\"stat_unlocked\":\""); + configJSON.concat(unlockedState); + configJSON.concat("\",\"opt\":\"false\"}"); - Serial.println("HASS Config:"); - Serial.println(configJSON); + String path = discoveryTopic; + path.concat("/lock/"); + path.concat(uidString); + path.concat("/smartlock/config"); - _device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true); + Serial.println("HASS Config:"); + Serial.println(configJSON); - configJSON = "{\"dev\":{\"ids\":[\"nuki_"; - configJSON.concat(uidString); - configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\""); - configJSON.concat(deviceType); - configJSON.concat("\",\"name\":\""); - configJSON.concat(name); - configJSON.concat("\"},\"~\":\""); - configJSON.concat(baseTopic); - configJSON.concat("\",\"name\":\""); - configJSON.concat(name); - configJSON.concat(" battery low\",\"unique_id\":\""); - configJSON.concat(uidString); - configJSON.concat("_battery_low\",\"dev_cla\":\"battery\",\"ent_cat\":\"diagnostic\",\"pl_off\":\"0\",\"pl_on\":\"1\",\"stat_t\":\"~"); - configJSON.concat(mqtt_topic_battery_critical); - configJSON.concat("\"}"); + _device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true); - path = "homeassistant/binary_sensor/"; - path.concat(uidString); - path.concat("/battery_low/config"); + configJSON = "{\"dev\":{\"ids\":[\"nuki_"; + configJSON.concat(uidString); + configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\""); + configJSON.concat(deviceType); + configJSON.concat("\",\"name\":\""); + configJSON.concat(name); + configJSON.concat("\"},\"~\":\""); + configJSON.concat(baseTopic); + configJSON.concat("\",\"name\":\""); + configJSON.concat(name); + configJSON.concat(" battery low\",\"unique_id\":\""); + configJSON.concat(uidString); + configJSON.concat("_battery_low\",\"dev_cla\":\"battery\",\"ent_cat\":\"diagnostic\",\"pl_off\":\"0\",\"pl_on\":\"1\",\"stat_t\":\"~"); + configJSON.concat(mqtt_topic_battery_critical); + configJSON.concat("\"}"); - _device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true); + path = discoveryTopic; + path.concat("/binary_sensor/"); + path.concat(uidString); + path.concat("/battery_low/config"); + + _device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true); + } } void Network::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *)) diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 56466db..a471121 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -13,6 +13,7 @@ #define preference_mqtt_ca "mqttca" #define preference_mqtt_crt "mqttcrt" #define preference_mqtt_key "mqttkey" +#define preference_mqtt_hass_discovery "hassdiscovery" #define preference_hostname "hostname" #define preference_network_timeout "nettmout" #define preference_query_interval_lockstate "lockStInterval" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 6c91c0c..1ae7afc 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -225,6 +225,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putString(preference_mqtt_key, value); configChanged = true; } + else if(key == "HASSDISCOVERY") + { + _preferences->putString(preference_mqtt_hass_discovery, value); + configChanged = true; + } else if(key == "HOSTNAME") { _preferences->putString(preference_hostname, value); @@ -493,6 +498,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE); printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE); printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE); + printInputField(response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30); printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); response.concat(""); response.concat("* If no encryption is configured for the MQTT broker, leave empty.
");