-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.py
executable file
·464 lines (408 loc) · 22 KB
/
plugin.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#
# LG TV WebOS 3 Plugin Author: Chris Gheen @GameDevHobby, 2017
#
"""
<plugin key="lgtv" name="LG TV (with Kodi remote)" author="Chris Gheen @GameDevHobby" version="0.7" wikilink="https://github.com/GameDevHobby/lgtv-webos-domoticz-plugin" externallink="http://www.lg.com/us/tvs">
<description>
* Enable remote start on your TV: [Settings] => [Network] => [Home Network Setup] => [Remote Start] => [On]<br/>
* Give your TV a static IP address, or make a DHCP reservation for a specific IP address in your router.<br/>
* Determine the MAC address of your TV: [Settings] => [Network] => [Network Setup] => [View Network Status]<br/>
</description>
<params>
<param field="Address" label="IP address" width="200px" required="true" default="192.168.1.191"/>
<param field="Mode1" label="Max volume" width="30px" required="true" default="20"/>
<param field="Mode2" label="MAC address" width="200px" required="true" default="AA:BB:CC:DD:EE:FF"/>
<param field="Mode3" label="Volume bar" width="75px">
<options>
<option label="True" value="Volume"/>
<option label="False" value="Fixed" default="true" />
</options>
</param>
<param field="Mode4" label="Notifier Name" width="100px" default=""/>
<param field="Mode5" label="Update interval (sec)" width="30px" required="true" default="30"/>
<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 datetime
import subprocess
class BasePlugin:
isConnected = False
powerOn = False
tvState = 0
tvVolume = 0
tvSource = 0
tvPlaying = {} #''
SourceOptions3 = {}
SourceOptions4 = {}
startTime = ''
endTime = ''
perc_playingTime = 0
debug = False
def run(self, command, arg=""):
cmd = "python3 " + Parameters["HomeFolder"] + "lg.py " + Parameters["Address"] + " -c " + command
if arg != "":
cmd = cmd + " -a " + arg
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.decode("utf-8")
err = err.decode("utf-8")
#Domoticz.Debug(str(p.returncode))
#Domoticz.Debug(out)
#Domoticz.Debug(err)
self.debug = False
if p.returncode == 1:
return str(err)
else:
return str(out)
# Executed once at reboot/update, can create up to 255 devices
def onStart(self):
global _tv
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
self.debug = True
#TODO: get number of inputs and apps to build list
self.SourceOptions3 = { "LevelActions" : "||||||",
"LevelNames" : "Off|TV|HDMI1|HDMI2|HDMI3|Hulu|Netflix|Amazon|Youtube|iPlayer|Unknown",
"LevelOffHidden": "true",
"SelectorStyle" : "0"
}
if (len(Devices) == 0):
Domoticz.Device(Name="Status", Unit=1, Type=17, Image=2, Switchtype=17).Create()
if Parameters["Mode3"] == "Volume": Domoticz.Device(Name="Volume", Unit=2, Type=244, Subtype=73, Switchtype=7, Image=8).Create()
Domoticz.Device(Name="Source", Unit=3, TypeName="Selector Switch", Switchtype=18, Image=2, Options=self.SourceOptions3).Create()
Domoticz.Log("Devices created")
elif (Parameters["Mode3"] == "Volume" and 2 not in Devices):
Domoticz.Device(Name="Volume", Unit=2, Type=244, Subtype=73, Switchtype=7, Image=8).Create()
Domoticz.Log("Volume device created")
elif (Parameters["Mode3"] != "Volume" and 2 in Devices):
Devices[2].Delete()
Domoticz.Log("Volume device deleted")
elif 1 not in Devices:
Domoticz.Device(Name="Status", Unit=1, Type=17, Image=2, Switchtype=17).Create()
Domoticz.Log("TV device created")
elif 3 not in Devices:
Domoticz.Device(Name="Source", Unit=3, TypeName="Selector Switch", Switchtype=18, Image=2, Options=self.SourceOptions3).Create()
Domoticz.Log("Source device created")
else:
if (1 in Devices): self.tvState = Devices[1].nValue #--> of sValue
if (2 in Devices): self.tvVolume = Devices[2].nValue #--> of sValue
if (3 in Devices): self.tvSource = Devices[3].sValue
# Set update interval, values below 10 seconds are not allowed due to timeout of 5 seconds in bravia.py script
updateInterval = int(Parameters["Mode5"])
if updateInterval < 30:
if updateInterval < 10: updateInterval == 10
Domoticz.Log("Update interval set to " + str(updateInterval) + " (minimum is 10 seconds)")
Domoticz.Heartbeat(updateInterval)
else:
Domoticz.Heartbeat(30)
if self.debug == True:
DumpConfigToLog()
if Parameters["Mode4"] != "":
Domoticz.Notifier(Parameters["Mode4"])
else:
Domoticz.Log("Notifications are off, add a Notifier Name to enable.")
return #--> return True
def onConnect(self, Status, Description):
if (Status == 0):
self.isConnected = True
Domoticz.Log("Connected successfully to: "+Parameters["Address"])
else:
self.isConnected = False
self.powerOn = False
Domoticz.Log("Failed to connect ("+str(Status)+") to: "+Parameters["Address"])
Domoticz.Debug("Failed to connect ("+str(Status)+") to: "+Parameters["Address"]+" with error: "+Description)
self.SyncDevices()
return
# Called when a single,complete message is received from the external hardware
def onMessage(self, Data, Status, Extra):
Domoticz.Log('onMessage: '+str(Data)+" ,"+str(Status)+" ,"+str(Extra))
return True
# Executed each time we click on device through Domoticz GUI
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()
if self.powerOn == False:
if Unit == 1: # TV power switch
if action == "On":
try:
self.run("on", Parameters["Mode2"])#_tv.turn_on()
self.tvPlaying = "TV starting" # Show that the TV is starting, as booting the TV takes some time
#self.tvSource = "10"
self.SyncDevices()
except Exception as err:
Domoticz.Log('Error when starting TV using WOL (' + err + ')')
else:
if Unit == 1: # TV power switch
if action == "Off":
self.run("off")#_tv.turn_off()
self.tvPlaying = "Off"
self.SyncDevices()
# Remote buttons (action is capitalized so chosen for Command)
elif Command == "ChannelUp": self.run("channel-up")#_tv.send_req_ircc("AAAAAQAAAAEAAAAQAw==") # ChannelUp
elif Command == "ChannelDown": self.run("channel-down")#_tv.send_req_ircc("AAAAAQAAAAEAAAARAw==") # ChannelDown
#elif Command == "Channels": #_tv.send_req_ircc("AAAAAQAAAAEAAAA6Aw==") # Display, shows information on what is playing
elif Command == "VolumeUp": self.run("volume-up")#_tv.send_req_ircc("AAAAAQAAAAEAAAASAw==") # VolumeUp
elif Command == "VolumeDown": self.run("volume-down")#_tv.send_req_ircc("AAAAAQAAAAEAAAATAw==") # VolumeDown
#elif Command == "Mute": #_tv.send_req_ircc("AAAAAQAAAAEAAAAUAw==") # Mute
elif Command == "Select": self.run("enter")#_tv.send_req_ircc("AAAAAQAAAAEAAABlAw==") # Confirm
#elif Command == "Up": #_tv.send_req_ircc("AAAAAQAAAAEAAAB0Aw==") # Up
#elif Command == "Down": #_tv.send_req_ircc("AAAAAQAAAAEAAAB1Aw==") # Down
#elif Command == "Left": #_tv.send_req_ircc("AAAAAQAAAAEAAAA0Aw==") # Left
#elif Command == "Right": #_tv.send_req_ircc("AAAAAQAAAAEAAAAzAw==") # Right
#elif Command == "Home": #_tv.send_req_ircc("AAAAAQAAAAEAAABgAw==") # Home
elif Command == "Info": self.run("info")#_tv.send_req_ircc("AAAAAgAAAKQAAABbAw==") # EPG
#elif Command == "Back": #_tv.send_req_ircc("AAAAAgAAAJcAAAAjAw==") # Return
#elif Command == "ContextMenu": #_tv.send_req_ircc("AAAAAgAAAJcAAAA2Aw==") # Options
#elif Command == "FullScreen": #_tv.send_req_ircc("AAAAAQAAAAEAAABjAw==") # Exit
#elif Command == "ShowSubtitles": #_tv.send_req_ircc("AAAAAQAAAAEAAAAlAw==") # Input
elif Command == "Stop": self.run("stop")#_tv.send_req_ircc("AAAAAgAAAJcAAAAYAw==") # Stop
elif Command == "BigStepBack": self.run("pause")#_tv.send_req_ircc("AAAAAgAAAJcAAAAZAw==") # Pause
elif Command == "Rewind": self.run("rewind")#_tv.send_req_ircc("AAAAAgAAAJcAAAAbAw==") # Rewind
elif Command == "PlayPause": self.run("pause")#_tv.send_req_ircc("AAAAAgAAABoAAABnAw==") # TV pause
elif Command == "FastForward": self.run("fast-forward")#_tv.send_req_ircc("AAAAAgAAAJcAAAAcAw==") # Forward
elif Command == "BigStepForward": self.run("play")#_tv.send_req_ircc("AAAAAgAAAJcAAAAaAw==") # Play
if Unit == 2: # TV volume
if action == 'Set': #--> and (params.capitalize() == 'Level') or (Command.lower() == 'Volume')
max = int(Parameters["Mode1"])
if Level > max:
self.tvVolume = str(max)
else:
self.tvVolume = str(Level)
#_tv.set_volume_level(self.tvVolume)
self.run("set-volume", self.tvVolume)
elif action == "Off":
#_tv.mute_volume()
self.run("mute")
UpdateDevice(2, 0, str(self.tvVolume))
elif action == "On":
#_tv.mute_volume()
self.run("unmute")
UpdateDevice(2, 1, str(self.tvVolume))
if Unit == 3: # TV source
if Command == 'Set Level':
if Level == 10:
#_tv.send_req_ircc("AAAAAQAAAAEAAAAAAw==") #TV Num1
self.run("app", "com.webos.app.livetv")
self.GetTVInfo()
if Level == 20:
#_tv.send_req_ircc("AAAAAgAAABoAAABaAw==") #HDMI1
self.run("app", "com.webos.app.hdmi1")
self.tvPlaying = "HDMI 1"
if Level == 30:
#_tv.send_req_ircc("AAAAAgAAABoAAABbAw==") #HDMI2
self.run("app", "com.webos.app.hdmi2")
self.tvPlaying = "HDMI 2"
if Level == 40:
#_tv.send_req_ircc("AAAAAgAAABoAAABcAw==") #HDMI3
self.tvPlaying = "HDMI 3"
self.run("app", "com.webos.app.hdmi3")
if Level == 50:
#_tv.send_req_ircc("AAAAAgAAABoAAABdAw==") #HDMI4
self.tvPlaying = "Hulu"
self.run("app", "hulu")
if Level == 60:
#_tv.send_req_ircc("AAAAAgAAABoAAAB8Aw==") #Netflix
self.tvPlaying = "Netflix"
self.run("app", "netflix")
if Level == 70:
#_tv.send_req_ircc("AAAAAgAAABoAAAB8Aw==") #Amazon
self.tvPlaying = "Amazon"
self.run("app", "Lovefilm")
if Level == 80:
#_tv.send_req_ircc("AAAAAgAAABoAAAB8Aw==") #Youtube
self.tvPlaying = "Youtube"
self.run("app", "Youtube.Leanback.V4")
if Level == 90:
#_tv.send_req_ircc("AAAAAgAAABoAAAB8Aw==") #iPlayer
self.tvPlaying = "iPlayer"
self.run("app", "Bbc.Iplayer.3.0")
if Level == 100:
#_tv.send_req_ircc("AAAAAgAAABoAAAB8Aw==") #Unknown
self.tvPlaying = "Unknown"
self.tvSource = Level
self.SyncDevices()
return
def onDisconnect(self):
self.isConnected = False
Domoticz.Log("LG TV has disconnected.")
return
# Executed once when HW updated/removed
def onStop(self):
Domoticz.Log("onStop called")
return True
# Execution depend of Domoticz.Heartbeat(x) x in seconds
def onHeartbeat(self):
tvStatus = ''
out = self.run("software-info")
#Domoticz.Debug(out)
if 'TimeoutError()' in out:
tvStatus = 'off'#_tv.get_power_status()
else:
tvStatus = 'active'
#Domoticz.Debug(out)
Domoticz.Debug('Status TV: ' + tvStatus)
if tvStatus == 'active': # TV is on
self.powerOn = True
self.GetTVInfo()
else: # TV is off or standby
self.powerOn = False
self.SyncDevices()
return
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
self.run("send-message", "\"" + Text + "\"")
def SyncDevices(self):
# TV is off
if self.powerOn == False:
if self.tvPlaying == "TV starting": # TV is booting and not yet responding to get_power_status
UpdateDevice(1, 1, self.tvPlaying)
#UpdateDevice(3, 1, self.tvSource)
else: # TV is off so set devices to off
self.ClearDevices()
# TV is on
else:
if self.tvPlaying == "Off": # TV is set to off in Domoticz, but self.powerOn is still true
self.ClearDevices()
else: # TV is on so set devices to on
if not self.tvPlaying:
Domoticz.Debug("No information from TV received (TV was paused and then continued playing from disk) - SyncDevices")
else:
UpdateDevice(1, 1, self.tvPlaying)
UpdateDevice(3, 1, str(self.tvSource))
if Parameters["Mode3"] == "Volume": UpdateDevice(2, 2, str(self.tvVolume))
return
def ClearDevices(self):
self.tvPlaying = "Off"
UpdateDevice(1, 0, self.tvPlaying) #Status
if Parameters["Mode3"] == "Volume": UpdateDevice(2, 0, str(self.tvVolume)) #Volume
self.tvSource = 0
UpdateDevice(3, 0, str(self.tvSource)) #Source
return
def GetTVInfo(self):
currentApp = str(self.run("current-app")).rstrip()
currentInput = str(self.run("get-input")).rstrip()
currentChannel = self.run("get-channel")
currentInfo = self.run("info")
self.tvPlaying = {'title': 'test', 'programTitle': None}#_tv.get_playing_info()
Domoticz.Debug("App: " + currentApp)
Domoticz.Debug("Input: " + currentInput)
Domoticz.Debug("Channel: " + currentChannel)
Domoticz.Debug("Info: " + currentInfo)
if not "errorCode" in currentChannel:#self.tvPlaying['programTitle'] != None: # Get information on channel and program title if tuner of TV is used
# pylgtv seems to return invalid JSON, so parse the fragment ourselves
if "channelName" in currentChannel:
currentChannelInfo = currentChannel.split(',')
currentChannelName = next((s for s in currentChannelInfo if 'channelName' in s), None).split('\'')[3]
else:
currentChannelName = None
if "channelNumber" in currentChannel:
currentChannelInfo = currentChannel.split(',')
currentChannelNumber = next((s for s in currentChannelInfo if 'channelNumber' in s), None).split('\'')[3]
else:
currentChannelNumber = None
if currentChannelNumber is not None:
self.tvPlaying = str(currentChannelNumber + ': ' + currentChannelName )
else:
self.tvPlaying = str(currentChannel + ' (' + currentInfo + ')' )
UpdateDevice(1, 1, self.tvPlaying)
self.tvSource = 10
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to TV
else: # No channel info found
# When TV plays apps, no title information is available, it can return '' or 'com.webos.app.*'
self.tvPlaying = currentApp.replace('com.webos.app.', '').title() # Set the appname as fallback
if "hdmi1" in self.tvPlaying.lower():
self.tvSource = 20
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to HDMI1
self.tvPlaying = 'HDMI1'
elif "hdmi2" in self.tvPlaying.lower():
self.tvSource = 30
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to HDMI2
self.tvPlaying = 'HDMI2'
elif "hdmi3" in self.tvPlaying.lower():
self.tvSource = 40
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to HDMI3
self.tvPlaying = 'HDMI3'
elif "hulu" in self.tvPlaying.lower():
self.tvSource = 50
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to Hulu
elif "netflix" in self.tvPlaying.lower():
self.tvSource = 60
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to Netflix
elif "lovefilm" in self.tvPlaying.lower():
self.tvSource = 70
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to Amazon
elif "youtube" in self.tvPlaying.lower():
self.tvSource = 80
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to Youtube
elif "iplayer" in self.tvPlaying.lower():
self.tvSource = 90
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to iPlayer
else:
self.tvSource = 100
UpdateDevice(3, 1, str(self.tvSource)) # Set source device to Unknown
UpdateDevice(1, 1, self.tvPlaying)
# Get volume information of TV
if Parameters["Mode3"] == "Volume":
#self.tvVolume = _tv.get_volume_info()
output = self.run("get-volume")#.replace("\\n'", "").replace("'", "").replace("b", "")
# Check if output is a digit instead of garbage
try:
output = int(output)
except ValueError:
output = 0
mute = self.run("get-mute")
mute_level = 2
if mute == "True":
mute_level = 0
Domoticz.Debug("vol: " + str(output))
self.tvVolume = int(output)
if self.tvVolume != None: UpdateDevice(2, mute_level, str(self.tvVolume))
return
_plugin = BasePlugin()
def onStart():
_plugin.onStart()
def onConnect(Status, Description):
_plugin.onConnect(Status, Description)
def onMessage(Data, Status, Extra):
_plugin.onMessage(Data, Status, Extra)
def onCommand(Unit, Command, Level, Hue):
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect():
_plugin.onDisconnect()
def onHeartbeat():
_plugin.onHeartbeat()
# Update Device into database
def UpdateDevice(Unit, nValue, sValue, AlwaysUpdate=False):
# 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) or (AlwaysUpdate == True)):
Devices[Unit].Update(nValue, str(sValue))
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
return
# Generic helper functions
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