From bd0a8a7b705a3d5b1e026dd5c2199a7be2ace884 Mon Sep 17 00:00:00 2001 From: Cody Cooper <50791984+codyc1515@users.noreply.github.com> Date: Sun, 4 Feb 2024 08:11:33 +1300 Subject: [PATCH] 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 --- custom_components/managemyhealth/api.py | 19 ++++++++++++++++--- .../managemyhealth/coordinator.py | 8 ++++++++ .../managemyhealth/manifest.json | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/custom_components/managemyhealth/api.py b/custom_components/managemyhealth/api.py index 1910141..6c94b60 100644 --- a/custom_components/managemyhealth/api.py +++ b/custom_components/managemyhealth/api.py @@ -228,12 +228,12 @@ async def _api_wrapper( headers=headers, ) if response.status in (400, 401, 403): - raise MmhApiAuthenticationError( - "Invalid credentials", - ) + raise MmhApiAuthenticationError() response.raise_for_status() return await response.json() + except MmhApiAuthenticationError as exception: + raise MmhApiAuthenticationError("Invalid credentials") from exception except asyncio.TimeoutError as exception: raise MmhApiCommunicationError( "Timeout error fetching information: %s", exception @@ -246,3 +246,16 @@ async def _api_wrapper( raise MmhApiError( "Something really wrong happened!: %s", exception ) from exception + + async def disconnect(self) -> None: + """Disconnect from the client.""" + _LOGGER.debug("Invoked close manually") + await self.__aexit__() + + async def __aexit__(self, *excinfo): + """Destroy the device and http sessions.""" + _LOGGER.debug("Invoked close automatically") + if not self._session: + return + + await self._session.close() diff --git a/custom_components/managemyhealth/coordinator.py b/custom_components/managemyhealth/coordinator.py index b0647f9..b5d4411 100644 --- a/custom_components/managemyhealth/coordinator.py +++ b/custom_components/managemyhealth/coordinator.py @@ -5,6 +5,7 @@ from datetime import timedelta from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, @@ -40,6 +41,13 @@ def __init__( update_interval=timedelta(minutes=30), ) + async def disconnect() -> None: + """Close ClientSession.""" + await self.api.disconnect() + + # Disconnect the ClientSession on stop + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, disconnect) + async def _async_update_data(self): """Update data via library.""" try: diff --git a/custom_components/managemyhealth/manifest.json b/custom_components/managemyhealth/manifest.json index d2791de..deda2e2 100644 --- a/custom_components/managemyhealth/manifest.json +++ b/custom_components/managemyhealth/manifest.json @@ -8,5 +8,5 @@ "documentation": "https://github.com/codyc1515/ha-managemyhealth", "iot_class": "cloud_polling", "issue_tracker": "https://github.com/codyc1515/ha-managemyhealth/issues", - "version": "2.1.0" + "version": "2.2.0" } \ No newline at end of file