Make upc more robust (#5404)
* Make upc more robust * update unittest * add test for parse errorpull/5413/head
parent
298c1654f8
commit
cfc936761b
|
@ -74,12 +74,15 @@ class UPCDeviceScanner(DeviceScanner):
|
|||
return []
|
||||
|
||||
raw = yield from self._async_ws_function(CMD_DEVICES)
|
||||
if raw is None:
|
||||
_LOGGER.warning("Can't read device from %s", self.host)
|
||||
return
|
||||
|
||||
xml_root = ET.fromstring(raw)
|
||||
return [mac.text for mac in xml_root.iter('MACAddr')]
|
||||
try:
|
||||
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)
|
||||
self.token = None
|
||||
|
||||
return []
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_get_device_name(self, device):
|
||||
|
@ -107,7 +110,7 @@ class UPCDeviceScanner(DeviceScanner):
|
|||
})
|
||||
|
||||
# successfull?
|
||||
if data.find("successful") != -1:
|
||||
if data is not None:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
|
|
@ -230,6 +230,50 @@ class TestUPCConnect(object):
|
|||
cookies={'sessionToken': '1235678'}
|
||||
)
|
||||
|
||||
scanner.token = None
|
||||
mac_list = run_coroutine_threadsafe(
|
||||
scanner.async_scan_devices(), self.hass.loop).result()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
assert aioclient_mock.mock_calls[1][2]['fun'] == 15
|
||||
assert mac_list == ['30:D3:2D:0:69:21', '5C:AA:FD:25:32:02',
|
||||
'70:EE:50:27:A1:38']
|
||||
|
||||
def test_scan_devices_without_session_wrong_re(self, aioclient_mock):
|
||||
"""Setup a upc platform and scan device with no token and wrong."""
|
||||
aioclient_mock.get(
|
||||
"http://{}/common_page/login.html".format(self.host),
|
||||
cookies={'sessionToken': '654321'}
|
||||
)
|
||||
aioclient_mock.post(
|
||||
"http://{}/xml/getter.xml".format(self.host),
|
||||
content=b'successful',
|
||||
cookies={'sessionToken': '654321'}
|
||||
)
|
||||
|
||||
scanner = run_coroutine_threadsafe(platform.async_get_scanner(
|
||||
self.hass, {DOMAIN: {
|
||||
CONF_PLATFORM: 'upc_connect',
|
||||
CONF_HOST: self.host,
|
||||
CONF_PASSWORD: '123456'
|
||||
}}
|
||||
), self.hass.loop).result()
|
||||
|
||||
assert aioclient_mock.mock_calls[1][2]['Password'] == '123456'
|
||||
assert aioclient_mock.mock_calls[1][2]['fun'] == 15
|
||||
assert aioclient_mock.mock_calls[1][2]['token'] == '654321'
|
||||
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.get(
|
||||
"http://{}/common_page/login.html".format(self.host),
|
||||
cookies={'sessionToken': '654321'}
|
||||
)
|
||||
aioclient_mock.post(
|
||||
"http://{}/xml/getter.xml".format(self.host),
|
||||
status=400,
|
||||
cookies={'sessionToken': '1235678'}
|
||||
)
|
||||
|
||||
scanner.token = None
|
||||
mac_list = run_coroutine_threadsafe(
|
||||
scanner.async_scan_devices(), self.hass.loop).result()
|
||||
|
@ -237,3 +281,42 @@ class TestUPCConnect(object):
|
|||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[1][2]['fun'] == 15
|
||||
assert mac_list == []
|
||||
|
||||
def test_scan_devices_parse_error(self, aioclient_mock):
|
||||
"""Setup a upc platform and scan device with parse error."""
|
||||
aioclient_mock.get(
|
||||
"http://{}/common_page/login.html".format(self.host),
|
||||
cookies={'sessionToken': '654321'}
|
||||
)
|
||||
aioclient_mock.post(
|
||||
"http://{}/xml/getter.xml".format(self.host),
|
||||
content=b'successful',
|
||||
cookies={'sessionToken': '654321'}
|
||||
)
|
||||
|
||||
scanner = run_coroutine_threadsafe(platform.async_get_scanner(
|
||||
self.hass, {DOMAIN: {
|
||||
CONF_PLATFORM: 'upc_connect',
|
||||
CONF_HOST: self.host,
|
||||
CONF_PASSWORD: '123456'
|
||||
}}
|
||||
), self.hass.loop).result()
|
||||
|
||||
assert aioclient_mock.mock_calls[1][2]['Password'] == '123456'
|
||||
assert aioclient_mock.mock_calls[1][2]['fun'] == 15
|
||||
assert aioclient_mock.mock_calls[1][2]['token'] == '654321'
|
||||
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.post(
|
||||
"http://{}/xml/getter.xml".format(self.host),
|
||||
text="Blablebla blabalble",
|
||||
cookies={'sessionToken': '1235678'}
|
||||
)
|
||||
|
||||
mac_list = run_coroutine_threadsafe(
|
||||
scanner.async_scan_devices(), self.hass.loop).result()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
assert aioclient_mock.mock_calls[0][2]['fun'] == 123
|
||||
assert scanner.token is None
|
||||
assert mac_list == []
|
||||
|
|
Loading…
Reference in New Issue