From d9057d49c6ddb523a4eeaf5660c1d2cce3125c0d Mon Sep 17 00:00:00 2001 From: aschwith Date: Tue, 2 Jan 2024 16:57:10 +0100 Subject: [PATCH] vicare: reset token refresh only on success. Retry otherwise. --- vicare/__init__.py | 19 +++++++++++++------ vicare/plugin.yaml | 4 ++-- vicare/user_doc.rst | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/vicare/__init__.py b/vicare/__init__.py index b14c6381b..c323dda1d 100644 --- a/vicare/__init__.py +++ b/vicare/__init__.py @@ -38,7 +38,7 @@ TOKEN_URL = 'https://iam.viessmann.com/idp/v3/token' class Vicare(SmartPlugin): - PLUGIN_VERSION = '1.9.1' + PLUGIN_VERSION = '1.9.2' def __init__(self, sh): """ @@ -223,10 +223,11 @@ def generate_request_url(self): def refresh_accessToken(self): # Refreshes the accessToken validity by using the refresh token. Works up to 180 days. # Procedure is described on: https://documentation.viessmann.com/static/authentication + # Return True on success and False otherwise if self.refreshToken == '': self.logger.error(f"No refresh token available. Aborting.") - return + return False self.logger.debug(f"Refreshing accessToken...") headers = {'Content-Type': 'application/x-www-form-urlencoded'} @@ -238,13 +239,17 @@ def refresh_accessToken(self): response = self.session.post(TOKEN_URL, headers = headers, data = data, verify=False, timeout=4) except Exception as e: self.logger.warning(f"Exception occurred during refresh token request: {e}") + return False - if response is not None and response.status_code == 200: + if response is None: + return False + + if response.status_code == 200: self.logger.info(f"Refresh token request successfull") else: self.logger.warning(f"Refresh token request was unsuccessfull. Status code: {response.status_code}") self.logger.warning(f"Refresh token request was unsuccessfull. Response: {response.text}") - return + return False responseJson = response.json() if 'access_token' in responseJson : @@ -254,8 +259,10 @@ def refresh_accessToken(self): param_dict = {"accessToken": str(self.accessToken)} self.update_config_section(param_dict) self.logger.debug(f"Successfully saved accessToken in plugin.yaml") + return True else: self.logger.error(f"refreshToken: Response did not contain an access token!") + return False def retrieve_accessToken(self, code): # pass code from login procedure, see browser argument on redirected page: @@ -334,9 +341,9 @@ def poll_backend(self): # Token refresh mechanism: self.count_to_renew = self.count_to_renew + 1 if self.count_to_renew >= self.fixed_cylces_to_renew: - self.count_to_renew = 0 self.logger.debug(f"Refreshing token...") - self.refresh_accessToken() + if self.refresh_accessToken() == True: + self.count_to_renew = 0 # else: # self.logger.debug(f"{self.fixed_cylces_to_renew - self.count_to_renew} remaining cycles to refresh token.") diff --git a/vicare/plugin.yaml b/vicare/plugin.yaml index 1c61e6121..b921f90c2 100644 --- a/vicare/plugin.yaml +++ b/vicare/plugin.yaml @@ -114,8 +114,7 @@ plugin_functions: NONE logic_parameters: NONE item_structs: - -heizung: + heizung: serial: name: serial number type: str @@ -292,3 +291,4 @@ heizung: vicare_rx_key: device.messages.errors.raw vicare_path: ['entries','value'] visu_acl: ro + diff --git a/vicare/user_doc.rst b/vicare/user_doc.rst index c1491894b..c09298da9 100644 --- a/vicare/user_doc.rst +++ b/vicare/user_doc.rst @@ -1,9 +1,9 @@ .. index:: Plugins; vicare .. index:: vicare -========== +====== vicare -========== +====== .. image:: webif/static/img/plugin_logo.png :alt: plugin logo