From 30cd07b6665a72961f0cd62f13a64f829b4497e0 Mon Sep 17 00:00:00 2001 From: Frederick Gnodtke Date: Thu, 19 Sep 2024 11:11:46 +0200 Subject: [PATCH] Add everything to library exports --- myskoda/__init__.py | 21 +++++++++++++++--- myskoda/cli.py | 22 +++++++++--------- myskoda/myskoda.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ myskoda/rest_api.py | 53 -------------------------------------------- 4 files changed, 83 insertions(+), 67 deletions(-) diff --git a/myskoda/__init__.py b/myskoda/__init__.py index dc1e2477..5ec700aa 100644 --- a/myskoda/__init__.py +++ b/myskoda/__init__.py @@ -7,8 +7,20 @@ IDKSession, idk_authorize, ) -from .models import air_conditioning, charging, common, health, info, position, status -from .rest_api import RestApi, Vehicle +from .models import ( + air_conditioning, + charging, + common, + health, + info, + operation_request, + position, + service_event, + status, +) +from .mqtt import Mqtt +from .myskoda import MySkoda +from .rest_api import RestApi __all__ = [ "AuthorizationError", @@ -24,5 +36,8 @@ "position", "status", "RestApi", - "Vehicle", + "MySkoda", + "operation_request", + "service_event", + "Mqtt", ] diff --git a/myskoda/cli.py b/myskoda/cli.py index c6854d41..eb0a4701 100644 --- a/myskoda/cli.py +++ b/myskoda/cli.py @@ -79,7 +79,7 @@ async def list_vehicles(ctx: Context) -> None: myskoda: MySkoda = ctx.obj["myskoda"] print(f"{colored("vehicles:", "blue")}") - for vehicle in await myskoda.rest_api.list_vehicles(): + for vehicle in await myskoda.list_vehicles(): print(f"- {vehicle}") @@ -89,7 +89,7 @@ async def list_vehicles(ctx: Context) -> None: async def info(ctx: Context, vin: str) -> None: """Print info for the specified vin.""" myskoda: MySkoda = ctx.obj["myskoda"] - info = await myskoda.rest_api.get_info(vin) + info = await myskoda.get_info(vin) if info.specification.battery is not None: print(f"{colored("battery capacity:", "blue")} {info.specification.battery.capacity}kwh") @@ -109,7 +109,7 @@ async def info(ctx: Context, vin: str) -> None: async def status(ctx: Context, vin: str) -> None: """Print current status for the specified vin.""" myskoda: MySkoda = ctx.obj["myskoda"] - status = await myskoda.rest_api.get_status(vin) + status = await myskoda.get_status(vin) print( f"{colored("doors:", "blue")} {c_open(status.overall.doors)}, " @@ -128,7 +128,7 @@ async def status(ctx: Context, vin: str) -> None: async def air_conditioning(ctx: Context, vin: str) -> None: """Print current status about air conditioning.""" myskoda: MySkoda = ctx.obj["myskoda"] - ac = await myskoda.rest_api.get_air_conditioning(vin) + ac = await myskoda.get_air_conditioning(vin) print(f"{colored("window heating:", "blue")} {bool_state(ac.window_heating_enabled)}") print(f"{colored("window heating (front):", "blue")} {on(ac.window_heating_state.front)}") @@ -157,7 +157,7 @@ async def air_conditioning(ctx: Context, vin: str) -> None: async def positions(ctx: Context, vin: str) -> None: """Print the vehicle's current position.""" myskoda: MySkoda = ctx.obj["myskoda"] - positions = await myskoda.rest_api.get_positions(vin) + positions = await myskoda.get_positions(vin) for position in positions.positions: print(f"- {colored("type:", "blue")} {position.type}") @@ -181,7 +181,7 @@ async def positions(ctx: Context, vin: str) -> None: async def health(ctx: Context, vin: str) -> None: """Print the vehicle's mileage.""" myskoda: MySkoda = ctx.obj["myskoda"] - health = await myskoda.rest_api.get_health(vin) + health = await myskoda.get_health(vin) print(f"{colored("mileage:", "blue")} {health.mileage_in_km}km") print(f"{colored("last updated:", "blue")} {health.captured_at}") @@ -193,7 +193,7 @@ async def health(ctx: Context, vin: str) -> None: async def charging(ctx: Context, vin: str) -> None: """Print the vehicle's current charging state.""" myskoda: MySkoda = ctx.obj["myskoda"] - charging = await myskoda.rest_api.get_charging(vin) + charging = await myskoda.get_charging(vin) if charging.status is not None: print( @@ -232,7 +232,7 @@ async def charging(ctx: Context, vin: str) -> None: async def maintenance(ctx: Context, vin: str) -> None: """Print the vehicle's maintenance information.""" myskoda: MySkoda = ctx.obj["myskoda"] - maintenance = await myskoda.rest_api.get_maintenance(vin) + maintenance = await myskoda.get_maintenance(vin) print(f"{colored("mileage:", "blue")} {maintenance.maintenance_report.mileage_in_km}km") print( @@ -261,7 +261,7 @@ async def maintenance(ctx: Context, vin: str) -> None: async def driving_range(ctx: Context, vin: str) -> None: """Print the vehicle's estimated driving range information.""" myskoda: MySkoda = ctx.obj["myskoda"] - driving_range = await myskoda.rest_api.get_driving_range(vin) + driving_range = await myskoda.get_driving_range(vin) print(f"{colored("range:", "blue")} {driving_range.total_range_in_km}km") print(f"{colored("car type:", "blue")} {driving_range.car_type}") @@ -277,7 +277,7 @@ async def driving_range(ctx: Context, vin: str) -> None: async def user(ctx: Context) -> None: """Print information about currently logged in user.""" myskoda: MySkoda = ctx.obj["myskoda"] - user = await myskoda.rest_api.get_user() + user = await myskoda.get_user() print(f"{colored("id:", "blue")} {user.id}") print(f"{colored("name:", "blue")} {user.first_name} {user.last_name}") @@ -290,7 +290,7 @@ async def user(ctx: Context) -> None: async def trip_statistics(ctx: Context, vin: str) -> None: """Print the last trip statics.""" myskoda: MySkoda = ctx.obj["myskoda"] - stats = await myskoda.rest_api.get_trip_statistics(vin) + stats = await myskoda.get_trip_statistics(vin) print( f"{colored("overall fuel consumption:", "blue")} " diff --git a/myskoda/myskoda.py b/myskoda/myskoda.py index 78b12cc5..257d3d40 100644 --- a/myskoda/myskoda.py +++ b/myskoda/myskoda.py @@ -9,7 +9,17 @@ from aiohttp import ClientSession from .event import Event +from .models.air_conditioning import AirConditioning +from .models.charging import Charging +from .models.driving_range import DrivingRange +from .models.health import Health +from .models.info import Info +from .models.maintenance import Maintenance from .models.operation_request import OperationName +from .models.position import Positions +from .models.status import Status +from .models.trip_statistics import TripStatistics +from .models.user import User from .mqtt import Mqtt from .rest_api import RestApi @@ -115,3 +125,47 @@ async def stop_air_conditioning(self, vin: str) -> None: def get_auth_token(self) -> str: """Retrieve the main access token for the IDK session.""" return self.rest_api.idk_session.access_token + + async def get_info(self, vin: str) -> Info: + """Retrieve the basic vehicle information for the specified vehicle.""" + return await self.rest_api.get_info(vin) + + async def get_charging(self, vin: str) -> Charging: + """Retrieve information related to charging for the specified vehicle.""" + return await self.rest_api.get_charging(vin) + + async def get_status(self, vin: str) -> Status: + """Retrieve the current status for the specified vehicle.""" + return await self.rest_api.get_status(vin) + + async def get_air_conditioning(self, vin: str) -> AirConditioning: + """Retrieve the current air conditioning status for the specified vehicle.""" + return await self.rest_api.get_air_conditioning(vin) + + async def get_positions(self, vin: str) -> Positions: + """Retrieve the current position for the specified vehicle.""" + return await self.rest_api.get_positions(vin) + + async def get_driving_range(self, vin: str) -> DrivingRange: + """Retrieve estimated driving range for combustion vehicles.""" + return await self.rest_api.get_driving_range(vin) + + async def get_trip_statistics(self, vin: str) -> TripStatistics: + """Retrieve statistics about past trips.""" + return await self.rest_api.get_trip_statistics(vin) + + async def get_maintenance(self, vin: str) -> Maintenance: + """Retrieve maintenance report.""" + return await self.rest_api.get_maintenance(vin) + + async def get_health(self, vin: str) -> Health: + """Retrieve health information for the specified vehicle.""" + return await self.rest_api.get_health(vin) + + async def get_user(self) -> User: + """Retrieve user information about logged in user.""" + return await self.rest_api.get_user() + + async def list_vehicles(self) -> list[str]: + """List all vehicles by their vins.""" + return await self.rest_api.list_vehicles() diff --git a/myskoda/rest_api.py b/myskoda/rest_api.py index 9e092150..32a12e7b 100644 --- a/myskoda/rest_api.py +++ b/myskoda/rest_api.py @@ -1,7 +1,6 @@ """Contains API representation for the MySkoda REST API.""" import logging -from asyncio import gather from aiohttp import ClientSession @@ -21,33 +20,6 @@ _LOGGER = logging.getLogger(__name__) -class Vehicle: - """Wrapper class for all information from all endpoints.""" - - info: Info - charging: Charging - status: Status - air_conditioning: AirConditioning - position: Positions - health: Health - - def __init__( # noqa: D107, PLR0913 - self, - info: Info, - charging: Charging, - status: Status, - air_conditioning: AirConditioning, - position: Positions, - health: Health, - ) -> None: - self.info = info - self.charging = charging - self.status = status - self.air_conditioning = air_conditioning - self.position = position - self.health = health - - class RestApi: """API hub class that can perform all calls to the MySkoda API.""" @@ -177,31 +149,6 @@ async def list_vehicles(self) -> list[str]: json = await response.json() return [vehicle["vin"] for vehicle in json["vehicles"]] - async def get_vehicle(self, vin: str) -> Vehicle: - """Retrieve all information about a given vehicle by calling all endpoints.""" - [info, charging, status, air_conditioning, position, health] = await gather( - *[ - self.get_info(vin), - self.get_charging(vin), - self.get_status(vin), - self.get_air_conditioning(vin), - self.get_positions(vin), - self.get_health(vin), - ] - ) - return Vehicle( - info=info, - charging=charging, - status=status, - air_conditioning=air_conditioning, - position=position, - health=health, - ) - - async def get_all_vehicles(self) -> list[Vehicle]: - """Call all endpoints for all vehicles in the user's garage.""" - return await gather(*[self.get_vehicle(vehicle) for vehicle in await self.list_vehicles()]) - async def _headers(self) -> dict[str, str]: return {"authorization": f"Bearer {await self.idk_session.get_access_token(self.session)}"}