#!/bin/sh # # Amazon Alexa Remote Control # alex(at)loetzimmer.de # # 2017-10-10: v0.1 initial release # 2017-10-11: v0.2 TuneIn Station Search # 2017-10-11: v0.2a commands on special device "ALL" are executed on all ECHO+WHA # 2017-10-16: v0.3 added playback of library tracks # 2017-10-24: v0.4 added playback information # 2017-11-21: v0.5 added Prime station and playlist # 2017-11-22: v0.6 added Prime historical queue and replaced getopts # 2017-11-25: v0.6a cURL is now configurable # 2017-11-25: v0.7 added multiroom create/delete, playback of library playlist # 2017-11-30: v0.7a added US config, fixed device names containing spaces # 2017-12-07: v0.7b added Bluetooth connect/disconnect # 2017-12-18: v0.7c fixed US version # 2017-12-19: v0.7d fixed AWK csrf extraction on some systems # 2017-12-20: v0.7e moved get_devlist after check_status # 2018-01-08: v0.7f added echo-show to ALL group, TuneIn station can now be up to 6 digits # 2018-01-08: v0.8 added bluetooth list function # 2018-01-10: v0.8a abort when login was unsuccessful # 2018-01-25: v0.8b added echo-spot to ALL group # 2018-01-28: v0.8c added configurable browser string # 2018-02-17: v0.8d no need to write the cookie file on every "check_status" # 2018-02-27: v0.8e added "lastalexa" option for HA-Bridge to send its command to a specific device # (Markus Wennesheimer: https://wennez.wordpress.com/light-on-with-alexa-for-each-room/) # 2018-02-27: v0.9 unsuccessful logins will now give a short info how to debug the login # 2018-03-09: v0.9a workaround for login problem, force curl to use http1.1 # 2018-05-17: v0.9b update browser string and accept language # 2018-05-23: v0.9c update accept language (again) # 2018-06-12: v0.10 introducing TTS and more # (thanks to Michael Geramb and his openHAB2 Amazon Echo Control binding) # https://github.com/openhab/openhab2-addons/tree/master/addons/binding/org.openhab.binding.amazonechocontrol # (thanks to Ralf Otto for implementing this feature in this script) # 2018-06-13: v0.10a added album play of imported library # 2018-06-18: v0.10b added Alex routine execution # 2018-06-18: v0.10b added Alexa routine execution # 2019-01-22: v0.11 added repeat command, ES language options, persistent cookie for hass.io # ### # # (no BASHisms were used, should run with any shell) # - requires cURL for web communication # - (GNU) sed and awk for extraction # - jq as command line JSON parser (optional for the fancy bits) # ########################################## # EMAIL and PASSWORD are set on the keys 'alexa_email' and 'alexa_password' # in your secrets.yaml file. #SECRETS_YAML='/home/homeassistant/.homeassistant/secrets.yaml' SECRETS_YAML='/config/secrets.yaml' LANGUAGE="de-DE" #LANGUAGE="en-GB" #LANGUAGE="es-ES" #LANGUAGE="en-US" AMAZON='amazon.de' #AMAZON='amazon.co.uk' #AMAZON='amazon.es' #AMAZON='amazon.com' ALEXA='layla.amazon.de' #ALEXA='layla.amazon.co.uk' #ALEXA='alexa.amazon.es' #ALEXA='pitangui.amazon.com' # binaries CURL='/usr/bin/curl' AWK='/usr/bin/awk' SED='/bin/sed' # cURL options # -k : if your cURL cannot verify CA certificates, you'll have to trust any # --compressed : if your cURL was compiled with libz you may use compression # --http1.1 : cURL defaults to HTTP/2 on HTTPS connections if available OPTS='--compressed --http1.1' #OPTS='-k --compressed --http1.1' # browser identity BROWSER='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0' # Change your TMP for Hass.io to keep cookie between reboots TMP="/config/tmp" #TMP="/tmp" ########################################### # nothing to configure below here # COOKIE="${TMP}/.alexa.cookie" DEVLIST="${TMP}/.alexa.devicelist.json" GUIVERSION=0 LIST="" LOGOFF="" COMMAND="" TTS="" UTTERANCE="" SEQUENCECMD="" STATIONID="" QUEUE="" SONG="" ALBUM="" ARTIST="" TYPE="" ASIN="" SEEDID="" HIST="" LEMUR="" CHILD="" PLIST="" BLUETOOTH="" LASTALEXA="" usage() { echo "$0 [-d |ALL] -e > |" echo " -b [list|<\"AA:BB:CC:DD:EE:FF\">] | -q | -r <\"station name\"|stationid> |" echo " -s | -t | -u | -v | -w |" echo " -i | -p | -P | -S | -a | -m [device_1 .. device_X] | -lastalexa | -l | -h" echo echo " -e : run command, additional SEQUENCECMDs:" echo " weather,traffic,flashbriefing,goodmorning,singasong,tellstory,speak:'',automation:''" echo " -b : connect/disconnect/list bluetooth device" echo " -q : query queue" echo " -r : play tunein radio" echo " -s : play library track/library album" echo " -t : play Prime playlist" echo " -u : play Prime station" echo " -v : play Prime historical queue" echo " -w : play library playlist" echo " -i : list imported library tracks" echo " -p : list purchased library tracks" echo " -P : list Prime playlists" echo " -S : list Prime stations" echo " -a : list available devices" echo " -m : delete multiroom and/or create new multiroom containing devices" echo " -lastalexa : print device that received the last voice command" echo " -l : logoff" echo " -h : help" } # # Read yaml file and generate variables. # Github driven development. https://gist.github.com/pkuczynski/8665367 # parse_yaml() { local prefix=$2 local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') ${SED} -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | ${AWK} -F$fs '{ indent = length($1)/2; vname[indent] = $2; for (i in vname) {if (i > indent) {delete vname[i]}} if (length($3) > 0) { vn=""; for (i=0; i