Skip to content

Commit 90c84e1

Browse files
authored
Merge pull request #207 from CoMPaTech/dnsfix
Initial connection DNS fix
2 parents 6d8a1be + 3d963b8 commit 90c84e1

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Changelog
44

5+
### JUL 2025 [0.4.2]
6+
7+
- Attempt to fix getting initial code (due to DNS issues) #139
8+
9+
### SEP 2024 [0.4.1]
10+
11+
- Onboarding devices fix
12+
513
### AUG 2024 [0.4.0]
614

715
- Add multibike support

custom_components/stromer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/CoMPaTech/stromer/issues",
1010
"requirements": [],
11-
"version": "0.4.0"
11+
"version": "0.4.2"
1212
}

custom_components/stromer/stromer.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
__version__ = "0.2.0"
44

5+
import asyncio
56
import json
67
import logging
78
import re
89
from typing import Any
910
from urllib.parse import urlencode
1011

12+
import aiodns
1113
import aiohttp
1214

1315
LOGGER = logging.getLogger(__name__)
@@ -109,12 +111,33 @@ async def stromer_update(self) -> None:
109111
LOGGER.error("Stromer error: api call failed 10 times, cowardly failing")
110112
raise ApiError
111113

114+
async def stromer_api_debouncer(self, url: str, timeout: int = 10, retries: int = 10, delay: int = 10) -> aiohttp.ClientResponse:
115+
"""Debounce API-request to leverage DNS issues."""
116+
for attempt in range(retries):
117+
try:
118+
log = f"Attempt {attempt + 1}/{retries} to interface with Stromer on {url}"
119+
LOGGER.debug(log)
120+
res = await self._websession.get(url, timeout=timeout)
121+
res.raise_for_status()
122+
return res
123+
except (aiodns.error.DNSError, aiohttp.ClientError, TimeoutError) as e:
124+
log = f"Error getting Stromer API (attempt {attempt + 1}/{retries}): {e}"
125+
LOGGER.warning(log)
126+
if attempt < retries - 1:
127+
log = f"Retrying in {delay} seconds..."
128+
LOGGER.warning(log)
129+
await asyncio.sleep(delay)
130+
else:
131+
log = f"Failed to get to Stromer API after {retries} attempts."
132+
LOGGER.error(log)
133+
raise # Re-raise the last exception if all retries fail
134+
112135
async def stromer_get_code(self) -> None:
113136
"""Retrieve authorization code from API."""
114137
url = f"{self.base_url}/mobile/v4/login/"
115138
if self._api_version == "v3":
116139
url = f"{self.base_url}/users/login/"
117-
res = await self._websession.get(url)
140+
res = await self.stromer_api_debouncer(url)
118141
try:
119142
cookie = res.headers.get("Set-Cookie")
120143
pattern = "=(.*?);"

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ lint.select = [
5151
"S317", # suspicious-xml-sax-usage
5252
"S318", # suspicious-xml-mini-dom-usage
5353
"S319", # suspicious-xml-pull-dom-usage
54-
"S320", # suspicious-xmle-tree-usage
5554
"S601", # paramiko-call
5655
"S602", # subprocess-popen-with-shell-equals-true
5756
"S604", # call-with-shell-equals-true

0 commit comments

Comments
 (0)