]> git.giorgioravera.it Git - mercedes_me_api.git/commitdiff
Fixed readme & minor fixes v0.11
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Sat, 9 Jan 2021 21:02:52 +0000 (22:02 +0100)
committerGitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
Sat, 9 Jan 2021 21:05:30 +0000 (21:05 +0000)
README.md
config.py
const.py
custom_components/mercedesmeapi/const.py
custom_components/mercedesmeapi/oauth.py
custom_components/mercedesmeapi/resources.py
info.md
mercedes_me_api.py
oauth.py
resources.py

index 7e0f51413d5e4bb1764872674f3cbfc99635dc59..f74c281cfe83f22234af92eefc85b25d18caa38c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ This repository contains a collection of scripts (including the Home Assistant C
 ## Pre-Requirements
 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:
+3) Register to the following APIs (all of them):
    - [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) 
@@ -78,6 +78,17 @@ ENABLE_RESOURCES_FILE: <**ENABLE (true) OR DISABLE (false) RESOURCES - (DEFAULT
 
 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.
 
+## First Token Creation
+To create the first token it's necessary to perform the following steps:
+1) Create the APP and register it to all APIs as describe in the Pre-Requirements paragraph
+2) Logout in all browser from Mercedes Me (Developer site included)
+3) Execute the shell or python script with -t parameter to obrain the first token
+4) Open with a browser the link provided by the script: a login page will be shown
+5) Using your Mercedes Me credential log in: a new web-page will ask to authorize the APP created in the step 1 to access to the personal information associated to the APIs registered
+6) Enable all information and press Allow
+7) An error page will appear: check the URI: it's something like https://localhost/?code=DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa
+8) Copy the part of the URI after code= (in this case DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa) and paste into the running script
+
 ### Python Usage
 To execute the script read below:
 ```bash
index b449f97cfab922d5371d05b392c4465df18c9311..045577deede2a0af374a6557e609b1eb3f58c4b9 100644 (file)
--- a/config.py
+++ b/config.py
@@ -30,8 +30,6 @@ class MercedesMeConfig:
     # Read Configuration
     ########################
     def ReadConfig(self):
-        needToRefresh = False
-
         # Read Config from file
         if not os.path.isfile(self.config_file):
             _LOGGER.error(f"Credential File {self.config_file} not found")
index 42a5ff7bcc13f4ef67f855ad45da2c54d81fbf62..7967c05795763aca0b834da930555c8a82459e48 100644 (file)
--- 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.10"
+VERSION = "0.11"
 # Software Parameters
 TOKEN_FILE = ".mercedesme_token"
 CONFIG_FILE = ".mercedesme_config"
index e489f02397951753980d6ce303381ad20f4d1ca3..f9cbe81f67c7ff866b07c0720c6e88588a6fbeb2 100644 (file)
@@ -9,7 +9,7 @@ https://github.com/xraver/mercedes_me_api/
 # Software Name & Version
 NAME = "Mercedes Me API"
 DOMAIN = "mercedesmeapi"
-VERSION = "0.10"
+VERSION = "0.11"
 # Software Parameters
 TOKEN_FILE = ".mercedesme_token"
 RESOURCES_FILE = ".mercedesme_resources"
index 94c210bc2e3fafc996c6f368a04268c8ede43ea0..3e34214ea24355d6c0663c251c76d0f53705780e 100644 (file)
@@ -56,12 +56,11 @@ class MercedesMeOauth:
     def ReadToken(self):\r
 \r
         found = False\r
-        needToRefresh = False\r
 \r
         # Read Token\r
         if not os.path.isfile(self.token_file):\r
-            # Token File not present - Creating new one\r
-            _LOGGER.error("Token File missing - Creating a new one")\r
+            # Token File not present\r
+            _LOGGER.error("Token File missing")\r
             found = False\r
         else:\r
             with open(self.token_file, "r") as file:\r
@@ -72,23 +71,16 @@ class MercedesMeOauth:
                     else:\r
                         found = True\r
                 except ValueError:\r
-                    _LOGGER.error("Error reading token file - Creating a new one")\r
+                    _LOGGER.error("Error reading token file")\r
                     found = False\r
 \r
-        if not found:\r
-            # Not valid or file missing\r
-            #if (not self.CreateToken()): GRGR -> to be fixed\r
-            if (True):\r
-                _LOGGER.error("Error creating token")\r
-                return False\r
-        else:\r
+        if found:\r
             # Valid: just import\r
             self.access_token = token["access_token"]\r
             self.refresh_token = token["refresh_token"]\r
             self.token_expires_in = token["expires_in"]\r
-            needToRefresh = True\r
 \r
-        if needToRefresh:\r
+                       # Refreshing\r
             if not self.RefreshToken():\r
                 _LOGGER.error("Error refreshing token")\r
                 return False\r
@@ -164,6 +156,10 @@ class MercedesMeOauth:
     ########################\r
     def RefreshToken(self):\r
 \r
+        if self.access_token == "":\r
+          _LOGGER.error("Error: Token not found or not valid")\r
+          return False\r
+\r
         data = f"grant_type=refresh_token&refresh_token={self.refresh_token}"\r
         token = GetToken(URL_OAUTH_TOKEN, self.headers, data, refresh=True)\r
 \r
index cf5f23a99ce9fcc0632d2c7bba8f2c7eb9c62cac..9331753506fbc2e67fc73accb4a70896930265b0 100644 (file)
@@ -111,6 +111,10 @@ class MercedesMeResources:
         found = False
         resources = None
 
+        if self.mercedesConfig.token.access_token == "":
+          _LOGGER.error("Error: Token not found or not valid")
+          return 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
diff --git a/info.md b/info.md
index 88dbb9e1cec76fc6ace885b0a397a6dd0360ea69..e5483209b7fa169a419b6f121ed4a456180c63ee 100644 (file)
--- a/info.md
+++ b/info.md
@@ -11,7 +11,7 @@ This component is still in development.
 ## Pre-Requirements
 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:
+3) Register to the following APIs (all of them):
    - [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) 
@@ -59,6 +59,17 @@ logger:
     custom_components.mercedesmeapi: debug
 ```
 
+## First Token Creation
+To create the first token it's necessary to perform the following steps:
+1) Create the APP and register it to all APIs as describe in the Pre-Requirements paragraph
+2) Logout in all browser from Mercedes Me (Developer site included)
+3) Execute the shell or python script with -t parameter to obrain the first token
+4) Open with a browser the link provided by the script: a login page will be shown
+5) Using your Mercedes Me credential log in: a new web-page will ask to authorize the APP created in the step 1 to access to the personal information associated to the APIs registered
+6) Enable all information and press Allow
+7) An error page will appear: check the URI: it's something like https://localhost/?code=DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa
+8) Copy the part of the URI after code= (in this case DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa) and paste into the running script
+
 ## Change Log
 You can find change log under [releases][releases-url]
 
index 4c8d0f38a19df3f9ce9d0f710de0677be4c48d3b..038eefa3997325873dc75cf85a70a30500452164 100644 (file)
@@ -95,12 +95,13 @@ if __name__ == "__main__":
             exit(1)
 
     # Read Resources
-    if not data.mercedesResources.ReadResources():
-        _LOGGER.error("Error initializing resources")
-        exit(1)
+    if (args.resources == True) | (args.status == True):
+        if not data.mercedesResources.ReadResources():
+            _LOGGER.error("Error initializing resources")
+            exit(1)
 
     # Print Available Resources
-    if args.resources:
+    if args.resources == True:
         data.mercedesResources.PrintAvailableResources()
 
     # Print Resources State
index e01b75579773dbc87be007a60e8a6cb09039f8fe..ea35f0d78d4c051ee0e41d7a959f840eabb1c0f8 100644 (file)
--- a/oauth.py
+++ b/oauth.py
@@ -56,12 +56,11 @@ class MercedesMeOauth:
     def ReadToken(self):
 
         found = False
-        needToRefresh = False
 
         # Read Token
         if not os.path.isfile(self.token_file):
-            # Token File not present - Creating new one
-            _LOGGER.error("Token File missing - Creating a new one")
+            # Token File not present
+            _LOGGER.error("Token File missing")
             found = False
         else:
             with open(self.token_file, "r") as file:
@@ -72,22 +71,16 @@ class MercedesMeOauth:
                     else:
                         found = True
                 except ValueError:
-                    _LOGGER.error("Error reading token file - Creating a new one")
+                    _LOGGER.error("Error reading token file")
                     found = False
 
-        if not found:
-            # Not valid or file missing
-            if not self.CreateToken():
-                _LOGGER.error("Error creating token")
-                return False
-        else:
+        if found:
             # Valid: just import
             self.access_token = token["access_token"]
             self.refresh_token = token["refresh_token"]
             self.token_expires_in = token["expires_in"]
-            needToRefresh = True
 
-        if needToRefresh:
+            # Refreshing
             if not self.RefreshToken():
                 _LOGGER.error("Error refreshing token")
                 return False
@@ -163,6 +156,10 @@ class MercedesMeOauth:
     ########################
     def RefreshToken(self):
 
+        if self.access_token == "":
+            _LOGGER.error("Error: Token not found or not valid")
+            return False
+
         data = f"grant_type=refresh_token&refresh_token={self.refresh_token}"
         token = GetToken(URL_OAUTH_TOKEN, self.headers, data, refresh=True)
 
index bd3c56f3c3259e0bd37c12b34ded466dbe88fbc9..7424488cd7a870dc5e44130c735d39c4abbda4a0 100644 (file)
@@ -106,6 +106,10 @@ class MercedesMeResources:
         found = False
         resources = None
 
+        if self.mercedesConfig.token.access_token == "":
+            _LOGGER.error("Error: Token not found or not valid")
+            return 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