]> git.giorgioravera.it Git - mercedes_me_api.git/commitdiff
Added possibility to enable or disable resources file
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Sun, 27 Dec 2020 18:29:25 +0000 (19:29 +0100)
committerGitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
Tue, 29 Dec 2020 01:15:59 +0000 (01:15 +0000)
README.md
config.py
const.py
custom_components/mercedesmeapi/config.py
custom_components/mercedesmeapi/const.py
custom_components/mercedesmeapi/query.py
custom_components/mercedesmeapi/resources.py
info.md
resources.py

index 933d8a7b8fdea1646ca53f88dbac410db4fb39a6..5625ba3b793a934770b10d0a3d647d471b3d0150 100644 (file)
--- 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.
index 4f698e04a868a57d9d90ded9174b35125113d170..ebca0dc9096db1f9b7e3062bb59b576e894a9e0b 100644 (file)
--- 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():
index 53136e54155c05c2ab240062a5463decded80c94..3ef10616b21aa1bf53ebc1d8b450220858974625 100644 (file)
--- 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"
index 023648c37b738f9b685c9a2386e01e5e3e0e151d..7043cc2fcb228eb51019feba8ba5cc49e79f78eb 100644 (file)
@@ -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)
index 60a141d9dddda6ff3f0293bc02565d9667d41dd4..54a6bdc26cfa4b031ff3015472ee809ab50b4338 100644 (file)
@@ -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"
index a4be239964a4cc282dbb52091fc169e77c0fdaa0..8fdb77810c05ce6ae75ebeff2fd011ed15bae4e6 100644 (file)
@@ -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 *
index 79dd947fdda409cd7c5663d2710cdf3d4eb780bd..e68467bf503a298a1598035135e725c73dd348f9 100644 (file)
@@ -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 3b0d7385bd4e2e252d76d13cfffe82f47730ffb9..ad14e9a8c8cc2de3443eeb38009f6aca8c451ce6 100644 (file)
--- 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)
 
index d6c3e306fc3fd09cf7a8cf220fe1035287f4bc6e..bd3c56f3c3259e0bd37c12b34ded466dbe88fbc9 100644 (file)
@@ -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