Skip to content

Commit

Permalink
Change retry strategy (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenbos authored Nov 27, 2023
1 parent d2ed388 commit 9e1b9a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
14 changes: 14 additions & 0 deletions custom_components/alpha_innotec/api.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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 = ""
Expand Down
6 changes: 3 additions & 3 deletions custom_components/alpha_innotec/controller_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
)
Expand All @@ -64,15 +64,15 @@ 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
})

_LOGGER.debug('[api/user/token/challenge] - response body: %s', response.json())

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,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alpha_innotec/gateway_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
)
Expand Down

1 comment on commit 9e1b9a5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HA Alpha Innotec Coverage

HA Alpha Innotec
FileStmtsMissCoverMissing
__init__.py21957%19–20, 22, 27, 29, 34, 36–37, 39
api.py49295%62, 65
binary_sensor.py50500%2, 4–5, 7, 9–13, 16–19, 21, 24, 27, 29, 31, 33, 35–36, 42, 45, 48, 50–52, 54–55, 57, 65–67, 69–70, 72, 74–76, 78–80, 82–87, 89, 91
climate.py62620%2, 4–5, 7, 11–16, 18–20, 22, 25, 28, 30, 32, 34, 36–37, 43, 46, 49–51, 55, 57–59, 61–62, 64, 72–73, 75, 77–79, 81–82, 84–86, 88, 90, 93–94, 96, 98–99, 101, 103, 105–107, 109–110, 112, 114–115, 118
config_flow.py371559%24–25, 27–30, 32–35, 57–61
const.py70100% 
controller_api.py786121%17–18, 20–21, 23–26, 28, 30–33, 35, 38, 40, 43, 45, 50, 52–55, 57, 59–60, 62, 64, 71, 73, 75, 82, 84–85, 87, 89, 91, 93, 95–96, 98, 101, 104–105, 107–110, 112, 115, 118, 124, 126–130, 142–144, 146
coordinator.py51510%1–2, 4–6, 8–11, 13, 16, 18, 20, 22, 29–30, 32–35, 37–38, 40–42, 44–45, 47–49, 51, 53–55, 57–58, 60, 72, 74–75, 77–78, 80–81, 83, 85–87, 89, 99, 101
gateway_api.py604525%17–19, 21–23, 26–27, 29–30, 32–35, 37, 39–42, 44, 47, 49, 52, 54, 59, 61, 63–65, 67, 69–70, 72, 74, 77, 79–80, 82, 85, 87, 90, 93–95, 97
sensor.py49490%2, 4–6, 8, 10–14, 16–18, 20, 23, 26, 28, 30, 32, 34–35, 41, 44, 47–48, 50, 53–55, 57–58, 60, 68–69, 71, 73–75, 77–79, 81–86, 88, 90
structs
   Thermostat.py11918%8–16
TOTAL47535325% 

Tests Skipped Failures Errors Time
3 0 💤 0 ❌ 0 🔥 0.481s ⏱️

Please sign in to comment.