From d0fbf36bb917bc5613f0464ce61ae66ff1a006a0 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 22 Jul 2020 12:32:48 +0200 Subject: [PATCH] OpenZWave 0.5.2 (#1489) * OpenZWave 0.5.2 * Fix typo in code comment * LTW -> LWT * Convert mosquitto.conf dos2unix * Add additional MQTT bridge settings * Change client name to match add-on slug * Extract MQTT logic into a helper * Disable shellcheck SC1091 --- zwave/CHANGELOG.md | 6 +++ zwave/Dockerfile | 1 + zwave/build.json | 4 +- zwave/config.json | 29 ++++--------- zwave/rootfs/etc/cont-finish.d/mqtt.sh | 22 ++++++++++ zwave/rootfs/etc/cont-init.d/mqtt.sh | 20 ++++++++- zwave/rootfs/etc/mosquitto.conf | 40 +++++++++--------- zwave/rootfs/etc/services.d/zwave/run | 9 +++++ zwave/rootfs/usr/lib/mqtt_helper.sh | 56 ++++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 46 deletions(-) create mode 100644 zwave/rootfs/etc/cont-finish.d/mqtt.sh create mode 100644 zwave/rootfs/usr/lib/mqtt_helper.sh diff --git a/zwave/CHANGELOG.md b/zwave/CHANGELOG.md index 114acc6..915276e 100644 --- a/zwave/CHANGELOG.md +++ b/zwave/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.5.2 + +- Fix startup failure due to stray OZW Daemon status retained in MQTT +- Propagate shutdown OZW Daemon status to upstream MQTT on shutdown +- Update OpenZWave to 6cf3729 + ## 0.5.1 - Roll-back alpine to 3.11 and qt 5.12 diff --git a/zwave/Dockerfile b/zwave/Dockerfile index 3e0ecf8..d834a08 100644 --- a/zwave/Dockerfile +++ b/zwave/Dockerfile @@ -16,6 +16,7 @@ RUN \ eudev-libs \ fontconfig \ mosquitto \ + mosquitto-clients \ openssl \ pwgen \ qt5-qtbase \ diff --git a/zwave/build.json b/zwave/build.json index 4fb336e..8b25f49 100644 --- a/zwave/build.json +++ b/zwave/build.json @@ -7,10 +7,10 @@ "aarch64": "homeassistant/aarch64-base:3.11" }, "args": { - "OZW_VERSION": "6c2ca6131cbba4200460256293d719053107a9e8", + "OZW_VERSION": "6cf372959ee88baabfe68742297e7b13f57fef14", "QTOZW_VERSION": "3ad9138f40a856366a7d85fb110b1ed91e1534a0", "QTOZWADMIN_VERSION": "da04ebfbadc57484c487ebc846431eb283e83176", "QTMQTT_VERSION": "5.12.8", "QTWEBSOCKETS_VERSION": "5.12.8" } -} \ No newline at end of file +} diff --git a/zwave/config.json b/zwave/config.json index f9e12e6..a70494e 100644 --- a/zwave/config.json +++ b/zwave/config.json @@ -1,15 +1,9 @@ { "name": "OpenZWave", - "version": "0.5.1", + "version": "0.5.2", "slug": "zwave", "description": "Control a ZWave network with Home Assistant", - "arch": [ - "amd64", - "i386", - "armhf", - "armv7", - "aarch64" - ], + "arch": ["amd64", "i386", "armhf", "armv7", "aarch64"], "url": "https://github.com/home-assistant/hassio-addons/tree/master/zwave", "ingress": true, "ingress_entry": "vnc_lite.html", @@ -28,22 +22,13 @@ "1983/tcp": "ozw-admin port", "5900/tcp": "VNC port" }, - "services": [ - "mqtt:want" - ], - "discovery": [ - "ozw" - ], + "services": ["mqtt:want"], + "discovery": ["ozw"], "auto_uart": true, "gpio": true, "apparmor": false, - "privileged": [ - "SYS_RAWIO" - ], - "devices": [ - "/dev/bus/usb:/dev/bus/usb:rwm", - "/dev/mem:/dev/mem:rw" - ], + "privileged": ["SYS_RAWIO"], + "devices": ["/dev/bus/usb:/dev/bus/usb:rwm", "/dev/mem:/dev/mem:rw"], "options": { "device": null, "network_key": "" @@ -54,4 +39,4 @@ "instance": "int(1,)?" }, "image": "homeassistant/{arch}-addon-zwave" -} \ No newline at end of file +} diff --git a/zwave/rootfs/etc/cont-finish.d/mqtt.sh b/zwave/rootfs/etc/cont-finish.d/mqtt.sh new file mode 100644 index 0000000..2c67b4b --- /dev/null +++ b/zwave/rootfs/etc/cont-finish.d/mqtt.sh @@ -0,0 +1,22 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Ensure upstream MQTT server has the correct OZW status retained on shutdown. +# ============================================================================== +# shellcheck disable=SC1091 +source /usr/lib/mqtt_helper.sh + +declare host +declare password +declare port +declare username + +if bashio::services.available "mqtt"; then + bashio::log.info "Ensure upstream MQTT server has the correct OZW status" + host=$(bashio::services "mqtt" "host") + password=$(bashio::services "mqtt" "password") + port=$(bashio::services "mqtt" "port") + username=$(bashio::services "mqtt" "username") + + mqtt::ensure_ozw_offline_status \ + "${host}" "${port}" "${username}" "${password}" +fi diff --git a/zwave/rootfs/etc/cont-init.d/mqtt.sh b/zwave/rootfs/etc/cont-init.d/mqtt.sh index aa06160..356a670 100644 --- a/zwave/rootfs/etc/cont-init.d/mqtt.sh +++ b/zwave/rootfs/etc/cont-init.d/mqtt.sh @@ -1,7 +1,10 @@ #!/usr/bin/with-contenv bashio # ============================================================================== -# Setup mqtt settings +# Setup MQTT settings # ============================================================================== +# shellcheck disable=SC1091 +source /usr/lib/mqtt_helper.sh + declare host declare password declare port @@ -18,6 +21,11 @@ else ( echo "connection main-mqtt" echo "address ${host}:${port}" + echo "remote_clientid zwave" + echo "local_clientid zwave" + echo "cleansession true" + echo "notifications true" + echo "try_private true" ) >> /etc/mosquitto.conf # Need auth? @@ -33,5 +41,13 @@ else echo "topic # IN OpenZWave/" ) >> /etc/mosquitto.conf - bashio::log.info "Connect to internal MqTT service" + # Ensure upstream MQTT server has the right OZW status + # Workaround for an incorrect retained OZW status in MQTT + # In this case, the LWT is not relayed to the upstream MQTT server. + # https://github.com/home-assistant/hassio-addons/issues/1462 + mqtt::ensure_ozw_offline_status \ + "${host}" "${port}" "${username}" "${password}" + + + bashio::log.info "Connected to internal MQTT service" fi diff --git a/zwave/rootfs/etc/mosquitto.conf b/zwave/rootfs/etc/mosquitto.conf index a06add8..a6d7b68 100644 --- a/zwave/rootfs/etc/mosquitto.conf +++ b/zwave/rootfs/etc/mosquitto.conf @@ -1,20 +1,20 @@ -## -# defaults -listener 1883 -user root - -## -# logging -log_dest stdout - -## -# datastore -persistence true -persistence_location /data/ - -## -# auth -allow_anonymous false -password_file /data/auth.db - -# Dynamic config +## +# defaults +listener 1883 +user root + +## +# logging +log_dest stdout + +## +# datastore +persistence true +persistence_location /data/ + +## +# auth +allow_anonymous false +password_file /data/auth.db + +# Dynamic config diff --git a/zwave/rootfs/etc/services.d/zwave/run b/zwave/rootfs/etc/services.d/zwave/run index b8f42bb..2d0a551 100644 --- a/zwave/rootfs/etc/services.d/zwave/run +++ b/zwave/rootfs/etc/services.d/zwave/run @@ -2,6 +2,9 @@ # ============================================================================== # Start OpenZWave service for zwave radio # ============================================================================== +# shellcheck disable=SC1091 +source /usr/lib/mqtt_helper.sh + export MQTT_PASSWORD export OZW_NETWORK_KEY @@ -20,6 +23,12 @@ fi bashio::log.info "Starting OpenZWave..." bashio::net.wait_for 1883 +# Ensure local MQTT server has the right OZW status +# Workaround for an incorrect retained OZW status in MQTT +# https://github.com/home-assistant/hassio-addons/issues/1462 +mqtt::ensure_ozw_offline_status \ + 127.0.0.1 1883 ozw "${MQTT_PASSWORD}" "${OZW_INSTANCE}" + # Send out discovery information to Home Assistant ./discovery & diff --git a/zwave/rootfs/usr/lib/mqtt_helper.sh b/zwave/rootfs/usr/lib/mqtt_helper.sh new file mode 100644 index 0000000..7c849b5 --- /dev/null +++ b/zwave/rootfs/usr/lib/mqtt_helper.sh @@ -0,0 +1,56 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# MQTT helpers for the zwave add-on. +# ============================================================================== + +# ------------------------------------------------------------------------------ +# Ensure upstream MQTT server has the correct OZW status retained on shutdown. +# +# Arguments: +# $1 MQTT Server host +# $2 MQTT Server port +# $3 MQTT Server username +# $4 MQTT Server password +# $5 OZW Instance ID (optional) +# ------------------------------------------------------------------------------ +function mqtt::ensure_ozw_offline_status() { + local host=${1} + local port=${2} + local username=${3} + local password=${4} + local ozw_instance=${5:-} + local ozw_status + + bashio::log.trace "${FUNCNAME[0]}:" "$@" + + if ! bashio::var.has_value "${ozw_instance}"; then + ozw_instance=1 + if bashio::config.has_value 'instance'; then + ozw_instance=$(bashio::config 'instance') + fi + fi + + ozw_status=$(\ + mosquitto_sub \ + --host "${host}" \ + --port "${port}" \ + --username "${username}" \ + --pw "${password}" \ + -C 1 \ + -W 3 \ + --retained-only \ + --topic "OpenZWave/${ozw_instance}/status/" \ + ) + if bashio::var.has_value "${ozw_status}" \ + && [[ $(bashio::jq "${ozw_status}" ".Status") != "Offline" ]]; + then + mosquitto_pub \ + --host "${host}" \ + --port "${port}" \ + --username "${username}" \ + --pw "${password}" \ + --retain \ + --topic "OpenZWave/${ozw_instance}/status/" \ + --message "$(bashio::var.json "Status" "Offline")" + fi +}