Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code tweaks and minor refactoring of the rest #134

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mytoyota/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Toyota Connected Services Client"""
from .client import MyT # pylint: disable=unused-import # NOQA

try:
import importlib.metadata as importlib_metadata
Expand Down
52 changes: 22 additions & 30 deletions mytoyota/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Toyota Connected Services API"""
from typing import Optional
from __future__ import annotations

from typing import Any

from .const import BASE_URL, BASE_URL_CARS
from .controller import Controller
Expand All @@ -10,89 +12,79 @@ class Api:

def __init__(self, controller: Controller) -> None:
"""Toyota Controller"""

self.controller = controller

async def uuid(self):
@property
def uuid(self) -> str | None:
"""Returns uuid from controller"""
return await self.controller.get_uuid()
return self.controller.uuid

async def set_vehicle_alias_endpoint(
self, new_alias: str, vehicle_id: int
) -> Optional[dict]:
) -> dict[str, Any] | None:
"""Set vehicle alias."""

return await self.controller.request(
method="PUT",
base_url=BASE_URL_CARS,
endpoint=f"/api/users/{await self.uuid()}/vehicles/{vehicle_id}",
endpoint=f"/api/users/{self.uuid}/vehicles/{vehicle_id}",
body={"id": vehicle_id, "alias": new_alias},
)

async def get_vehicles_endpoint(self) -> Optional[list]:
async def get_vehicles_endpoint(self) -> list[dict[str, Any] | None] | None:
"""Retrieves list of cars you have registered with MyT"""

arguments = "?services=uio&legacy=true"

return await self.controller.request(
method="GET",
base_url=BASE_URL_CARS,
endpoint=f"/vehicle/user/{await self.uuid()}/vehicles{arguments}",
endpoint=f"/vehicle/user/{self.uuid}/vehicles?services=uio&legacy=true",
)

async def get_connected_services_endpoint(self, vin: str) -> Optional[dict]:
async def get_connected_services_endpoint(self, vin: str) -> dict[str, Any] | None:
"""Get information about connected services for the given car."""

arguments = "?legacy=true&services=fud,connected"

return await self.controller.request(
method="GET",
base_url=BASE_URL_CARS,
endpoint=f"/vehicle/user/{await self.uuid()}/vehicle/{vin}{arguments}",
endpoint=f"/vehicle/user/{self.uuid}/vehicle/{vin}?legacy=true&services=fud,connected",
)

async def get_odometer_endpoint(self, vin: str) -> Optional[list]:
async def get_odometer_endpoint(self, vin: str) -> list[dict[str, Any]] | None:
"""Get information from odometer."""

return await self.controller.request(
method="GET",
base_url=BASE_URL,
endpoint=f"/vehicle/{vin}/addtionalInfo",
)

async def get_parking_endpoint(self, vin: str) -> Optional[dict]:
async def get_parking_endpoint(self, vin: str) -> dict[str, Any] | None:
"""Get where you have parked your car."""

return await self.controller.request(
method="GET",
base_url=BASE_URL,
endpoint=f"/users/{await self.uuid()}/vehicle/location",
endpoint=f"/users/{self.uuid}/vehicle/location",
headers={"VIN": vin},
)

async def get_vehicle_status_endpoint(self, vin: str) -> Optional[dict]:
async def get_vehicle_status_endpoint(self, vin: str) -> dict[str, Any] | None:
"""Get information about the vehicle."""

return await self.controller.request(
method="GET",
base_url=BASE_URL,
endpoint=f"/users/{await self.uuid()}/vehicles/{vin}/vehicleStatus",
endpoint=f"/users/{self.uuid}/vehicles/{vin}/vehicleStatus",
)

async def get_vehicle_status_legacy_endpoint(self, vin: str) -> Optional[dict]:
async def get_vehicle_status_legacy_endpoint(
self, vin: str
) -> dict[str, Any] | None:
"""Get information about the vehicle."""

return await self.controller.request(
method="GET",
base_url=BASE_URL,
endpoint=f"/vehicles/{vin}/remoteControl/status",
)

async def get_driving_statistics_endpoint(
self, vin: str, from_date: str, interval: Optional[str] = None
) -> Optional[dict]:
self, vin: str, from_date: str, interval: str | None = None
) -> dict[str, Any] | None:
"""Get driving statistic"""

return await self.controller.request(
method="GET",
base_url=BASE_URL,
Expand Down
33 changes: 19 additions & 14 deletions mytoyota/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
client = MyT()
vehicles = await client.get_vehicles()
"""
from __future__ import annotations

import asyncio
import json
import logging
from typing import Any

import arrow

Expand Down Expand Up @@ -111,7 +113,8 @@ async def login(self) -> None:
_LOGGER.debug("Performing first login")
await self.api.controller.first_login()

async def get_uuid(self) -> str:
@property
def uuid(self) -> str | None:
"""Get UUID.

Retrieves the UUID returned for the account.
Expand All @@ -121,9 +124,9 @@ async def get_uuid(self) -> str:

9cc70412-27d6-4b81-83fb-542b3a9feb65
"""
return await self.api.uuid()
return self.api.uuid

async def set_alias(self, vehicle_id: int, new_alias: str) -> dict:
async def set_alias(self, vehicle_id: int, new_alias: str) -> dict[str, Any]:
"""Set a new alias for your vehicle.

Sets a new alias for a vehicle specified by its vehicle id.
Expand All @@ -145,12 +148,11 @@ async def set_alias(self, vehicle_id: int, new_alias: str) -> dict:
_LOGGER.debug(
f"Setting new alias: {new_alias} for vehicle with id: {censor(str(vehicle_id))}"
)
result = await self.api.set_vehicle_alias_endpoint(
return await self.api.set_vehicle_alias_endpoint(
vehicle_id=vehicle_id, new_alias=new_alias
)
return result

async def get_vehicles(self) -> list:
async def get_vehicles(self) -> list[dict[str, Any]]:
"""Returns a list of vehicles.

Retrieves list of vehicles associated with the account. The list contains static
Expand Down Expand Up @@ -222,10 +224,9 @@ async def get_vehicles_json(self) -> str:
vehicles = await self.get_vehicles()

_LOGGER.debug("Returning it as json...")
json_string = json.dumps(vehicles, indent=3)
return json_string
return json.dumps(vehicles, indent=3)

async def get_vehicle_status(self, vehicle: dict) -> Vehicle:
async def get_vehicle_status(self, vehicle: dict[str, Any]) -> Vehicle:
"""Returns vehicle status.

Collects and formats different vehicle status endpoints into
Expand All @@ -242,9 +243,9 @@ async def get_vehicle_status(self, vehicle: dict) -> Vehicle:
ToyotaInternalError: An error occurred when making a request.
ToyotaApiError: Toyota's API returned an error.
"""
_LOGGER.debug(f"Getting status for vehicle - {censor_vin(vehicle['vin'])}...")
vin = vehicle.get("vin")
_LOGGER.debug(f"Getting status for vehicle - {censor_vin(vin)}...")

vin = vehicle["vin"]
data = await asyncio.gather(
*[
self.api.get_connected_services_endpoint(vin),
Expand All @@ -265,8 +266,12 @@ async def get_vehicle_status(self, vehicle: dict) -> Vehicle:
)

async def get_driving_statistics( # pylint: disable=too-many-branches
self, vin: str, interval: str = MONTH, from_date: str = None, unit: str = METRIC
) -> list:
self,
vin: str,
interval: str = MONTH,
from_date: str | None = None,
unit: str = METRIC,
) -> list[dict[str, Any]]:
"""Returns driving statistics from a given period.

Retrieves and formats driving statistics from a given periode. Will return
Expand Down Expand Up @@ -428,7 +433,7 @@ async def get_driving_statistics( # pylint: disable=too-many-branches
return statistics.as_list()

async def get_driving_statistics_json(
self, vin: str, interval: str = MONTH, from_date: str = None
self, vin: str, interval: str = MONTH, from_date: str | None = None
) -> str:
"""Returns driving statistics from a given period as json.

Expand Down
4 changes: 0 additions & 4 deletions mytoyota/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
}
}

# LOGIN
USERNAME = "username"
PASSWORD = "password"

# So we don't have to test the token if multiple endpoints is requested at the same time.
TOKEN_DURATION = 900
TOKEN_LENGTH = 114
Expand Down
Loading