diff --git a/mytoyota/client.py b/mytoyota/client.py index 462738af..b8fc1419 100644 --- a/mytoyota/client.py +++ b/mytoyota/client.py @@ -10,7 +10,7 @@ DATE_FORMAT, DAY, IMPERIAL, - IMPERIAL_MPG, + IMPERIAL_LITERS, INTERVAL_SUPPORTED, ISOWEEK, METRIC, @@ -233,19 +233,19 @@ async def get_driving_statistics( # pylint: disable=too-many-branches # Format data so we get a uniform output. imperial = False - use_mpg = False + use_liters = False if unit is IMPERIAL: imperial = True - if unit is IMPERIAL_MPG: + if unit is IMPERIAL_LITERS: imperial = True - use_mpg = True + use_liters = True statistics = Statistics( raw_statistics=raw_statistics, interval=interval, imperial=imperial, - use_mpg=use_mpg, + use_liters=use_liters, ) return statistics.as_list() diff --git a/mytoyota/const.py b/mytoyota/const.py index 5858303a..01f6386a 100644 --- a/mytoyota/const.py +++ b/mytoyota/const.py @@ -66,7 +66,7 @@ METRIC = "metric" IMPERIAL = "imperial" -IMPERIAL_MPG = "imperial_mpg" +IMPERIAL_LITERS = "imperial_liters" # DATE FORMATS DATE_FORMAT_YEAR = "YYYY" diff --git a/mytoyota/statistics.py b/mytoyota/statistics.py index 60da3e34..0dc73557 100644 --- a/mytoyota/statistics.py +++ b/mytoyota/statistics.py @@ -14,7 +14,7 @@ DAYOFYEAR, HISTOGRAM, IMPERIAL, - IMPERIAL_MPG, + IMPERIAL_LITERS, ISOWEEK, METRIC, MONTH, @@ -36,7 +36,7 @@ def __init__( raw_statistics: dict, interval: str, imperial: bool = False, - use_mpg: bool = False, + use_liters: bool = False, ) -> None: self._now: Arrow = arrow.now() @@ -48,7 +48,7 @@ def __init__( stats_as_list = self._add_bucket(raw_statistics, interval) if imperial: - stats_as_list = self._convert_to_imperial(stats_as_list, use_mpg) + stats_as_list = self._convert_to_imperial(stats_as_list, use_liters) self._statistic = stats_as_list @@ -57,7 +57,7 @@ def as_list(self) -> list: return self._statistic @staticmethod - def _convert_to_imperial(data: list, use_mpg: bool) -> list: + def _convert_to_imperial(data: list, use_liters: bool) -> list: """ Toyota converts some of the data, but not all for some reason. This function corrects these values and adds the possibility to show them in MPG also. @@ -65,15 +65,15 @@ def _convert_to_imperial(data: list, use_mpg: bool) -> list: def convert_to_miles(kilometers: float) -> float: """Convert kilometers to miles""" - return kilometers * 0.621371192 + 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""" - return liters * 1.609344 + return round(liters * 1.609344, 4) def convert_to_mpg(liters_per_100_km: float) -> float: """Convert to miles per UK gallon (MPG)""" - return 282.5 / liters_per_100_km + return round(282.5 / liters_per_100_km, 4) attributes_to_convert = [ "evDistanceInKm", @@ -87,22 +87,22 @@ def convert_to_mpg(liters_per_100_km: float) -> float: for periode in data: periode[BUCKET].update( { - UNIT: IMPERIAL_MPG if use_mpg else IMPERIAL, + UNIT: IMPERIAL_LITERS if use_liters else IMPERIAL, } ) for attribute in attributes_to_convert: if attribute in periode[DATA]: + if attribute == "totalFuelConsumedInL": + periode[DATA][attribute] = ( + convert_to_liter_per_100_miles(periode[DATA][attribute]) + if use_liters + else convert_to_mpg(periode[DATA][attribute]) + ) + continue + periode[DATA][attribute] = convert_to_miles( periode[DATA][attribute] ) - - if attribute == "totalFuelConsumedInL" and attribute in periode[DATA]: - periode[DATA][attribute] = ( - convert_to_mpg(periode[DATA][attribute]) - if use_mpg - else convert_to_liter_per_100_miles(periode[DATA][attribute]) - ) - return data def _add_bucket(self, data: dict, interval: str) -> list: