From 331202d36018fa81c4644047dc68b5d01b9c327c Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Thu, 24 Dec 2020 11:00:03 +0100 Subject: [PATCH] Minor changes and increased version number to 0.9 --- README.md | 5 +++-- config.py | 6 +++--- const.py | 4 ++-- custom_components/mercedesmeapi/const.py | 4 ++-- custom_components/mercedesmeapi/oauth.py | 6 +++--- custom_components/mercedesmeapi/query.py | 27 ++++++++++++------------ info.md | 4 +++- query.py | 1 - 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index c8c6267..5fbc838 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,14 @@ This repository contains a collection of scripts (including the Home Assistant C 1) Own a Mercedes Benz Car with Mercedes me installed and working. 2) Create an application in https://developer.mercedes-benz.com/ 3) Register to the following APIs: + - [Electric Vehicle Status](https://developer.mercedes-benz.com/products/electric_vehicle_status) - [Fuel Status](https://developer.mercedes-benz.com/products/fuel_status) + - [Pay As You Drive Insurance](https://developer.mercedes-benz.com/products/pay_as_you_drive_insurance) - [Vehicle Lock Status](https://developer.mercedes-benz.com/products/vehicle_lock_status) - [Vehicle Status](https://developer.mercedes-benz.com/products/vehicle_status) - - [Electric Vehicle Status](https://developer.mercedes-benz.com/products/electric_vehicle_status) Note: the APIs described above do not require any subscription in case you use them with your own car associated with the Mercedes me Account. -Note2: not all sensors may be available in your own car. +Note2: not all sensors may be available in your own car; if a sensor is not available the data request returns no data. Note3: only one car is supported for the moment. ## Home Assistant Custom Component diff --git a/config.py b/config.py index a826d36..87e161d 100644 --- a/config.py +++ b/config.py @@ -48,17 +48,17 @@ class MercedesMeConfig: # Client ID self.client_id = f.get(CONF_CLIENT_ID) if not self.client_id: - _LOGGER.error("No {CONF_CLIENT_ID} found in the configuration") + _LOGGER.error(f"No {CONF_CLIENT_ID} found in the configuration") return False # Client Secret self.client_secret = f.get(CONF_CLIENT_SECRET) if not self.client_secret: - _LOGGER.error("No {CONF_CLIENT_SECRET} found in the configuration") + _LOGGER.error(f"No {CONF_CLIENT_SECRET} found in the configuration") return False # Vehicle ID self.vin = f.get(CONF_VEHICLE_ID) if not self.vin: - _LOGGER.error("No {CONF_VEHICLE_ID} found in the configuration") + _LOGGER.error(f"No {CONF_VEHICLE_ID} found in the configuration") return False # Read Token self.token = MercedesMeOauth(self.client_id, self.client_secret) diff --git a/const.py b/const.py index e03ee93..da2a1f1 100644 --- a/const.py +++ b/const.py @@ -9,7 +9,7 @@ https://github.com/xraver/mercedes_me_api/ # Software Name & Version NAME = "Mercedes Me API" DOMAIN = "mercedesmeapi" -VERSION = "0.8" +VERSION = "0.9" # Software Parameters TOKEN_FILE = ".mercedesme_token" CREDENTIALS_FILE = ".mercedesme_credentials" @@ -19,7 +19,7 @@ REDIRECT_URL = "https://localhost" SCOPE = "mb:vehicle:mbdata:fuelstatus%20mb:vehicle:mbdata:vehiclestatus%20mb:vehicle:mbdata:vehiclelock%20mb:vehicle:mbdata:evstatus%20mb:vehicle:mbdata:payasyoudrive%20offline_access" URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" -# File Parameters +# Configuration File Parameters CONF_CLIENT_ID = "CLIENT_ID" CONF_CLIENT_SECRET = "CLIENT_SECRET" CONF_VEHICLE_ID = "VEHICLE_ID" diff --git a/custom_components/mercedesmeapi/const.py b/custom_components/mercedesmeapi/const.py index c937c2d..0f028f3 100644 --- a/custom_components/mercedesmeapi/const.py +++ b/custom_components/mercedesmeapi/const.py @@ -9,7 +9,7 @@ https://github.com/xraver/mercedes_me_api/ # Software Name & Version NAME = "Mercedes Me API" DOMAIN = "mercedesmeapi" -VERSION = "0.8" +VERSION = "0.9" # Software Parameters TOKEN_FILE = ".mercedesme_token" CREDENTIALS_FILE = ".mercedesme_credentials" @@ -20,7 +20,7 @@ SCOPE = "mb:vehicle:mbdata:fuelstatus%20mb:vehicle:mbdata:vehiclestatus%20mb:veh URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" #UPDATE_SIGNAL = "mercedesmeapi_update" -# File Parameters +# Configuration File Parameters CONF_CLIENT_ID = "client_id" CONF_CLIENT_SECRET = "client_secret" CONF_VEHICLE_ID = "vehicle_id" diff --git a/custom_components/mercedesmeapi/oauth.py b/custom_components/mercedesmeapi/oauth.py index eafee6d..94c210b 100644 --- a/custom_components/mercedesmeapi/oauth.py +++ b/custom_components/mercedesmeapi/oauth.py @@ -42,12 +42,12 @@ class MercedesMeOauth: self.token_file = hass.config.path(TOKEN_FILE) # Base64 b64_str = f"{client_id}:{client_secret}" - b64_bytes = base64.b64encode( b64_str.encode('ascii') ) - self.base64 = b64_bytes.decode('ascii') + b64_bytes = base64.b64encode(b64_str.encode("ascii")) + self.base64 = b64_bytes.decode("ascii") # Headers self.headers = { "Authorization": f"Basic {self.base64}", - "content-type": "application/x-www-form-urlencoded" + "content-type": "application/x-www-form-urlencoded", } ######################## diff --git a/custom_components/mercedesmeapi/query.py b/custom_components/mercedesmeapi/query.py index 37e886c..a4be239 100644 --- a/custom_components/mercedesmeapi/query.py +++ b/custom_components/mercedesmeapi/query.py @@ -21,8 +21,8 @@ def GetResource(resourceURL, config): # Set Header headers = { - "accept": "application/json;charset=utf-8", - "authorization": f"Bearer {config.token.access_token}" + "accept": "application/json;charset=utf-8", + "authorization": f"Bearer {config.token.access_token}", } # Send Request @@ -30,13 +30,11 @@ def GetResource(resourceURL, config): try: data = res.json() except ValueError: - data = { "reason": "No Data", - "code" : res.status_code - } + data = {"reason": "No Data", "code": res.status_code} # Check Error if not res.ok: - if ("reason" in data): + if "reason" in data: reason = data["reason"] else: if res.status_code == 400: @@ -50,7 +48,9 @@ def GetResource(resourceURL, config): elif res.status_code == 404: reason = "Page not found" elif res.status_code == 429: - reason = "The service received too many requests in a given amount of time" + reason = ( + "The service received too many requests in a given amount of time" + ) elif res.status_code == 500: reason = "An error occurred on the server side" elif res.status_code == 503: @@ -61,25 +61,24 @@ def GetResource(resourceURL, config): data["code"] = res.status_code return data + ######################## # GetToken ######################## def GetToken(tokenURL, headers, data, refresh=True): - res = requests.post(tokenURL, data = data, headers = headers) + res = requests.post(tokenURL, data=data, headers=headers) try: data = res.json() except ValueError: - _LOGGER.error (f"Error retrieving token {res.status_code}") - data = { "reason": "No Data", - "code" : res.status_code - } + _LOGGER.error(f"Error retrieving token {res.status_code}") + data = {"reason": "No Data", "code": res.status_code} # Check Error if not res.ok: - if ("reason" in data): + if "reason" in data: reason = data["reason"] else: - if (refresh == False): + if refresh == False: # New Token Errors if res.status_code == 302: reason = "The request scope is invalid" diff --git a/info.md b/info.md index e6786cd..2080f8e 100644 --- a/info.md +++ b/info.md @@ -12,12 +12,14 @@ This component is still in development. 1) Own a Mercedes Benz Car with Mercedes me installed and working. 2) Create an application in https://developer.mercedes-benz.com/ 3) Register to the following APIs: + - [Electric Vehicle Status](https://developer.mercedes-benz.com/products/electric_vehicle_status) - [Fuel Status](https://developer.mercedes-benz.com/products/fuel_status) + - [Pay As You Drive Insurance](https://developer.mercedes-benz.com/products/pay_as_you_drive_insurance) - [Vehicle Lock Status](https://developer.mercedes-benz.com/products/vehicle_lock_status) - [Vehicle Status](https://developer.mercedes-benz.com/products/vehicle_status) Note: the APIs described above do not require any subscription in case you use them with your own car associated with the Mercedes me Account. -Note2: not all sensors may be available in your own car. +Note2: not all sensors may be available in your own car; if a sensor is not available the data request returns no data. Note3: only one car is supported for the moment. ## Open Points diff --git a/query.py b/query.py index 0c9569a..1a5190c 100644 --- a/query.py +++ b/query.py @@ -7,7 +7,6 @@ For more details about this component, please refer to the documentation at https://github.com/xraver/mercedes_me_api/ """ import logging - import requests from const import * -- 2.47.3