From 393c7f2cf18880364d6c28c7c393ac3a16e0433d Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 25 Jan 2017 18:20:31 +0100 Subject: [PATCH] [device.upc_connect] Discount on STOP. (#5553) * [device.upc_connect] Discount on STOP. * close session it self * Update upc_connect.py --- .../components/device_tracker/upc_connect.py | 19 ++++++++++++++++++- tests/test_util/aiohttp.py | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/upc_connect.py b/homeassistant/components/device_tracker/upc_connect.py index a042985c08d..5de412f05bc 100644 --- a/homeassistant/components/device_tracker/upc_connect.py +++ b/homeassistant/components/device_tracker/upc_connect.py @@ -12,6 +12,7 @@ import aiohttp import async_timeout import voluptuous as vol +from homeassistant.const import EVENT_HOMEASSISTANT_STOP import homeassistant.helpers.config_validation as cv from homeassistant.components.device_tracker import ( DOMAIN, PLATFORM_SCHEMA, DeviceScanner) @@ -29,6 +30,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ }) CMD_LOGIN = 15 +CMD_LOGOUT = 16 CMD_DEVICES = 123 @@ -62,7 +64,21 @@ class UPCDeviceScanner(DeviceScanner): } self.websession = async_create_clientsession( - hass, cookie_jar=aiohttp.CookieJar(unsafe=True, loop=hass.loop)) + hass, auto_cleanup=False, + cookie_jar=aiohttp.CookieJar(unsafe=True, loop=hass.loop) + ) + + @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() + + hass.buss.async_listen_once( + EVENT_HOMEASSISTANT_STOP, async_logout) @asyncio.coroutine def async_scan_devices(self): @@ -94,6 +110,7 @@ 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) diff --git a/tests/test_util/aiohttp.py b/tests/test_util/aiohttp.py index afe2f626de7..7f46f11166d 100644 --- a/tests/test_util/aiohttp.py +++ b/tests/test_util/aiohttp.py @@ -17,6 +17,8 @@ class AiohttpClientMocker: self._cookies = {} self.mock_calls = [] + self.cookie_jar = mock.MagicMock() + def request(self, method, url, *, auth=None, status=200,