55import httpx
66import json
77
8+ from . import exceptions
9+
810from .constants import (
911 BASE_URL ,
1012 ENDPOINT_DEVICES ,
@@ -52,11 +54,12 @@ async def connect(self):
5254 "username" : self .username ,
5355 "password" : self .password ,
5456 }
55-
56- async with httpx .AsyncClient () as client :
57- response = await client .post (str (url ), headers = headers , data = data )
58-
59- _LOGGER .debug ("Status code:" , response .status_code )
57+ try :
58+ async with httpx .AsyncClient () as client :
59+ response = await client .post (str (url ), headers = headers , data = data )
60+ _LOGGER .debug ("Status code:" , response .status_code )
61+ except httpx .ConnectError :
62+ raise exceptions .ElmaxConnectionError (f"Connection to { BASE_URL } failed" )
6063
6164 try :
6265 response_data = response .json ()
@@ -80,7 +83,7 @@ def is_authenticated(self):
8083 async def get_devices (self ):
8184 """Retrieve the devices."""
8285 if self .authorized is False :
83- self .connect ()
86+ await self .connect ()
8487
8588 url = URL (BASE_URL ) / ENDPOINT_DEVICES
8689 headers ["Authorization" ] = self .authorization
@@ -109,7 +112,7 @@ async def list_devices(self):
109112 async def get_units (self , device_id , pin ):
110113 """List all units of a devices."""
111114 if self .authorized is False :
112- self .connect ()
115+ await self .connect ()
113116
114117 url = URL (BASE_URL ) / ENDPOINT_DISCOVERY / device_id / str (pin )
115118
0 commit comments