forked from filimonic/Kodi.Screensaver.TurnOffLGTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
400 lines (366 loc) · 15.4 KB
/
addon.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
__author__ = "Alexey D. Filimoniov <filimonic>"
__credits__ = ["dreamcat4 for LG TV Remote 2011","msloth for LG TV Remote 2015", "ubaransel for LG TV Remote 2012-2014"]
__license__ = "GPL"
__version__ = "1.1.5"
__maintainer__ = "Alexey D. Filimonov <filimonic>"
__email__ = "alexey@filimonic.net"
__status__ = "Developement"
__url__ = "https://github.com/filimonic/Kodi.Screensaver.TurnOffLGTV"
import sys
import xbmcgui
import xbmcaddon
import xbmc
import time
import json
import urllib2
import threading
import os
Addon = xbmcaddon.Addon()
Player = xbmc.Player()
#This is done because we can not import ws4py from resources/lib directly
__path__ = Addon.getAddonInfo('path')
sys.path.insert(1,os.path.join( __path__ ,'resources/lib'))
#This import available only after adding sys.path
from ws4py.client.threadedclient import WebSocketClient
Dialog = xbmcgui.Dialog()
__scriptname__ = Addon.getAddonInfo('name')
class xbmc_log:
@staticmethod
def log(message, debuglevel=xbmc.LOGDEBUG):
xbmc.log("LG TV PowerOff Screensaver :: " + str(message), debuglevel)
class LGTVNetworkShutdownScreensaver():
TV_TYPE_2015 = '0'
TV_TYPE_2012 = '1'
TV_TYPE_2011 = '2'
ip_address = '0.0.0.0'
tv_type = '0'
timeout = 10
timeout_timer = None
cli = None
def __init__(self):
_tv_type = Addon.getSetting('tv_type')
if _tv_type != '':
self.tv_type = _tv_type
ip_address = Addon.getSetting('ip_address')
if ip_address != '':
self.ip_address = ip_address
self.timeout_timer = threading.Timer(self.timeout,self.timeout_timer_fired)
xbmc_log.log("Tv type is: " + self.tv_type)
if self.tv_type == self.TV_TYPE_2015:
xbmc_log.log("Running timer")
self.timeout_timer.start()
xbmc_log.log("Creating shutdowneer")
try:
self.cli = LGTVNetworkShutdown2015(ip_address)
except RuntimeWarning as detail:
xbmc_log.log('{W}:' + detail.message)
elif self.tv_type == self.TV_TYPE_2012:
xbmc_log.log("Running timer")
self.timeout_timer.start()
xbmc_log.log("Creating shutdowneer")
try:
self.cli = LGTVNetworkShutdown2012(ip_address)
except RuntimeWarning as detail:
xbmc_log.log('{W}:' + detail.message)
elif self.tv_type == self.TV_TYPE_2011:
Dialog.notification("LG TV 2011","2011 LG TV is not supported yet")
xbmc_log.log("2011 LG TV is not supported yet")
else:
xbmc_log.log("Ignoring TV type" + str(self.tv_type))
xbmc_log.log("finished")
self.exit()
def timeout_timer_fired(self):
xbmc_log.log("Timer fired!")
self.timeout_timer.cancel()
try:
self.cli.close()
except:
pass
def exit(self):
xbmc_log.log("Exiting LGTVNetworkShutdownScreensaver")
try:
self.timeout_timer.cancel()
except:
pass
try:
self.cli.close()
except:
pass
try:
del self.cli
except:
pass
class Screensaver(xbmcgui.WindowXMLDialog):
shutter = None
class ExitMonitor(xbmc.Monitor):
def __init__(self, exit_callback):
self.exit_callback = exit_callback
def onScreensaverDeactivated(self):
xbmc_log.log('ExitMonitor: sending exit_callback')
self.exit_callback()
def onInit(self):
xbmc_log.log('Screensaver: onInit')
self.monitor = self.ExitMonitor(self.exit)
self.shutter = LGTVNetworkShutdownScreensaver()
def exit(self):
xbmc_log.log('Screensaver: Exit requested')
try:
self.shutter.exit()
except:
pass
try:
del self.monitor
except:
pass
self.close()
class LGTVNetworkShutdown2012:
PAIRING_KEY_PARAMETER_NAME = 'pairing_key_2012'
HTTP_HEADERS = {"Content-Type": "application/atom+xml"}
COMMAND_KEY_POWER = str(1) #Refer to http://developer.lgappstv.com/TV_HELP/index.jsp?topic=%2Flge.tvsdk.references.book%2Fhtml%2FUDAP%2FUDAP%2FAnnex+A+Table+of+virtual+key+codes+on+remote+Controller.htm
COMMAND_KEY_ESM = str(409)
COMMAND_KEY_DOWN = str(13)
COMMAND_KEY_OK = str(20)
HTTP_TIMEOUT = 3
@property
def client_key(self):
key = "000000"
try:
key_tmp = xbmcaddon.Addon().getSetting(self.PAIRING_KEY_PARAMETER_NAME)
xbmc_log.log("Pairing key read: " + key_tmp, xbmc.LOGDEBUG)
if key_tmp != '':
key = key_tmp
except:
xbmc_log.log("Unable to read pairing key", xbmc.LOGERROR)
return key
def check_connection(self, ip_address):
try:
connection_url = 'http://' + ip_address + ':8080'
xbmc_log.log("Checking connection to " + connection_url )
response=urllib2.urlopen(connection_url,timeout=3)
xbmc_log.log("Got response, code = " + str(response.getcode()))
if (response.getcode() == 404):
xbmc_log.log("Check passed, 404 expected {1}")
return True
else:
xbmc_log.log("Check failed, response not as expected")
Dialog.notification("LG TV 2012-2014","Seems this is is not TV")
return False
except urllib2.HTTPError as err:
if err.code == 404:
xbmc_log.log("Check passed, 404 expected {2}")
return True
else:
xbmc_log.log("Check failed, response is not as expected" + str(err.code))
Dialog.notification("LG TV 2012-2014","Seems this is is not TV")
return False
except urllib2.URLError as err:
Dialog.notification("LG TV 2012-2014","Connection failed. Maybe IP or type is incorrect?")
xbmc_log.log("Check failed, URLError")
return False
def check_registration(self,ip_address):
data = "<?xml version=\"1.0\" encoding=\"utf-8\"?><auth><type>AuthReq</type><value>" + self.client_key + "</value></auth>"
try:
request = urllib2.Request('http://'+ip_address+':8080/roap/api/auth',data=data,headers=self.HTTP_HEADERS)
response = urllib2.urlopen(request, timeout=self.HTTP_TIMEOUT)
print(response.read())
return True
except urllib2.HTTPError as err:
if err.code == 401:
xbmc_log.log("Wrong key supplied: " + self.client_key)
Dialog.notification("LG TV 2012-2014","Go to settings to set up key")
return False
else:
xbmc_log.log("Unexpected response code " + str(err.code))
except urllib2.URLError:
xbmc_log.log("Error checking registration: unable to connect or make a request {URLError)")
return False
def send_turn_off_command(self,ip_address):
bIsMusicModeEnabled = ( Addon.getSetting("music_mode_2012") == "true" )
bIsInMusicMode = ( Player.isPlayingAudio() == 1 )
xbmc_log.log("music_mode_2012:" + str(Addon.getSetting("music_mode_2012")) + "; bIsMusicModeEnabled:" + str(bIsMusicModeEnabled) + "; bIsInMusicMode:" + str(bIsInMusicMode) + "; Player.isPlayingAudio():" + str(Player.isPlayingAudio()) + "; (bIsMusicModeEnabled and bIsInMusicMode): " + str((bIsMusicModeEnabled and bIsInMusicMode)))
if (not (bIsMusicModeEnabled and bIsInMusicMode) ):
xbmc_log.log("Sending TURN OFF command")
return self.send_command(ip_address,self.COMMAND_KEY_POWER)
else:
i = int(float(Addon.getSetting("music_mode_2012_value")))
xbmc_log.log("Sending ESM MENU command")
self.send_command(ip_address,self.COMMAND_KEY_ESM)
while i > 0 :
xbmc_log.log("Sending DOWN command. i=" + str(i))
self.send_command(ip_address,self.COMMAND_KEY_DOWN)
i = i - 1
xbmc_log.log("Sending OK command")
return self.send_command(ip_address,self.COMMAND_KEY_OK);
def send_command(self,ip_address,command):
data = "<?xml version=\"1.0\" encoding=\"utf-8\"?><command><name>HandleKeyInput</name><value>" + command + "</value></command>"
try:
request = urllib2.Request('http://'+ip_address+':8080/roap/api/command',data=data,headers=self.HTTP_HEADERS)
response = urllib2.urlopen(request, timeout=self.HTTP_TIMEOUT)
Dialog.notification("LG TV 2012-2014","Command sent")
xbmc_log.log("Command sent")
time.sleep(1);
return True
except urllib2.HTTPError as err:
xbmc_log.log("Error sending PWR_OFF: unable to connect or make a request {HTTPErrror): " + str(err.code))
return False
except urllib2.URLError:
xbmc_log.log("Error sending PWR_OFF: unable to connect or make a request {URLError)")
return False
def __init__(self, ip_address):
if self.check_connection(ip_address) == True:
if self.check_registration(ip_address) == True:
if self.send_turn_off_command(ip_address) == True:
xbmc_log.log("Successfully sent PWR_OFF")
else:
raise RuntimeWarning('Unable to send PWR_OFF')
else:
raise RuntimeWarning('Unable to check registration - possibly wrong key')
else:
raise RuntimeWarning('Unable to check connection')
def close(self):
pass
class LGTVNetworkShutdown2015(WebSocketClient):
_msg_id = 0
_registered = 0
_power_off_sent = 0
PAIRING_KEY_PARAMETER_NAME = 'pairing_key_2015'
def send(self, payload, binary=False):
self._msg_id = self._msg_id+1
xbmc_log.log("Sending data to TV" + payload, xbmc.LOGDEBUG)
super(LGTVNetworkShutdown2015,self).send(payload,binary)
def save_pairing_key(self, key):
try:
xbmcaddon.Addon().setSetting(self.PAIRING_KEY_PARAMETER_NAME,key)
xbmc_log.log("Pairing key saved: " + key, xbmc.LOGDEBUG)
except:
xbmc_log.log("Unable to save pairng key", xbmc.LOGERROR)
@property
def client_key(self):
key = "123"
try:
key = xbmcaddon.Addon().getSetting(self.PAIRING_KEY_PARAMETER_NAME)
xbmc_log.log("Pairing key read: " + key, xbmc.LOGDEBUG)
except:
xbmc_log.log("Unable to read pairing key", xbmc.LOGERROR)
return key
@property
def register_string(self):
key = self.client_key
if key == "":
register_string = json.JSONEncoder().encode(
{
"type" : "register",
"id" : "register_" + str(self._msg_id),
"payload" : {
"pairingType" : "PROMPT",
"manifest" : {
"permissions": [
"CONTROL_POWER"
]
}
}
}
)
else:
register_string = json.JSONEncoder().encode(
{
"type" : "register",
"id" : "register_" + str(self._msg_id),
"payload" : {
"pairingType" : "PROMPT",
"client-key" : key,
"manifest" : {
"permissions": [
"CONTROL_POWER"
]
}
}
}
)
xbmc_log.log("Register string is" + register_string, xbmc.LOGDEBUG)
return register_string
def opened(self):
xbmc_log.log("Connection to TV opened", xbmc.LOGDEBUG)
self._msg_id = 0
self.send(self.register_string)
def closed(self, code, reason=None):
xbmc_log.log("Connection to TV closed : " + str(code) + "(" + reason + ")", xbmc.LOGDEBUG)
def received_message(self, message):
xbmc_log.log("Message received : (" + str(message) + ")", xbmc.LOGDEBUG)
if message.is_text:
response = json.loads(message.data.decode("utf-8"),"utf-8" )
if 'client-key' in response['payload']:
self.save_pairing_key(response['payload']['client-key'])
if response['type'] == 'registered':
xbmc_log.log("State changed to REGISTERED", xbmc.LOGDEBUG)
self._registered = 1
if self._registered == 0 and response['type'] == 'error':
xbmc_log.log("Pairing error " + str(response['error']), xbmc.LOGERROR)
if self._power_off_sent == 0 and self._registered == 1:
xbmc_log.log("Sending POWEROFF", xbmc.LOGDEBUG)
self.send_power_off()
self.close()
else:
xbmc_log.log("Unreadable message", xbmc.LOGDEBUG)
def send_power_off(self):
power_off_string = json.JSONEncoder().encode(
{
"type" : "request",
"id" : "request_" + str(self._msg_id),
"uri" : "ssap://system/turnOff",
"payload" : {
"client-key" : self.client_key
}
}
)
self.send(power_off_string)
self._power_off_sent = 1
Dialog.notification("LG TV 2015+","Sent command to turn off TV")
xbmc_log.log("Sent POWEROFF successfully", xbmc.LOGDEBUG)
@property
def handshake_headers(self):
"""
Should overload this, because LG TVs do not operate with Origin correctly
"""
return [(p, v)
for p,v in super(LGTVNetworkShutdown2015,self).handshake_headers
if p != "Origin"
]
def __init__(self,ip_address):
xbmc_log.log("Initing")
if self.check_connection(ip_address):
connection_string = 'ws://' + ip_address + ':3000'
xbmc_log.log("Connection string is [" + connection_string+ "]", xbmc.LOGDEBUG)
super(LGTVNetworkShutdown2015,self).__init__(connection_string,protocols=['http-only', 'chat'])
try:
self.connect()
except:
raise RuntimeWarning('Unable to estabilish connection')
return
self.run_forever()
else:
Dialog.notification("LG TV 2015","Connection failed. Maybe IP or type is incorrect?")
raise RuntimeWarning('Unable to test connection')
def check_connection(self, ip_address):
try:
connection_url = 'http://' + ip_address + ':3000'
xbmc_log.log("Checking connection to " + connection_url )
response=urllib2.urlopen(connection_url,timeout=3)
xbmc_log.log("Check passed")
return True
except urllib2.URLError as err:
xbmc_log.log("Check failed")
return False
#class LGTVNetworkShutdown2011:
####### TODO: https://github.com/dreamcat4/lgremote/blob/master/lgremote
if __name__ == '__main__':
if 'show_help' in sys.argv:
raise NotImplementedError()
else:
screensaver_gui = Screensaver("screensaver-display.xml",__path__,"default")
screensaver_gui.doModal()
del screensaver_gui
del Addon
del Dialog
xbmc_log.log('Screensaver deleted')
sys.modules.clear()