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

Add range conversion to miles if car reports in miles #72

Merged
merged 3 commits into from
Oct 4, 2021
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ dmypy.json
.pyre/


/test.py
.myt_app/
tests/
/blahblah.py
19 changes: 5 additions & 14 deletions mytoyota/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
WEEK,
YEAR,
)
from mytoyota.utils import (
convert_to_liter_per_100_miles,
convert_to_miles,
convert_to_mpg,
)

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -65,20 +70,6 @@ def _convert_to_imperial(data: list, use_liters: bool) -> list:

_LOGGER.debug("Converting statistics to imperial...")

def convert_to_miles(kilometers: float) -> float:
"""Convert kilometers to miles"""
return round(kilometers * 0.621371192, 4)

def convert_to_liter_per_100_miles(liters: float) -> float:
"""Convert liters per 100 km to liters per 100 miles"""
_LOGGER.debug("Converting to L/100miles...")
return round(liters * 1.609344, 4)

def convert_to_mpg(liters_per_100_km: float) -> float:
"""Convert to miles per UK gallon (MPG)"""
_LOGGER.debug("Converting to MPG...")
return round(282.5 / liters_per_100_km, 4)

attributes_to_convert = [
"evDistanceInKm",
"totalDistanceInKm",
Expand Down
34 changes: 30 additions & 4 deletions mytoyota/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from mytoyota.const import DOORS, HOOD, KEY, LIGHTS, WINDOWS
from mytoyota.sensors import Doors, Hood, Key, Lights, Windows
from mytoyota.utils import convert_to_miles

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -78,7 +79,14 @@ class Energy:

chargeinfo: dict = None

def __init__(self, data: Union[list, dict] = None, legacy: bool = False):
_is_imperial: bool = False

def __init__(
self, data: Union[list, dict] = None, unit: str = "km", legacy: bool = False
):

if unit == "mi":
self._is_imperial = True

# Support for old endpoint for fuel level. Some cars still uses this.
if legacy:
Expand All @@ -95,8 +103,15 @@ def as_dict(self) -> dict:

def _set(self, data: list) -> None:
"""Set attributes"""

range_in_km = data[0].get("remainingRange", None)

self.level = data[0].get("level", None)
self.range = data[0].get("remainingRange", None)
self.range = (
convert_to_miles(range_in_km)
if range_in_km is not None and self._is_imperial
else range_in_km
)
self.type = data[0].get("type", "Unknown").capitalize()
self.last_updated = data[0].get("timestamp", None)

Expand All @@ -109,8 +124,19 @@ def _set_legacy(self, data: dict) -> None:
def set_battery_attributes(self, data: dict) -> None:
"""Set charge info from legacy endpoint"""

self.range = data.get("EvDistanceInKm", None)
self.range_with_aircon = data.get("EvDistanceWithAirCoInKm", None)
range_in_km = data.get("EvDistanceInKm", None)
range_in_km_with_aircon = data.get("EvDistanceWithAirCoInKm", None)

self.range = (
convert_to_miles(range_in_km)
if range_in_km is not None and self._is_imperial
else range_in_km
)
self.range_with_aircon = (
convert_to_miles(range_in_km_with_aircon)
if range_in_km_with_aircon is not None and self._is_imperial
else range_in_km_with_aircon
)

self.chargeinfo = {
"status": data.get("ChargingStatus", None),
Expand Down
22 changes: 22 additions & 0 deletions mytoyota/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""Toyota Connected Services API."""
import logging

from langcodes import Language

from .const import TOKEN_LENGTH
from .exceptions import ToyotaInvalidToken

_LOGGER: logging.Logger = logging.getLogger(__package__)


def is_valid_locale(locale: str) -> bool:
"""Is locale string valid."""
Expand All @@ -27,3 +31,21 @@ def format_odometer(raw: list) -> dict:
instruments[instrument["type"] + "_unit"] = instrument["unit"]

return instruments


def convert_to_miles(kilometers: float) -> float:
"""Convert kilometers to miles"""
_LOGGER.debug(f"Converting {kilometers} to miles...")
return round(kilometers * 0.621371192, 4)


def convert_to_liter_per_100_miles(liters: float) -> float:
"""Convert liters per 100 km to liters per 100 miles"""
_LOGGER.debug("Converting to L/100miles...")
return round(liters * 1.609344, 4)


def convert_to_mpg(liters_per_100_km: float) -> float:
"""Convert to miles per UK gallon (MPG)"""
_LOGGER.debug("Converting to MPG...")
return round(282.5 / liters_per_100_km, 4)
16 changes: 9 additions & 7 deletions mytoyota/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ def __init__(

remote_control_info = remote_control.get("VehicleInfo", {})

# Extract mileage and if the car reports in km or mi
self.odometer = (
Odometer(format_odometer(odometer)) if odometer else Odometer({})
)

# Extract fuel level/Energy capacity information from status.
if "energy" in status:
_LOGGER.debug("Using energy data: %s", str(status.get("energy")))
self.energy = Energy(status.get("energy"))
self.energy = Energy(status.get("energy"), self.odometer.unit)
# Use legacy odometer to get fuel level. Older cars still uses this.
elif odometer:
_LOGGER.debug("Using legacy odometer data: %s", str(odometer))
self.energy = Energy(format_odometer(odometer), True)
self.energy = Energy(
format_odometer(odometer), self.odometer.unit, True
)
fueltype = self.details.get("fuel", "Unknown")
# PATCH: Toyota Aygo reports wrong type.
if fueltype == "1.0P":
Expand All @@ -95,11 +102,6 @@ def __init__(
remote_control_info.get("ChargeInfo", {})
)

# Extract mileage and if the car reports in km or mi
self.odometer = (
Odometer(format_odometer(odometer)) if odometer else Odometer({})
)

# Extract parking information from status.
self.parking = ParkingLocation(status.get("event", {}))

Expand Down