1
mirror of https://github.com/home-assistant/core synced 2024-09-09 12:51:22 +02:00

Update aiohttp handling with upc connect (#6108)

* Refactor upc connect

* fix aiohttp cleanup

* fix test

* fix tests

* allow status code for login
This commit is contained in:
Pascal Vizeli 2017-02-19 23:25:45 +01:00 committed by GitHub
parent 58f813b518
commit 5d8e219448
2 changed files with 13 additions and 21 deletions

View File

@ -17,7 +17,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.aiohttp_client import async_get_clientsession
_LOGGER = logging.getLogger(__name__)
@ -63,19 +63,13 @@ class UPCDeviceScanner(DeviceScanner):
"Chrome/47.0.2526.106 Safari/537.36")
}
self.websession = async_create_clientsession(
hass, auto_cleanup=False,
cookie_jar=aiohttp.CookieJar(unsafe=True, loop=hass.loop)
)
self.websession = async_get_clientsession(hass)
@asyncio.coroutine
def async_logout(event):
"""Logout from upc connect box."""
try:
yield from self._async_ws_function(CMD_LOGOUT)
self.token = None
finally:
self.websession.detach()
yield from self._async_ws_function(CMD_LOGOUT)
self.token = None
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, async_logout)
@ -92,8 +86,7 @@ class UPCDeviceScanner(DeviceScanner):
raw = yield from self._async_ws_function(CMD_DEVICES)
try:
xml_root = yield from self.hass.loop.run_in_executor(
None, ET.fromstring, raw)
xml_root = ET.fromstring(raw)
return [mac.text for mac in xml_root.iter('MACAddr')]
except (ET.ParseError, TypeError):
_LOGGER.warning("Can't read device from %s", self.host)
@ -111,7 +104,6 @@ class UPCDeviceScanner(DeviceScanner):
response = None
try:
# get first token
self.websession.cookie_jar.clear()
with async_timeout.timeout(10, loop=self.hass.loop):
response = yield from self.websession.get(
"http://{}/common_page/login.html".format(self.host)
@ -150,27 +142,26 @@ class UPCDeviceScanner(DeviceScanner):
if additional_form:
form_data.update(additional_form)
redirects = True if function != CMD_DEVICES else False
response = None
try:
with async_timeout.timeout(10, loop=self.hass.loop):
response = yield from self.websession.post(
"http://{}/xml/getter.xml".format(self.host),
data=form_data,
headers=self.headers
headers=self.headers,
allow_redirects=redirects
)
# error on UPC webservice
# error?
if response.status != 200:
_LOGGER.warning(
"Error %d on %s.", response.status, function)
_LOGGER.warning("Receive http code %d", response.status)
self.token = None
return
# load data, store token for next request
raw = yield from response.text()
self.token = response.cookies['sessionToken'].value
return raw
return (yield from response.text())
except (asyncio.TimeoutError, aiohttp.errors.ClientError):
_LOGGER.error("Error on %s", function)

View File

@ -73,8 +73,9 @@ class AiohttpClientMocker:
self.mock_calls.clear()
@asyncio.coroutine
# pylint: disable=unused-variable
def match_request(self, method, url, *, data=None, auth=None, params=None,
headers=None): # pylint: disable=unused-variable
headers=None, allow_redirects=None):
"""Match a request against pre-registered requests."""
for response in self._mocks:
if response.match_request(method, url, params):