Skip to content

Commit

Permalink
Add everything to library exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Prior99 committed Sep 19, 2024
1 parent dbd7e9f commit 30cd07b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 67 deletions.
21 changes: 18 additions & 3 deletions myskoda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,5 +36,8 @@
"position",
"status",
"RestApi",
"Vehicle",
"MySkoda",
"operation_request",
"service_event",
"Mqtt",
]
22 changes: 11 additions & 11 deletions myskoda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")


Expand All @@ -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")
Expand All @@ -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)}, "
Expand All @@ -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)}")
Expand Down Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -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")} "
Expand Down
54 changes: 54 additions & 0 deletions myskoda/myskoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
53 changes: 0 additions & 53 deletions myskoda/rest_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains API representation for the MySkoda REST API."""

import logging
from asyncio import gather

from aiohttp import ClientSession

Expand All @@ -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."""

Expand Down Expand Up @@ -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)}"}

Expand Down

0 comments on commit 30cd07b

Please sign in to comment.