-
Notifications
You must be signed in to change notification settings - Fork 0
/
Child MonoPrice 6 Zone Amp Controller.groovy
318 lines (306 loc) · 9.55 KB
/
Child MonoPrice 6 Zone Amp Controller.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
metadata {
definition (name: "Child MonoPrice 6 Zone Amp Controller", namespace: "jorge.martinez", author: "Jorge Martinez") {
capability "Switch"
capability "Initialize"
capability "Actuator"
capability "Switch"
capability "Sensor"
capability "AudioVolume"
attribute "zone" , "NUMBER"
attribute "mediaSource", "NUMBER"
attribute "level" , "NUMBER"
attribute "balance" , "NUMBER"
attribute "bass" , "NUMBER"
attribute "treble" , "NUMBER"
command "Source1"
command "Source2"
command "Source3"
command "Source4"
command "Source5"
command "Source6"
// command "setLevel" ,["NUMBER"]
command "setZone"
command "nextTrack"
command "previousTrack"
}
preferences {
section("Device Settings:"){
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
input name: "Percent",type: "enum", description: "", title: "Percent to dec/enc", options: [[1:1],[2:2],[3:3],[5:5],[10:10],[15:15]], defaultValue: 2
input name: "MaxVolumen", type: "NUMBER", description: "", title: "Max volumen allow", defaultValue: 38
}
}
}
def UpdateData (NewData){
log.debug NewData
def power = NewData.substring(7,9)
if (power.toInteger()){
state.switch = "on"
sendEvent(name: "switch", value: "on", isStateChange: true)
}
if (!power.toInteger()){
state.switch = "off"
sendEvent(name: "switch", value: "off", isStateChange: true)
}
def mute = NewData.substring(9,11)
if (mute.toInteger()){
state.mute = "muted"
sendEvent(name: "mute", value: "muted", isStateChange: true)
}
if (!mute.toInteger()){
state.mute = "unmuted"
sendEvent(name: "mute", value: "unmuted", isStateChange: true)
}
def vol = NewData.substring(13,15)
if (state.volume != vol){
state.volume = vol.toInteger()
sendEvent(name: "volume", value: state.volume.toInteger(), isStateChange: true)
}
def source = NewData.substring(21,23)
if (state.mediaSource != source){
state.mediaSource = source.toInteger()
sendEvent(name: "mediaSource", value: state.mediaSource.toInteger(), isStateChange: true)
}
def Balance = NewData.substring(19,21)
if (state.balance != Balance){
state.balance = Balance.toInteger()
sendEvent(name: "balance", value: state.balance.toInteger(), isStateChange: true)
}
def Bass = NewData.substring(17,19)
if (state.bass != Bass){
state.bass = Bass.toInteger()
sendEvent(name: "bass", value: state.bass.toInteger(), isStateChange: true)
}
def Treble = NewData.substring(15,17)
if (state.treble != Treble){
state.treble = Treble.toInteger()
sendEvent(name: "treble", value: state.treble.toInteger(), isStateChange: true)
}
}
def setZone (){
// sendEvent(name: "zone", value: Zone, isStateChange: true)
// state.zone = Zone
// log.debug device.deviceNetworkId
len = device.deviceNetworkId.length()
state.zone = device.deviceNetworkId.substring(len-2,len).toInteger()
// log.debug "Reading the zone form internal variable "+ state.zone
sendEvent(name: "zone", value: state.zone, isStateChange: true)
}
def nextTrack(){
if(state.mediaSource<6)
{
def newmediaSource = state.mediaSource+1
if (logEnable) {
log.debug "next Source"
log.debug state.mediaSource
}
try {
sendEvent(name: "mediaSource", value: (newmediaSource), isStateChange: true)
state.mediaSource = newmediaSource
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
}
catch (Exception e) {
log.warn "next Source failed: ${e.message}"
}
}
}
def previousTrack(){
if(state.mediaSource>1)
{
def newmediaSource = state.mediaSource-1
if (logEnable) {
log.debug "previous Source"
log.debug state.mediaSource
}
try {
sendEvent(name: "mediaSource", value: (newmediaSource), isStateChange: true)
state.mediaSource = newmediaSource
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
}
catch (Exception e) {
log.warn "previous Source failed: ${e.message}"
}
}
}
def installed() {
log.info('MonoPrice 6 Zone Amp Controller: installed() '+state.name + state.zone)
initialize()
}
def updated(){
log.info('MonoPrice 6 Zone Amp Controller: updated()')
initialize()
// unschedule()
// runEvery1Minute(pollSchedule)
}
def pollSchedule(){
// forcePoll()
}
def initialize(){
log.info('MonoPrice 6 Zone Amp Controller: initialize() '+state.name + state.zone)
// telnetClose()
// telnetConnect([termChars:[13]], settings.IP, settings.port as int, '', '')
}
def setLevel (Value){
if (logEnable) log.debug Value
try {
if (Value>settings.MaxVolumen)
Value = settings.MaxVolumen
if (Value<0)
Value = 0
state.volume = Value
sendEvent(name: "volume", value: state.volume, isStateChange: true)
parent.sendMsg ("Zone${state.zone}vo=${state.volume}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def on() {
if (logEnable) log.debug "trunning on"
try {
state.switch = "on"
sendEvent(name: "switch", value: "on", isStateChange: true)
parent.sendMsg ("Zone${state.zone}pr=01")
}
catch (Exception e) {
log.warn "Call to on failed: ${e.message}"
}
}
def off() {
if (logEnable) log.debug "trunning off"
try {
state.switch = "off"
sendEvent(name: "switch", value: "off", isStateChange: true)
parent.sendMsg ("Zone${state.zone}pr=00")
}
catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def volumeUp() {
if (state.volume<100){
if (logEnable) {
log.debug "Volumen UP ${state.volume}"
}
try {
def newvolume = (state.volume.toInteger() + settings.Percent.toInteger()).toInteger()
log.debug newvolume
if (newvolume.toInteger()>settings.MaxVolumen.toInteger()) //ovewrite volume if > that max allow
newvolume = settings.MaxVolumen.toInteger()
state.volume = newvolume
sendEvent(name: "volume", value: state.volume.toInteger(), isStateChange: true)
parent.sendMsg ("Zone${state.zone}vo=${state.volume}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
}
def volumeDown() {
if (state.volume>0){
if (logEnable) log.debug "Volumen DOWN ${state.volume}"
try {
def newvolume = ((state.volume as long) - (settings.Percent as long))
log.debug newvolume
if (newvolume <0)
newvolume = 0
state.volume = newvolume
sendEvent(name: "volume", value: state.volume, isStateChange: true)
parent.sendMsg ("Zone${state.zone}vo=${state.volume}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
}
def setVolume(Volume) {
if (logEnable) log.debug Volume
try {
if (Volume>settings.MaxVolumen)
Volume = settings.MaxVolumen
if (Volume<0)
Volume = 0
state.volume = Volume
sendEvent(name: "volume", value: state.volume, isStateChange: true)
parent.sendMsg ("Zone${state.zone}vo=${state.volume}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def mute() {
if (logEnable) log.debug "mute"
try {
state.mute = "muted"
sendEvent(name: "mute", value: "muted", isStateChange: true)
parent.sendMsg ("Zone${state.zone}mu=01")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def unmute() {
if (logEnable) log.debug "unmute"
try {
state.mute = "unmuted"
sendEvent(name: "mute", value: "unmuted", isStateChange: true)
parent.sendMsg ("Zone${state.zone}mu=00")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source1(){
if (logEnable) log.debug "Source1"
try {
state.mediaSource = 1
sendEvent(name: "mediaSource", value: 1, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source2(){
if (logEnable) log.debug "Source2"
try {
state.mediaSource = 2
sendEvent(name: "mediaSource", value: 2, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source3(){
if (logEnable) log.debug "Source3"
try {
state.mediaSource = 3
sendEvent(name: "mediaSource", value: 3, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source4(){
if (logEnable) log.debug "Source4"
try {
state.mediaSource = 4
sendEvent(name: "mediaSource", value: 4, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source5(){
if (logEnable) log.debug "Source5"
try {
state.mediaSource = 5
sendEvent(name: "mediaSource", value: 5, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}
def Source6(){
if (logEnable) log.debug "Source6"
try {
state.mediaSource = 6
sendEvent(name: "mediaSource", value: 6, isStateChange: true)
parent.sendMsg ("Zone${state.zone}ch=0${state.mediaSource}")
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}