Skip to content

Commit bd0a8a7

Browse files
committed
Close client sessions
Resolves Logger: homeassistant Source: runner.py:145 First occurred: 00:02:26 (2 occurrences) Last logged: 00:02:26 Error doing job: Unclosed client session
1 parent 0453cd1 commit bd0a8a7

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

custom_components/managemyhealth/api.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ async def _api_wrapper(
228228
headers=headers,
229229
)
230230
if response.status in (400, 401, 403):
231-
raise MmhApiAuthenticationError(
232-
"Invalid credentials",
233-
)
231+
raise MmhApiAuthenticationError()
234232
response.raise_for_status()
235233
return await response.json()
236234

235+
except MmhApiAuthenticationError as exception:
236+
raise MmhApiAuthenticationError("Invalid credentials") from exception
237237
except asyncio.TimeoutError as exception:
238238
raise MmhApiCommunicationError(
239239
"Timeout error fetching information: %s", exception
@@ -246,3 +246,16 @@ async def _api_wrapper(
246246
raise MmhApiError(
247247
"Something really wrong happened!: %s", exception
248248
) from exception
249+
250+
async def disconnect(self) -> None:
251+
"""Disconnect from the client."""
252+
_LOGGER.debug("Invoked close manually")
253+
await self.__aexit__()
254+
255+
async def __aexit__(self, *excinfo):
256+
"""Destroy the device and http sessions."""
257+
_LOGGER.debug("Invoked close automatically")
258+
if not self._session:
259+
return
260+
261+
await self._session.close()

custom_components/managemyhealth/coordinator.py

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import timedelta
66

77
from homeassistant.config_entries import ConfigEntry
8+
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
89
from homeassistant.core import HomeAssistant
910
from homeassistant.helpers.update_coordinator import (
1011
DataUpdateCoordinator,
@@ -40,6 +41,13 @@ def __init__(
4041
update_interval=timedelta(minutes=30),
4142
)
4243

44+
async def disconnect() -> None:
45+
"""Close ClientSession."""
46+
await self.api.disconnect()
47+
48+
# Disconnect the ClientSession on stop
49+
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, disconnect)
50+
4351
async def _async_update_data(self):
4452
"""Update data via library."""
4553
try:

custom_components/managemyhealth/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"documentation": "https://github.com/codyc1515/ha-managemyhealth",
99
"iot_class": "cloud_polling",
1010
"issue_tracker": "https://github.com/codyc1515/ha-managemyhealth/issues",
11-
"version": "2.1.0"
11+
"version": "2.2.0"
1212
}

0 commit comments

Comments
 (0)