#!/bin/bash set -e # helppage if [ "$1" == "help" ] || [ "$#" -lt 2 ]; then cat << EOF ---- Hass.IO Cli ---- HomeAssistant: $ hassio homeassistant logs $ hassio homeassistant restart $ hassio homeassistant update $ hassio homeassistant check Host: $ hassio host hardware $ hassio host reboot $ hassio host shutdown $ hassio host update Supervisor $ hassio supervisor logs $ hassio supervisor info $ hassio supervisor reload $ hassio supervisor update EOF fi function call_api() { get_cmd=('info' 'logs' 'hardware') command='POST' if [[ ${get_cmd[*]} =~ $2 ]]; then command='GET' fi if ! api_ret="$(curl -s -X $command "http://hassio/$1/$2")"; then echo "API error: $api_ret" return 1 fi if [ "$2" == "logs" ]; then echo "$api_ret" return 0 fi result="$(echo "$api_ret" | jq --raw-output ".result")" # On error if [ "$result" == "error" ]; then message="$(echo "$api_ret" | jq --raw-output ".message // empty")" echo "Error on $1/$2:" "$message" return 0 fi # On success data="$(echo "$api_ret" | jq 'if .data == {} then empty else .data end')" if [ ! -z "$data" ]; then echo "$data" | jq . else echo "Success $1/$2" fi return 0 } ###### # homeassistant functions if [ "$1" == "homeassistant" ]; then hass_cmd=('logs' 'restart' 'update' 'check') if [[ ! ${hass_cmd[*]} =~ $2 ]]; then echo "No homeassistant command '$2' found!" exit 1 fi call_api "$1" "$2" fi ###### # host functions if [ "$1" == "host" ]; then hass_cmd=('reboot' 'shutdown' 'update' 'hardware') if [[ ! ${hass_cmd[*]} =~ $2 ]]; then echo "No host command '$2' found!" exit 1 fi call_api "$1" "$2" fi ###### # supervisor functions if [ "$1" == "supervisor" ]; then hass_cmd=('logs' 'reload' 'update' 'info') if [[ ! ${hass_cmd[*]} =~ $2 ]]; then echo "No supervisor command '$2' found!" exit 1 fi call_api "$1" "$2" fi