Skip to content

Commit

Permalink
Minor code rewrite (#30)
Browse files Browse the repository at this point in the history
* Minor code rewrite + solving driving statistics

* Change version number to 0.3.0
  • Loading branch information
DurgNomis-drol authored Aug 1, 2021
1 parent 31cd30f commit df14608
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: [3.8]
python-version: [3.8, 3.9]
fail-fast: false

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Pre-commit
strategy:
matrix:
python-version: [3.8]
python-version: [3.8, 3.9]
runs-on: ubuntu-latest
steps:
- name: Check out the repository
Expand Down
4 changes: 2 additions & 2 deletions mytoyota/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ async def get_vehicle_status_endpoint(self, vin: str) -> dict:
)

async def get_driving_statistics_endpoint(
self, vin: str, from_date, interval
) -> list:
self, vin: str, from_date: str, interval: str = None
) -> dict:
"""Get driving statistic"""

params = {"from": from_date, "calendarInterval": interval}
Expand Down
122 changes: 39 additions & 83 deletions mytoyota/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import asyncio
import json
import logging
from typing import Optional

import pendulum

Expand Down Expand Up @@ -81,9 +80,9 @@ async def set_alias(self, vehicle_id: int, new_alias: str) -> dict:
async def get_vehicles(self) -> list:
"""Return list of vehicles with basic information about them"""

cars = await self.api.get_vehicles_endpoint()
if cars:
return cars
vehicles = await self.api.get_vehicles_endpoint()
if vehicles:
return vehicles

async def get_vehicles_json(self) -> str:
"""Return vehicle list as json"""
Expand All @@ -92,7 +91,7 @@ async def get_vehicles_json(self) -> str:
json_string = json.dumps(vehicles, indent=3)
return json_string

async def get_vehicle_information(self, vehicle: dict) -> dict:
async def get_vehicle_status(self, vehicle: dict) -> dict:
"""Return information for given vehicle"""

vin = vehicle["vin"]
Expand All @@ -113,99 +112,56 @@ async def get_vehicle_information(self, vehicle: dict) -> dict:

return car.as_dict()

async def get_vehicle_information_json(self, vehicle: dict) -> str:
async def get_vehicle_status_json(self, vehicle: dict) -> str:
"""Return vehicle information as json"""
vehicle = await self.get_vehicle_information(vehicle)
vehicle = await self.get_vehicle_status(vehicle)

json_string = json.dumps(vehicle, indent=3)
return json_string

async def gather_all_information(self) -> list:
"""Gather all information, format it and return it as list"""
vehicles = []
cars = await self.api.get_vehicles_endpoint()
if cars:
for car in cars:

vehicle = await self.get_vehicle_information(car)

vehicles.append(vehicle)
async def get_driving_statistics(
self, vin: str, interval: str = "month", from_date=None
) -> dict:
"""
params: vin: Vin number of your car.
interval: can be "day", "week" or "month". Default "month"
from_date: from which date you want statistics. Default is current day,
week or month if None.
"""

return vehicles
if interval not in ("day", "week", "month"):
return {"Error_mesg": "Invalid interval provided!"}

async def gather_all_information_json(self) -> str:
"""Gather all information, format it and return a json string"""
vehicles = await self.gather_all_information()
def calculate_from_date() -> str:
if interval == "day":
date = pendulum.now().subtract(days=1).format("YYYY-MM-DD")
return date

json_string = json.dumps(vehicles, indent=3)
return json_string
date = pendulum.now().start_of(interval).format("YYYY-MM-DD")

async def get_driving_statistics_from_date(
self, vin, from_date=None
) -> Optional[list]:
"""Get driving statistics from date.
from_date should be in this format (YYYY-MM-DD).
Default is current day"""
if date == pendulum.now().format("YYYY-MM-DD"):
date = (
pendulum.now()
.subtract(days=1)
.start_of(interval)
.format("YYYY-MM-DD")
)
return date
return date

if from_date is None:
from_date = pendulum.now().subtract(days=1).format("YYYY-MM-DD")
from_date = calculate_from_date()

statistics = await self.api.get_driving_statistics_endpoint(
vin, from_date, "day"
vin, from_date, interval
)
return statistics

async def get_driving_statistics_from_date_json(self, vin, from_date=None) -> str:
"""Return driving statistics from date in json"""
statistics = await self.get_driving_statistics_from_date(vin, from_date)

json_string = json.dumps(statistics, indent=3)
return json_string

async def get_driving_statistics_from_week(self, vin) -> Optional[list]:
"""Get driving statistics from week. Default is current week.
NOTICE: Week numbers are not ISO week numbers but Japan week numbers!
Example: 2021-01-31 is on week 6 instead of ISO week 4!
"""
from_date = pendulum.now().start_of("week").format("YYYY-MM-DD")

if from_date == pendulum.now().format("YYYY-MM-DD"):
from_date = (
pendulum.now().subtract(days=1).start_of("week").format("YYYY-MM-DD")
)

statistics = await self.api.get_driving_statistics_endpoint(
vin, from_date, "week"
)
return statistics

async def get_driving_statistics_from_week_json(self, vin) -> str:
"""Return driving statistics from date in json"""
statistics = await self.get_driving_statistics_from_week(vin)

json_string = json.dumps(statistics, indent=3)
return json_string

async def get_driving_statistics_from_month(self, vin) -> Optional[list]:
"""Get driving statistics from month. Default is current month."""

from_date = pendulum.now().start_of("month").format("YYYY-MM-DD")

if from_date == pendulum.now().format("YYYY-MM-DD"):
from_date = (
pendulum.now().subtract(days=1).start_of("month").format("YYYY-MM-DD")
)

statistics = await self.api.get_driving_statistics_endpoint(
vin, from_date, "month"
async def get_driving_statistics_json(
self, vin: str, interval: str = "month", from_date=None
) -> str:
"""Return driving statistics in json"""
return json.dumps(
await self.get_driving_statistics(vin, interval, from_date), indent=3
)
return statistics

async def get_driving_statistics_from_month_json(self, vin) -> str:
"""Return driving statistics from date in json"""
statistics = await self.get_driving_statistics_from_month(vin)

json_string = json.dumps(statistics, indent=3)
return json_string
10 changes: 7 additions & 3 deletions mytoyota/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ def __init__( # pylint: disable=too-many-arguments
# Format vehicle information.
self.details = self.format_details(vehicle_info)

self.services["connectedServices"] = self.has_connected_services_enabled(
connected_services
)
if connected_services is not None:
self.services["connectedServices"] = self.has_connected_services_enabled(
connected_services
)
else:
self.services["connectedServices"] = False

# Checks if connected services has been enabled.
if self.services["connectedServices"]:
Expand Down Expand Up @@ -85,6 +88,7 @@ def extract_status(self, status) -> None:

def has_connected_services_enabled(self, json_dict) -> bool:
"""Checks if the user has enabled connected services."""

if (
"connectedService" in json_dict
and "status" in json_dict["connectedService"]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Python client for Toyota Connected Services."
authors = ["Simon Grud Hansen <[email protected]>"]
license = "MIT"
readme = "README.md"
version = "0.2.3"
version = "0.3.0"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
Expand Down

0 comments on commit df14608

Please sign in to comment.