File tree 3 files changed +25
-4
lines changed
custom_components/managemyhealth
3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -228,12 +228,12 @@ async def _api_wrapper(
228
228
headers = headers ,
229
229
)
230
230
if response .status in (400 , 401 , 403 ):
231
- raise MmhApiAuthenticationError (
232
- "Invalid credentials" ,
233
- )
231
+ raise MmhApiAuthenticationError ()
234
232
response .raise_for_status ()
235
233
return await response .json ()
236
234
235
+ except MmhApiAuthenticationError as exception :
236
+ raise MmhApiAuthenticationError ("Invalid credentials" ) from exception
237
237
except asyncio .TimeoutError as exception :
238
238
raise MmhApiCommunicationError (
239
239
"Timeout error fetching information: %s" , exception
@@ -246,3 +246,16 @@ async def _api_wrapper(
246
246
raise MmhApiError (
247
247
"Something really wrong happened!: %s" , exception
248
248
) 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 ()
Original file line number Diff line number Diff line change 5
5
from datetime import timedelta
6
6
7
7
from homeassistant .config_entries import ConfigEntry
8
+ from homeassistant .const import EVENT_HOMEASSISTANT_STOP
8
9
from homeassistant .core import HomeAssistant
9
10
from homeassistant .helpers .update_coordinator import (
10
11
DataUpdateCoordinator ,
@@ -40,6 +41,13 @@ def __init__(
40
41
update_interval = timedelta (minutes = 30 ),
41
42
)
42
43
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
+
43
51
async def _async_update_data (self ):
44
52
"""Update data via library."""
45
53
try :
Original file line number Diff line number Diff line change 8
8
"documentation" : " https://github.com/codyc1515/ha-managemyhealth" ,
9
9
"iot_class" : " cloud_polling" ,
10
10
"issue_tracker" : " https://github.com/codyc1515/ha-managemyhealth/issues" ,
11
- "version" : " 2.1 .0"
11
+ "version" : " 2.2 .0"
12
12
}
You can’t perform that action at this time.
0 commit comments