diff --git a/snips/CHANGELOG.md b/snips/CHANGELOG.md index 68f272c..93479fa 100644 --- a/snips/CHANGELOG.md +++ b/snips/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 4 +- Use new Hass.io Services API and cleanup config + ## 3.3 - Updated to latest snips 0.58.3 - NOTE: This version requires an updated assistant from the Snips Console. diff --git a/snips/Dockerfile b/snips/Dockerfile index defb045..35bfbc2 100644 --- a/snips/Dockerfile +++ b/snips/Dockerfile @@ -9,9 +9,9 @@ COPY customtts.sh /usr/bin RUN chmod a+x /run.sh \ && chmod a+x /usr/bin/customtts.sh \ && apt-get update \ - && apt-get install -y mpg123 curl \ + && apt-get install -y mpg123 curl tzdata \ && apt-get install -y snips-asr-injection || true \ - && apt-get clean all \ + && rm -rf /var/lib/apt/lists/* \ && curl -L -o /assistant_Hass_de.zip https://s3.amazonaws.com/hassio-addons-data/assistant_Hass_de.zip \ && curl -L -o /assistant_Hass_en.zip https://s3.amazonaws.com/hassio-addons-data/assistant_Hass_en.zip \ && curl -L -o /assistant_Hass_fr.zip https://s3.amazonaws.com/hassio-addons-data/assistant_Hass_fr.zip diff --git a/snips/config.json b/snips/config.json index d712581..019c756 100644 --- a/snips/config.json +++ b/snips/config.json @@ -1,27 +1,18 @@ { "name": "Snips.AI", - "version": "3.3", + "version": "4", "slug": "snips", "description": "Local voice control platform", "url": "https://home-assistant.io/addons/snips/", "startup": "application", "boot": "auto", "arch": ["armhf", "amd64"], - "ports": { - "1883/tcp": 9898 - }, "map": ["share"], "tmpfs": "size=10m", "audio": true, + "services": ["mqtt:need"], "homeassistant_api": true, "options": { - "mqtt_bridge": { - "active": true, - "host": "core-mosquitto", - "port": 1883, - "user": "", - "password": "" - }, "assistant": "assistant.zip", "language": "en", "custom_tts": { @@ -30,13 +21,6 @@ } }, "schema": { - "mqtt_bridge": { - "active": "bool", - "host": "str", - "port": "port", - "user": "str", - "password": "str" - }, "assistant": "str", "language": "match(en|de|fr)", "custom_tts": { diff --git a/snips/customtts.sh b/snips/customtts.sh index 3f69129..ef4e9cf 100644 --- a/snips/customtts.sh +++ b/snips/customtts.sh @@ -1,30 +1,29 @@ -#!/bin/sh +#!/bin/bash #set -x -API_KEY=$HASSIO_TOKEN -PLATFORM=$1 -FILE=$2 -LANG=$3 -TEXT=$4 +API_KEY="$HASSIO_TOKEN" +PLATFORM="$1" +FILE="$2" +LANG="$3" +TEXT="$4" -MESSAGE="\"{\\\"message\\\": \\\"$TEXT\\\", \\\"platform\\\": \\\"$PLATFORM\\\"}\"" -echo "$MESSAGE" +MESSAGE="\"{\\\"message\\\": \\\"${TEXT}\\\", \\\"platform\\\": \\\"${PLATFORM}\\\"}\"" +echo "${MESSAGE}" -RESPONSE=$(eval curl -s -H \"x-ha-access: "$API_KEY"\" -H \"Type: application/json\" http://hassio/homeassistant/api/tts_get_url -d "$MESSAGE") -if [ "$RESPONSE" = "" ]; then +RESPONSE=$(eval curl -s -d "${MESSAGE}" -H \"Authorization: Bearer "${API_KEY}"\" -H \"Type: application/json\" http://hassio/homeassistant/api/tts_get_url) +if [ -z "${RESPONSE}" ]; then exit 1 fi -echo "$RESPONSE" +echo "${RESPONSE}" -URL=$(echo "$RESPONSE" | jq --raw-output '.url') -if [ "$URL" = "" ]; then +URL="$(echo "${RESPONSE}" | jq --raw-output '.url')" +if [ -z "${URL}" ]; then exit 1 fi -rm /tmpfs/temp.mp3 -curl -s -H "x-ha-access: $API_KEY" "$URL" -o /tmpfs/temp.mp3 +rm -f /tmpfs/temp.mp3 +curl -s -H "Authorization: Bearer ${API_KEY}" "${URL}" -o /tmpfs/temp.mp3 if [ -f /tmpfs/temp.mp3 ]; then - /usr/bin/mpg123 -w "$FILE" /tmpfs/temp.mp3 + /usr/bin/mpg123 -w "${FILE}" /tmpfs/temp.mp3 fi -rm /tmpfs/temp.mp3 - +rm -f /tmpfs/temp.mp3 diff --git a/snips/run.sh b/snips/run.sh index 471b578..e128fb9 100644 --- a/snips/run.sh +++ b/snips/run.sh @@ -3,13 +3,12 @@ set -e CONFIG_PATH=/data/options.json -MQTT_BRIDGE=$(jq --raw-output '.mqtt_bridge.active' $CONFIG_PATH) ASSISTANT=$(jq --raw-output '.assistant' $CONFIG_PATH) LANG=$(jq --raw-output '.language // "en"' $CONFIG_PATH) CUSTOMTTS=$(jq --raw-output '.custom_tts.active' $CONFIG_PATH) -PLATFORM=$(jq --raw-output '.custom_tts.platform' $CONFIG_PATH) +MQTT_CONFIG= -echo "[INFO] LANG: $LANG" +echo "[INFO] LANG: ${LANG}" echo "[INFO] Checking for /share/snips.toml" if [ -f "/share/snips.toml" ]; then @@ -17,40 +16,44 @@ if [ -f "/share/snips.toml" ]; then cp -v /share/snips.toml /etc/ fi -if [ "$CUSTOMTTS" == "true" ]; then - if [ -z "$PLATFORM" ]; then +if [ "${CUSTOMTTS}" == "true" ]; then + PLATFORM=$(jq --raw-output '.custom_tts.platform' $CONFIG_PATH) + + if [ -z "${PLATFORM}" ]; then echo "[ERROR] - platform must be set to use custom tts!" else echo "[INFO] - Using custom tts" - echo "provider = \"customtts\"" >> /etc/snips.toml - echo "customtts = { command = [\"/usr/bin/customtts.sh\", \"$PLATFORM\", \"%%OUTPUT_FILE%%\", \"$LANG\", \"%%TEXT%%\"] }" >> /etc/snips.toml + ( + echo "provider = \"customtts\"" + echo "customtts = { command = [\"/usr/bin/customtts.sh\", \"${PLATFORM}\", \"%%OUTPUT_FILE%%\", \"${LANG}\", \"%%TEXT%%\"] }" + ) >> /etc/snips.toml fi else echo "[INFO] - Using default tts (picotts)" fi -# mqtt bridge -if [ "$MQTT_BRIDGE" == "true" ]; then - HOST=$(jq --raw-output '.mqtt_bridge.host' $CONFIG_PATH) - PORT=$(jq --raw-output '.mqtt_bridge.port' $CONFIG_PATH) - USER=$(jq --raw-output '.mqtt_bridge.user' $CONFIG_PATH) - PASSWORD=$(jq --raw-output '.mqtt_bridge.password' $CONFIG_PATH) +# Use Hass.io mqtt services +if MQTT_CONFIG="$(curl -s -f -H "X-Hassio-Key: ${HASSIO_TOKEN}" http://hassio/services/mqtt)"; then + HOST="$(echo "${MQTT_CONFIG}" | jq --raw-output '.data.host')" + PORT="$(echo "${MQTT_CONFIG}" | jq --raw-output '.data.port')" + USER="$(echo "${MQTT_CONFIG}" | jq --raw-output '.data.username')" + PASSWORD="$(echo "${MQTT_CONFIG}" | jq --raw-output '.data.password')" - echo "[INFO] Setup internal mqtt bridge" + echo "[INFO] Setup Hass.io mqtt service to ${HOST}" - { + ( echo "connection main-mqtt" - echo "address $HOST:$PORT" - } >> /etc/mosquitto.conf + echo "address ${HOST}:${PORT}" + ) >> /etc/mosquitto.conf - if [ -n "$USER" ]; then - { - echo "username $USER" - echo "password $PASSWORD" - } >> /etc/mosquitto.conf + if [ -n "${USER}" ]; then + ( + echo "username ${USER}" + echo "password ${PASSWORD}" + ) >> /etc/mosquitto.conf fi - { + ( echo "topic hermes/intent/# out" echo "topic hermes/hotword/toggleOn out" echo "topic hermes/hotword/toggleOff out" @@ -60,26 +63,29 @@ if [ "$MQTT_BRIDGE" == "true" ]; then echo "topic hermes/audioServer/+/playBytes/# out" echo "topic hermes/audioServer/+/playFinished out" echo "topic # IN hermes/" - } >> /etc/mosquitto.conf + ) >> /etc/mosquitto.conf +else + echo "[ERROR] No Hass.io mqtt service found!" + exit 1 fi echo "[INFO] Start internal mqtt broker" mosquitto -c /etc/mosquitto.conf & -echo "[INFO] Checking for updated $ASSISTANT in /share" +echo "[INFO] Checking for updated ${ASSISTANT} in /share" # check if a new assistant file exists -if [ -f "/share/$ASSISTANT" ]; then +if [ -f "/share/${ASSISTANT}" ]; then echo "[INFO] Install/Update snips assistant" rm -rf /usr/share/snips/assistant - unzip -o -u "/share/$ASSISTANT" -d /usr/share/snips -# otherwise use the default + unzip -o -u "/share/${ASSISTANT}" -d /usr/share/snips +# otherwise use the default else - echo "[INFO] Checking for /assistant_Hass_$LANG.zip" - if [ -f "/assistant_Hass_$LANG.zip" ]; then - echo "[INFO] - Using default assistant_Hass_$LANG.zip" + echo "[INFO] Checking for /assistant_Hass_${LANG}.zip" + if [ -f "/assistant_Hass_${LANG}.zip" ]; then + echo "[INFO] - Using default assistant_Hass_${LANG}.zip" rm -rf /usr/share/snips/assistant - unzip -o -u "/assistant_Hass_$LANG.zip" -d /usr/share/snips + unzip -o -u "/assistant_Hass_${LANG}.zip" -d /usr/share/snips else echo "[ERROR] Could not find assistant!" fi