Skip to content

Commit

Permalink
Extend error handling (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli committed Sep 17, 2019
1 parent a1c0975 commit 774b831
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 56 deletions.
66 changes: 10 additions & 56 deletions connect_box/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""A Python Client to get data from UPC Connect Boxes."""
import asyncio
from ipaddress import IPv4Address, IPv6Address, ip_address as convert_ip
import logging
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional

import aiohttp
from aiohttp.hdrs import REFERER, USER_AGENT
import attr
import defusedxml.ElementTree as element_tree

from .data import Device, DownstreamChannel, UpstreamChannel
from . import exceptions

_LOGGER = logging.getLogger(__name__)
Expand All @@ -22,49 +21,6 @@
CMD_UPSTREAM = 11


@attr.s
class Device:
"""A single device."""

mac: str = attr.ib()
hostname: str = attr.ib(cmp=False)
ip: Union[IPv4Address, IPv6Address] = attr.ib(cmp=False, convert=convert_ip)


@attr.s
class DownstreamChannel:
"""A locked downstream channel."""

frequency: int = attr.ib()
powerLevel: int = attr.ib()
modulation: str = attr.ib()
id: str = attr.ib()
snr: float = attr.ib()
preRs: int = attr.ib()
postRs: int = attr.ib()
qamLocked: bool = attr.ib()
fecLocked: bool = attr.ib()
mpegLocked: bool = attr.ib()


@attr.s
class UpstreamChannel:
"""A locked upstream channel."""

frequency: int = attr.ib()
powerLevel: int = attr.ib()
symbolRate: str = attr.ib()
id: str = attr.ib()
modulation: str = attr.ib()
type: str = attr.ib()
t1Timeouts: int = attr.ib()
t2Timeouts: int = attr.ib()
t3Timeouts: int = attr.ib()
t4Timeouts: int = attr.ib()
channelType: str = attr.ib()
messageType: int = attr.ib()


class ConnectBox:
"""A class for handling the data retrieval from an UPC Connect Box ."""

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

except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Can not load login page from %s", self.host)
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
_LOGGER.error("Can not load login page from %s: %s", self.host, err)
raise exceptions.ConnectBoxConnectionError()

await self._async_initialize_token_with_password(CMD_LOGIN)
Expand All @@ -213,18 +169,16 @@ async def _async_initialize_token_with_password(self, function: int) -> None:
allow_redirects=False,
timeout=10,
) as response:

await response.text()

if response.status != 200:
_LOGGER.warning("Receive http code %d", response.status)
_LOGGER.warning("Login error with code %d", response.status)
self.token = None
raise exceptions.ConnectBoxLoginError()

self.token = response.cookies["sessionToken"].value

except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Can not login to %s", self.host)
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
_LOGGER.error("Can not login to %s: %s", self.host, err)
raise exceptions.ConnectBoxConnectionError()

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

# If there is an error
if response.status != 200:
_LOGGER.warning("Receive http code %d", response.status)
_LOGGER.debug("Receive http code %d", response.status)
self.token = None
raise exceptions.ConnectBoxError()

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

except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Error on %s", function)
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
_LOGGER.error("Error received on %s: %s", function, err)
self.token = None

raise exceptions.ConnectBoxConnectionError()
48 changes: 48 additions & 0 deletions connect_box/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Handle Data attributes."""
from ipaddress import IPv4Address, IPv6Address, ip_address as convert_ip
from typing import Union

import attr


@attr.s
class Device:
"""A single device."""

mac: str = attr.ib()
hostname: str = attr.ib(cmp=False)
ip: Union[IPv4Address, IPv6Address] = attr.ib(cmp=False, convert=convert_ip)


@attr.s
class DownstreamChannel:
"""A locked downstream channel."""

frequency: int = attr.ib()
powerLevel: int = attr.ib()
modulation: str = attr.ib()
id: str = attr.ib()
snr: float = attr.ib()
preRs: int = attr.ib()
postRs: int = attr.ib()
qamLocked: bool = attr.ib()
fecLocked: bool = attr.ib()
mpegLocked: bool = attr.ib()


@attr.s
class UpstreamChannel:
"""A locked upstream channel."""

frequency: int = attr.ib()
powerLevel: int = attr.ib()
symbolRate: str = attr.ib()
id: str = attr.ib()
modulation: str = attr.ib()
type: str = attr.ib()
t1Timeouts: int = attr.ib()
t2Timeouts: int = attr.ib()
t3Timeouts: int = attr.ib()
t4Timeouts: int = attr.ib()
channelType: str = attr.ib()
messageType: int = attr.ib()

0 comments on commit 774b831

Please sign in to comment.