forked from KristopherKubicki/device-yamaha-rx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device-type-yamaha-rx.groovy
268 lines (224 loc) · 8.3 KB
/
device-type-yamaha-rx.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Yamaha Network Receiver
* Works on RX-V*
* SmartThings driver to connect your Yamaha Network Receiver to SmartThings
*
* Loosely based on: https://github.com/BirdAPI/yamaha-network-receivers
* and: http://openremote.org/display/forums/Controlling++RX+2065+Yamaha+Amp
*/
preferences {
input("destIp", "text", title: "IP", description: "The device IP")
input("destPort", "number", title: "Port", description: "The port you wish to connect")
}
metadata {
definition (name: "Yamaha Network Receiver", namespace: "KristopherKubicki", author: "[email protected]") {
capability "Actuator"
capability "Switch"
capability "Polling"
capability "Switch Level"
attribute "mute", "string"
attribute "input", "string"
attribute "model_name", "string"
command "mute"
command "unmute"
command "toggleMute"
command "zoneOneOn"
command "zoneOneOff"
command "zoneTwoOn"
command "zoneTwoOff"
command "configure"
}
simulator {
// TODO-: define status and reply messages here
}
tiles(scale: 2) {
standardTile("main", "device.switch", width: 2, height: 2, canChangeIcon: false, canChangeBackground: false) {
state "on", label: '${name}', action:"switch.off", backgroundColor: "#79b821", icon:"st.Electronics.electronics13"
state "off", label: '${name}', action:"switch.on", backgroundColor: "#ffffff", icon:"st.Electronics.electronics13"
}
standardTile("zone1", "device.switchZone1", width: 2, height: 2, canChangeIcon: false, canChangeBackground: true) {
state "on", label: 'zone 1', action:"zoneOneOff", backgroundColor: "#79b821", icon:"st.Electronics.electronics16"
state "off", label: 'zone 1', action:"zoneOneOn", backgroundColor: "#ffffff", icon:"st.Electronics.electronics16"
}
standardTile("zone2", "device.switchZone2", width: 2, height: 2, canChangeIcon: false, canChangeBackground: true) {
state "on", label: 'zone 2', action:"zoneTwoOff", backgroundColor: "#79b821", icon:"st.Electronics.electronics16"
state "off", label: 'zone 2', action:"zoneTwoOn", backgroundColor: "#ffffff", icon:"st.Electronics.electronics16"
}
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: 5, inactiveLabel: false, range: "(0..100)") {
state "level", label: '${name}', action:"setLevel"
}
valueTile("input", "device.input", width: 6, height: 2, decoration: 'flat') {
state "input", label: '${currentValue}'
}
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"
}
main "main"
details(["main", "zone1", "zone2", "mute", "level", "input"])
}
}
def setOrGet(name, value) {
if(value == null) {
return device.currentValue(name)
}
else {
sendEvent(name: name, value: value)
return value
}
}
def getVolumeXml(state, mute, level) {
if(state == 'off' || mute == 'muted') {
return "<Volume><Mute>On</Mute></Volume>"
}
else {
def int dB = level * 9 - 800
log.debug "scaled $dB"
dB = ((dB/5) as Integer) * 5
log.debug "scaled $dB"
return "<Volume><Lvl><Val>${dB}</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume>"
}
}
def configure(config) {
def input = setOrGet('input', config['input'])
def level = setOrGet('level', config['level'])
def mute = setOrGet('mute', config['mute'])
def zone1 = setOrGet('switchZone1', config['zone1'])
def zone2 = setOrGet('switchZone2', config['zone2'])
log.debug("input[$input], level[$level], mute[$mute], zone1[$zone1], zone2[$zone2]")
def inputXml = "<Input><Input_Sel>${input}</Input_Sel></Input>"
put("<System><Party_Mode><Mode>On</Mode></Party_Mode></System><Main_Zone>${getVolumeXml(zone1, mute, level)}${inputXml}</Main_Zone><Zone_2>${getVolumeXml(zone2, mute, level)}</Zone_2>")
}
def initialize() {
log.debug "Yamaha Initialize"
}
def installed() {
log.debug "Yamaha Installed"
}
def updated() {
log.debug "Yamaha Updated"
}
def parse(String description) {
def map = stringToMap(description)
if(!map.body) { return }
def body = new String(map.body.decodeBase64())
log.debug "body: " + body
def r = new XmlSlurper().parseText(body)
if(r.System.Config.Model_Name) {
log.debug "Model: " + r.System.Config.Model_Name
createEvent(name: "model_name", value: r.System.Config.Model_Name)
}
createEvent(name: "model_name", value: "Go Fuck Yourself")
def power = r.Main_Zone.Basic_Status.Power_Control.Power.text()
if(power == "On") {
sendEvent(name: "switch", value: 'on')
}
if(power != "" && power != "On") {
sendEvent(name: "switch", value: 'off')
}
def inputChan = r.Main_Zone.Basic_Status.Input.Input_Sel.text()
if(inputChan != "") {
log.debug "input: $inputChan"
sendEvent(name: "input", value: inputChan.replace('OPTICAL', 'O'))
}
def muteLevel = r.Main_Zone.Basic_Status.Volume.Mute.text()
if(muteLevel == "On") {
sendEvent(name: "mute", value: 'muted')
}
if(muteLevel != "" && muteLevel != "On") {
sendEvent(name: "mute", value: 'unmuted')
}
if(r.Main_Zone.Basic_Status.Volume.Lvl.Val.text()) {
def int volLevel = r.Main_Zone.Basic_Status.Volume.Lvl.Val.toInteger() ?: -250
log.debug "volume (dB): $volLevel"
volLevel = (((volLevel + 800) / 9)/5)*5
log.debug "volume (percent): $volLevel"
def int curLevel = 65
try {
curLevel = device.currentValue("level")
} catch(org.codehaus.groovy.runtime.typehandling.GroovyCastException nfe) {
curLevel = 65
}
if(curLevel != volLevel) {
sendEvent(name: "level", value: volLevel)
}
}
}
def setLevel(value) {
log.debug "setLevel($value)"
configure(level: value)
}
def on(args) {
log.debug "on()"
sendEvent(name: "switch", value: 'on')
put('<Main_Zone><Power_Control><Power>On</Power></Power_Control></Main_Zone>')
}
def off() {
log.debug "off()"
sendEvent(name: "switch", value: 'off')
put('<Main_Zone><Power_Control><Power>Standby</Power></Power_Control></Main_Zone>')
}
def zoneOneOn(evt) {
log.debug "zoneOneOn()"
configure(zone1: 'on')
}
def zoneOneOff(evt) {
log.debug "zoneOneOff()"
configure(zone1: 'off')
}
def zoneTwoOn() {
log.debug "zoneTwoOn()"
configure(zone2: 'on')
}
def zoneTwoOff() {
log.debug "zoneTwoOff()"
configure(zone2: 'off')
}
def toggleMute(){
device.currentValue("mute") == "muted" ? unmute() : mute()
}
def mute() {
configure(mute: 'muted')
}
def unmute() {
configure(mute: 'unmuted')
}
def poll() {
log.debug "poll"
refresh()
}
def refresh() {
log.debug "refresh"
get('<System><Config>GetParam</Config></System>');
}
def get(body) {
request('<YAMAHA_AV cmd="GET">' + body + '</YAMAHA_AV>')
}
def put(body) {
request('<YAMAHA_AV cmd="PUT">' + body + '</YAMAHA_AV>')
}
def request(body) {
log.debug "request: " + body
def hosthex = convertIPtoHex(destIp)
def porthex = convertPortToHex(destPort)
device.deviceNetworkId = "$hosthex:$porthex"
def hubAction = new physicalgraph.device.HubAction(
'method': 'POST',
'path': "/YamahaRemoteControl/ctrl",
'body': body,
'headers': [ HOST: "$destIp:$destPort" ]
)
//log.debug "HUB Action: $hubAction"
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
}