forked from artmg/domoticz-denon-marantz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DenonMarantz.py
417 lines (377 loc) · 19.2 KB
/
DenonMarantz.py
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#
# Denon & Marantz AVR plugin
#
# Author: Dnpwwo, 2016 - 2017, Artemgy 2017
#
# Mode3 ("Sources") needs to have '|' delimited names of sources that the Denon knows about.
# The Selector can be changed afterwards to any text and the plugin will still map to the actual Denon name.
#
"""
<plugin key="DenonMarantz" version="2.5.5" name="Denon / Marantz AVR Amplifier" author="dnpwwo/artemgy" wikilink="" externallink="http://www.denon.co.uk/uk">
<description>
Denon & Marantz AVR plugin.<br/><br/>
"Sources" need to have '|' delimited names of sources that the Denon knows about from the technical manual.<br/>
The Sources Selector(s) can be changed after initial creation to any text and the plugin will still map to the actual Denon name.<br/><br/>
Devices will be created in the Devices Tab only and will need to be manually made active.
</description>
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="127.0.0.1"/>
<param field="Port" label="Port" width="30px" required="true" default="23"/>
<param field="Mode2" label="Startup Delay" width="50px" required="true">
<options>
<option label="2" value="2"/>
<option label="3" value="3"/>
<option label="4" value="4" default="true" />
<option label="5" value="5"/>
<option label="6" value="6"/>
<option label="7" value="7"/>
<option label="10" value="10"/>
</options>
</param>
<param field="Mode3" label="Sources" width="550px" required="true" default="Off|DVD|VDP|TV|CD|DBS|Tuner|Phono|VCR-1|VCR-2|V.Aux|CDR/Tape|AuxNet|AuxIPod"/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import base64
import datetime
class BasePlugin:
isConnected = False
nextConnect = 3
oustandingPings = 0
powerOn = False
mainOn = False
mainSource = 0
mainVolume1 = 0
zone2On = False
zone2Source = 0
zone2Volume = 0
zone3On = False
zone3Source = 0
zone3Volume = 0
ignoreMessages = "|SS|SV|SD|MS|PS|CV|SY|TP|"
selectorMap = {}
pollingDict = {"PW":"ZM?\r", "ZM":"SI?\r", "SI":"MV?\r", "MV":"MU?\r", "MU":"PW?\r" }
lastMessage = ""
lastHeartbeat = datetime.datetime.now()
SourceOptions = {}
def onStart(self):
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
self.SourceOptions = {'LevelActions': '|'*Parameters["Mode3"].count('|'),
'LevelNames': Parameters["Mode3"],
'LevelOffHidden': 'false',
'SelectorStyle': '1'}
if (len(Devices) == 0):
Domoticz.Device(Name="Power", Unit=1, TypeName="Switch", Image=5).Create()
Domoticz.Device(Name="Main Zone", Unit=2, TypeName="Selector Switch", Switchtype=18, Image=5, Options=self.SourceOptions).Create()
Domoticz.Device(Name="Main Volume", Unit=3, Type=244, Subtype=73, Switchtype=7, Image=8).Create()
else:
if (2 in Devices and (len(Devices[2].sValue) > 0)):
self.mainSource = int(Devices[2].sValue)
self.mainOn = (Devices[2].nValue != 0)
if (3 in Devices and (len(Devices[3].sValue) > 0)):
self.mainVolume1 = int(Devices[3].sValue) if (Devices[3].nValue != 0) else int(Devices[3].sValue)*-1
if (4 in Devices and (len(Devices[4].sValue) > 0)):
self.zone2Source = int(Devices[4].sValue)
self.zone2On = (Devices[4].nValue != 0)
if (5 in Devices and (len(Devices[5].sValue) > 0)):
self.zone2Volume = int(Devices[5].sValue) if (Devices[5].nValue != 0) else int(Devices[5].sValue)*-1
if (6 in Devices and (len(Devices[6].sValue) > 0)):
self.zone3Source = int(Devices[6].sValue)
self.zone3On = (Devices[6].nValue != 0)
if (7 in Devices and (len(Devices[7].sValue) > 0)):
self.zone3Volume = int(Devices[7].sValue) if (Devices[7].nValue != 0) else int(Devices[7].sValue)*-1
if (1 in Devices):
self.powerOn = (self.mainOn or self.zone2On or self.zone3On)
DumpConfigToLog()
dictValue=0
for item in Parameters["Mode3"].split('|'):
self.selectorMap[dictValue] = item
dictValue = dictValue + 10
Domoticz.Transport("TCP/IP", Parameters["Address"], Parameters["Port"])
Domoticz.Protocol("Line")
Domoticz.Connect()
return
def onConnect(self, Status, Description):
if (Status == 0):
self.isConnected = True
Domoticz.Log("Connected successfully to: "+Parameters["Address"]+":"+Parameters["Port"])
Domoticz.Send('PW?\r')
Domoticz.Send('ZM?\r', Delay=1)
Domoticz.Send('Z2?\r', Delay=2)
Domoticz.Send('Z3?\r', Delay=3)
else:
self.isConnected = False
self.powerOn = False
Domoticz.Log("Failed to connect ("+str(Status)+") to: "+Parameters["Address"]+":"+Parameters["Port"])
Domoticz.Debug("Failed to connect ("+str(Status)+") to: "+Parameters["Address"]+":"+Parameters["Port"]+" with error: "+Description)
self.SyncDevices()
return
def onMessage(self, Data, Status, Extra):
self.oustandingPings = self.oustandingPings - 1
strData = Data.decode("utf-8", "ignore")
Domoticz.Debug("onMessage called with Data: '"+str(strData)+"'")
strData = strData.strip()
action = strData[0:2]
detail = strData[2:]
if (action in self.pollingDict): self.lastMessage = action
if (action == "PW"): # Power Status
if (detail == "STANDBY"):
self.powerOn = False
elif (detail == "ON"):
self.powerOn = True
else: Domoticz.Debug("Unknown: Action "+action+", Detail '"+detail+"' ignored.")
elif (action == "ZM"): # Main Zone on/off
if (detail == "ON"):
self.mainOn = True
elif (detail == "OFF"):
self.mainOn = False
else: Domoticz.Debug("Unknown: Action "+action+", Detail '"+detail+"' ignored.")
elif (action == "SI"): # Main Zone Source Input
for key, value in self.selectorMap.items():
if (detail == value): self.mainSource = key
elif (action == "MV"): # Master Volume
if (detail.isdigit()):
if (abs(self.mainVolume1) != int(detail[0:2])): self.mainVolume1 = int(detail[0:2])
elif (detail[0:3] == "MAX"): Domoticz.Debug("Unknown: Action "+action+", Detail '"+detail+"' ignored.")
else: Domoticz.Log("Unknown: Action "+action+", Detail '"+detail+"' ignored.")
elif (action == "MU"): # Overall Mute
if (detail == "ON"): self.mainVolume1 = abs(self.mainVolume1)*-1
elif (detail == "OFF"): self.mainVolume1 = abs(self.mainVolume1)
else: Domoticz.Debug("Unknown: Action "+action+", Detail '"+detail+"' ignored.")
elif (action == "Z2"): # Zone 2
# Zone 2 response, make sure we have Zone 2 devices in Domoticz and they are polled
if (4 not in Devices):
LevelActions = '|'*Parameters["Mode3"].count('|')
Domoticz.Device(Name="Zone 2", Unit=4, TypeName="Selector Switch", Switchtype=18, Image=5, Options=self.SourceOptions).Create()
Domoticz.Log("Zone 2 responded, devices added.")
if (5 not in Devices):
Domoticz.Device(Name="Volume 2", Unit=5, Type=244, Subtype=73, Switchtype=7, Image=8).Create()
if ("Z2" not in self.pollingDict):
self.pollingDict = {"PW":"ZM?\r", "ZM":"SI?\r", "SI":"MV?\r", "MV":"MU?\r", "MU":"Z2?\r", "Z2":"PW?\r" }
if (detail == "ON"):
self.zone2On = True
elif (detail == "OFF"):
self.zone2On = False
elif (detail == "MUON"):
self.zone2Volume = abs(self.zone2Volume)*-1
elif (detail == "MUOFF"):
self.zone2Volume = abs(self.zone2Volume)
elif (detail.isdigit()):
if (abs(self.zone2Volume) != int(detail[0:2])): self.zone2Volume = int(detail[0:2])
else:
for key, value in self.selectorMap.items():
if (detail == value): self.zone2Source = key
elif (action == "Z3"): # Zone 3
# Zone 3 response, make sure we have Zone 3 devices in Domoticz and they are polled
if (6 not in Devices):
LevelActions = '|'*Parameters["Mode3"].count('|')
Domoticz.Device(Name="Zone 3", Unit=6, TypeName="Selector Switch", Switchtype=18, Image=5, Options=self.SourceOptions).Create()
Domoticz.Log("Zone 3 responded, devices added.")
if (7 not in Devices):
Domoticz.Device(Name="Volume 3", Unit=7, Type=244, Subtype=73, Switchtype=7, Image=8).Create()
if ("Z3" not in self.pollingDict):
self.pollingDict = {"PW":"ZM?\r", "ZM":"SI?\r", "SI":"MV?\r", "MV":"MU?\r", "MU":"Z2?\r", "Z2":"Z3?\r", "Z3":"PW?\r" }
if (detail == "ON"):
self.zone3On = True
elif (detail == "OFF"):
self.zone3On = False
elif (detail == "MUON"):
self.zone3Volume = abs(self.zone3Volume)*-1
elif (detail == "MUOFF"):
self.zone3Volume = abs(self.zone3Volume)
elif (detail.isdigit()):
if (abs(self.zone3Volume) != int(detail[0:2])): self.zone3Volume = int(detail[0:2])
else:
for key, value in self.selectorMap.items():
if (detail == value): self.zone3Source = key
else:
if (self.ignoreMessages.find(action) < 0):
Domoticz.Debug("Unknown message '"+action+"' ignored.")
self.SyncDevices()
return
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
Command = Command.strip()
action, sep, params = Command.partition(' ')
action = action.capitalize()
params = params.capitalize()
delay = 0
if (self.powerOn == False):
delay = int(Parameters["Mode2"])
else:
# Amp will ignore commands if it is responding to a heartbeat so delay send
lastHeartbeatDelta = (datetime.datetime.now()-self.lastHeartbeat).total_seconds()
if (lastHeartbeatDelta < 0.5):
delay = 1
Domoticz.Log("Last heartbeat was "+str(lastHeartbeatDelta)+" seconds ago, delaying command send.")
if (Unit == 1): # Main power switch
if (action == "On"):
Domoticz.Send(Message='PWON\r')
elif (action == "Off"):
Domoticz.Send(Message='PWSTANDBY\r', Delay=delay)
# Main Zone devices
elif (Unit == 2): # Main selector
if (action == "On"):
Domoticz.Send(Message='ZMON\r')
elif (action == "Set"):
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
Domoticz.Send(Message='SI'+self.selectorMap[Level]+'\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='ZMOFF\r', Delay=delay)
elif (Unit == 3): # Main Volume control
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
if (action == "On"):
Domoticz.Send(Message='MUOFF\r', Delay=delay)
elif (action == "Set"):
Domoticz.Send(Message='MV'+str(Level)+'\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='MUON\r', Delay=delay)
# Zone 2 devices
elif (Unit == 4): # Zone 2 selector
if (action == "On"):
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
Domoticz.Send(Message='Z2ON\r', Delay=delay)
elif (action == "Set"):
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
if (self.zone2On == False):
Domoticz.Send(Message='Z2ON\r', Delay=delay)
delay += 1
Domoticz.Send(Message='Z2'+self.selectorMap[Level]+'\r', Delay=delay)
delay += 1
Domoticz.Send(Message='Z2?\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='Z2OFF\r', Delay=delay)
elif (Unit == 5): # Zone 2 Volume control
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
if (self.zone2On == False):
Domoticz.Send(Message='Z2ON\r', Delay=delay)
delay += 1
if (action == "On"):
Domoticz.Send(Message='Z2MUOFF\r', Delay=delay)
elif (action == "Set"):
Domoticz.Send(Message='Z2'+str(Level)+'\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='Z2MUON\r', Delay=delay)
# Zone 3 devices
elif (Unit == 6): # Zone 3 selector
if (action == "On"):
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
Domoticz.Send(Message='Z3ON\r', Delay=delay)
elif (action == "Set"):
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
if (self.zone3On == False):
Domoticz.Send(Message='Z3ON\r', Delay=delay)
delay += 1
Domoticz.Send(Message='Z3'+self.selectorMap[Level]+'\r', Delay=delay)
delay += 1
Domoticz.Send(Message='Z3?\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='Z3OFF\r', Delay=delay)
elif (Unit == 7): # Zone 3 Volume control
if (self.powerOn == False): Domoticz.Send(Message='PWON\r')
if (self.zone3On == False):
Domoticz.Send(Message='Z3ON\r', Delay=delay)
delay += 1
if (action == "On"):
Domoticz.Send(Message='Z3MUOFF\r', Delay=delay)
elif (action == "Set"):
Domoticz.Send(Message='Z3'+str(Level)+'\r', Delay=delay)
elif (action == "Off"):
Domoticz.Send(Message='Z3MUON\r', Delay=delay)
return
def onDisconnect(self):
self.isConnected = False
Domoticz.Log("Denon device has disconnected.")
return
def onHeartbeat(self):
if (self.isConnected == True):
if (self.oustandingPings > 5):
Domoticz.Disconnect()
self.nextConnect = 0
else:
Domoticz.Send(self.pollingDict[self.lastMessage])
Domoticz.Debug("onHeartbeat: self.lastMessage "+self.lastMessage+", Sending '"+self.pollingDict[self.lastMessage][0:2]+"'.")
self.oustandingPings = self.oustandingPings + 1
else:
# if not connected try and reconnected every 3 heartbeats
self.oustandingPings = 0
self.nextConnect = self.nextConnect - 1
if (self.nextConnect <= 0):
self.nextConnect = 3
Domoticz.Connect()
self.lastHeartbeat = datetime.datetime.now()
return
def SyncDevices(self):
if (self.powerOn == False):
UpdateDevice(1, 0, "Off")
UpdateDevice(2, 0, "0")
UpdateDevice(3, 0, str(abs(self.mainVolume1)))
UpdateDevice(4, 0, "0")
UpdateDevice(5, 0, str(abs(self.zone2Volume)))
UpdateDevice(6, 0, "0")
UpdateDevice(7, 0, str(abs(self.zone3Volume)))
else:
UpdateDevice(1, 1, "On")
UpdateDevice(2, self.mainSource if self.mainOn else 0, str(self.mainSource if self.mainOn else 0))
if (self.mainVolume1 <= 0 or self.mainOn == False): UpdateDevice(3, 0, str(abs(self.mainVolume1)))
else: UpdateDevice(3, 2, str(self.mainVolume1))
UpdateDevice(4, self.zone2Source if self.zone2On else 0, str(self.zone2Source if self.zone2On else 0))
if (self.zone2Volume <= 0 or self.zone2On == False): UpdateDevice(5, 0, str(abs(self.zone2Volume)))
else: UpdateDevice(5, 2, str(self.zone2Volume))
UpdateDevice(6, self.zone3Source if self.zone3On else 0, str(self.zone3Source if self.zone3On else 0))
if (self.zone3Volume <= 0 or self.zone3On == False): UpdateDevice(7, 0, str(abs(self.zone3Volume)))
else: UpdateDevice(7, 2, str(self.zone3Volume))
return
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onConnect(Status, Description):
global _plugin
_plugin.onConnect(Status, Description)
def onMessage(Data, Status, Extra):
global _plugin
_plugin.onMessage(Data, Status, Extra)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onDisconnect():
global _plugin
_plugin.onDisconnect()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
def UpdateDevice(Unit, nValue, sValue):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Unit in Devices):
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue):
Devices[Unit].Update(nValue, str(sValue))
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
return
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Internal ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("External ID: '" + str(Devices[x].DeviceID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return
def stringToBase64(s):
return base64.b64encode(s.encode('utf-8')).decode("utf-8")
def base64ToString(b):
return base64.b64decode(b).decode('utf-8')