-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrospect.sh
executable file
·69 lines (53 loc) · 2.08 KB
/
introspect.sh
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
verbose=0
if [ "$1" = "-v" ]
then
verbose=1
shift
fi
HA_HOST="$1"
token="$2"
if [ "$3" = "" ]
then
echo "usage: introspect.sh <HA-HOST> <HA-TOKEN> 'python expression'"
exit
fi
# introspect.sh
# simple example of using the ha_introspection custom_component
# define your own library functions on the fly:
lib="
import inspect
import sys
import os
import importlib
global get
get = lambda d, e : d[e] if str(type(d)) in (\"<class 'dict'>\") else d.as_dict()[e] if 'as_dict' in dir(d) else getattr(d,e)
def no_(a):
return [ e for e in a if not e.startswith('_') ]
def head(a,cnt=20):
return a[:cnt]
def fmt(a):
result=''
for n in a:
result += str(n) + '\n'
return result
"
expr=$( sed -e 's/\\/\\\\/g' -e "s/'/\\\'/g" -e 's/"/\\\"/g' -e "s/,/\\\,/g" <<<"$3" )
lib=$( sed -e 's/\\/\\\\/g' -e "s/'/\\\'/g" -e 's/"/\\\"/g' -e "s/,/\\\,/g" <<<"$lib" )
fetch="{% for i in range(1+states.introspection.len.state|int) %}{{ states.introspection['result_' ~ i]['state'] }}{% endfor %}"
details="fetched {{ states.introspection.len.state }}x255 buffers, total_len={{states.introspection.total_len.state }}, truncated={{states.introspection.truncated.state }}"
hass-cli -o json -x -s http://$HA_HOST:8123 --token $token service call ha_introspection.do_introspection --arguments statement="$lib",expression="$expr" >/dev/null
[ "$?" = "0" ] || exit
hass-cli -o json -x -s http://$HA_HOST:8123 --token $token template <(echo "$fetch")
if [ "$verbose" = "1" ]
then
hass-cli -o json -x -s http://$HA_HOST:8123 --token $token template <(echo "$details")
fi
###########################################################
# example uses:
# introspect.sh $HOST $TOKEN "dir()"
# ['call', 'expression', 'hass']
# introspect.sh $HOST $TOKEN "[e for e in dir(hass)[12:] if not e.startswith('_')]"
# ['add_job', 'async_add_executor_job', 'async_add_hass_job', 'async_add_job',...
# introspect.sh $HOST $TOKEN "[e for e in dir(hass.components) if not e.startswith('_')]"
# ['frontend', 'group', 'hassio', 'webhook', 'websocket_api']
###########################################################