diff --git a/custom_components/alpha_innotec/api.py b/custom_components/alpha_innotec/api.py index 4b0db10..4a41222 100644 --- a/custom_components/alpha_innotec/api.py +++ b/custom_components/alpha_innotec/api.py @@ -1,9 +1,12 @@ import base64 import logging +import requests from backports.pbkdf2 import pbkdf2_hmac from Crypto.Cipher import AES from Crypto.Hash import SHA256 +from requests.adapters import HTTPAdapter +from urllib3 import Retry _LOGGER = logging.getLogger(__name__) @@ -22,6 +25,17 @@ def __init__(self, hostname: str, username: str, password: str) -> None: self.last_request_signature: str | None = None self.udid: str = "homeassistant" + self.session = requests.Session() + self.session.mount( + prefix='http://', + adapter=HTTPAdapter( + max_retries=Retry( + total=5, + backoff_factor=3 + ) + ) + ) + @staticmethod def string_to_charcodes(data: str) -> str: a = "" diff --git a/custom_components/alpha_innotec/controller_api.py b/custom_components/alpha_innotec/controller_api.py index 25ba967..06eb1ac 100644 --- a/custom_components/alpha_innotec/controller_api.py +++ b/custom_components/alpha_innotec/controller_api.py @@ -42,7 +42,7 @@ def call(self, endpoint: str, data: dict = None) -> dict: _LOGGER.debug("[%s] - body: %s", endpoint, urlencoded_body) - response = requests.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint), + response = self.session.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint), data=urlencoded_body, headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} ) @@ -64,7 +64,7 @@ def call(self, endpoint: str, data: dict = None) -> dict: return json_response def login(self): - response = requests.post("http://" + self.api_host + "/api/user/token/challenge", data={ + response = self.session.post("http://" + self.api_host + "/api/user/token/challenge", data={ "udid": self.udid }) @@ -72,7 +72,7 @@ def login(self): device_token = response.json()['devicetoken'] - response = requests.post("http://" + self.api_host + "/api/user/token/response", data={ + response = self.session.post("http://" + self.api_host + "/api/user/token/response", data={ "login": self.username, "token": device_token, "udid": self.udid, diff --git a/custom_components/alpha_innotec/gateway_api.py b/custom_components/alpha_innotec/gateway_api.py index abce3c3..61c3a92 100644 --- a/custom_components/alpha_innotec/gateway_api.py +++ b/custom_components/alpha_innotec/gateway_api.py @@ -51,7 +51,7 @@ def call(self, endpoint: str, data: dict = None) -> dict: _LOGGER.debug("[%s] - body: %s", endpoint, urlencoded_body) - response = requests.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint), + response = self.session.post("http://{hostname}/{endpoint}".format(hostname=self.api_host, endpoint=endpoint), data=urlencoded_body, headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} )