Skip to content

Instantly share code, notes, and snippets.

@jceloria
Last active December 28, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jceloria/a2bb510cddd9c0e38a66f7ebbfdb4a4d to your computer and use it in GitHub Desktop.
Save jceloria/a2bb510cddd9c0e38a66f7ebbfdb4a4d to your computer and use it in GitHub Desktop.
Home Assistant API info from the commandline
#!/usr/bin/env bash
SELF=${0##*/} SDIR=${0%/*}
########################################################################################################################
# Get home-assistant states
#
# Copyright © 2017 by John Celoria.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
######################################################## config ########################################################
# Set some defaults
VERSION=0.2
SERVER=localhost
####################################################### functions ######################################################
# Print usage information
function help() {
cat << EOF
Usage: ${SELF} [OPTION]... <search string>
Get home-assistant states
-h Display this help message and exit
-s Optional server [Default: ${SERVER}]
-a Return all information available
-q Quiet output
EOF
return
}
########################################################################################################################
# Logging function
function log() {
local level levels=(notice warning crit)
level="+($(IFS='|';echo "${levels[*]}"))"
shopt -s extglob; case ${1} in
${level}) level=${1}; shift ;;
*) level=notice ;;
esac; shopt -u extglob
[[ -z ${RETVAL} ]] && { for RETVAL in "${!levels[@]}"; do
[[ ${levels[${RETVAL}]} = "${level}" ]] && break
done }
logger -s -p ${level} -t "[${SELF}:${FUNCNAME[1]}()]" -- $@;
}
########################################################################################################################
# Log and then exit
function die() { local retval=${RETVAL:-$?}; log "$@"; exit ${retval}; }
########################################################################################################################
# Sanity checks
while getopts ":hs:ad" opt; do
case ${opt} in
h) help >&2; exit 1 ;;
s) SERVER="${OPTARG}"; ;;
a) ALL=1 ;;
d) DRYRUN=1 ;;
\?) echo "Invalid option: -${OPTARG}" >&2 ;;
:) echo "Option -${OPTARG} requires an argument." >&2; exit 1 ;;
esac
done; shift $((${OPTIND} - 1))
req_progs=(logger jq)
for p in ${req_progs[@]}; do
hash "${p}" 2>&- || \
{ die "Required program \"${p}\" not found in \${PATH}."; }
done
######################################################### main #########################################################
function run() {
if [[ ${DRYRUN} -eq 1 ]]; then
echo "$*"; return 0
fi
eval "$@"
}
function main() {
JQ_FILTER='.[]|select(.entity_id|contains("'$*'"))'
[[ ! ${ALL} ]] && JQ_FILTER+='|{entity_id, state}'
run "curl -s -X GET -H 'Content-Type: application/json' http://${SERVER}:8123/api/states | jq '${JQ_FILTER}'"
exit 0
}
main $@
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment