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

Fix conversion not working correctly + inverting imperial parameter t… #54

Merged
merged 2 commits into from
Sep 15, 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
10 changes: 5 additions & 5 deletions mytoyota/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DATE_FORMAT,
DAY,
IMPERIAL,
IMPERIAL_MPG,
IMPERIAL_LITERS,
INTERVAL_SUPPORTED,
ISOWEEK,
METRIC,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion mytoyota/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

METRIC = "metric"
IMPERIAL = "imperial"
IMPERIAL_MPG = "imperial_mpg"
IMPERIAL_LITERS = "imperial_liters"

# DATE FORMATS
DATE_FORMAT_YEAR = "YYYY"
Expand Down
32 changes: 16 additions & 16 deletions mytoyota/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DAYOFYEAR,
HISTOGRAM,
IMPERIAL,
IMPERIAL_MPG,
IMPERIAL_LITERS,
ISOWEEK,
METRIC,
MONTH,
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -57,23 +57,23 @@ 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.
"""

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",
Expand All @@ -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:
Expand Down