-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdevice-denon-avr.groovy
243 lines (196 loc) · 7.33 KB
/
device-denon-avr.groovy
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Denon / Marantz Network Receiver
* Works on Network Receivers newer than 2012
* SmartThings driver to connect your Denon Network Receiver to SmartThings
*
*/
preferences {
input("destIp", "text", title: "IP", description: "The device IP")
input("destPort", "number", title: "Port", description: "The port you wish to connect", defaultValue: 80)
}
metadata {
definition (name: "Denon / Marantz Network Receiver", namespace: "KristopherKubicki",
author: "kristopher@acm.org") {
capability "Actuator"
capability "Switch"
capability "Polling"
capability "Switch Level"
attribute "mute", "string"
attribute "input", "string"
attribute "inputChan", "enum"
command "mute"
command "unmute"
command "inputSelect", ["string"] //define that inputSelect takes a string of the input name as a parameter
command "inputNext"
command "toggleMute"
}
simulator {
// TODO-: define status and reply messages here
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: false, canChangeBackground: true) {
state "on", label: '${name}', action:"switch.off", backgroundColor: "#79b821", icon:"st.Electronics.electronics16"
state "off", label: '${name}', action:"switch.on", backgroundColor: "#ffffff", icon:"st.Electronics.electronics16"
}
standardTile("poll", "device.poll", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "poll", label: "", action: "polling.poll", icon: "st.secondary.refresh", backgroundColor: "#FFFFFF"
}
standardTile("input", "device.input", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "input", label: '${currentValue}', action: "inputNext", icon: "", backgroundColor: "#FFFFFF"
}
standardTile("mute", "device.mute", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "muted", label: '${name}', action:"unmute", backgroundColor: "#79b821", icon:"st.Electronics.electronics13"
state "unmuted", label: '${name}', action:"mute", backgroundColor: "#ffffff", icon:"st.Electronics.electronics13"
}
controlTile("level", "device.level", "slider", height: 1, width: 2, inactiveLabel: false, range: "(0..100)") {
state "level", label: '${name}', action:"setLevel"
}
main "switch"
details(["switch","input","mute","level","poll"])
}
}
def parse(String description) {
log.debug "Parsing '${description}'"
def map = stringToMap(description)
if(!map.body || map.body == "DQo=") { return }
log.debug "${map.body} "
def body = new String(map.body.decodeBase64())
def statusrsp = new XmlSlurper().parseText(body)
def power = statusrsp.Power.value.text()
if(power == "ON") {
sendEvent(name: "switch", value: 'on')
}
if(power != "" && power != "ON") {
sendEvent(name: "switch", value: 'off')
}
def muteLevel = statusrsp.Mute.value.text()
if(muteLevel == "on") {
sendEvent(name: "mute", value: 'muted')
}
if(muteLevel != "" && muteLevel != "on") {
sendEvent(name: "mute", value: 'unmuted')
}
def inputCanonical = statusrsp.InputFuncSelect.value.text()
def inputTmp = []
//check to see if the VideoSelectLists node is available
if(!statusrsp.VideoSelectLists.isEmpty()){
log.debug "VideoSelectLists is available... parsing"
statusrsp.VideoSelectLists.value.each {
log.debug "$it"
if(it.@index != "ON" && it.@index != "OFF") {
inputTmp.push(it.'@index')
log.debug "Adding Input ${it.@index}"
if(it.toString().trim() == inputCanonical) {
sendEvent(name: "input", value: it.'@index')
}
}
}
}
//if the VideoSelectLists node is not available, let's try the InputFuncList
else if(!statusrsp.InputFuncList.isEmpty()){
log.debug "InputFuncList is available... parsing"
statusrsp.InputFuncList.value.each {
if(it != "ON" && it != "OFF") {
inputTmp.push(it)
//log.debug "Adding Input ${it}"
if(it.toString().trim() == inputCanonical) {
sendEvent(name: "input", value: it)
}
}
}
}
sendEvent(name: "inputChan", value: inputTmp)
if(statusrsp.MasterVolume.value.text()) {
def int volLevel = (int) statusrsp.MasterVolume.value.toFloat() ?: -40.0
volLevel = (volLevel + 80) * 0.9
def int curLevel = 36
try {
curLevel = device.currentValue("level")
} catch(NumberFormatException nfe) {
curLevel = 36
}
if(curLevel != volLevel) {
sendEvent(name: "level", value: volLevel)
}
}
}
def setLevel(val) {
sendEvent(name: "mute", value: "unmuted")
sendEvent(name: "level", value: val)
def int scaledVal = val * 0.9 - 80
request("cmd0=PutMasterVolumeSet%2F$scaledVal")
}
def on() {
sendEvent(name: "switch", value: 'on')
request('cmd0=PutZone_OnOff%2FON')
}
def off() {
sendEvent(name: "switch", value: 'off')
request('cmd0=PutZone_OnOff%2FOFF')
}
def mute() {
sendEvent(name: "mute", value: "muted")
request('cmd0=PutVolumeMute%2FON')
}
def unmute() {
sendEvent(name: "mute", value: "unmuted")
request('cmd0=PutVolumeMute%2FOFF')
}
def toggleMute(){
if(device.currentValue("mute") == "muted") { unmute() }
else { mute() }
}
def inputNext() {
def cur = device.currentValue("input")
def selectedInputs = device.currentValue("inputChan").substring(1,device.currentValue("inputChan").length()-1).split(', ').collect{it}
selectedInputs.push(selectedInputs[0])
log.debug "SELECTED: $selectedInputs"
def semaphore = 0
for(selectedInput in selectedInputs) {
if(semaphore == 1) {
// log.debug "SELECT: ($semaphore) '$selectedInput'"
return inputSelect(selectedInput)
}
if(cur == selectedInput) {
semaphore = 1
}
}
}
def inputSelect(channel) {
sendEvent(name: "input", value: channel )
request("cmd0=PutZone_InputFunction%2F$channel")
}
def poll() {
refresh()
}
def refresh() {
def hosthex = convertIPtoHex(destIp)
def porthex = convertPortToHex(destPort)
device.deviceNetworkId = "$hosthex:$porthex"
def hubAction = new physicalgraph.device.HubAction(
'method': 'GET',
'path': "/goform/formMainZone_MainZoneXml.xml",
'headers': [ HOST: "$destIp:$destPort" ]
)
hubAction
}
def request(body) {
def hosthex = convertIPtoHex(destIp)
def porthex = convertPortToHex(destPort)
device.deviceNetworkId = "$hosthex:$porthex"
def hubAction = new physicalgraph.device.HubAction(
'method': 'POST',
'path': "/MainZone/index.put.asp",
'body': body,
'headers': [ HOST: "$destIp:$destPort" ]
)
hubAction
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02X', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04X', port.toInteger() )
return hexport
}