-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
executable file
·149 lines (122 loc) · 5.56 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
"""
<plugin key="HuaweiModemLte" name="Huawei B525 LTE modem" author="fcoquelet" version="0.0.1">
<params>
<param field="Address" label="IP Address" required="true" default="192.168.8.1" />
<param field="Mode1" label="Polling interval" default="10" width="40px" required="true" />
<param field="Password" label="Password" required="true" />
</params>
</plugin>
"""
import Domoticz
import base64
import hashlib
import time
import huawei_urllib
class HuaweiPlugin:
DATA_SWITCH = 1
DATA_PLAN_CONSUMPTION = 2
DATA_DOWNLOAD = 3
DATA_UPLOAD = 4
def __init__(self):
self.saltedPassword = None
self.nextUpdate = None
return
def onStart(self):
Domoticz.Log("onStart called")
if len(Devices) < HuaweiPlugin.DATA_UPLOAD:
Domoticz.Device(Name="Data Switch",Unit=HuaweiPlugin.DATA_SWITCH,TypeName="Switch").Create()
Domoticz.Device(Name="Data Plan consumption",Unit=HuaweiPlugin.DATA_PLAN_CONSUMPTION,TypeName="Percentage").Create()
Domoticz.Device(Name="Data Download",Unit=HuaweiPlugin.DATA_DOWNLOAD,TypeName="Custom", Options = { "Custom": "1;MB"} ).Create()
Domoticz.Device(Name="Data Upload",Unit=HuaweiPlugin.DATA_UPLOAD,TypeName="Custom", Options = { "Custom": "1;MB"}).Create()
Domoticz.Log("Calculating salted Password")
self.saltedPassword = base64.b64encode(hashlib.sha256(Parameters["Password"].encode('utf-8')).hexdigest().encode('utf-8')).decode()
Domoticz.Log(self.saltedPassword)
self.nextUpdate = time.time()
self.client = huawei_urllib.Client(Parameters["Address"],no_ssl=True)
self.refresh()
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Connection, Data, Status, Extra):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if Unit == HuaweiPlugin.DATA_SWITCH:
if not self.client.isLogged():
self.client.getToken()
Domoticz.Log("Trying to log on")
self.client.login(self.saltedPassword)
Domoticz.Log(str(self.client.isLogged()))
try:
enable=(Command=="On")
if huawei_urllib.enable_data(self.client,enable):
self.updateDataSwitch(enable)
except HuaweiPlugin.NotLoggedException:
Domoticz.error("Unable to log on")
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")
def onHeartbeat(self):
currentTime = time.time()
if self.nextUpdate < currentTime:
#Compute next running time
self.nextUpdate = int(currentTime) + int(Parameters["Mode1"])
self.refresh()
def refresh(self):
if self.client.isLogged() or self.client.getToken():
self.updateDataSwitch(huawei_urllib.is_data_enabled(self.client))
usage = huawei_urllib.get_usage(self.client)
Devices[HuaweiPlugin.DATA_UPLOAD].Update(usage.upload,str(usage.upload))
Devices[HuaweiPlugin.DATA_DOWNLOAD].Update(usage.download,str(usage.download))
Devices[HuaweiPlugin.DATA_PLAN_CONSUMPTION].Update(usage.consumption,str(usage.consumption))
else:
Domoticz.Error("Unable to reach the Huawei Modem ({})".format(Parameters["Address"]))
def updateDataSwitch(self, enabled):
if enabled:
Domoticz.Log("On")
Devices[HuaweiPlugin.DATA_SWITCH].Update(1,"On")
else:
Domoticz.Log("Off")
Devices[HuaweiPlugin.DATA_SWITCH].Update(0,"Off")
global _plugin
_plugin = HuaweiPlugin()
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, Status, Extra):
global _plugin
_plugin.onMessage(Connection, Data, Status, Extra)
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()
# 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("Device ID: '" + str(Devices[x].ID) + "'")
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