Skip to content

Commit 18b1198

Browse files
authored
Merge pull request #134 from dbrgn/morebikes-improvements
morebikes: Close aiohttp connections when probing API
2 parents 2991ce8 + a1a9e0c commit 18b1198

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

custom_components/stromer/config_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ async def validate_input(_: HomeAssistant, data: dict[str, Any]) -> dict:
3131
client_id = data[CONF_CLIENT_ID]
3232
client_secret = data.get(CONF_CLIENT_SECRET, None)
3333

34-
# Initialize connection to stromer
34+
# Initialize connection to stromer to validate credentials
3535
stromer = Stromer(username, password, client_id, client_secret)
3636
if not await stromer.stromer_connect():
3737
raise InvalidAuth
38+
LOGGER.debug("Credentials validated successfully")
39+
await stromer.stromer_disconnect()
3840

3941
# All bikes information available
4042
all_bikes = await stromer.stromer_detect()
@@ -67,7 +69,7 @@ async def async_step_bike(
6769
self.user_input_data["nickname"] = nickname
6870
self.user_input_data["model"] = self.all_bikes[bike_id]["biketype"]
6971

70-
LOGGER.info(f"Using {selected_bike} (i.e. bike ID {bike_id} to talk to the Stromer API")
72+
LOGGER.info(f"Using {selected_bike} (i.e. bike ID {bike_id}) to talk to the Stromer API")
7173

7274
await self.async_set_unique_id(f"stromerbike-{bike_id}")
7375
self._abort_if_unique_id_configured()

custom_components/stromer/stromer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def __init__(self, username: str, password: str, client_id: str, client_secret:
2727
self._api_version = "v3"
2828
self.base_url: str = "https://api3.stromer-portal.ch"
2929

30+
LOGGER.debug("Initializing Stromer with API version %s", self._api_version)
31+
3032
self._timeout: int = timeout
3133
self._username: str = username
3234
self._password: str = password
@@ -43,6 +45,7 @@ def __init__(self, username: str, password: str, client_id: str, client_secret:
4345

4446
async def stromer_connect(self) -> dict:
4547
"""Connect to stromer API."""
48+
LOGGER.debug("Creating aiohttp session")
4649
aio_timeout = aiohttp.ClientTimeout(total=self._timeout)
4750
self._websession = aiohttp.ClientSession(timeout=aio_timeout)
4851

@@ -56,6 +59,11 @@ async def stromer_connect(self) -> dict:
5659

5760
return True
5861

62+
async def stromer_disconnect(self) -> None:
63+
"""Close API web session."""
64+
LOGGER.debug("Closing aiohttp session")
65+
await self._websession.close()
66+
5967
async def stromer_detect(self) -> dict:
6068
"""Get full data (to determine bike(s))."""
6169
try:

0 commit comments

Comments
 (0)