diff --git a/mytoyota/const.py b/mytoyota/const.py index c0a8bf27..5e6c71e6 100644 --- a/mytoyota/const.py +++ b/mytoyota/const.py @@ -1,51 +1,12 @@ """Toyota Connected Services API constants.""" - -BUCKET = "bucket" -DAYOFYEAR = "dayOfYear" -PERIODE_START = "periode_start" -DATE = "date" -DATA = "data" -SUMMARY = "summary" -HISTOGRAM = "histogram" -UNIT = "unit" - -DAY = "day" -WEEK = "week" -ISOWEEK = "isoweek" -MONTH = "month" -YEAR = "year" - -METRIC = "metric" -IMPERIAL = "imperial" -IMPERIAL_LITERS = "imperial_liters" - -# DATE FORMATS -DATE_FORMAT = "YYYY-MM-DD" - -# HTTP -TIMEOUT = 15 -RETURNED_BAD_REQUEST = "bad_request" -TME_B2C_ERR_CPSERVICES = "TME_B2C_ERR_CPSERVICES_GET_FAILURE" -INTERVAL_SUPPORTED = ["day", "week", "isoweek", "month", "year"] -BASE_HEADERS = { - "Content-Type": "application/json;charset=UTF-8", - "Accept": "application/json, text/plain, */*", - "Sec-Fetch-Dest": "empty", - "User-Agent": ( - "Mozilla/5.0 (X11; Linux x86_64) " - "AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/51.0.2704.103 Safari/537.36" - ), -} - # API URLs API_BASE_URL = "HTTPS://ctpa-oneapi.tceu-ctp-prd.toyotaconnectedeurope.io" ACCESS_TOKEN_URL = "HTTPS://b2c-login.toyota-europe.com/oauth2/realms/root/realms/tme/access_token" AUTHENTICATE_URL = "HTTPS://b2c-login.toyota-europe.com/json/realms/root/realms/tme/authenticate?authIndexType=service&authIndexValue=oneapp" AUTHORIZE_URL = "HTTPS://b2c-login.toyota-europe.com/oauth2/realms/root/realms/tme/authorize?client_id=oneapp&scope=openid+profile+write&response_type=code&redirect_uri=com.toyota.oneapp:/oauth2Callback&code_challenge=plain&code_challenge_method=plain" -# Endpoints +# Endpoint URLs CUSTOMER_ACCOUNT_ENDPOINT = "TBD" VEHICLE_ASSOCIATION_ENDPOINT = "/v1/vehicle-association/vehicle" VEHICLE_GUID_ENDPOINT = "/v2/vehicle/guid" @@ -57,6 +18,3 @@ VEHICLE_NOTIFICATION_HISTORY_ENDPOINT = "/v2/notification/history" VEHICLE_TRIPS_ENDPOINT = "/v1/trips?from={from_date}&to={to_date}&route={route}&summary={summary}&limit={limit}&offset={offset}" # noqa: E501 VEHICLE_SERVICE_HISTORY_ENDPONT = "/v1/servicehistory/vehicle/summary" - -# Timestamps -UNLOCK_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ" diff --git a/mytoyota/statistics.py b/mytoyota/statistics.py deleted file mode 100644 index 5ef6b6a8..00000000 --- a/mytoyota/statistics.py +++ /dev/null @@ -1,167 +0,0 @@ -"""Statistics class.""" -import logging - -import arrow -from arrow import Arrow - -import mytoyota.utils.logging.logging_config # noqa # pylint: disable=unused-import -from mytoyota.const import ( - BUCKET, - DATA, - DATE, - DATE_FORMAT, - DAY, - DAYOFYEAR, - HISTOGRAM, - IMPERIAL, - IMPERIAL_LITERS, - ISOWEEK, - METRIC, - MONTH, - PERIODE_START, - SUMMARY, - UNIT, - WEEK, - YEAR, -) -from mytoyota.utils.conversions import ( - convert_to_liter_per_100_miles, - convert_to_miles, - convert_to_mpg, -) - -_LOGGER: logging.Logger = logging.getLogger(__name__) - - -class Statistics: - """Class to hold statistical information.""" - - def __init__( - self, - raw_statistics: dict, - interval: str, - imperial: bool = False, - use_liters: bool = False, - ) -> None: - """Initialise the Class to hold statistical information.""" - self._now: Arrow = arrow.now() - - if not raw_statistics: - _LOGGER.error("No statistical information provided!") - return - - stats_as_list = self._add_bucket(raw_statistics, interval) - - if imperial: - stats_as_list = self._convert_to_imperial(stats_as_list, use_liters) - - self._statistic = stats_as_list - - def as_list(self) -> list: - """Return formatted data.""" - return self._statistic - - @staticmethod - 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. - """ - _LOGGER.debug("Converting statistics to imperial...") - - attributes_to_convert = [ - "evDistanceInKm", - "totalDistanceInKm", - "maxSpeedInKmph", - "averageSpeedInKmph", - "highwayDistanceInKm", - "totalFuelConsumedInL", - ] - - for periode in data: - periode[BUCKET].update( - { - UNIT: IMPERIAL_LITERS if use_liters else IMPERIAL, - }, - ) - for attribute in attributes_to_convert: - if attribute in periode[DATA]: - if attribute == "totalFuelConsumedInL": - _LOGGER.debug(f"Converting attribute {attribute}...") - periode[DATA][attribute] = ( - convert_to_liter_per_100_miles(periode[DATA][attribute]) - if use_liters - else convert_to_mpg(periode[DATA][attribute]) - ) - continue - - _LOGGER.debug(f"Converting attribute {attribute} to miles...") - periode[DATA][attribute] = convert_to_miles(periode[DATA][attribute]) - return data - - def _add_bucket(self, data: dict, interval: str) -> list: - """Add bucket and return statistics in a uniform way.""" - _LOGGER.debug("Updating bucket for statistics....") - - if interval is DAY: - for day in data[HISTOGRAM]: - year = day[BUCKET][YEAR] - dayofyear = day[BUCKET][DAYOFYEAR] - - day[BUCKET].update( - { - UNIT: METRIC, - # As we use 1 january of the year as the initial date - # we only have to shift it with 1 day less - DATE: arrow.Arrow(year, 1, 1) - .shift(days=dayofyear - 1) - .format(DATE_FORMAT), - }, - ) - return data[HISTOGRAM] - - if interval is ISOWEEK: - data_with_bucket: dict = { - BUCKET: { - YEAR: self._now.year, - WEEK: self._now.week, - UNIT: METRIC, - PERIODE_START: data["from"], - }, - DATA: data[SUMMARY], - } - return [data_with_bucket] - - if interval is MONTH: - for month in data[HISTOGRAM]: - month[BUCKET].update( - { - UNIT: METRIC, - PERIODE_START: self._now.replace( - year=month[BUCKET][YEAR], month=month[BUCKET][MONTH], day=1 - ) - .floor(MONTH) - .format(DATE_FORMAT), - }, - ) - return data[HISTOGRAM] - - if interval is YEAR: - data_with_bucket: dict = { - BUCKET: { - YEAR: self._now.year, - UNIT: METRIC, - PERIODE_START: data["from"], - }, - DATA: data[SUMMARY], - } - return [data_with_bucket] - - for periode in data[HISTOGRAM]: - periode[BUCKET].update( - { - UNIT: METRIC, - }, - ) - - return data[HISTOGRAM]