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

Expose electric status #379

Merged
merged 3 commits into from
Jan 28, 2025
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
120 changes: 120 additions & 0 deletions mytoyota/models/electric_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""models for vehicle electric status."""
from datetime import date
from typing import Optional

from mytoyota.models.endpoints.electric import ElectricStatusModel
from mytoyota.utils.conversions import convert_distance


class ElectricStatus:
"""ElectricStatus."""

def __init__(
self,
electric_status: ElectricStatusModel = None,
metric: bool = True,
):
"""Initialise ElectricStatus."""
self._electric_status: Optional[ElectricStatusModel] = (
electric_status.payload if electric_status else None
)
self._distance_unit: str = "km" if metric else "mi"

def __repr__(self):
"""Representation of the model."""
return " ".join(
[
f"{k}={getattr(self, k)!s}"
for k, v in type(self).__dict__.items()
if isinstance(v, property)
],
)

@property
def battery_level(self) -> Optional[float]:
"""Battery level of the vehicle.

Returns
-------
float: Battery level of the vehicle in percentage.

"""
return self._electric_status.battery_level if self._electric_status else None

@property
def charging_status(self) -> Optional[str]:
"""Charging status of the vehicle.

Returns
-------
str: Charging status of the vehicle.

"""
return self._electric_status.charging_status

@property
def remaining_charge_time(self) -> Optional[int]:
"""Remaining time to full charge in minutes.

Returns
-------
int: Remaining time to full charge in minutes.

"""
return self._electric_status.remaining_charge_time

@property
def ev_range(self) -> Optional[float]:
"""Electric vehicle range.

Returns
-------
float: Electric vehicle range in the current selected units.

"""
if self._electric_status:
return convert_distance(
self._distance_unit,
self._electric_status.ev_range.unit,
self._electric_status.ev_range.value,
)
return None

@property
def ev_range_with_ac(self) -> Optional[float]:
"""Electric vehicle range with AC.

Returns
-------
float: Electric vehicle range with AC in the current selected units.

"""
if self._electric_status:
return convert_distance(
self._distance_unit,
self._electric_status.ev_range_with_ac.unit,
self._electric_status.ev_range_with_ac.value,
)
return None

@property
def can_set_next_charging_event(self) -> Optional[bool]:
"""Can set next charging event.

Returns
-------
bool: Can set next charging event.

"""
return self._electric_status.can_set_next_charging_event if self._electric_status else None

@property
def last_update_timestamp(self) -> Optional[date]:
"""Last update timestamp.

Returns
-------
date: Last update timestamp.

"""
return self._electric_status.last_update_timestamp if self._electric_status else None
16 changes: 16 additions & 0 deletions mytoyota/models/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from mytoyota.api import Api
from mytoyota.models.dashboard import Dashboard
from mytoyota.models.electric_status import ElectricStatus
from mytoyota.models.endpoints.vehicle_guid import VehicleGuidModel
from mytoyota.models.location import Location
from mytoyota.models.lock_status import LockStatus
Expand Down Expand Up @@ -170,6 +171,21 @@ def dashboard(self) -> Optional[Dashboard]:
self._metric,
)

@property
def electric_status(self) -> Optional[ElectricStatus]:
"""Returns the Electric Status of the vehicle.

Returns
-------
Electric Status

"""
return (
ElectricStatus(self._endpoint_data["electric_status"])
if "electric_status" in self._endpoint_data
else None
)

@property
def location(self) -> Optional[Location]:
"""Return the vehicles latest reported Location.
Expand Down
2 changes: 2 additions & 0 deletions simple_client_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ async def get_information():

# Dashboard Information
pp.pprint(f"Dashboard: {car.dashboard}")
# Electric Status Information
pp.pprint(f"Electric Status: {car.electric_status}")
# Location Information
pp.pprint(f"Location: {car.location}")
# Lock Status
Expand Down