From: Giorgio Ravera Date: Tue, 29 Dec 2020 14:42:02 +0000 (+0100) Subject: Fixed configuration validation (#21) X-Git-Tag: v0.10~2 X-Git-Url: http://git.giorgioravera.it/?a=commitdiff_plain;h=02ed72add530aad48bd1f3798a3e1f06117a7f7c;p=mercedes_me_api.git Fixed configuration validation (#21) --- diff --git a/README.md b/README.md index 5625ba3..be823ef 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ mercedesmeapi: client_id: <**INSERT_YOUR_CLIENT_ID**> client_secret: <**INSERT_YOUR_CLIENT_SECRET**> vehicle_id: <**INSERT_YOUR_VEHICLE_ID**> - enable_resources_file: <**ENABLE (True) OR DISABLE (False) RESOURCES - OPTIONAL **> - scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES - OPTIONAL **> + enable_resources_file: <**ENABLE (true) OR DISABLE (false) RESOURCES - OPTIONAL (DEFAULT DISABLED)**> + scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES - OPTIONAL**> ``` 2) Actually it's not possible to retrieve the token from scratch. Please use the other script to retrieve the first token and copy it into hacs folder (.mercedesme_token) @@ -64,7 +64,7 @@ To use this script it's necessary to perform the following instructions: CLIENT_ID=<**INSERT_YOUR_CLIENT_ID**> CLIENT_SECRET=<**INSERT_YOUR_CLIENT_SECRET**> VEHICLE_ID=<**INSERT_YOUR_VEHICLE_ID**> -ENABLE_RESOURCES_FILE: <**ENABLE (True) OR DISABLE (False) RESOURCES - OPTIONAL **> +ENABLE_RESOURCES_FILE: <**ENABLE (true) OR DISABLE (false) RESOURCES - (DEFAULT DISABLED)**> ``` where CLIENT_ID and CLIENT_SECRET referring to the application information that can be found in [Mercedes Developer Console](https://developer.mercedes-benz.com/console) and VEHICLE_ID is the VIN of your car. diff --git a/config.py b/config.py index ebca0dc..b449f97 100644 --- a/config.py +++ b/config.py @@ -25,11 +25,6 @@ class MercedesMeConfig: ######################## def __init__(self): self.config_file = CONFIG_FILE - self.client_id = "" - self.client_secret = "" - self.vin = "" - self.enable_resources_file = True - self.token = "" ######################## # Read Configuration @@ -63,19 +58,10 @@ class MercedesMeConfig: return False # Enable Resources File (optional) valueFromFile = f.get(CONF_ENABLE_RESOURCES_FILE) - if not valueFromFile: - _LOGGER.error( - f"No {CONF_ENABLE_RESOURCES_FILE} found in the configuration, using default value ({self.enable_resources_file})" - ) + if (valueFromFile == "true") | (valueFromFile == "True"): + self.enable_resources_file = True else: - if valueFromFile == "True": - self.enable_resources_file = True - elif valueFromFile == "False": - self.enable_resources_file = False - else: - _LOGGER.error( - f"Wrong {CONF_ENABLE_RESOURCES_FILE} value found in the configuration ({valueFromFile}), using default value ({self.enable_resources_file})" - ) + self.enable_resources_file = False # Read Token self.token = MercedesMeOauth(self.client_id, self.client_secret) if not self.token.ReadToken(): diff --git a/custom_components/mercedesmeapi/__init__.py b/custom_components/mercedesmeapi/__init__.py index 19d4e0e..ad3ca8c 100644 --- a/custom_components/mercedesmeapi/__init__.py +++ b/custom_components/mercedesmeapi/__init__.py @@ -8,12 +8,38 @@ https://github.com/xraver/mercedes_me_api/ """ from datetime import timedelta import logging +import voluptuous as vol + +from homeassistant.const import ( + CONF_SCAN_INTERVAL, +# LENGTH_KILOMETERS, +# LENGTH_MILES, +) +from homeassistant.helpers import discovery, config_validation as cv from .config import MercedesMeConfig from .oauth import MercedesMeOauth from .resources import MercedesMeResources from .const import * +# Config Schema +CONFIG_SCHEMA = vol.Schema ( + { + DOMAIN: vol.Schema ( + { + vol.Required(CONF_CLIENT_ID): cv.string, + vol.Required(CONF_CLIENT_SECRET): cv.string, + vol.Required(CONF_VEHICLE_ID): cv.string, + vol.Optional(CONF_ENABLE_RESOURCES_FILE, default=False): cv.boolean, + vol.Optional(CONF_SCAN_INTERVAL, default=30): vol.All( + cv.positive_int, vol.Clamp(min=120) + ), + } + ) + }, + extra=vol.ALLOW_EXTRA, +) + # Logger _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/mercedesmeapi/config.py b/custom_components/mercedesmeapi/config.py index 7043cc2..312f7f2 100644 --- a/custom_components/mercedesmeapi/config.py +++ b/custom_components/mercedesmeapi/config.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 voluptuous as vol from homeassistant.const import ( CONF_SCAN_INTERVAL, @@ -15,30 +14,12 @@ from homeassistant.const import ( # LENGTH_MILES, ) -from homeassistant.helpers import discovery, config_validation as cv - -from .oauth import MercedesMeOauth from .const import * +from .oauth import MercedesMeOauth # Logger _LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = vol.Schema ( - { - DOMAIN: vol.Schema ( - { - vol.Required(CONF_CLIENT_ID): cv.string, - vol.Required(CONF_CLIENT_SECRET): cv.string, - vol.Required(CONF_VEHICLE_ID): cv.string, - vol.Optional(CONF_ENABLE_RESOURCES_FILE, default=True): cv.boolean, - vol.Optional(CONF_SCAN_INTERVAL, default=30): vol.All( - cv.positive_int, vol.Clamp(min=60) - ), - } - ) - }, - extra=vol.ALLOW_EXTRA, -) class MercedesMeConfig: @@ -48,35 +29,22 @@ class MercedesMeConfig: def __init__(self, hass, config): # Home Assistant structures self.hass = hass - self.config = config - self.client_id = "" - self.client_secret = "" - self.vin = "" - self.enable_resources_file = True - self.token = "" + self.config = config[DOMAIN] ######################## # Read Configuration ######################## def ReadConfig(self): - needToRefresh = False - - config = self.config[DOMAIN] # Client ID - self.client_id = config[CONF_CLIENT_ID] + self.client_id = self.config.get(CONF_CLIENT_ID) # Client Secret - self.client_secret = config[CONF_CLIENT_SECRET] + self.client_secret = self.config.get(CONF_CLIENT_SECRET) # Vehicle ID - self.vin = config[CONF_VEHICLE_ID] + self.vin = self.config.get(CONF_VEHICLE_ID) # Enable Resources File (optional) - if (config[CONF_ENABLE_RESOURCES_FILE] == True): - self.enable_resources_file = True - elif(config[CONF_ENABLE_RESOURCES_FILE] == False): - self.enable_resources_file = False - else: - _LOGGER.error(f"Wrong {CONF_ENABLE_RESOURCES_FILE} value found in configuration ({config[CONF_ENABLE_RESOURCES_FILE]}), using default value ({self.enable_resources_file})") + self.enable_resources_file = self.config.get(CONF_ENABLE_RESOURCES_FILE) # Scan Interval (optional) - self.scan_interval = config[CONF_SCAN_INTERVAL] + self.scan_interval = self.config.get(CONF_SCAN_INTERVAL) # Read Token self.token = MercedesMeOauth(self.hass, self.client_id, self.client_secret) if not self.token.ReadToken(): diff --git a/info.md b/info.md index ad14e9a..4632a6a 100644 --- a/info.md +++ b/info.md @@ -45,8 +45,8 @@ mercedesmeapi: client_id: <**INSERT_YOUR_CLIENT_ID**> client_secret: <**INSERT_YOUR_CLIENT_SECRET**> vehicle_id: <**INSERT_YOUR_VEHICLE_ID**> - enable_resources_file: <**ENABLE (True) OR DISABLE (False) RESOURCES - OPTIONAL **> - scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES - OPTIONAL **> + enable_resources_file: <**ENABLE (true) OR DISABLE (false) RESOURCES - OPTIONAL (DEFAULT DISABLED)**> + scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES - OPTIONAL**> ``` 2) Actually it's not possible to retrieve the token from scratch. Please use the other script to retrieve the first token and copy it into hacs folder (.mercedesme_token)