-
Notifications
You must be signed in to change notification settings - Fork 2
/
appletv_control.sh
executable file
·174 lines (165 loc) · 5.82 KB
/
appletv_control.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
set -e
# Exit immediately for unbound variables.
set -u
length=$#
device=""
io=""
characteristic=""
option=""
ATV_id="AA:BB:CC:DD:EE:FF"
airplay_credentials="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
companion_credentials="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef:0123456789abcdef0123456789abcdef0123456789abcdef"
atvremote_path="/home/pi/.local/bin/"
if [ $length -le 1 ]; then
printf "Usage: $0 Get < AccessoryName > < Characteristic >\n"
printf "Usage: $0 Set < AccessoryName > < Characteristic > < Value >\n"
exit -1
fi
# printf "args =$#\n" # debug
# printf "arg1 =$1\n" # debug
if [ $length -ge 1 ]; then
io=$1
# printf "io=$io\n" # debug
fi
if [ $length -ge 2 ]; then
device=$2
# printf "device = ${device}\n" # debug
fi
if [ $length -ge 3 ]; then
characteristic=$3
# printf "Characteristic = ${characteristic}\n" # debug
fi
if [ $length -ge 4 ]; then
option=$4
# printf "option = ${option}\n" # debug
fi
if [ "${io}" == "Get" ]; then
case $device in
'ATV Power')
case $characteristic in
'On')
# Get Apple TV power state
ATV_POWER_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} power_state)
if [ "${ATV_POWER_STATE}" = "PowerState.On" ]
then
printf "1\n"
else
printf "0\n"
fi
exit 0
;;
*)
printf "UnHandled Get ${device} Characteristic ${characteristic}\n"
exit -1
;;
esac
exit 0
;;
'ATV Play')
case $characteristic in
'On')
# Get Apple TV play status
ATV_PLAYING_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} playing | grep -oP '(?<=Device state: ).*')
if [ "${ATV_PLAYING_STATE}" = "Playing" ]
then
printf "1\n"
else
printf "0\n"
fi
exit 0
;;
*)
printf "UnHandled Get ${device} Characteristic ${characteristic}\n"
exit -1
;;
esac
exit 0
;;
'ATV Video Play')
case $characteristic in
'On')
# Get Apple TV play status and media type
ATV_PLAYING=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} playing)
ATV_PLAYING_STATE=$(echo "$ATV_PLAYING" | grep -oP '(?<=Device state: ).*')
ATV_MEDIA_PLAYING=$(echo "$ATV_PLAYING" | grep -oP '(?<=Media type: ).*')
if [ "${ATV_MEDIA_PLAYING}" = "Video" ]
then
if [ "${ATV_PLAYING_STATE}" = "Playing" ] || [ "${ATV_PLAYING_STATE}" = "Paused" ]
then
# Get active app
ATV_APP=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} app | grep -oP '(?<=App: ).*')
kREGEX_TV='com\.apple\.TVWatchList'
kREGEX_COMPUTERS='com\.apple\.TVHomeSharing'
# Consider video playing only when app is TV or Computers
if [[ $ATV_APP =~ $kREGEX_TV ]] || [[ $ATV_APP =~ $kREGEX_COMPUTERS ]]
then
printf "1\n"
else
printf "0\n"
fi
else
printf "0\n"
fi
else
printf "0\n"
fi
exit 0
;;
*)
printf "UnHandled Get ${device} Characteristic ${characteristic}\n"
exit -1
;;
esac
exit 0
;;
*)
printf "UnHandled Get ${device} Characteristic ${characteristic}\n"
exit -1
;;
esac
fi
if [ "${io}" == 'Set' ]; then
case $device in
'ATV Power')
case $characteristic in
'On')
# Get Apple TV current power state and switch accordingly
ATV_POWER_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} power_state)
if [ "${ATV_POWER_STATE}" = "PowerState.On" ]
then
${atvremote_path}atvremote --id ${ATV_id} --companion-credentials ${companion_credentials} turn_off
else
${atvremote_path}atvremote --id ${ATV_id} --companion-credentials ${companion_credentials} turn_on
fi
exit 0
;;
*)
printf "UnHandled Set ${device} Characteristic ${characteristic}"
exit -1
;;
esac
exit 0
;;
'ATV Play')
case $characteristic in
'On')
# Toggle between play and pause
${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} play_pause
exit 0
;;
*)
printf "UnHandled Set ${device} Characteristic ${characteristic}"
exit -1
;;
esac
exit 0
;;
*)
printf "UnHandled Get ${device} Characteristic ${characteristic}\n"
exit -1
;;
esac
fi
printf "Unknown io command ${io}\n"
exit -1