-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.py
480 lines (377 loc) · 15 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# BBox plugin
#
# Author: Smanar
#
"""
<plugin key="BBox" name="BBox plugin" author="Smanar" version="1.0.3" wikilink="https://github.com/Smanar/Domoticz-BBox">
<description>
<br/><br/>
<h2>Plugin pour le routeur de Bouygues telecom</h2><br/>
Pour le moment sert juste a lister les peripheriques, WOL, afficher la qualite du Wifi.
<br/><br/>
<h3>Remarque</h3>
<ul style="list-style-type:square">
<li>Le mot de passe n'est utile que pour les actions demandant des droits d'acces, vous pouvez laisser le champs vide.</li>
</ul>
<h3>Configuration</h3>
</description>
<params>
<param field="Mode1" label="Delai en secondes" width="75px" required="true" default="300" />
<param field="Mode2" label="Password (si besoin)" width="100px" required="false" default="" />
<param field="Mode3" label="Debug" width="150px">
<options>
<option label="None" value="0" default="true" />
<option label="Debug info Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic+Messages" value="126"/>
<option label="Connections Only" value="16"/>
<option label="Connections+Python" value="18"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
# https://api.bbox.fr/doc/#Getting%20started
# API complete : https://api.bbox.fr/doc/apirouter/index.html
# All imports
import Domoticz
import json
import requests
requests.packages.urllib3.disable_warnings()
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
try:
requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
except AttributeError:
# no pyopenssl support used / needed / available
pass
URL = "mabbox.bytel.fr"
NO_DOMOTICZ_LIB = True
ADSL_QUALITY = False
class BasePlugin:
def __init__(self):
self.httpConn = None
self.url = None
self.data = None
self.listdevice = {}
self.tempo = 0
self.counter = 0
self.UpdateSucced = False
self.down = 0
self.up = 0
self.password = None
self.cookie = None
return
def onStart(self):
Domoticz.Debug("onSart")
if Parameters["Mode3"] != "0":
Domoticz.Debugging(int(Parameters["Mode3"]))
self.tempo = int(Parameters["Mode1"]) / 10
#Retreive cookies for authentification
self.Login()
#To force an update
self.ForceUpdate(10)
Domoticz.Status("BBox plugin running !")
def onStop(self):
Domoticz.Log("onStop - Plugin is stopping.")
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("Connect")
if (Status == 0):
Domoticz.Debug("connected successfully.")
h = {
'User-Agent':'Domoticz',\
'Accept':'*/*' ,\
'Host':URL,\
'Connection':'keep-alive'\
}
if self.cookie:
h['Cookie'] = self.cookie
sendData = {}
if self.data:
sendData['Verb'] = 'POST'
sendData['Data'] = self.data
else:
sendData['Verb'] = 'GET'
sendData['URL'] = self.url
sendData['Headers'] = h
self.url = None
self.data = None
Connection.Send(sendData)
else:
Domoticz.Log("Failed to connect ("+str(Status)+") with error: "+Description)
def onMessage(self, Connection, Data):
#Domoticz.Log("****************** " + str(Data) )
Domoticz.Debug("OnMessage")
self.ManageAnswer(Data)
def ManageAnswer(self,Data):
self.UpdateSucced = True
Status = int(Data["Status"])
Response = ''
try:
if "Data" in Data:
strData = Data["Data"].decode("utf-8", "ignore")
try:
Response = json.loads(strData)
except:
Response = str(strData)
except:
Domoticz.Error("On message error " + str(Data) )
if (Status == 200):
if len(Response) == 0:
Domoticz.Status("Requete effectuée")
return
#Traitement
if type(Response) is str:
# Html code
if 'Computer will begin to sleep' in Response:
Domoticz.Status("Command send succesfull at Switchoff")
else:
Domoticz.Error("Unknow responde : " + str(Response))
else:
# Json code
_json = Response[0]
if 'hosts' in _json:
_json = _json['hosts']['list']
for i in _json:
macaddress = i['macaddress']
if macaddress not in self.listdevice:
self.listdevice[macaddress] = {}
self.listdevice[macaddress]['id'] = i['id']
self.listdevice[macaddress]['hostname'] = i['hostname']
self.listdevice[macaddress]['ipaddress'] = i['ipaddress']
self.listdevice[macaddress]['active'] = i['active']
link = i['link']
rssi = 12
if link.startswith('Wifi'):
_rssi = i['wireless']['rssi0']
_rssi = - int ( _rssi)
if _rssi < 60:
rssi = 11
elif _rssi > 75:
rssi = 4
else:
rssi = 8
self.listdevice[macaddress]['rssi'] = rssi
#for i in self.listdevice:
# Domoticz.Log('Device: ' + self.listdevice[i]['hostname'] + ' active : ' + str(self.listdevice[i]['active']))
self.UpdateDevice()
elif 'wan' in _json:
_json = _json['wan']['xdsl']
self.up = _json['up']['power'] - _json['up']['noise']
self.down = _json['down']['power'] - _json['down']['noise']
else:
Domoticz.Log('Not managed Json')
elif (Status == 307) or (Status == 401):
Domoticz.Error("Router returned a status: " + str(Status) + " Operation requires authentication")
self.Login()
else:
Domoticz.Error("Router returned a status: " + str(Status))
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if len(self.listdevice) == 0:
Domoticz.Log("Plugin not ready")
return
if Command == "On":
#Get device id
mac = Devices[Unit].DeviceID
#Domoticz.Log(str(self.listdevice[mac]))
id = self.listdevice[mac]['id']
#Get token
token = GetToken(self.cookie)
if not token:
if self.password:
self.cookie = GetCookie(self.password)
else:
Domoticz.Error("And no password set.")
return
#Make request
url = '/api/v1/hosts/' + str(id) + '?btoken=' + token
data = "action=wakeup"
self.Request(url,data)
if Command == "Off":
#Get device id
mac = Devices[Unit].DeviceID
#Domoticz.Log(str(self.listdevice[mac]))
ip = self.listdevice[mac]['ipaddress']
#Make request
url = 'http://' + ip + ':8000/?action=System.Sleep'
self.Request(url)
#Recuperation de l'etat 30s apres, ca peut prendre du temps
self.ForceUpdate(30)
def onDisconnect(self, Connection):
Domoticz.Debug("onDisconnect called for connection to: "+Connection.Address+":"+Connection.Port)
if self.UpdateSucced == False:
Domoticz.Error("Request not answer")
self.httpConn = None
self.url = None
def onHeartbeat(self):
self.counter += 1
if self.counter > self.tempo:
self.counter = 0
Domoticz.Log("MAJ BBox")
self.Request('/api/v1/hosts')
if ADSL_QUALITY and (self.counter == self.tempo - 1):
Domoticz.Log("MAJ ADSL quality")
self.Request('/api/v1/wan/xdsl')
Domoticz.Debug("HeartBeat")
#---------------------------------------------------------------------------------------------------------
def ForceUpdate(self,time):
self.counter = int(self.tempo - int(time) / 10)
def Login(self):
if Parameters["Mode2"] != "":
self.password = Parameters["Mode2"]
self.cookie = GetCookie(self.password)
def Request(self,url,data=None):
_port = '443'
_proto = 'HTTPS'
_address = URL
#if it's not a request to the box
if url.startswith('http'):
if not url.startswith('https'):
_port = '80'
_proto = 'HTTP'
t = url.split('/')
_address = t[2]
url = '/' + '/'.join(t[3:])
if ':' in _address:
t = _address.split(':')
_address = t[0]
_port = t[1]
if NO_DOMOTICZ_LIB:
Domoticz.Debug("Making request " + _proto.lower() + "://" + _address + url)
h = {
'User-Agent':'Domoticz',\
'Accept':'*/*' ,\
'Host':URL,\
'Connection':'keep-alive'\
}
if self.cookie:
h['Cookie'] = self.cookie
try:
if data:
result = requests.post( _proto.lower() + "://" + _address + url , headers=h, data = data, timeout = 5, verify=False)
else:
result = requests.get( _proto.lower() + "://" + _address + url , headers=h, timeout = 5, verify=False)
data2 = {}
data2["Status"] = result.status_code
data2["Data"] = result.content
self.ManageAnswer(data2)
except:
Domoticz.Error("Connection error : Box non joignable")
else:
if not self.httpConn:
self.UpdateSucced = False
Domoticz.Debug("Making request " + _proto.lower() + "://" + _address + url)
self.url = url
self.data = data
self.httpConn = Domoticz.Connection(Name="BBox", Transport="TCP/IP", Protocol=_proto, Address=_address, Port=_port)
self.httpConn.Connect()
else:
Domoticz.Debug("Connection already active")
def UpdateDevice(self):
for i in self.listdevice:
mac = i
unit = GetDevice(mac)
Dev_name = self.listdevice[i]['hostname']
if Dev_name == "":
Dev_name = self.listdevice[i]['ipaddress']
#Create device if not exist
if not unit:
unit = FreeUnit()
Domoticz.Status("DeviceCreation : " + Dev_name )
Domoticz.Device(Name=Dev_name, Unit=unit, DeviceID=mac , Type=244, Subtype=73, Switchtype=0, Image=17, ).Create()
active = Devices[unit].nValue
if self.listdevice[i]['active'] != Devices[unit].nValue:
kwarg = {}
Domoticz.Log("Device Update : " + Dev_name )
kwarg['SignalLevel'] = self.listdevice[i]['rssi']
kwarg['nValue'] = self.listdevice[i]['active']
if kwarg['nValue'] == 1:
kwarg['sValue'] = "On"
else:
kwarg['sValue'] = "Off"
Devices[unit].Update(**kwarg)
if ADSL_QUALITY:
unit = GetDevice("ADSL_QUALITY")
if not unit:
unit = FreeUnit()
Domoticz.Device(Name="ADSL_QUALITY", Unit=unit, DeviceID="ADSL_QUALITY" , Type=243, Subtype=19 ).Create()
kwarg = {}
kwarg['nValue'] = 0
kwarg['sValue'] = "Up: " + str(self.up) + " Down : " + str(self.down)
Devices[unit].Update(**kwarg)
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
#-----------------------------------------------------------------
def GetDevice(mac):
for x in Devices:
if Devices[x].DeviceID == str(mac) :
return x
return False
def FreeUnit() :
FreeUnit = ""
for x in range(1,256):
if x not in Devices :
FreeUnit=x
return FreeUnit
if FreeUnit == "" :
FreeUnit=len(Devices)+1
return FreeUnit
def GetToken(cookie):
if not cookie:
Domoticz.Error("Cookie manquants")
try:
headers={'Accept':'*/*','host':URL,"Cookie":cookie}
result = requests.get('https://' + URL + '/api/v1/device/token' , headers=headers, timeout = 5, verify=False)
_json = result.json()
if 'exception' in _json:
Domoticz.Error("Token Error : " + str(_json['exception']))
if _json['exception']['code'] == '401':
Domoticz.Error("Token Error : Not Authorized")
return False
_json = _json[0]['device']['token']
Domoticz.Log("Token ok")
return _json
except:
Domoticz.Error("Token non recupéré")
return False
def GetCookie(password):
try:
data = {'password': password,'remember':'1'}
headers={'Accept':'*/*','host':URL}
result = requests.post('https://' + URL + '/api/v1/login' , headers=headers, data = data, timeout = 5, verify=False)
cookie = 'BBOX_ID=' + result.cookies['BBOX_ID']
Domoticz.Status("Cookie recupéré, mode admin possible !")
return cookie
except:
Domoticz.Error("Pas de cookie recupéré, les fonctions demandant une autorisation ne marcheront pas")
try:
Domoticz.Error(str(result.json()))
except:
Domoticz.Error("No connexion usable")
return None