forked from yleroux/DomoticzNissanLeaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
317 lines (257 loc) · 12.8 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
# Basic Python Plugin Example
#
# Author: Breizhcat
#
"""
<plugin key="NissanLeaf" name="Domoticz Nissan Leaf" author="breizhcat" version="1.0.4" wikilink="http://www.domoticz.com/wiki/plugins/plugin.html" externallink="https://github.com/BreizhCat/DomoticzNissanLeaf">
<description>
<h2>Nissan Leaf</h2><br/>
<h3>Features</h3>
<ul style="list-style-type:square">
<li>Battery Level</li>
<li>Charging Status</li>
<li>Range autonomy (with / without AC)</li>
<li>Information about distance driven</li>
</ul>
</description>
<params>
<param field="Username" label="Nissan Account" width="150px" required="true" />
<param field="Password" label="Nissan Password" width="150px" required="true" password="true" />
<param field="Mode5" label="Region Code" width="150px">
<options>
<option label="Europe" value="NE" />
<option label="United States" value="NNA" />
<option label="Canada" value="NCI" />
<option label="Japan" value="NML" />
<option label="Australia" value="NMA" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
BASE_URL = 'https://gdcportalgw.its-mo.com/api_v210707_NE/gdc/'
IMAGE_CAR = 'NissanLeafCar'
IMAGE_BATTERY = 'NissanLeafBattery'
IMAGE_PLUG = 'NissanLeafPlug'
DEVICE_BATTERY = 1
DEVICE_RANGE_AC = 2
DEVICE_RANGE_NO_AC = 3
DEVICE_CHARGE = 4
DEVICE_UPDATE = 5
DEVICE_ODOMETER = 6
import Domoticz
from datetime import datetime
import threading
# Based on https://github.com/nricklin/leafpy
from Crypto.Cipher import Blowfish
import requests, base64
def login(username, password, region_code='NNA', initial_app_strings='9s5rfKVuMrT03RtzajWNcA'):
baseprm = b'88dSp7wWnV3bvv9Z88zEwg'
c1 = Blowfish.new(baseprm, Blowfish.MODE_ECB)
packingLength = 8 - len(password) % 8
packedPassword = password + chr(packingLength) * packingLength
encryptedPassword = c1.encrypt(packedPassword.encode('latin-1'))
encodedPassword = base64.standard_b64encode(encryptedPassword)
url = BASE_URL + "/UserLoginRequest.php"
data = {
"RegionCode": region_code,
"UserId": username,
"initial_app_str": initial_app_strings,
"Password": encodedPassword,
}
headers = {'User-Agent': 'Mozilla/5.0'}
r = requests.post(url,data=data, headers=headers)
r.raise_for_status()
if not r.json()['status'] == 200:
raise Exception('Cannot login. Probably username & password are wrong. ' + r.text)
custom_sessionid = r.json()['VehicleInfoList']['vehicleInfo'][0]['custom_sessionid']
VIN = r.json()['CustomerInfo']['VehicleInfo']['VIN']
return custom_sessionid, VIN
class Leaf(object):
"""Make requests to the Nissan Connect API to get Leaf Info"""
custom_sessionid = None
VIN = None
region_code = None
def __init__(self, username=None, password=None, custom_sessionid=None, VIN=None, region_code='NNA'):
self.region_code = region_code
if username and password:
self.custom_sessionid, self.VIN = login(username, password, self.region_code)
elif custom_sessionid and VIN:
self.custom_sessionid = custom_sessionid
self.VIN = VIN
else:
raise Exception('Need either username & password or custom_sessionid & VIN.')
def __getattr__(self, name):
"""
Top secret magic. Calling Leaf.<some_function_name>() hits <some_function_name>.php
"""
if name.startswith('__'):
raise AttributeError(name)
def call(**kwargs):
url = BASE_URL + name + '.php'
data = {
"RegionCode": self.region_code,
"custom_sessionid": self.custom_sessionid,
"VIN": self.VIN
}
for k in kwargs:
data[k] = kwargs[k]
r = requests.post(url, data=data)
r.raise_for_status()
if not r.json()['status'] == 200:
raise Exception('Error making request. Perhaps the session has expired.')
return r.json()
return call
class BasePlugin:
enabled = False
def __init__(self):
Domoticz.Log("__init__ called")
def onStart(self):
if Parameters['Mode6'] == "Debug":
Domoticz.Debugging(1)
self._create_icons()
self._create_devices()
self._updateDevices()
Domoticz.Log("onStart called")
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Connection, Data):
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 == DEVICE_UPDATE:
#self._updateDevices()
Domoticz.Log(str(Devices[DEVICE_ODOMETER].Options))
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):
_time = datetime.now()
if (_time.minute in [0,15,30,45] and _time.second < 10):
Domoticz.Log("Updating devices")
self._updateDevices()
def _create_icons(self):
if IMAGE_CAR not in Images:
Domoticz.Image("IconNissanLeafCar.zip").Create()
if IMAGE_BATTERY not in Images:
Domoticz.Image("IconNissanLeafBattery.zip").Create()
if IMAGE_PLUG not in Images:
Domoticz.Image("IconNissanLeafPlug.zip").Create()
def _create_devices(self):
if DEVICE_BATTERY not in Devices:
Domoticz.Device(Name= 'Battery', Unit=DEVICE_BATTERY, TypeName='Percentage', Image=Images[IMAGE_BATTERY].ID,
Description='Battery Remaining Amount', Used=1).Create()
if DEVICE_RANGE_AC not in Devices:
Domoticz.Device(Name= 'Distance AC', Unit=DEVICE_RANGE_AC, TypeName="Custom", Options={"Custom": "0;km"},
Image=Images[IMAGE_CAR].ID, Description='Maximum Distance with A/C', Used=1).Create()
if DEVICE_RANGE_NO_AC not in Devices:
Domoticz.Device(Name= 'Distance w/o AC', Unit=DEVICE_RANGE_NO_AC, TypeName="Custom", Options={"Custom": "0;km"},
Image=Images[IMAGE_CAR].ID, Description='Maximum Distance with A/C', Used=1).Create()
if DEVICE_CHARGE not in Devices:
Domoticz.Device(Unit=DEVICE_CHARGE, Name="Charge", Image=Images[IMAGE_PLUG].ID, Type=244, Subtype=62, Switchtype=0, Used=1).Create()
if DEVICE_UPDATE not in Devices:
Domoticz.Device(Unit=DEVICE_UPDATE, Name="Refresh Data", Type=244, Subtype=62, Switchtype=9, Used=1).Create()
if DEVICE_ODOMETER not in Devices:
Domoticz.Device(Name= 'Driven', Unit=DEVICE_ODOMETER, Type=243, Subtype=33, Switchtype=3,
Options={'AddjValue2': 1000, 'ValueQuantity': 'Kilometers', 'ValueUnits': 'Km', 'AddDBLogEntry' : 'true', 'DisableLogAutoUpdate' : 'true'},
Image=Images[IMAGE_CAR].ID, Description='Distance driven', Used=1).Create()
def _updateDevices(self):
thread = threading.Thread(name="UpdateLeafInformations", target=BasePlugin._connect_and_update, args=(self,))
thread.start()
def _connect_and_update(self):
try:
Domoticz.Log("Plugin running - Trying to connect")
leaf = Leaf(Parameters["Username"], Parameters["Password"], region_code=Parameters["Mode5"])
if leaf:
battery = leaf.BatteryStatusRecordsRequest()
batteryRemaining = battery['BatteryStatusRecords']['BatteryStatus']['BatteryRemainingAmount']
batteryCapacity = battery['BatteryStatusRecords']['BatteryStatus']['BatteryCapacity']
batteryValue = float("{:.2f}".format((int(batteryRemaining) / int(batteryCapacity)) * 100))
nValue = int(batteryValue)
sValue = str(batteryValue) + ';' + str(batteryValue) + ';' + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Devices[DEVICE_BATTERY].Update(nValue = nValue, sValue = sValue)
Domoticz.Log("Battery = " + str(batteryValue) + " %")
nValue = int(int(battery['BatteryStatusRecords']['CruisingRangeAcOn']) / 1000)
sValue = str(nValue) + ';' + str(nValue) + ';' + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Domoticz.Log("Range with AC = {} km".format(nValue))
Devices[DEVICE_RANGE_AC].Update(nValue = nValue, sValue = sValue)
nValue = int(int(battery['BatteryStatusRecords']['CruisingRangeAcOff']) / 1000)
sValue = str(nValue) + ';' + str(nValue) + ';' + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Devices[DEVICE_RANGE_NO_AC].Update(nValue = nValue, sValue = sValue)
Domoticz.Log("Range without AC = {} km".format(nValue))
status = battery['BatteryStatusRecords']['BatteryStatus']['BatteryChargingStatus']
if status == 'NOT_CHARGING':
Devices[DEVICE_CHARGE].Update(nValue = 0, sValue = "0")
else:
Devices[DEVICE_CHARGE].Update(nValue = 1, sValue = "1")
Domoticz.Log("Charging State = {}".format(status))
distance = leaf.PriceSimulatorDetailInfoRequest()
today = False
for i in distance['PriceSimulatorDetailInfoResponsePersonalData']['PriceSimulatorDetailInfoDateList']['PriceSimulatorDetailInfoDate']:
km = 0
for j in i['PriceSimulatorDetailInfoTripList']['PriceSimulatorDetailInfoTrip']:
km += int(j['TravelDistance'])
nValue = 0
kmsValue = float("{:.2f}".format(km))
sValue = str(kmsValue) + ';' + str(kmsValue) + ';' + i['TargetDate']
Devices[DEVICE_ODOMETER].Update(nValue = nValue, sValue = sValue)
if i['TargetDate'] == datetime.now().strftime('%Y-%m-%d'):
today = True
sValue = str(kmsValue) + ';' + str(kmsValue)
Devices[DEVICE_ODOMETER].Update(nValue = nValue, sValue = sValue)
if not today:
Devices[DEVICE_ODOMETER].Update(nValue = 0, sValue = "0;0")
Domoticz.Log("onHeartbeat Connection ok")
else:
Domoticz.Log("onHeartbeat Connection ko")
except Exception as err:
Domoticz.Error("handleThread: "+str(err)+' line '+format(sys.exc_info()[-1].tb_lineno))
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()
# 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