-
Notifications
You must be signed in to change notification settings - Fork 26
/
vehicle.py
550 lines (458 loc) · 18.9 KB
/
vehicle.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"""Vehicle model."""
import asyncio
import copy
import json
from datetime import date
from functools import partial
from itertools import groupby
from operator import attrgetter
from typing import Any, Dict, List, Optional, Tuple
from arrow import Arrow
from mytoyota.api import Api
from mytoyota.models.dashboard import Dashboard
from mytoyota.models.endpoints.vehicle_guid import VehicleGuidModel
from mytoyota.models.location import Location
from mytoyota.models.lock_status import LockStatus
from mytoyota.models.nofication import Notification
from mytoyota.models.service_history import ServiceHistory
from mytoyota.models.summary import Summary, SummaryType
from mytoyota.models.trips import Trip
from mytoyota.utils.helpers import add_with_none
from mytoyota.utils.log_utils import censor_all
class Vehicle:
"""Vehicle data representation."""
def __init__(self, api: Api, vehicle_info: VehicleGuidModel, metric: bool = True) -> None:
"""Initialise the Vehicle data representation."""
self._vehicle_info = vehicle_info
self._api = api
self._endpoint_data: Dict[str, Any] = {}
self._metric = metric
# Endpoint Name, Function to check if car supports the endpoint, endpoint to call to update
api_endpoints = [
{
"name": "location",
"capable": vehicle_info.extended_capabilities.last_parked_capable
or vehicle_info.features.last_parked,
"function": partial(self._api.get_location_endpoint, vin=vehicle_info.vin),
},
{
"name": "health_status",
"capable": True, # TODO Unsure of the required capability
"function": partial(
self._api.get_vehicle_health_status_endpoint,
vin=vehicle_info.vin,
),
},
{
"name": "electric_status",
"capable": vehicle_info.extended_capabilities.econnect_vehicle_status_capable,
"function": partial(
self._api.get_vehicle_electric_status_endpoint,
vin=vehicle_info.vin,
),
},
{
"name": "telemetry",
"capable": vehicle_info.extended_capabilities.telemetry_capable,
"function": partial(self._api.get_telemetry_endpoint, vin=vehicle_info.vin),
},
{
"name": "notifications",
"capable": True, # TODO Unsure of the required capability
"function": partial(self._api.get_notification_endpoint, vin=vehicle_info.vin),
},
{
"name": "status",
"capable": vehicle_info.extended_capabilities.vehicle_status,
"function": partial(self._api.get_remote_status_endpoint, vin=vehicle_info.vin),
},
{
"name": "service_history",
"capable": vehicle_info.features.service_history,
"function": partial(self._api.get_service_history_endpoint, vin=vehicle_info.vin),
},
]
self._endpoint_collect = [
(endpoint["name"], endpoint["function"])
for endpoint in api_endpoints
if endpoint["capable"]
]
async def update(self) -> None:
"""Update the data for the vehicle.
This method asynchronously updates the data for the vehicle by
calling the endpoint functions in parallel.
Returns
-------
None
"""
async def parallel_wrapper(name: str, function: partial) -> Tuple[str, Dict[str, Any]]:
r = await function()
return name, r
responses = asyncio.gather(
*[parallel_wrapper(name, function) for name, function in self._endpoint_collect]
)
for name, data in await responses:
self._endpoint_data[name] = data
@property
def vin(self) -> Optional[str]:
"""Return the vehicles VIN number.
Returns
-------
The vehicles VIN number
"""
return self._vehicle_info.vin
@property
def alias(self) -> Optional[str]:
"""Vehicle's alias.
Returns
-------
Nickname of vehicle
"""
return self._vehicle_info.nickname
@property
def type(self) -> Optional[str]:
"""Returns the "type" of vehicle.
Returns
-------
"fuel" if only fuel based
"mildhybrid" if hybrid
"phev" if plugin hybrid
"ev" if full electric vehicle
"""
# TODO enum
# TODO currently guessing until we see a mild hybrid and full EV
# TODO should probably use electricalPlatformCode but values currently unknown
# TODO list of fuel types. ?: G=Petrol Only, I=Hybrid
return "phev" if self._vehicle_info.ev_vehicle and self._vehicle_info.fuel_type else "ev"
@property
def dashboard(self) -> Optional[Dashboard]:
"""Returns the Vehicle dashboard.
The dashboard consists of items of information you would expect to
find on the dashboard. i.e. Fuel Levels.
Returns
-------
A dashboard
"""
# Always returns a Dashboard object as we can always get the odometer value
return Dashboard(
self._endpoint_data["telemetry"] if "telemetry" in self._endpoint_data else None,
self._endpoint_data["electric_status"]
if "electric_status" in self._endpoint_data
else None,
self._endpoint_data["health_status"]
if "health_status" in self._endpoint_data
else None,
self._metric,
)
@property
def location(self) -> Optional[Location]:
"""Return the vehicles latest reported Location.
Returns
-------
The latest location or None. If None vehicle car does not support
providing location information.
_Note_ an empty location object can be returned when the Vehicle
supports location but none is currently available.
"""
return (
Location(self._endpoint_data["location"])
if "location" in self._endpoint_data
else None
)
@property # TODO: Cant have a property with parameters! Split into two methods?
def notifications(self) -> Optional[List[Notification]]:
r"""Returns a list of notifications for the vehicle.
Args:
----
include_read (bool, optional): Indicates whether to include read notifications. \n
Defaults to False.
Returns:
-------
Optional[List[Notification]]: A list of notifications for the vehicle,
or None if not supported.
"""
if "notifications" in self._endpoint_data:
ret: List[Notification] = []
for p in self._endpoint_data["notifications"].payload:
ret.extend(Notification(n) for n in p.notifications)
return ret
return None
@property
def service_history(self) -> Optional[List[ServiceHistory]]:
r"""Returns a list of service history entries for the vehicle.
Returns
-------
Optional[List[ServiceHistory]]: A list of service history entries for the vehicle,
or None if not supported.
"""
if "service_history" in self._endpoint_data:
ret: List[ServiceHistory] = []
payload = self._endpoint_data["service_history"].payload
ret.extend(
ServiceHistory(service_history) for service_history in payload.service_histories
)
return ret
return None
def get_latest_service_history(self) -> Optional[ServiceHistory]:
r"""Return the latest service history entry for the vehicle.
Returns
-------
Optional[ServiceHistory]: A service history entry for the vehicle,
ordered by date and service_category. None if not supported or unknown.
"""
if self.service_history is not None:
return max(self.service_history, key=lambda x: (x.service_date, x.service_category))
return None
@property
def lock_status(self) -> Optional[LockStatus]:
"""Returns the latest lock status of Doors & Windows.
Returns
-------
Optional[LockStatus]: The latest lock status of Doors & Windows,
or None if not supported.
"""
return LockStatus(
self._endpoint_data["status"] if "status" in self._endpoint_data else None
)
async def get_summary(
self,
from_date: date,
to_date: date,
summary_type: SummaryType = SummaryType.MONTHLY,
) -> List[Summary]:
"""Return a Daily, Weekly, Monthly or Yearly summary between the provided dates.
All but Daily can return a partial time range. For example if the summary_type is weekly
and the date ranges selected include partial weeks these partial weeks will be returned.
The dates contained in the summary will indicate the range of dates that made up the
partial week.
Note: Weekly and yearly summaries lose a small amount of accuracy due to rounding issues
Args:
----
from_date (date, required): The inclusive from date to report summaries.
to_date (date, required): The inclusive to date to report summaries.
summary_type (SummaryType, optional): Daily, Monthly or Yearly summary.
Monthly by default.
Returns:
-------
List[Summary]: A list of summaries or empty list if not supported.
"""
if to_date > date.today(): # Future dates not allowed
to_date = date.today()
# Summary information is always returned in the first response.
# No need to check all the following pages
resp = await self._api.get_trips_endpoint(
self.vin, from_date, to_date, summary=True, limit=1, offset=0
)
if resp.payload is None or len(resp.payload.summary) == 0:
return []
# Convert to response
if summary_type == SummaryType.DAILY:
return self._generate_daily_summaries(resp.payload.summary)
elif summary_type == SummaryType.WEEKLY:
return self._generate_weekly_summaries(resp.payload.summary)
elif summary_type == SummaryType.MONTHLY:
return self._generate_monthly_summaries(resp.payload.summary, from_date, to_date)
elif summary_type == SummaryType.YEARLY:
return self._generate_yearly_summaries(resp.payload.summary, to_date)
else:
raise AssertionError("No such SummaryType")
async def get_current_day_summary(self) -> Optional[Summary]:
"""Return a summary for the current day.
Returns
-------
Optional[Summary]: A summary or None if not supported.
"""
summary = await self.get_summary(
from_date=Arrow.now().date(),
to_date=Arrow.now().date(),
summary_type=SummaryType.DAILY,
)
assert len(summary) < 2
return summary[0] if len(summary) > 0 else None
async def get_current_week_summary(self) -> Optional[Summary]:
"""Return a summary for the current week.
Returns
-------
Optional[Summary]: A summary or None if not supported.
"""
summary = await self.get_summary(
from_date=Arrow.now().floor("week").date(),
to_date=Arrow.now().date(),
summary_type=SummaryType.WEEKLY,
)
assert len(summary) < 2
return summary[0] if len(summary) > 0 else None
async def get_current_month_summary(self) -> Optional[Summary]:
"""Return a summary for the current month.
Returns
-------
Optional[Summary]: A summary or None if not supported.
"""
summary = await self.get_summary(
from_date=Arrow.now().floor("month").date(),
to_date=Arrow.now().date(),
summary_type=SummaryType.MONTHLY,
)
assert len(summary) < 2
return summary[0] if len(summary) > 0 else None
async def get_current_year_summary(self) -> Optional[Summary]:
"""Return a summary for the current year.
Returns
-------
Optional[Summary]: A summary or None if not supported.
"""
summary = await self.get_summary(
from_date=Arrow.now().floor("year").date(),
to_date=Arrow.now().date(),
summary_type=SummaryType.YEARLY,
)
assert len(summary) < 2
return summary[0] if len(summary) > 0 else None
async def get_trips(
self, from_date: date, to_date: date, full_route: bool = False
) -> Optional[List[Trip]]:
"""Return information on all trips made between the provided dates.
Args:
----
from_date (date, required): The inclusive from date
to_date (date, required): The inclusive to date
full_route (bool, optional): Provide the full route information for each trip
Returns:
-------
Optional[List[Something]]: A list of all trips or None if not supported.
"""
ret: List[Trip] = []
offset = 0
while True:
resp = await self._api.get_trips_endpoint(
self.vin,
from_date,
to_date,
summary=False,
limit=5,
offset=offset,
route=full_route,
)
if resp.payload is None:
break
# Convert to response
for t in resp.payload.trips:
ret.append(Trip(t, self._metric))
offset = resp.payload.metadata.pagination.next_offset
if offset is None:
break
return ret
#
# More get functionality depending on what we find
#
async def set_alias(self, value) -> bool:
"""Set the alias for the vehicle.
Args:
----
value: The alias value to set for the vehicle.
Returns:
-------
bool
"""
return value
#
# More set functionality depending on what we find
#
def _dump_all(self) -> Dict[str, Any]:
"""Dump data from all endpoints for debugging and further work."""
dump: [str, Any] = {"vehicle_info": json.loads(self._vehicle_info.model_dump_json())}
for name, data in self._endpoint_data.items():
dump[name] = json.loads(data.model_dump_json())
return censor_all(copy.deepcopy(dump))
def _generate_daily_summaries(self, summary) -> List[Summary]:
summary.sort(key=attrgetter("year", "month"))
return [
Summary(
histogram.summary,
self._metric,
Arrow(histogram.year, histogram.month, histogram.day).date(),
Arrow(histogram.year, histogram.month, histogram.day).date(),
histogram.hdc,
)
for month in summary
for histogram in sorted(month.histograms, key=attrgetter("day"))
]
def _generate_weekly_summaries(self, summary) -> List[Summary]:
ret: List[Summary] = []
summary.sort(key=attrgetter("year", "month"))
# Flatten the list of histograms
histograms = [histogram for month in summary for histogram in month.histograms]
histograms.sort(key=lambda h: date(day=h.day, month=h.month, year=h.year))
# Group histograms by week
for _, week_histograms_iter in groupby(
histograms, key=lambda h: Arrow(h.year, h.month, h.day).span("week")[0]
):
week_histograms = list(week_histograms_iter)
build_hdc = copy.copy(week_histograms[0].hdc)
build_summary = copy.copy(week_histograms[0].summary)
start_date = Arrow(
week_histograms[0].year,
week_histograms[0].month,
week_histograms[0].day,
)
for histogram in week_histograms[1:]:
add_with_none(build_hdc, histogram.hdc)
build_summary += histogram.summary
end_date = Arrow(
week_histograms[-1].year,
week_histograms[-1].month,
week_histograms[-1].day,
)
ret.append(
Summary(
build_summary,
self._metric,
start_date.date(),
end_date.date(),
build_hdc,
)
)
return ret
def _generate_monthly_summaries(
self, summary, from_date: date, to_date: date
) -> List[Summary]:
# Convert all the monthly responses from the payload to a summary response
ret: List[Summary] = []
summary.sort(key=attrgetter("year", "month"))
for month in summary:
month_start = Arrow(month.year, month.month, 1).date()
month_end = Arrow(month.year, month.month, 1).shift(months=1).shift(days=-1).date()
ret.append(
Summary(
month.summary,
self._metric,
# The data might not include an entire month so update start and end dates
max(month_start, from_date),
min(month_end, to_date),
month.hdc,
)
)
return ret
def _generate_yearly_summaries(self, summary, to_date: date) -> List[Summary]:
summary.sort(key=attrgetter("year", "month"))
ret: List[Summary] = []
build_hdc = copy.copy(summary[0].hdc)
build_summary = copy.copy(summary[0].summary)
start_date = date(day=1, month=summary[0].month, year=summary[0].year)
if len(summary) == 1:
ret.append(Summary(build_summary, self._metric, start_date, to_date, build_hdc))
else:
for month, next_month in zip(summary[1:], summary[2:] + [None]):
summary_month = date(day=1, month=month.month, year=month.year)
add_with_none(build_hdc, month.hdc)
build_summary += month.summary
if next_month is None or next_month.year != month.year:
end_date = min(to_date, date(day=31, month=12, year=summary_month.year))
ret.append(
Summary(build_summary, self._metric, start_date, end_date, build_hdc)
)
if next_month:
start_date = date(day=1, month=next_month.month, year=next_month.year)
build_hdc = copy.copy(next_month.hdc)
build_summary = copy.copy(next_month.summary)
return ret