Skip to content

Commit

Permalink
Decrease token refresh period, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Dec 25, 2022
1 parent 86bc864 commit 66cb9a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
39 changes: 27 additions & 12 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import ChainMap
from datetime import datetime, timedelta
import requests
from requests import cookies
import json
import hashlib

Expand Down Expand Up @@ -380,7 +381,7 @@ def set_register_value(
):
self.__set_register_value(device, register_index, value)

def __get_register_group(self, device_id: str, register_group: str):
def __get_register_group(self, device_id: str, register_group: str) -> list:
self.__check_token_validity()

url = (
Expand All @@ -400,7 +401,7 @@ def __get_register_group(self, device_id: str, register_group: str):
+ ", Status: "
+ str(status)
)
return None
return []

return request.json()

Expand Down Expand Up @@ -443,6 +444,8 @@ def __fetch_configuration(self):
return request.json()

def __authenticate(self):
result = False

try:
result = self.__authenticate_azure()
except Exception as e:
Expand All @@ -453,7 +456,7 @@ def __authenticate(self):

return result

def __authenticate_azure(self):
def __authenticate_azure(self) -> bool:
# Azure auth URLs
azure_auth_authorize_url = THERMIA_AZURE_AUTH_URL + "/oauth2/v2.0/authorize"
azure_auth_get_token_url = THERMIA_AZURE_AUTH_URL + "/oauth2/v2.0/token"
Expand Down Expand Up @@ -485,6 +488,16 @@ def __authenticate_azure(self):
headers=azure_auth_request_headers,
data=request_token__data,
)

if request_token.status_code != 200:
error_text = (
"Reauthentication request failed with previous refresh token. Status: "
+ str(request_token.status_code)
+ ", Response: "
+ request_token.text
)
_LOGGER.error(error_text)
raise AuthenticationException(error_text)
else:
code_challenge = utils.generate_challenge(43)

Expand Down Expand Up @@ -559,7 +572,7 @@ def __authenticate_azure(self):
)

request_confirmed__cookies = request_self_asserted.cookies
cookie_obj = requests.cookies.create_cookie(
cookie_obj = cookies.create_cookie(
name="x-ms-cpim-csrf", value=request_auth.cookies.get("x-ms-cpim-csrf")
)
request_confirmed__cookies.set_cookie(cookie_obj)
Expand Down Expand Up @@ -592,22 +605,24 @@ def __authenticate_azure(self):
)

if request_token.status_code != 200:
_LOGGER.error(
"Authentication request failed, please check credentials. "
error_text = (
"Authentication request failed, please check credentials. Status: "
+ str(request_token.status_code)
+ ", Response: "
+ request_token.text
)
raise AuthenticationException(
"Authentication request failed, please check credentials.",
request_token.status_code,
)
_LOGGER.error(error_text)
raise AuthenticationException(error_text)

token_data = json.loads(request_token.text)

self.__token = token_data["access_token"]
self.__token_valid_to = token_data["expires_on"]

# refresh token valid for 24h
self.__refresh_token_valid_to = (datetime.now() + timedelta(days=1)).timestamp()
# refresh token valid for 24h, but we refresh it every 12h for safety
self.__refresh_token_valid_to = (
datetime.now() + timedelta(hours=12)
).timestamp()
self.__refresh_token = token_data.get("refresh_token")

self.__default_request_headers = {
Expand Down
17 changes: 10 additions & 7 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
USERNAME = "demo"
PASSWORD = "demo"

CHANGE_HEAT_PUMP_DATA_DURING_TEST = True

thermia = Thermia(USERNAME, PASSWORD, "classic")

print("Connected: " + str(thermia.connected))
Expand Down Expand Up @@ -124,13 +126,14 @@

thermia.update_data()

heat_pump.set_temperature(19)
heat_pump.set_register_data_by_register_group_and_name(
"REG_GROUP_HEATING_CURVE", "REG_HEATING_HEAT_CURVE", 30
)
heat_pump.set_operation_mode("COMPRESSOR")
if heat_pump.is_hot_water_switch_available:
heat_pump.set_hot_water_switch_state(1)
if CHANGE_HEAT_PUMP_DATA_DURING_TEST:
heat_pump.set_temperature(19)
heat_pump.set_register_data_by_register_group_and_name(
"REG_GROUP_HEATING_CURVE", "REG_HEATING_HEAT_CURVE", 30
)
heat_pump.set_operation_mode("COMPRESSOR")
if heat_pump.is_hot_water_switch_available:
heat_pump.set_hot_water_switch_state(1)

print("Heat Temperature: " + str(heat_pump.heat_temperature))
print("Operation Mode: " + str(heat_pump.operation_mode))
Expand Down

0 comments on commit 66cb9a0

Please sign in to comment.