From ccb3f899cec7ebd00c50a815b7fc0c95f1f67e53 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 4 Feb 2024 21:48:06 +0100 Subject: [PATCH 1/5] Add Home Assistant configuration URL --- Network.cpp | 12 ++++++++++++ PreferencesKeys.h | 3 ++- WebCfgServer.cpp | 6 ++++++ networkDevices/EthLan8720Device.cpp | 5 +++++ networkDevices/W5500Device.cpp | 5 +++++ networkDevices/WifiDevice.cpp | 5 +++++ 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Network.cpp b/Network.cpp index fe48b7c..f19fefb 100644 --- a/Network.cpp +++ b/Network.cpp @@ -719,6 +719,18 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n json["dev"]["mf"] = "Nuki"; json["dev"]["mdl"] = deviceType; json["dev"]["name"] = name; + + String cuUrl = _preferences->getString(preference_mqtt_hass_cu_url); + + if (cuUrl != "") + { + json["dev"]["cu"] = cuUrl; + } + else + { + json["dev"]["cu"] = "http://" + _device->localIP(); + } + json["~"] = baseTopic; json["name"] = nullptr; json["unique_id"] = String(uidString) + "_lock"; diff --git a/PreferencesKeys.h b/PreferencesKeys.h index b658772..13757d5 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -20,6 +20,7 @@ #define preference_mqtt_crt "mqttcrt" #define preference_mqtt_key "mqttkey" #define preference_mqtt_hass_discovery "hassdiscovery" +#define preference_mqtt_hass_cu_url "hassConfigUrl" #define preference_ip_dhcp_enabled "dhcpena" #define preference_ip_address "ipaddr" #define preference_ip_subnet "ipsub" @@ -64,7 +65,7 @@ private: preference_mqtt_user, preference_mqtt_password, preference_mqtt_log_enabled, preference_lock_enabled, preference_mqtt_lock_path, preference_opener_enabled, preference_mqtt_opener_path, preference_lock_max_keypad_code_count, preference_opener_max_keypad_code_count, preference_mqtt_ca, - preference_mqtt_crt, preference_mqtt_key, preference_mqtt_hass_discovery, + preference_mqtt_crt, preference_mqtt_key, preference_mqtt_hass_discovery, preference_mqtt_hass_cu_url, preference_ip_dhcp_enabled, preference_ip_address, preference_ip_subnet, preference_ip_gateway, preference_ip_dns_server, preference_network_hardware, preference_network_wifi_fallback_disabled, preference_rssi_publish_interval, preference_hostname, preference_network_timeout, preference_restart_on_disconnect, diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 07f1446..d6a1372 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -352,6 +352,11 @@ bool WebCfgServer::processArgs(String& message) configChanged = true; } } + else if(key == "HASSCUURL") + { + _preferences->putString(preference_mqtt_hass_cu_url, value); + configChanged = true; + } else if(key == "HOSTNAME") { _preferences->putString(preference_hostname, value); @@ -764,6 +769,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) response.concat("

Advanced MQTT and Network Configuration

"); response.concat(""); printInputField(response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable; usually homeassistant)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30); + printInputField(response, "HASSCUURL", "Home Assistant device configuration URL (empty to use http://LOCALIP; fill when using a reverse proxy for example)", _preferences->getString(preference_mqtt_hass_cu_url).c_str(), 261); printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE, _network->encryptionSupported(), true); printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE, _network->encryptionSupported(), true); printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE, _network->encryptionSupported(), true); diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index def7bc6..95ea554 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -159,6 +159,11 @@ int8_t EthLan8720Device::signalStrength() return -1; } +String EthLan8720Device::localIP() +{ + return Ethernet.localIP().toString(); +} + void EthLan8720Device::mqttSetClientId(const char *clientId) { if(_useEncryption) diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index e66b6bd..0b3045d 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -229,6 +229,11 @@ int8_t W5500Device::signalStrength() return 127; } +String W5500Device::localIP() +{ + return Ethernet.localIP().toString(); +} + void W5500Device::mqttSetClientId(const char *clientId) { _mqttClient.setClientId(clientId); diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index a90ff82..b36606d 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -180,6 +180,11 @@ int8_t WifiDevice::signalStrength() return WiFi.RSSI(); } +String WifiDevice::localIP() +{ + return WiFi.localIP().toString(); +} + void WifiDevice::clearRtcInitVar(WiFiManager *) { memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect); From c62f3b9e165a918d24f90b4f775f6b847d0daca2 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 4 Feb 2024 22:16:29 +0100 Subject: [PATCH 2/5] Add files via upload --- networkDevices/EthLan8720Device.h | 2 ++ networkDevices/NetworkDevice.h | 2 ++ networkDevices/W5500Device.h | 2 ++ networkDevices/WifiDevice.h | 2 ++ 4 files changed, 8 insertions(+) diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index 1453e1b..ada4590 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -36,6 +36,8 @@ public: virtual bool isConnected(); int8_t signalStrength() override; + + String localIP() override; void mqttSetClientId(const char *clientId) override; diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index a850696..c41d476 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -31,6 +31,8 @@ public: virtual bool isConnected() = 0; virtual int8_t signalStrength() = 0; + + virtual String localIP() = ''; virtual void mqttSetClientId(const char* clientId) = 0; virtual void mqttSetCleanSession(bool cleanSession) = 0; diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index ca909eb..e77d3c9 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -32,6 +32,8 @@ public: virtual bool isConnected(); int8_t signalStrength() override; + + String localIP() override; void mqttSetClientId(const char *clientId) override; diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index d45b30e..eaf3129 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -26,6 +26,8 @@ public: virtual bool isConnected(); int8_t signalStrength() override; + + String localIP() override; void mqttSetClientId(const char *clientId) override; From b586ed4e9ec8128a56d5b0be0caa644565d2f742 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 4 Feb 2024 22:18:43 +0100 Subject: [PATCH 3/5] Add files via upload --- networkDevices/NetworkDevice.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index c41d476..b417b5f 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -32,7 +32,7 @@ public: virtual bool isConnected() = 0; virtual int8_t signalStrength() = 0; - virtual String localIP() = ''; + virtual String localIP() = 0; virtual void mqttSetClientId(const char* clientId) = 0; virtual void mqttSetCleanSession(bool cleanSession) = 0; From aa465d198b41eb4f78e3c87b5e7b9dd815fec7c8 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 4 Feb 2024 22:20:09 +0100 Subject: [PATCH 4/5] Add files via upload --- networkDevices/EthLan8720Device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index 95ea554..e511481 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -161,7 +161,7 @@ int8_t EthLan8720Device::signalStrength() String EthLan8720Device::localIP() { - return Ethernet.localIP().toString(); + return ETH.localIP().toString(); } void EthLan8720Device::mqttSetClientId(const char *clientId) From 9ac5b4a0194c42a1f05916ae3628d1a9ae7a07a8 Mon Sep 17 00:00:00 2001 From: iranl Date: Wed, 7 Feb 2024 21:06:35 +0100 Subject: [PATCH 5/5] Add IP to MQTT + HA discovery --- MqttTopics.h | 1 + Network.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/MqttTopics.h b/MqttTopics.h index b5ae1ff..650a054 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -41,6 +41,7 @@ #define mqtt_topic_info_hardware_version "/info/hardwareVersion" #define mqtt_topic_info_firmware_version "/info/firmwareVersion" #define mqtt_topic_info_nuki_hub_version "/info/nukiHubVersion" +#define mqtt_topic_info_nuki_hub_ip "/info/nukiHubIp" #define mqtt_topic_keypad "/keypad" #define mqtt_topic_keypad_command_action "/keypad/command/action" diff --git a/Network.cpp b/Network.cpp index 4525456..c643a0b 100644 --- a/Network.cpp +++ b/Network.cpp @@ -491,6 +491,7 @@ bool Network::reconnect() } publishString(_maintenancePathPrefix, mqtt_topic_mqtt_connection_state, "online"); + publishString(_maintenancePathPrefix, mqtt_topic_info_nuki_hub_ip, _device->localIP().c_str()); _mqttConnectionState = 2; for(const auto& callback : _reconnectedCallbacks) @@ -902,7 +903,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n publishHassTopic("sensor", "nuki_hub_version", uidString, - "_nuki_hub__version", + "_nuki_hub_version", "NUKI Hub version", name, baseTopic, @@ -915,6 +916,23 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n { { "enabled_by_default", "true" }, {"ic", "mdi:counter"}}); + // NUKI Hub IP Address + publishHassTopic("sensor", + "nuki_hub_ip", + uidString, + "_nuki_hub_ip", + "NUKI Hub IP", + name, + baseTopic, + _lockPath + mqtt_topic_info_nuki_hub_ip, + deviceType, + "", + "", + "diagnostic", + "", + { { "enabled_by_default", "true" }, + {"ic", "mdi:ip"}}); + // LED enabled publishHassTopic("switch", "led_enabled", @@ -1391,6 +1409,12 @@ void Network::removeHASSConfig(char* uidString) path.concat(uidString); path.concat("/sound_level/config"); _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); + + path = discoveryTopic; + path.concat("/sensor/"); + path.concat(uidString); + path.concat("/nuki_hub_ip/config"); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/number/");