forked from sridharavinash/wemo-snitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhubitat-snitch.sh
executable file
·58 lines (48 loc) · 1.51 KB
/
hubitat-snitch.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
#!/bin/sh -x
#
# Toggles a hubitat virtual switch based on microsnitch's log
#
# To make the script automatically run, edit the local.hubitate.OnVideoCallSwitch.plist
# and move it to ~/Library/LaunchAgents
# and install it with launchctl
exec > /tmp/vlog 2>&1
# ACCESS_TOKEN is from your hubitat maker api
if [ -z "$ACCESS_TOKEN" ]; then
echo "Missing ACCESS_TOKEN env var"
exit 1
fi
# Configuration
# Micro Snitch log location
LOG="$HOME/Library/Logs/Micro Snitch.log"
# Name of the Camera event you'd like to listen for
CAMERA="FaceTime HD Camera (Built-in)"
# The hubitat switch id to toggle
SWITCHID="108"
# How frequently to check the log (default: 5s)
POLL_TIME=5
# This uses the maker api, your URL has to change
switch_status=$(http GET "http://192.168.7.46/apps/api/18/devices/$SWITCHID?access_token=$ACCESS_TOKEN" | jq -r '.attributes[0].currentValue')
if [[ ! -f $LOG ]]; then
echo >&2 "Error: No Micro Snitch log found in $LOG."
exit 1
fi
function toggle_switch(){
local action=$1
if [[ "$switch_status" != "$action" ]]; then
# get this url from your maker api
http GET "http://192.168.7.46/apps/api/18/devices/$SWITCHID/$action?access_token=$ACCESS_TOKEN"
echo $action
switch_status=$action
fi
}
while true
do
status=$(grep Camera "$LOG" | awk 'END{o=$10; for (i=11; i<=NF; i++) {o=o" "$i}; print o}')
if [[ "$status" = "active: $CAMERA" ]]; then
toggle_switch on
fi
if [[ "$status" = "inactive: $CAMERA" ]]; then
toggle_switch off
fi
sleep $POLL_TIME
done