summaryrefslogtreecommitdiff
path: root/playground/shelly-plug.sh
blob: 5619f93e42c894cc4f5f71bee551d54e6a84ad3c (plain)
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
#!/bin/sh
# Control and query a Shelly Plug M Gen 3 via its HTTP RPC API.
#
# Usage:
#   shelly-plug.sh [host] <command>
#
# Commands:
#   status   Show power, voltage, current, energy and temperature
#   on       Turn the plug on
#   off      Turn the plug off
#   toggle   Toggle the plug
#   info     Show device info (model, firmware, mac)
#
# The host defaults to 192.168.1.28 and can be overridden by the first
# argument or the SHELLY_HOST environment variable.
#
# If the device has authentication enabled, the password is read from the
# SHELLY_PASS environment variable, or from ~/.shelly_plug if that is unset
# (the username is always "admin" on Shelly Gen3). Digest auth is used
# automatically when a password is available.

set -eu

DEFAULT_HOST="192.168.1.28"
PASS_FILE="${HOME}/.shelly_plug"

# Argument forms:
#   (none)            -> host=default, command=status
#   <command>         -> host=default, command=<command>
#   <host> <command>  -> explicit host and command
if [ "$#" -ge 2 ]; then
	HOST="$1"
	CMD="$2"
elif [ "$#" -eq 1 ]; then
	HOST="${SHELLY_HOST:-$DEFAULT_HOST}"
	CMD="$1"
else
	HOST="${SHELLY_HOST:-$DEFAULT_HOST}"
	CMD="status"
fi

BASE="http://${HOST}"

# Resolve the password: SHELLY_PASS wins, otherwise read it from PASS_FILE.
PASS="${SHELLY_PASS:-}"
if [ -z "$PASS" ] && [ -r "$PASS_FILE" ]; then
	PASS="$(head -n 1 "$PASS_FILE" | tr -d '\r\n')"
fi

# Build optional digest-auth args when a password is available.
AUTH=""
if [ -n "$PASS" ]; then
	AUTH="--digest -u admin:${PASS}"
fi

rpc() {
	# shellcheck disable=SC2086
	curl -s --max-time 5 $AUTH "${BASE}/rpc/$1"
}

case "$CMD" in
	status)
		rpc "Switch.GetStatus?id=0"
		echo
		;;
	on)
		rpc "Switch.Set?id=0&on=true"
		echo
		;;
	off)
		rpc "Switch.Set?id=0&on=false"
		echo
		;;
	toggle)
		rpc "Switch.Toggle?id=0"
		echo
		;;
	info)
		curl -s --max-time 5 "${BASE}/shelly"
		echo
		;;
	*)
		echo "Unknown command: $CMD" >&2
		echo "Use one of: status, on, off, toggle, info" >&2
		exit 1
		;;
esac