Skip to content

Commit

Permalink
vicare: reset token refresh only on success. Retry otherwise.
Browse files Browse the repository at this point in the history
  • Loading branch information
aschwith committed Jan 2, 2024
1 parent 8c919fd commit d9057d4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
19 changes: 13 additions & 6 deletions vicare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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'}
Expand All @@ -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 :
Expand All @@ -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:
Expand Down Expand Up @@ -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.")

Expand Down
4 changes: 2 additions & 2 deletions vicare/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ plugin_functions: NONE
logic_parameters: NONE

item_structs:

heizung:
heizung:
serial:
name: serial number
type: str
Expand Down Expand Up @@ -292,3 +291,4 @@ heizung:
vicare_rx_key: device.messages.errors.raw
vicare_path: ['entries','value']
visu_acl: ro

4 changes: 2 additions & 2 deletions vicare/user_doc.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.. index:: Plugins; vicare
.. index:: vicare

==========
======
vicare
==========
======

.. image:: webif/static/img/plugin_logo.png
:alt: plugin logo
Expand Down

0 comments on commit d9057d4

Please sign in to comment.