From ccb3f899cec7ebd00c50a815b7fc0c95f1f67e53 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 4 Feb 2024 21:48:06 +0100 Subject: [PATCH 01/10] 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 02/10] 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 03/10] 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 04/10] 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 6c079d13403d6cf446a63ee6efd0d97eed36e6e6 Mon Sep 17 00:00:00 2001 From: Luca Oliano Date: Mon, 5 Feb 2024 18:11:53 +0100 Subject: [PATCH 05/10] add docker build script --- Docker/Dockerfile | 48 +++++++++++++++++++++++++++++++++++++ Docker/README.md | 10 ++++++++ Docker/build_with_docker.sh | 6 +++++ 3 files changed, 64 insertions(+) create mode 100644 Docker/Dockerfile create mode 100644 Docker/README.md create mode 100755 Docker/build_with_docker.sh diff --git a/Docker/Dockerfile b/Docker/Dockerfile new file mode 100644 index 0000000..7d4b323 --- /dev/null +++ b/Docker/Dockerfile @@ -0,0 +1,48 @@ +FROM eclipse-temurin:11-jdk-jammy AS builder + +RUN set -ex && \ + apt-get update && \ + apt-get install -y git cmake xz-utils python3 python3-serial + +RUN curl -L "https://downloads.arduino.cc/arduino-1.8.19-linux64.tar.xz" -o /tmp/arduino-ide.tar.xz +RUN tar -xf /tmp/arduino-ide.tar.xz --directory ~/ + +RUN cd ~/arduino* && \ + ./install.sh && \ + ./arduino --pref "boardsmanager.additional.urls=https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json" --save-prefs && \ + ./arduino --install-boards esp32:esp32:2.0.9 + +RUN git clone --recurse-submodules https://github.com/technyon/Arduino-CMake-Toolchain.git ~/Arduino-CMake-Toolchain + +COPY icon /usr/src/nuki_hub/icon +COPY include /usr/src/nuki_hub/include +COPY lib /usr/src/nuki_hub/lib +COPY networkDevices /usr/src/nuki_hub/networkDevices +COPY CMakeLists.txt /usr/src/nuki_hub +COPY index.html /usr/src/nuki_hub +COPY *.h /usr/src/nuki_hub +COPY *.cpp /usr/src/nuki_hub + +RUN mkdir -p /usr/src/nuki_hub/build + +RUN cd /usr/src/nuki_hub/build && \ + echo "# Espressif ESP32 Partition Table" > partitions.csv && \ + echo "# Name, Type, SubType, Offset, Size, Flags" >> partitions.csv && \ + echo "nvs, data, nvs, 0x9000, 0x5000," >> partitions.csv && \ + echo "otadata, data, ota, 0xe000, 0x2000," >> partitions.csv && \ + echo "app0, app, ota_0, 0x10000, 0x1E0000," >> partitions.csv && \ + echo "app1, app, ota_1, 0x1F0000,0x1E0000," >> partitions.csv && \ + echo "spiffs, data, spiffs, 0x3D0000,0x30000," >> partitions.csv + +RUN set -ex && \ + cd /usr/src/nuki_hub/build && \ + cmake -D CMAKE_TOOLCHAIN_FILE=~/Arduino-CMake-Toolchain/Arduino-toolchain.cmake .. && \ + make + +FROM builder AS runtime + +COPY --from=builder /usr/src/nuki_hub/build/nuki_hub.bin /usr/src/nuki_hub/build/release/nuki_hub.bin +COPY --from=builder /usr/src/nuki_hub/build/nuki_hub.partitions.bin /usr/src/nuki_hub/build/release/nuki_hub.partitions.bin +COPY --from=builder /root/.arduino15/packages/esp32/hardware/esp32/2.0.9/tools/partitions/boot_app0.bin /usr/src/nuki_hub/build/release/boot_app0.bin + +CMD ["/bin/bash"] \ No newline at end of file diff --git a/Docker/README.md b/Docker/README.md new file mode 100644 index 0000000..17caad5 --- /dev/null +++ b/Docker/README.md @@ -0,0 +1,10 @@ +# Build with Docker + +You can build this project using Docker. Just run the following commands in the console: + +```console +cd Docker +./build_with_docker.sh +``` + +once the script is complete you will find the nuki_nub binary in the `nuki_hub/build/release` folder. diff --git a/Docker/build_with_docker.sh b/Docker/build_with_docker.sh new file mode 100755 index 0000000..8ec1712 --- /dev/null +++ b/Docker/build_with_docker.sh @@ -0,0 +1,6 @@ +set -ex +docker build -f ./Dockerfile -t nuki_hub .. +docker create --name nuki_hub nuki_hub +rm -rf ../build +docker cp nuki_hub:/usr/src/nuki_hub/build/ ../ +docker rm -f nuki_hub From f66319620cbc392081080f538ce2eb2e3fc3645c Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 6 Feb 2024 17:15:00 +0100 Subject: [PATCH 06/10] Make Opener Sound Level a configurable number in HA --- Network.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Network.cpp b/Network.cpp index fe48b7c..a0366ff 100644 --- a/Network.cpp +++ b/Network.cpp @@ -1100,7 +1100,7 @@ void Network::publishHASSConfigLedBrightness(char *deviceType, const char *baseT void Network::publishHASSConfigSoundLevel(char *deviceType, const char *baseTopic, char *name, char *uidString) { - publishHassTopic("sensor", + publishHassTopic("number", "sound_level", uidString, "_sound_level", @@ -1111,11 +1111,13 @@ void Network::publishHASSConfigSoundLevel(char *deviceType, const char *baseTopi deviceType, "", "", - "diagnostic", + "config", mqtt_topic_config_sound_level, { { "ic", "mdi:volume-source" }, { "min", "0" }, - { "max", "255" }}); + { "max", "255" }, + { "mode", "slider" }, + { "step", "25.5" }}); } @@ -1366,6 +1368,18 @@ void Network::removeHASSConfig(char* uidString) path.concat("/battery_level/config"); _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); + path = discoveryTopic; + path.concat("/sensor/"); + path.concat(uidString); + path.concat("/sound_level/config"); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); + + path = discoveryTopic; + path.concat("/number/"); + path.concat(uidString); + path.concat("/sound_level/config"); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); + path = discoveryTopic; path.concat("/binary_sensor/"); path.concat(uidString); From 9463ee99788158e6602ba6c2e40b1449c4ff695e Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 6 Feb 2024 20:50:50 +0100 Subject: [PATCH 07/10] Fix Home Assistant discovery topics --- Network.cpp | 66 ++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/Network.cpp b/Network.cpp index fe48b7c..0581b3c 100644 --- a/Network.cpp +++ b/Network.cpp @@ -749,7 +749,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "battery low", name, baseTopic, - mqtt_topic_battery_critical, + String("~") + mqtt_topic_battery_critical, deviceType, "battery", "", @@ -768,7 +768,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "keypad battery low", name, baseTopic, - mqtt_topic_battery_keypad_critical, + String("~") + mqtt_topic_battery_keypad_critical, deviceType, "battery", "", @@ -790,7 +790,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "battery voltage", name, baseTopic, - mqtt_topic_battery_voltage, + String("~") + mqtt_topic_battery_voltage, deviceType, "voltage", "measurement", @@ -806,7 +806,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "trigger", name, baseTopic, - mqtt_topic_lock_trigger, + String("~") + mqtt_topic_lock_trigger, deviceType, "", "", @@ -822,7 +822,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "MQTT connected", name, baseTopic, - mqtt_topic_mqtt_connection_state, + _lockPath + mqtt_topic_mqtt_connection_state, deviceType, "", "", @@ -840,12 +840,12 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Restart NUKI Hub", name, baseTopic, - mqtt_topic_reset, + String("~") + mqtt_topic_reset, deviceType, "", "", "diagnostic", - mqtt_topic_reset, + String("~") + mqtt_topic_reset, { { "ic", "mdi:restart" }, { "pl_on", "1" }, { "pl_off", "0" }, @@ -860,7 +860,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Firmware version", name, baseTopic, - mqtt_topic_info_firmware_version, + String("~") + mqtt_topic_info_firmware_version, deviceType, "", "", @@ -877,7 +877,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Hardware version", name, baseTopic, - mqtt_topic_info_hardware_version, + String("~") + mqtt_topic_info_hardware_version, deviceType, "", "", @@ -894,7 +894,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "NUKI Hub version", name, baseTopic, - mqtt_topic_info_nuki_hub_version, + _lockPath + mqtt_topic_info_nuki_hub_version, deviceType, "", "", @@ -911,12 +911,12 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "LED enabled", name, baseTopic, - mqtt_topic_config_led_enabled, + String("~") + mqtt_topic_config_led_enabled, deviceType, "", "", "config", - mqtt_topic_config_led_enabled, + String("~") + mqtt_topic_config_led_enabled, { { "ic", "mdi:led-variant-on" }, { "pl_on", "1" }, { "pl_off", "0" }, @@ -931,12 +931,12 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Button enabled", name, baseTopic, - mqtt_topic_config_button_enabled, + String("~") + mqtt_topic_config_button_enabled, deviceType, "", "", "config", - mqtt_topic_config_button_enabled, + String("~") + mqtt_topic_config_button_enabled, { { "ic", "mdi:radiobox-marked" }, { "pl_on", "1" }, { "pl_off", "0" }, @@ -951,12 +951,12 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Unlatch", name, baseTopic, - mqtt_topic_mqtt_connection_state, + _lockPath + mqtt_topic_mqtt_connection_state, deviceType, "", "", "", - mqtt_topic_lock_action, + String("~") + mqtt_topic_lock_action, { { "enabled_by_default", "false" }, { "pl_prs", "unlatch" }}); @@ -974,12 +974,12 @@ void Network::publishHASSConfigAdditionalButtons(char *deviceType, const char *b "Lock 'n' Go", name, baseTopic, - mqtt_topic_mqtt_connection_state, + _lockPath + mqtt_topic_mqtt_connection_state, deviceType, "", "", "", - mqtt_topic_lock_action, + String("~") + mqtt_topic_lock_action, { { "enabled_by_default", "false" }, { "pl_prs", "lockNgo" }}); @@ -991,12 +991,12 @@ void Network::publishHASSConfigAdditionalButtons(char *deviceType, const char *b "Lock 'n' Go with unlatch", name, baseTopic, - mqtt_topic_mqtt_connection_state, + _lockPath + mqtt_topic_mqtt_connection_state, deviceType, "", "", "", - mqtt_topic_lock_action, + String("~") + mqtt_topic_lock_action, { { "enabled_by_default", "false" }, { "pl_prs", "lockNgoUnlatch" }}); } @@ -1016,7 +1016,7 @@ void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic, "battery level", name, baseTopic, - mqtt_topic_battery_level, + String("~") + mqtt_topic_battery_level, deviceType, "battery", "measurement", @@ -1041,7 +1041,7 @@ void Network::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopi "door sensor", name, baseTopic, - mqtt_topic_lock_door_sensor_state, + String("~") + mqtt_topic_lock_door_sensor_state, deviceType, "door", "", @@ -1066,7 +1066,7 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi "ring detect", name, baseTopic, - mqtt_topic_lock_state, + String("~") + mqtt_topic_lock_state, deviceType, "sound", "", @@ -1087,12 +1087,12 @@ void Network::publishHASSConfigLedBrightness(char *deviceType, const char *baseT "LED brightness", name, baseTopic, - mqtt_topic_config_led_brightness, + String("~") + mqtt_topic_config_led_brightness, deviceType, "", "", "config", - mqtt_topic_config_led_brightness, + String("~") + mqtt_topic_config_led_brightness, { { "ic", "mdi:brightness-6" }, { "min", "0" }, { "max", "5" }}); @@ -1107,12 +1107,12 @@ void Network::publishHASSConfigSoundLevel(char *deviceType, const char *baseTopi "Sound level", name, baseTopic, - mqtt_topic_config_sound_level, + String("~") + mqtt_topic_config_sound_level, deviceType, "", "", "diagnostic", - mqtt_topic_config_sound_level, + String("~") + mqtt_topic_config_sound_level, { { "ic", "mdi:volume-source" }, { "min", "0" }, { "max", "255" }}); @@ -1128,7 +1128,7 @@ void Network::publishHASSConfigAccessLog(char *deviceType, const char *baseTopic "Last action authorization", name, baseTopic, - mqtt_topic_lock_log, + String("~") + mqtt_topic_lock_log, deviceType, "", "", @@ -1147,7 +1147,7 @@ void Network::publishHASSConfigKeypadAttemptInfo(char *deviceType, const char *b "Keypad status", name, baseTopic, - mqtt_topic_lock_log, + String("~") + mqtt_topic_lock_log, deviceType, "", "", @@ -1175,7 +1175,7 @@ void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic, "wifi signal strength", name, baseTopic, - mqtt_topic_wifi_rssi, + _lockPath + mqtt_topic_wifi_rssi, deviceType, "signal_strength", "measurement", @@ -1198,7 +1198,7 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic, "bluetooth signal strength", name, baseTopic, - mqtt_topic_lock_rssi, + String("~") + mqtt_topic_lock_rssi, deviceType, "signal_strength", "measurement", @@ -1245,7 +1245,7 @@ void Network::publishHassTopic(const String& mqttDeviceType, { json["dev_cla"] = deviceClass; } - json["stat_t"] = String("~") + stateTopic; + json["stat_t"] = stateTopic; if(stateClass != "") { @@ -1257,7 +1257,7 @@ void Network::publishHassTopic(const String& mqttDeviceType, } if(commandTopic != "") { - json["cmd_t"] = String("~") + commandTopic; + json["cmd_t"] = commandTopic; } for(const auto& entry : additionalEntries) From ac58d23a088001c80bd9e43c6c9cf22a4e70b9cf Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 6 Feb 2024 22:13:21 +0100 Subject: [PATCH 08/10] Additional fixes --- Network.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Network.cpp b/Network.cpp index 0581b3c..afe1973 100644 --- a/Network.cpp +++ b/Network.cpp @@ -951,7 +951,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Unlatch", name, baseTopic, - _lockPath + mqtt_topic_mqtt_connection_state, + "", deviceType, "", "", @@ -974,7 +974,7 @@ void Network::publishHASSConfigAdditionalButtons(char *deviceType, const char *b "Lock 'n' Go", name, baseTopic, - _lockPath + mqtt_topic_mqtt_connection_state, + "", deviceType, "", "", @@ -991,7 +991,7 @@ void Network::publishHASSConfigAdditionalButtons(char *deviceType, const char *b "Lock 'n' Go with unlatch", name, baseTopic, - _lockPath + mqtt_topic_mqtt_connection_state, + "", deviceType, "", "", @@ -1245,8 +1245,12 @@ void Network::publishHassTopic(const String& mqttDeviceType, { json["dev_cla"] = deviceClass; } - json["stat_t"] = stateTopic; - + + if(stateTopic != "") + { + json["stat_t"] = stateTopic; + } + if(stateClass != "") { json["stat_cla"] = stateClass; @@ -1259,6 +1263,8 @@ void Network::publishHassTopic(const String& mqttDeviceType, { json["cmd_t"] = commandTopic; } + + json["avty"]["t"] = _lockPath + mqtt_topic_mqtt_connection_state; for(const auto& entry : additionalEntries) { From 9ac5b4a0194c42a1f05916ae3628d1a9ae7a07a8 Mon Sep 17 00:00:00 2001 From: iranl Date: Wed, 7 Feb 2024 21:06:35 +0100 Subject: [PATCH 09/10] 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/"); From 2cac62c0514b8627466b88558abf288f07b72357 Mon Sep 17 00:00:00 2001 From: iranl Date: Fri, 9 Feb 2024 09:02:56 +0100 Subject: [PATCH 10/10] Optimize HA entity naming --- Network.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Network.cpp b/Network.cpp index c643a0b..23a1730 100644 --- a/Network.cpp +++ b/Network.cpp @@ -759,7 +759,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "battery_low", uidString, "_battery_low", - "battery low", + "Battery low", name, baseTopic, String("~") + mqtt_topic_battery_critical, @@ -778,7 +778,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "keypad_battery_low", uidString, "_keypad_battery_low", - "keypad battery low", + "Keypad battery low", name, baseTopic, String("~") + mqtt_topic_battery_keypad_critical, @@ -800,7 +800,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "battery_voltage", uidString, "_battery_voltage", - "battery voltage", + "Battery voltage", name, baseTopic, String("~") + mqtt_topic_battery_voltage, @@ -816,7 +816,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "trigger", uidString, "_trigger", - "trigger", + "Trigger", name, baseTopic, String("~") + mqtt_topic_lock_trigger, @@ -850,7 +850,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "reset", uidString, "_reset", - "Restart NUKI Hub", + "Restart Nuki Hub", name, baseTopic, String("~") + mqtt_topic_reset, @@ -904,7 +904,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "nuki_hub_version", uidString, "_nuki_hub_version", - "NUKI Hub version", + "Nuki Hub version", name, baseTopic, _lockPath + mqtt_topic_info_nuki_hub_version, @@ -921,7 +921,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "nuki_hub_ip", uidString, "_nuki_hub_ip", - "NUKI Hub IP", + "Nuki Hub IP", name, baseTopic, _lockPath + mqtt_topic_info_nuki_hub_ip, @@ -1043,7 +1043,7 @@ void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic, "battery_level", uidString, "_battery_level", - "battery level", + "Battery level", name, baseTopic, String("~") + mqtt_topic_battery_level, @@ -1068,7 +1068,7 @@ void Network::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopi "door_sensor", uidString, "_door_sensor", - "door sensor", + "Door sensor", name, baseTopic, String("~") + mqtt_topic_lock_door_sensor_state, @@ -1093,7 +1093,7 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi "ring", uidString, "_ring_detect", - "ring detect", + "Ring detect", name, baseTopic, String("~") + mqtt_topic_lock_state, @@ -1204,7 +1204,7 @@ void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic, "wifi_signal_strength", uidString, "_wifi_signal_strength", - "wifi signal strength", + "WIFI signal strength", name, baseTopic, _lockPath + mqtt_topic_wifi_rssi, @@ -1227,7 +1227,7 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic, "bluetooth_signal_strength", uidString, "_bluetooth_signal_strength", - "bluetooth signal strength", + "Bluetooth signal strength", name, baseTopic, String("~") + mqtt_topic_lock_rssi,