-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathha
executable file
·134 lines (119 loc) · 3.72 KB
/
ha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
#
# ha.sh - Quick hack to control HA switches from the cmdline
#
# Copyright (C) 2019 Michael Davies <[email protected]>
#
# 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
# of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Or try here: http://www.fsf.org/copyleft/gpl.html
declare -a KNOWN_SWITCHES=($(ha-list-entities.py))
declare -a VALID_STATES=('on' 'off')
BASENAME=$(basename $0)
# Include bash library
command -v bashlib.sh &> /dev/null || \
{ echo >&2 "$(basename $0): Can't find bashlib.sh. Aborting."; exit 1; }
. bashlib.sh
# The base URL and authentication token can be either in the
# environment, or in a file. Either way, we need to retrieve this
# information from somewhere. Fail otherwise.
if [[ -z "$HA_BASE_URL" || -z "$HA_TOKEN" ]]; then
CREDS=${HOME}/.ha.sh
if [ ! -r $CREDS ]; then
printf "%s: Cannot find required credentials file %s\n" $BASENAME $CREDS
exit 1
fi
. $CREDS
fi
# Use DEBUG from the environment, but ensure that we don't error if it's not
if [ -z "$DEBUG" ]; then
DEBUG=0
fi
array_contains () {
# $1 = Thing to look for
# $2 = the array to search
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
check_switch() {
# Check to see if we have a known switch
# $1 == the switch to check
# $2 == the array of switches
local SWITCH=$1
shift
local ARR=("$@")
array_contains $SWITCH "${ARR[@]}"
local ALLOWED=$?
if [ $ALLOWED -eq 1 ]; then
printf "%s: Unknown switch '%s'. Exiting...\n" $BASENAME $SWITCH
exit 2
fi
}
# Check to see if we have a valid state
check_states() {
# Check to see if we have a known switch
# $1 == the state to check
# $2 == the array of states
local STATE=$1
shift
local ARR=("$@")
array_contains $STATE "${ARR[@]}"
local ALLOWED=$?
if [ $ALLOWED -eq 1 ]; then
printf "%s: Unknown state '%s'. Exiting...\n" $BASENAME $STATE
exit 2
fi
}
get_state() {
local SWITCH=$1
local AUTH="Authorization: Bearer $HA_TOKEN"
local CONTENT="Content-Type: application/json"
local CMD=$(curl -s -X GET -H "$AUTH" -H "$CONTENT" ${HA_BASE_URL}/api/states/${SWITCH})
if [ $DEBUG -eq 1 ]; then
echo curl -s -X GET -H \"$AUTH\" -H \"$CONTENT\" ${HA_BASE_URL}/api/states/${SWITCH}
fi
echo $CMD | python3 -m json.tool
}
set_state() {
local SWITCH=$1
local STATE=$2
local AUTH="Authorization: Bearer $HA_TOKEN"
local CONTENT="Content-Type: application/json"
local ENTITY="{\"entity_id\": \"$SWITCH\"}"
if [ $DEBUG -eq 1 ]; then
echo curl -s -X POST -H \"$AUTH\" -H \"$CONTENT\" -d \'$ENTITY\' ${HA_BASE_URL}/api/services/switch/turn_$STATE
fi
local CMD=$(curl -s -X POST -H "$AUTH" -H "$CONTENT" -d "$ENTITY" ${HA_BASE_URL}/api/services/switch/turn_$STATE)
}
is_available jq
if [ $? -eq 1 ]; then
PIPECMD=jq
else
PIPECMD=cat
fi
# cmdline processing
if [ "$#" -eq 1 ]; then
check_switch $1 "${KNOWN_SWITCHES[@]}"
get_state $1 | $PIPECMD
elif [ "$#" -eq 2 ]; then
check_switch $1 "${KNOWN_SWITCHES[@]}"
check_states $2 "${VALID_STATES[@]}"
set_state $1 $2
else
# Unknown command
printf "Usage: %s <switch> [<state>]\n" $BASENAME
exit 3
fi