11"""A Python Client to get data from UPC Connect Boxes."""
22import asyncio
3- from ipaddress import IPv4Address , IPv6Address , ip_address as convert_ip
43import logging
5- from typing import Dict , List , Optional , Union
4+ from typing import Dict , List , Optional
65
76import aiohttp
87from aiohttp .hdrs import REFERER , USER_AGENT
9- import attr
108import defusedxml .ElementTree as element_tree
119
10+ from .data import Device , DownstreamChannel , UpstreamChannel
1211from . import exceptions
1312
1413_LOGGER = logging .getLogger (__name__ )
2221CMD_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-
6824class 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 ()
0 commit comments