diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f7164959..443cc79c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index c8ed1db2..64ca19fb 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -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 diff --git a/mytoyota/api.py b/mytoyota/api.py index 799b02a3..f7f908ba 100644 --- a/mytoyota/api.py +++ b/mytoyota/api.py @@ -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} diff --git a/mytoyota/client.py b/mytoyota/client.py index a7d9e1fd..5d0f8b9b 100644 --- a/mytoyota/client.py +++ b/mytoyota/client.py @@ -2,7 +2,6 @@ import asyncio import json import logging -from typing import Optional import pendulum @@ -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""" @@ -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"] @@ -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 diff --git a/mytoyota/vehicle.py b/mytoyota/vehicle.py index 0f70f8dc..622c2c1e 100644 --- a/mytoyota/vehicle.py +++ b/mytoyota/vehicle.py @@ -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"]: @@ -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"] diff --git a/pyproject.toml b/pyproject.toml index 8aa0227e..c5955eb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ description = "Python client for Toyota Connected Services." authors = ["Simon Grud Hansen "] license = "MIT" readme = "README.md" -version = "0.2.3" +version = "0.3.0" classifiers = [ "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License",