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
# 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)
# 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"
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"
# 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"
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"
self.token_file = hass.config.path(TOKEN_FILE)\r
# Base64\r
b64_str = f"{client_id}:{client_secret}"\r
- b64_bytes = base64.b64encode( b64_str.encode('ascii') )\r
- self.base64 = b64_bytes.decode('ascii')\r
+ b64_bytes = base64.b64encode(b64_str.encode("ascii"))\r
+ self.base64 = b64_bytes.decode("ascii")\r
# Headers\r
self.headers = {\r
"Authorization": f"Basic {self.base64}",\r
- "content-type": "application/x-www-form-urlencoded"\r
+ "content-type": "application/x-www-form-urlencoded",\r
}\r
\r
########################\r
# 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
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:
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:
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"
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
https://github.com/xraver/mercedes_me_api/
"""
import logging
-
import requests
from const import *