diff --git a/google_assistant/Dockerfile b/google_assistant/Dockerfile new file mode 100644 index 0000000..4dd873f --- /dev/null +++ b/google_assistant/Dockerfile @@ -0,0 +1,20 @@ +#armhf:FROM multiarch/debian-debootstrap:armhf-stretch +#amd64:FROM multiarch/debian-debootstrap:amd64-stretch + +# Install packages +RUN apt-get update \ + && apt-get install -y python3 python3-dev python3-pip \ + python3-six python3-pyasn1 libportaudio2 \ + && pip3 install google-assistant-library google-auth \ + && apt-get remove -y --purge python3-pip python3-dev \ + && apt autoremove -y \ + && rm -rf /var/lib/apt/lists/* + +# Copy data +COPY run.sh / +COPY hassio_gassistant.py / +COPY asoundrc /root/.asoundrc + +RUN chmod a+x /run.sh + +ENTRYPOINT [ "/run.sh" ] diff --git a/google_assistant/asoundrc b/google_assistant/asoundrc new file mode 100644 index 0000000..21e0a3f --- /dev/null +++ b/google_assistant/asoundrc @@ -0,0 +1,17 @@ +pcm.!default { + type asym + capture.pcm "mic" + playback.pcm "speaker" +} +pcm.mic { + type plug + slave { + pcm "hw:%%MIC%%" + } +} +pcm.speaker { + type plug + slave { + pcm "hw:%%SPEAKER%%" + } +} diff --git a/google_assistant/config.json b/google_assistant/config.json new file mode 100644 index 0000000..2787b85 --- /dev/null +++ b/google_assistant/config.json @@ -0,0 +1,23 @@ +{ + "name": "Google Assistant", + "version": "0.0.3", + "slug": "google_assistant", + "description": "A virtual personal assistant developed by Google", + "url": "https://home-assistant.io/addons/google_assistant/", + "startup": "application", + "boot": "auto", + "arch": ["armhf", "amd64"], + "map": ["share"], + "devices": ["/dev/snd:/dev/snd:rwm"], + "options": { + "mic": "0,0", + "speaker": "1,0", + "service_account": "service_account.json" + }, + "schema": { + "mic": "str", + "speaker": "str", + "service_account": "str" + }, + "image": "homeassistant/{arch}-addon-google_assistant" +} diff --git a/google_assistant/hassio_gassistant.py b/google_assistant/hassio_gassistant.py new file mode 100644 index 0000000..fae5677 --- /dev/null +++ b/google_assistant/hassio_gassistant.py @@ -0,0 +1,26 @@ +"""Hass.IO Google Assistant.""" +import sys + +from google.assistant.library import Assistant +from google.assistant.library.event import EventType + +from google.oauth2 import service_account + + +def process_event(event): + if event.type == EventType.ON_CONVERSATION_TURN_STARTED: + print() + + print(event) + + if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and event.args and not event.args['with_follow_on_turn']): + print() + + +if __name__ == '__main__': + credentials = service_account.Credentials.from_service_account_file(sys.argv[1]) + scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/assistant-sdk-prototype']) + + with Assistant(scoped_credentials) as assistant: + for event in assistant.start(): + process_event(event) diff --git a/google_assistant/run.sh b/google_assistant/run.sh new file mode 100644 index 0000000..eb3ce65 --- /dev/null +++ b/google_assistant/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +CONFIG_PATH=/data/options.json +OAUTH_JSON=/data/auth.json + +SERVICE_ACCOUNT=$(jq --raw-output '.service_account' $CONFIG_PATH) +SPEAKER=$(jq --raw-output '.speaker' $CONFIG_PATH) +MIC=$(jq --raw-output '.mic' $CONFIG_PATH) + +echo "[Info] Show audio device" +aplay -l + +echo "[Info] Setup audio device" +sed -i "s/%%SPEAKER%%/$SPEAKER/g" /root/.asoundrc +sed -i "s/%%MIC%%/$MIC/g" /root/.asoundrc + +# check if a new assistant file exists +if [ -f "/share/$SERVICE_ACCOUNT" ]; then + echo "[Info] Install/Update service account key file" + cp -f "/share/$SERVICE_ACCOUNT" "$OAUTH_JSON" +fi + +exec python3 /hassio_gassistant.py "$OAUTH_JSON" < /dev/null