Skip to content

Commit

Permalink
adjust models and print trips in _dump_all()
Browse files Browse the repository at this point in the history
  • Loading branch information
CM000n committed Nov 27, 2023
1 parent a79e042 commit 74553e2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
16 changes: 8 additions & 8 deletions mytoyota/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .const import BASE_URL
from .controller import Controller
from .exceptions import ToyotaApiError
from .models.endpoints.trip import Trips
from .models.endpoints.trip import TripsModel


class Api:
Expand Down Expand Up @@ -140,26 +140,26 @@ async def get_driving_statistics_endpoint(
async def get_trips_endpoint(
self,
vin: str,
from_: date,
to: date,
from_date: date,
to_date: date,
route: bool = False,
summary: bool = True,
limit: int = 5,
offset: int = 0,
) -> Trips:
) -> TripsModel:
"""Get trip
The page parameter works a bit strange but setting to 1 gets last few trips"""
data = await self.controller.request_json(
method="GET",
base_url=BASE_URL,
endpoint=f"/v1/trips?from={from_}&to={to}&route={route}&summary={summary}&limit={limit}&offset={offset}", # pylint: disable=C0301
endpoint=f"/v1/trips?from={from_date}&to={to_date}&route={route}&summary={summary}&limit={limit}&offset={offset}", # pylint: disable=C0301
headers={"vin": vin},
)

return Trips(**data["payload"])
return TripsModel(**data["payload"])

# TODO: Check if this is still in use and delete it otherwise
async def get_trip_endpoint(self, vin: str, trip_id: str) -> Trips:
async def get_trip_endpoint(self, vin: str, trip_id: str) -> TripsModel:
"""Get data for a single trip"""
data = await self.controller.request(
method="GET",
Expand All @@ -168,7 +168,7 @@ async def get_trip_endpoint(self, vin: str, trip_id: str) -> Trips:
headers={"vin": vin},
)

return Trips(**data)
return TripsModel(**data)

async def set_lock_unlock_vehicle_endpoint(
self, vin: str, action: str
Expand Down
66 changes: 33 additions & 33 deletions mytoyota/models/endpoints/trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic import BaseModel, Field


class _Pagination(BaseModel):
class _PaginationModel(BaseModel):
currentPage: int
limit: int
offset: int
Expand All @@ -14,25 +14,25 @@ class _Pagination(BaseModel):
totalCount: int


class _SortedBy(BaseModel):
class _SortedByModel(BaseModel):
field: str
order: str


class _MetaData(BaseModel):
pagination: _Pagination
sortedBy: list[_SortedBy]
class _MetaDataModel(BaseModel):
pagination: _PaginationModel
sortedBy: list[_SortedByModel]


class _Scores(BaseModel):
class _ScoresModel(BaseModel):
acceleration: int = Field(ge=0, le=100)
advice: int = Field(ge=0, le=100, default=0)
braking: int = Field(ge=0, le=100)
constantSpeed: int = Field(ge=0, le=100, default=0)
global_: Optional[int] = Field(ge=0, le=100, alias="global", default=None)


class _Summary(BaseModel):
class _SummaryModel(BaseModel):
countries: list[str]
duration: int
durationHighway: int
Expand All @@ -45,31 +45,31 @@ class _Summary(BaseModel):
maxSpeed: float


class _HDC(BaseModel):
class _HDCModel(BaseModel):
# Depending on
# car not being EV
# car being ev only
# car being hybrid but only used ev/fuel
chargeDist: int = Field(0)
chargeTime: int = Field(0)
ecoDist: int = Field(0)
ecoTime: int = Field(0)
evDistance: int = Field(0)
evTime: int = Field(0)
powerDist: int = Field(0)
powerTime: int = Field(0)


class _MonthSummary(BaseModel):
hdc: Optional[_HDC] = Field(default=None) # Only available on EV cars
chargeDist: int = 0
chargeTime: int = 0
ecoDist: int = 0
ecoTime: int = 0
evDistance: int = 0
evTime: int = 0
powerDist: int = 0
powerTime: int = 0


class _MonthSummaryModel(BaseModel):
hdc: Optional[_HDCModel] = None # Only available on EV cars
# histograms not imported
month: int = Field(..., ge=1, le=12)
scores: _Scores
summary: _Summary
scores: _ScoresModel
summary: _SummaryModel
year: int


class _TripSummary(_Summary):
class _TripSummaryModel(_SummaryModel):
averageSpeed: float
endLat: float
endLon: float
Expand All @@ -80,18 +80,18 @@ class _TripSummary(_Summary):
startTs: datetime


class _Trip(BaseModel):
class _TripModel(BaseModel):
# behaviours not imported
category: int
hdc: Optional[_HDC] = Field(default=None) # Only available on EV cars
hdc: Optional[_HDCModel] = None # Only available on EV cars
id: UUID
scores: _Scores
summary: _TripSummary
scores: _ScoresModel
summary: _TripSummaryModel


class Trips(BaseModel):
_metadata: _MetaData
from_: date = Field(..., alias="from")
summary: list[_MonthSummary] = []
to: date
trips: list[_Trip] = []
class TripsModel(BaseModel):
_metadata: _MetaDataModel
from_date: date = Field(..., alias="from")
to_date: date = Field(..., alias="to")
summary: list[_MonthSummaryModel] = []
trips: list[_TripModel] = []
15 changes: 14 additions & 1 deletion mytoyota/models/vehicle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Vehicle model."""
import asyncio
import copy
from datetime import date, timedelta
from functools import partial
import logging
from typing import Any, Optional
Expand Down Expand Up @@ -28,7 +29,7 @@ def __init__(

self._vehicle_info = vehicle_info
self._api = api
self._endpoint_data: [str, Any] = {}
self._endpoint_data: dict[str, Any] = {}

# Endpoint Name, Function to check if car supports the endpoint, endpoint to call to update
all_endpoints = [
Expand Down Expand Up @@ -72,6 +73,18 @@ def __init__(
partial(self._supported, "vehicleStatus", None),
partial(self._api.get_vehicle_status_endpoint, vin=vehicle_info["vin"]),
],
[
"trips",
partial(
self._supported, None, None
), # TODO Unsure of the required capability
partial(
self._api.get_trips_endpoint,
vin=vehicle_info["vin"],
from_date=date.today() - timedelta(days=30),
to_date=date.today(),
),
],
]
self._endpoint_collect: list[tuple[str, partial]] = []
for ep in all_endpoints:
Expand Down

0 comments on commit 74553e2

Please sign in to comment.