From: Giorgio Ravera Date: Sun, 27 Dec 2020 18:29:25 +0000 (+0100) Subject: Added possibility to enable or disable resources file X-Git-Tag: v0.10~4 X-Git-Url: http://git.giorgioravera.it/?a=commitdiff_plain;h=88b3d277b892d5f99ff1dbc8efc98c8416ac892d;p=mercedes_me_api.git Added possibility to enable or disable resources file --- diff --git a/README.md b/README.md index 933d8a7..5625ba3 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ mercedesmeapi: client_id: <**INSERT_YOUR_CLIENT_ID**> client_secret: <**INSERT_YOUR_CLIENT_SECRET**> vehicle_id: <**INSERT_YOUR_VEHICLE_ID**> - scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES **> + enable_resources_file: <**ENABLE (True) OR DISABLE (False) RESOURCES - OPTIONAL **> + 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) @@ -63,6 +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 **> ``` 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 4f698e0..ebca0dc 100644 --- a/config.py +++ b/config.py @@ -28,6 +28,7 @@ class MercedesMeConfig: self.client_id = "" self.client_secret = "" self.vin = "" + self.enable_resources_file = True self.token = "" ######################## @@ -60,6 +61,21 @@ class MercedesMeConfig: if not self.vin: _LOGGER.error(f"No {CONF_VEHICLE_ID} found in the configuration") 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})" + ) + 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})" + ) # Read Token self.token = MercedesMeOauth(self.client_id, self.client_secret) if not self.token.ReadToken(): diff --git a/const.py b/const.py index 53136e5..3ef1061 100644 --- a/const.py +++ b/const.py @@ -23,3 +23,4 @@ URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" CONF_CLIENT_ID = "CLIENT_ID" CONF_CLIENT_SECRET = "CLIENT_SECRET" CONF_VEHICLE_ID = "VEHICLE_ID" +CONF_ENABLE_RESOURCES_FILE = "ENABLE_RESOURCES_FILE" diff --git a/custom_components/mercedesmeapi/config.py b/custom_components/mercedesmeapi/config.py index 023648c..7043cc2 100644 --- a/custom_components/mercedesmeapi/config.py +++ b/custom_components/mercedesmeapi/config.py @@ -30,6 +30,7 @@ CONFIG_SCHEMA = 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) ), @@ -51,6 +52,7 @@ class MercedesMeConfig: self.client_id = "" self.client_secret = "" self.vin = "" + self.enable_resources_file = True self.token = "" ######################## @@ -66,7 +68,14 @@ class MercedesMeConfig: self.client_secret = config[CONF_CLIENT_SECRET] # Vehicle ID self.vin = config[CONF_VEHICLE_ID] - # Scan Interval + # 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})") + # Scan Interval (optional) self.scan_interval = config[CONF_SCAN_INTERVAL] # Read Token self.token = MercedesMeOauth(self.hass, self.client_id, self.client_secret) diff --git a/custom_components/mercedesmeapi/const.py b/custom_components/mercedesmeapi/const.py index 60a141d..54a6bdc 100644 --- a/custom_components/mercedesmeapi/const.py +++ b/custom_components/mercedesmeapi/const.py @@ -23,3 +23,4 @@ URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" CONF_CLIENT_ID = "client_id" CONF_CLIENT_SECRET = "client_secret" CONF_VEHICLE_ID = "vehicle_id" +CONF_ENABLE_RESOURCES_FILE = "enable_resources_file" diff --git a/custom_components/mercedesmeapi/query.py b/custom_components/mercedesmeapi/query.py index a4be239..8fdb778 100644 --- a/custom_components/mercedesmeapi/query.py +++ b/custom_components/mercedesmeapi/query.py @@ -7,6 +7,7 @@ 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 * diff --git a/custom_components/mercedesmeapi/resources.py b/custom_components/mercedesmeapi/resources.py index 79dd947..e68467b 100644 --- a/custom_components/mercedesmeapi/resources.py +++ b/custom_components/mercedesmeapi/resources.py @@ -111,21 +111,22 @@ class MercedesMeResources: found = False resources = None - if not os.path.isfile(self.resources_file): - # Resources File not present - Retrieving new one from server - _LOGGER.error("Resource File missing - Creating a new one.") - found = False - else: - with open(self.resources_file, "r") as file: - try: - resources = json.load(file) - if not self.CheckResources(resources): - raise ValueError - else: - found = True - except ValueError: - _LOGGER.error("Error reading resource file - Creating a new one.") - found = False + if (self.mercedesConfig.enable_resources_file == True): + if not os.path.isfile(self.resources_file): + # Resources File not present - Retrieving new one from server + _LOGGER.error("Resource File missing - Creating a new one.") + found = False + else: + with open(self.resources_file, "r") as file: + try: + resources = json.load(file) + if not self.CheckResources(resources): + raise ValueError + else: + found = True + except ValueError: + _LOGGER.error("Error reading resource file - Creating a new one.") + found = False if not found: # Not valid or file missing @@ -209,13 +210,14 @@ class MercedesMeResources: # Write Resources File ######################## def WriteResourcesFile(self): - output = [] - # Extract List - for res in self.database: - output.append(res.getJson()) - # Write File - with open(self.resources_file, "w") as file: - json.dump(output, file) + if (self.mercedesConfig.enable_resources_file == True): + output = [] + # Extract List + for res in self.database: + output.append(res.getJson()) + # Write File + with open(self.resources_file, "w") as file: + json.dump(output, file) ######################## # Print Available Resources diff --git a/info.md b/info.md index 3b0d738..ad14e9a 100644 --- a/info.md +++ b/info.md @@ -45,7 +45,8 @@ mercedesmeapi: client_id: <**INSERT_YOUR_CLIENT_ID**> client_secret: <**INSERT_YOUR_CLIENT_SECRET**> vehicle_id: <**INSERT_YOUR_VEHICLE_ID**> - scan_interval: <** TIME PERIOD (NUMBER OF SECONDS) TO REFRESH RESOURCES **> + enable_resources_file: <**ENABLE (True) OR DISABLE (False) RESOURCES - OPTIONAL **> + 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) diff --git a/resources.py b/resources.py index d6c3e30..bd3c56f 100644 --- a/resources.py +++ b/resources.py @@ -106,21 +106,24 @@ class MercedesMeResources: found = False resources = None - if not os.path.isfile(self.resources_file): - # Resources File not present - Retrieving new one from server - _LOGGER.error("Resource File missing - Creating a new one.") - found = False - else: - with open(self.resources_file, "r") as file: - try: - resources = json.load(file) - if not self.CheckResources(resources): - raise ValueError - else: - found = True - except ValueError: - _LOGGER.error("Error reading resource file - Creating a new one.") - found = False + if self.mercedesConfig.enable_resources_file == True: + if not os.path.isfile(self.resources_file): + # Resources File not present - Retrieving new one from server + _LOGGER.error("Resource File missing - Creating a new one.") + found = False + else: + with open(self.resources_file, "r") as file: + try: + resources = json.load(file) + if not self.CheckResources(resources): + raise ValueError + else: + found = True + except ValueError: + _LOGGER.error( + "Error reading resource file - Creating a new one." + ) + found = False if not found: # Not valid or file missing @@ -204,13 +207,14 @@ class MercedesMeResources: # Write Resources File ######################## def WriteResourcesFile(self): - output = [] - # Extract List - for res in self.database: - output.append(res.getJson()) - # Write File - with open(self.resources_file, "w") as file: - json.dump(output, file) + if self.mercedesConfig.enable_resources_file == True: + output = [] + # Extract List + for res in self.database: + output.append(res.getJson()) + # Write File + with open(self.resources_file, "w") as file: + json.dump(output, file) ######################## # Print Available Resources