Skip to content

Commit 774b831

Browse files
authored
Extend error handling (#9)
1 parent a1c0975 commit 774b831

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

connect_box/__init__.py

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"""A Python Client to get data from UPC Connect Boxes."""
22
import asyncio
3-
from ipaddress import IPv4Address, IPv6Address, ip_address as convert_ip
43
import logging
5-
from typing import Dict, List, Optional, Union
4+
from typing import Dict, List, Optional
65

76
import aiohttp
87
from aiohttp.hdrs import REFERER, USER_AGENT
9-
import attr
108
import defusedxml.ElementTree as element_tree
119

10+
from .data import Device, DownstreamChannel, UpstreamChannel
1211
from . import exceptions
1312

1413
_LOGGER = logging.getLogger(__name__)
@@ -22,49 +21,6 @@
2221
CMD_UPSTREAM = 11
2322

2423

25-
@attr.s
26-
class Device:
27-
"""A single device."""
28-
29-
mac: str = attr.ib()
30-
hostname: str = attr.ib(cmp=False)
31-
ip: Union[IPv4Address, IPv6Address] = attr.ib(cmp=False, convert=convert_ip)
32-
33-
34-
@attr.s
35-
class DownstreamChannel:
36-
"""A locked downstream channel."""
37-
38-
frequency: int = attr.ib()
39-
powerLevel: int = attr.ib()
40-
modulation: str = attr.ib()
41-
id: str = attr.ib()
42-
snr: float = attr.ib()
43-
preRs: int = attr.ib()
44-
postRs: int = attr.ib()
45-
qamLocked: bool = attr.ib()
46-
fecLocked: bool = attr.ib()
47-
mpegLocked: bool = attr.ib()
48-
49-
50-
@attr.s
51-
class UpstreamChannel:
52-
"""A locked upstream channel."""
53-
54-
frequency: int = attr.ib()
55-
powerLevel: int = attr.ib()
56-
symbolRate: str = attr.ib()
57-
id: str = attr.ib()
58-
modulation: str = attr.ib()
59-
type: str = attr.ib()
60-
t1Timeouts: int = attr.ib()
61-
t2Timeouts: int = attr.ib()
62-
t3Timeouts: int = attr.ib()
63-
t4Timeouts: int = attr.ib()
64-
channelType: str = attr.ib()
65-
messageType: int = attr.ib()
66-
67-
6824
class ConnectBox:
6925
"""A class for handling the data retrieval from an UPC Connect Box ."""
7026

@@ -197,8 +153,8 @@ async def async_initialize_token(self) -> None:
197153
await response.text()
198154
self.token = response.cookies["sessionToken"].value
199155

200-
except (asyncio.TimeoutError, aiohttp.ClientError):
201-
_LOGGER.error("Can not load login page from %s", self.host)
156+
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
157+
_LOGGER.error("Can not load login page from %s: %s", self.host, err)
202158
raise exceptions.ConnectBoxConnectionError()
203159

204160
await self._async_initialize_token_with_password(CMD_LOGIN)
@@ -213,18 +169,16 @@ async def _async_initialize_token_with_password(self, function: int) -> None:
213169
allow_redirects=False,
214170
timeout=10,
215171
) as response:
216-
217172
await response.text()
218173

219174
if response.status != 200:
220-
_LOGGER.warning("Receive http code %d", response.status)
175+
_LOGGER.warning("Login error with code %d", response.status)
221176
self.token = None
222177
raise exceptions.ConnectBoxLoginError()
223-
224178
self.token = response.cookies["sessionToken"].value
225179

226-
except (asyncio.TimeoutError, aiohttp.ClientError):
227-
_LOGGER.error("Can not login to %s", self.host)
180+
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
181+
_LOGGER.error("Can not login to %s: %s", self.host, err)
228182
raise exceptions.ConnectBoxConnectionError()
229183

230184
async def _async_ws_function(self, function: int) -> Optional[str]:
@@ -242,16 +196,16 @@ async def _async_ws_function(self, function: int) -> Optional[str]:
242196

243197
# If there is an error
244198
if response.status != 200:
245-
_LOGGER.warning("Receive http code %d", response.status)
199+
_LOGGER.debug("Receive http code %d", response.status)
246200
self.token = None
247201
raise exceptions.ConnectBoxError()
248202

249203
# Load data, store token for next request
250204
self.token = response.cookies["sessionToken"].value
251205
return await response.text()
252206

253-
except (asyncio.TimeoutError, aiohttp.ClientError):
254-
_LOGGER.error("Error on %s", function)
207+
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
208+
_LOGGER.error("Error received on %s: %s", function, err)
255209
self.token = None
256210

257211
raise exceptions.ConnectBoxConnectionError()

connect_box/data.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Handle Data attributes."""
2+
from ipaddress import IPv4Address, IPv6Address, ip_address as convert_ip
3+
from typing import Union
4+
5+
import attr
6+
7+
8+
@attr.s
9+
class Device:
10+
"""A single device."""
11+
12+
mac: str = attr.ib()
13+
hostname: str = attr.ib(cmp=False)
14+
ip: Union[IPv4Address, IPv6Address] = attr.ib(cmp=False, convert=convert_ip)
15+
16+
17+
@attr.s
18+
class DownstreamChannel:
19+
"""A locked downstream channel."""
20+
21+
frequency: int = attr.ib()
22+
powerLevel: int = attr.ib()
23+
modulation: str = attr.ib()
24+
id: str = attr.ib()
25+
snr: float = attr.ib()
26+
preRs: int = attr.ib()
27+
postRs: int = attr.ib()
28+
qamLocked: bool = attr.ib()
29+
fecLocked: bool = attr.ib()
30+
mpegLocked: bool = attr.ib()
31+
32+
33+
@attr.s
34+
class UpstreamChannel:
35+
"""A locked upstream channel."""
36+
37+
frequency: int = attr.ib()
38+
powerLevel: int = attr.ib()
39+
symbolRate: str = attr.ib()
40+
id: str = attr.ib()
41+
modulation: str = attr.ib()
42+
type: str = attr.ib()
43+
t1Timeouts: int = attr.ib()
44+
t2Timeouts: int = attr.ib()
45+
t3Timeouts: int = attr.ib()
46+
t4Timeouts: int = attr.ib()
47+
channelType: str = attr.ib()
48+
messageType: int = attr.ib()

0 commit comments

Comments
 (0)