-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathkamereon.py
406 lines (337 loc) · 13.2 KB
/
kamereon.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
from .credentials import CredentialStore, requires_credentials
from .gigya import Gigya
from .schedule import ChargeSchedules, ChargeMode
from collections import namedtuple
from enum import Enum
from functools import lru_cache
import datetime
import dateutil.tz
import itertools
import jwt
import logging
import os
import requests
import simplejson
DEFAULT_ROOT_URL = 'https://api-wired-prod-1-euw1.wrd-aws.com'
_log = logging.getLogger('pyze.api.kamereon')
class AccountException(Exception):
pass
class CachingAPIObject(object):
def _clear_all_caches(self):
cached_funcs = [f for f in dir(self) if hasattr(f, 'cache_clear')]
for func in cached_funcs:
func.cache_clear()
class Kamereon(CachingAPIObject):
def __init__(
self,
api_key=None,
credentials=None,
gigya=None,
country='GB',
root_url=DEFAULT_ROOT_URL
):
self._root_url = root_url
self._credentials = credentials or CredentialStore()
self._country = country
self._gigya = gigya or Gigya(credentials=self._credentials)
self._session = requests.Session()
if api_key:
self.set_api_key(api_key)
@staticmethod
def print_multiple_account_warning(accounts):
print("WARNING: Multiple Kamereon accounts found:")
for acc in accounts:
print('- {}'.format(acc['accountId']))
print('Using the first of these. If that\'s not correct (perhaps you can\'t see your vehicle)')
print('or to silence this message, run `pyze set-account` or set the KAMEREON_ACCOUNT_ID')
print('environment variable to the account you want to use i.e.')
print(' KAMEREON_ACCOUNT_ID=abcdef123456789 pyze ...')
print('API users may instead call Kamereon#set_account_id().')
print('This setting will persist until you next log in.')
def set_api_key(self, api_key):
self._credentials.store('kamereon-api-key', api_key, None)
def get_account_id(self):
if 'KAMEREON_ACCOUNT_ID' in os.environ:
self.set_account_id(os.environ['KAMEREON_ACCOUNT_ID'])
if 'kamereon-account' in self._credentials:
return self._credentials['kamereon-account']
accounts = self.get_accounts()
if len(accounts) == 0:
raise AccountException('No Kamereon accounts found!')
if len(accounts) > 1:
Kamereon.print_multiple_account_warning(accounts)
account = accounts[0]
self._clear_all_caches()
self._credentials['kamereon-account'] = (account['accountId'], None)
return account['accountId']
@requires_credentials('gigya', 'gigya-person-id', 'kamereon-api-key')
def get_accounts(self):
response = self._session.get(
'{}/commerce/v1/persons/{}?country={}'.format(
self._root_url,
self._credentials['gigya-person-id'],
self._country
),
headers={
'apikey': self._credentials['kamereon-api-key'],
'x-gigya-id_token': self._gigya.get_jwt_token()
}
)
response.raise_for_status()
response_body = response.json()
_log.debug('Received Kamereon accounts response: {}'.format(response_body))
return response_body.get('accounts', [])
def set_account_id(self, account_id):
self._credentials['kamereon-account'] = (account_id, None)
@lru_cache(maxsize=1)
@requires_credentials('kamereon-api-key')
def get_vehicles(self):
response = self._session.get(
'{}/commerce/v1/accounts/{}/vehicles?country={}'.format(
self._root_url,
self.get_account_id(),
self._country
),
headers={
'apikey': self._credentials['kamereon-api-key'],
'x-gigya-id_token': self._gigya.get_jwt_token(),
}
)
response.raise_for_status()
response_body = response.json()
_log.debug('Received Kamereon vehicles response: {}'.format(response_body))
return response_body
class Vehicle(object):
def __init__(self, vin, kamereon=None):
self._vin = vin
self._kamereon = kamereon or Kamereon()
self._root_url = self._kamereon._root_url
@requires_credentials('kamereon-api-key')
def _request(self, method, endpoint, **kwargs):
return self._kamereon._session.request(
method,
endpoint,
headers={
'Content-type': 'application/vnd.api+json',
'apikey': self._kamereon._credentials['kamereon-api-key'],
'x-gigya-id_token': self._kamereon._gigya.get_jwt_token(),
},
params={
'country': self._kamereon._country
},
**kwargs
)
def _get(self, endpoint, version=1):
response = self._request(
'GET',
'{}/commerce/v1/accounts/{}/kamereon/kca/car-adapter/v{}/cars/{}/{}'.format(
self._root_url,
self._kamereon.get_account_id(),
version,
self._vin,
endpoint
)
)
_log.debug('Received Kamereon vehicle response: {}'.format(response.text))
_log.debug('Response headers: {}'.format(response.headers))
response.raise_for_status()
json = response.json()
return json['data']['attributes']
def _post(self, endpoint, data, version=1):
_log.debug('POSTing with data: {}'.format(data))
response = self._request(
'POST',
'{}/commerce/v1/accounts/{}/kamereon/kca/car-adapter/v{}/cars/{}/{}'.format(
self._root_url,
self._kamereon.get_account_id(),
version,
self._vin,
endpoint
),
json={
'data': data
}
)
_log.debug('Received Kamereon vehicle response: {}'.format(response.text))
_log.debug('Response headers: {}'.format(response.headers))
response.raise_for_status()
json = response.json()
return json
def battery_status(self):
return self._get('battery-status', 2)
def location(self):
return self._get('location')
def hvac_status(self):
return self._get('hvac-status')
def charge_mode(self):
raw_mode = self._get('charge-mode')['chargeMode']
if hasattr(ChargeMode, raw_mode):
return getattr(ChargeMode, raw_mode)
else:
return raw_mode
def mileage(self):
return self._get('cockpit', 1)
# Not (currently) implemented server-side
def lock_status(self):
return self._get('lock-status')
# Not implemented server-side for most vehicles
def charge_schedules(self):
return ChargeSchedules(
self._get('charging-settings')
)
def notification_settings(self):
return self._get('notification-settings')
def charge_history(self, start, end):
if not isinstance(start, datetime.datetime):
raise RuntimeError('`start` should be an instance of datetime.datetime, not {}'.format(start.__class__))
if not isinstance(end, datetime.datetime):
raise RuntimeError('`end` should be an instance of datetime.datetime, not {}'.format(end.__class__))
return self._get(
'charges?start={}&end={}'.format(
start.strftime('%Y%m%d'),
end.strftime('%Y%m%d')
)
).get('charges', [])
def charge_statistics(self, start, end, period='month'):
if not isinstance(start, datetime.datetime):
raise RuntimeError('`start` should be an instance of datetime.datetime, not {}'.format(start.__class__))
if not isinstance(end, datetime.datetime):
raise RuntimeError('`end` should be an instance of datetime.datetime, not {}'.format(end.__class__))
if period not in PERIOD_FORMATS.keys():
raise RuntimeError('`period` should be one of `month`, `day`')
return self._get(
'charge-history?type={}&start={}&end={}'.format(
period,
start.strftime(PERIOD_FORMATS[period]),
end.strftime(PERIOD_FORMATS[period])
)
)['chargeSummaries']
def hvac_history(self, start, end):
if not isinstance(start, datetime.datetime):
raise RuntimeError('`start` should be an instance of datetime.datetime, not {}'.format(start.__class__))
if not isinstance(end, datetime.datetime):
raise RuntimeError('`end` should be an instance of datetime.datetime, not {}'.format(end.__class__))
return self._get(
'hvac-sessions?start={}&end={}'.format(
start.strftime('%Y%m%d'),
end.strftime('%Y%m%d')
)
).get('hvacSessions', [])
def hvac_statistics(self, start, end, period='month'):
if not isinstance(start, datetime.datetime):
raise RuntimeError('`start` should be an instance of datetime.datetime, not {}'.format(start.__class__))
if not isinstance(end, datetime.datetime):
raise RuntimeError('`end` should be an instance of datetime.datetime, not {}'.format(end.__class__))
if period not in PERIOD_FORMATS.keys():
raise RuntimeError('`period` should be one of `month`, `day`')
return self._get(
'hvac-history?type={}&start={}&end={}'.format(
period,
start.strftime(PERIOD_FORMATS[period]),
end.strftime(PERIOD_FORMATS[period])
)
)['hvacSessionsSummaries']
# Actions
def ac_start(self, when=None, temperature=21):
attrs = {
'action': 'start',
'targetTemperature': temperature
}
if when:
if not isinstance(when, datetime.datetime):
raise RuntimeError('`when` should be an instance of datetime.datetime, not {}'.format(when.__class__))
attrs['startDateTime'] = when.astimezone(
dateutil.tz.tzutc()
).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
return self._post(
'actions/hvac-start',
{
'type': 'HvacStart',
'attributes': attrs
}
)
def cancel_ac(self):
return self._post(
'actions/hvac-start',
{
'type': 'HvacStart',
'attributes': {
'action': 'cancel'
}
}
)
def set_charge_schedules(self, schedules):
if not isinstance(schedules, ChargeSchedules):
raise RuntimeError('Expected schedule to be instance of ChargeSchedules, but got {} instead'.format(schedules.__class__))
schedules.validate()
data = {
'type': 'ChargeSchedule',
'attributes': schedules
}
return self._post(
'actions/charge-schedule',
simplejson.loads(simplejson.dumps(data, for_json=True)),
version=2
)
def set_charge_mode(self, charge_mode):
if not isinstance(charge_mode, ChargeMode):
raise RuntimeError('Expected charge_mode to be instance of ChargeMode, but got {} instead'.format(charge_mode.__class__))
data = {
'type': 'ChargeMode',
'attributes': {
'action': charge_mode.name
}
}
return self._post(
'actions/charge-mode',
data
)
def charge_start(self):
return self._post(
'actions/charging-start',
{
'type': 'ChargingStart',
'attributes': {
'action': 'start'
}
}
)
# Serious metaprogramming follows:
# https://www.notinventedhere.org/articles/python/how-to-use-strings-as-name-aliases-in-python-enums.html
_CHARGE_STATES = {
0.0: ['Not charging', 'NOT_IN_CHARGE'],
0.1: ['Waiting for planned charge', 'WAITING_FOR_PLANNED_CHARGE'],
0.2: ['Charge ended', 'CHARGE_ENDED'],
0.3: ['Waiting for current charge', 'WAITING_FOR_CURRENT_CHARGE'],
0.4: ['Energy flap opened', 'ENERGY_FLAP_OPENED'],
1.0: ['Charging', 'CHARGE_IN_PROGRESS'],
# This next is more accurately "not charging" (<= ZE40) or "error" (ZE50).
# But I don't want to include 'error' in the output text because people will
# think that it's an error in Pyze when their ZE40 isn't plugged in...
-1.0: ['Not charging or plugged in', 'CHARGE_ERROR'],
-1.1: ['Not available', 'NOT_AVAILABLE']
}
ChargeState = Enum(
value='ChargeState',
names=itertools.chain.from_iterable(
itertools.product(v, [k]) for k, v in _CHARGE_STATES.items()
)
)
_PLUG_STATES = {
0: ['Unplugged', 'UNPLUGGED'],
1: ['Plugged in', 'PLUGGED'],
-1: ['Plug error', 'PLUG_ERROR'],
-2147483648: ['Not available', 'NOT_AVAILABLE']
}
PlugState = Enum(
value='PlugState',
names=itertools.chain.from_iterable(
itertools.product(v, [k]) for k, v in _PLUG_STATES.items()
)
)
PERIOD_FORMATS = {
'day': '%Y%m%d',
'month': '%Y%m'
}