Skip to content

Commit a1a9e0c

Browse files
committed
Close aiohttp connection when probing API
After checking the API credentials, the `Stromer` instance is dropped, and thus we should close the aiohttp session as well.
1 parent 9818651 commit a1a9e0c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

custom_components/stromer/config_flow.py

Lines changed: 3 additions & 1 deletion
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()

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)