Skip to content

Commit 9025b58

Browse files
committed
Add exceptions
1 parent 953f85d commit 9025b58

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

elmax/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import httpx
66
import json
77

8+
from . import exceptions
9+
810
from .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

elmax/exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Exceptions for the Elmax Cloud services client."""
2+
3+
4+
class ElmaxError(Exception):
5+
"""General ElmxError exception occurred."""
6+
7+
pass
8+
9+
10+
class ElmaxConnectionError(ElmaxError):
11+
"""When a connection error is encountered."""
12+
13+
pass
14+
15+
16+
class ElmaxNoDataAvailable(ElmaxError):
17+
"""When no data is available."""
18+
19+
pass

0 commit comments

Comments
 (0)