#!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-or-later set -u KIOSK_CONFIG_FILE="${KIOSK_CONFIG_FILE:-$HOME/.config/ha-kiosk/config}" if [[ -r "$KIOSK_CONFIG_FILE" ]]; then # shellcheck source=/dev/null source "$KIOSK_CONFIG_FILE" fi : "${KIOSK_ID:=kiosk}" : "${COMMAND_ROOT:=kiosk/commands}" : "${STATUS_ROOT:=kiosk/status}" AVAILABILITY_TOPIC="${STATUS_ROOT}/${KIOSK_ID}/availability" PING_TOPIC="${COMMAND_ROOT}/${KIOSK_ID}/availability/ping" PUBLISH_INTERVAL_SECONDS="${AVAILABILITY_INTERVAL_SECONDS:-30}" publish_available() { /usr/bin/mosquitto_pub -r -t "$AVAILABILITY_TOPIC" -m "ON" } # Keep the MQTT connection that owns the Last Will open in the background. /usr/bin/mosquitto_sub \ -t "$PING_TOPIC" \ --will-topic "$AVAILABILITY_TOPIC" \ --will-payload "OFF" \ --will-retain & subscriber_pid=$! cleanup() { kill "$subscriber_pid" 2>/dev/null || true wait "$subscriber_pid" 2>/dev/null || true } trap cleanup EXIT INT TERM # Reassert retained availability after Mosquitto reconnects internally. The # Last Will has already published OFF when a broker connection was lost. while kill -0 "$subscriber_pid" 2>/dev/null; do publish_available || true sleep "$PUBLISH_INTERVAL_SECONDS" done wait "$subscriber_pid"