From a788dafffb6fc1ff06efd2be2bf7801fd7e1f431 Mon Sep 17 00:00:00 2001
From: Franck Nijhof <frenck@addons.community>
Date: Wed, 20 Mar 2019 21:36:02 +0100
Subject: [PATCH] :hammer: Rewrite add-on onto Bashio

---
 .../rootfs/etc/cont-init.d/50-airsonos.sh     | 11 ++--
 airsonos/rootfs/etc/services.d/airsonos/run   | 51 +++++++++----------
 2 files changed, 29 insertions(+), 33 deletions(-)

diff --git a/airsonos/rootfs/etc/cont-init.d/50-airsonos.sh b/airsonos/rootfs/etc/cont-init.d/50-airsonos.sh
index 0ca1404..2f3a996 100644
--- a/airsonos/rootfs/etc/cont-init.d/50-airsonos.sh
+++ b/airsonos/rootfs/etc/cont-init.d/50-airsonos.sh
@@ -1,21 +1,18 @@
-#!/usr/bin/with-contenv bash
+#!/usr/bin/with-contenv bashio
 # ==============================================================================
 # Community Hass.io Add-ons: AirSonos
 # Checks latency settings before starting the AirSonos server
 # ==============================================================================
-# shellcheck disable=SC1091
-source /usr/lib/hassio-addons/base.sh
-
 declare latency
 
 # Create a configuration file, if it does not exist yet
-if ! hass.file_exists '/config/airsonos.xml'; then
+if ! bashio::fs.file_exists '/config/airsonos.xml'; then
     cp /etc/airsonos.xml /config/airsonos.xml
 fi
 
 # Warn if latency is below 500ms
-latency=$(hass.config.get 'latency_rtp')
+latency=$(bashio::config 'latency_rtp')
 if [[ "${latency}" -lt 500 && "${latency}" -ne 0 ]]; then
-    hass.log.warning \
+    bashio::log.warning \
         'Setting the RTP latency of AirPlay audio below 500ms is not recommended!'
 fi
diff --git a/airsonos/rootfs/etc/services.d/airsonos/run b/airsonos/rootfs/etc/services.d/airsonos/run
index a2ed26a..b1b94ea 100644
--- a/airsonos/rootfs/etc/services.d/airsonos/run
+++ b/airsonos/rootfs/etc/services.d/airsonos/run
@@ -1,14 +1,11 @@
-#!/usr/bin/with-contenv bash
+#!/usr/bin/with-contenv bashio
 # ==============================================================================
 # Community Hass.io Add-ons: AirSonos
 # Runs the AirSonos server
 # ==============================================================================
-# shellcheck disable=SC1091
-source /usr/lib/hassio-addons/base.sh
-
 declare -a options
 
-hass.log.info 'Starting the AirSonos server'
+bashio::log.info 'Starting the AirSonos server...'
 
 # Non-interactive
 options+=(-Z)
@@ -20,34 +17,36 @@ options+=(-x '/config/airsonos.xml')
 options+=(-I)
 
 # Bind to a specific interface
-if hass.config.has_value 'address'; then
-    options+=(-b "$(hass.config.get 'address'):$(hass.config.get 'port')")
+if bashio::config.has_value 'address'; then
+    options+=(-b "$(bashio::config 'address'):$(bashio::config 'port')")
 fi
 
 # Find the matching AirSonos log level
-case "$(hass.string.lower "$(hass.config.get 'log_level')")" in
-    all|trace)
-        options+=(-d 'all=sdebug')
-        ;;
-    debug)
-        options+=(-d 'all=debug')
-        ;;
-    info|notice)
-        options+=(-d 'all=info')
-        ;;
-    warning)
-        options+=(-d 'all=warn')
-        ;;
-    error|fatal|off)
-        options+=(-d 'all=error')
-        ;;
-esac
+if bashio::config.exists 'log_level'; then
+    case "$(bashio::string.lower "$(bashio::config 'log_level')")" in
+        all|trace)
+            options+=(-d 'all=sdebug')
+            ;;
+        debug)
+            options+=(-d 'all=debug')
+            ;;
+        info|notice)
+            options+=(-d 'all=info')
+            ;;
+        warning)
+            options+=(-d 'all=warn')
+            ;;
+        error|fatal|off)
+            options+=(-d 'all=error')
+            ;;
+    esac
+fi
 
 # Set latency
-options+=(-l "$(hass.config.get 'latency_rtp'):$(hass.config.get 'latency_http')")
+options+=(-l "$(bashio::config 'latency_rtp'):$(bashio::config 'latency_http')")
 
 # Drift mode?
-if hass.config.true 'drift'; then
+if bashio::config.true 'drift'; then
     options+=(-r)
 fi