Skip to content

Commit

Permalink
Improve error handling (home-assistant#24204)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored and alandtse committed Aug 6, 2019
1 parent 681d36a commit 0269dd9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/ssdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ async def _fetch_description(self, xml_location):
if not xml:
resp = await session.get(xml_location, timeout=5)
xml = await resp.text()
except aiohttp.ClientError as err:
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
_LOGGER.debug("Error fetching %s: %s", xml_location, err)
return None
return {}

try:
tree = ElementTree.fromstring(xml)
except ElementTree.ParseError as err:
_LOGGER.debug("Error parsing %s: %s", xml_location, err)
return None
return {}

return util.etree_to_dict(tree).get('root', {}).get('device', {})

Expand Down
29 changes: 29 additions & 0 deletions tests/components/ssdp/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Test the SSDP integration."""
import asyncio
from unittest.mock import patch, Mock

import aiohttp
import pytest

from homeassistant.generated import ssdp as gn_ssdp
from homeassistant.components import ssdp

Expand Down Expand Up @@ -76,3 +80,28 @@ async def test_scan_match_device_type(hass, aioclient_mock):
assert len(mock_init.mock_calls) == 1
assert mock_init.mock_calls[0][1][0] == 'mock-domain'
assert mock_init.mock_calls[0][2]['context'] == {'source': 'ssdp'}


@pytest.mark.parametrize('exc', [asyncio.TimeoutError, aiohttp.ClientError])
async def test_scan_description_fetch_fail(hass, aioclient_mock, exc):
"""Test failing to fetch description."""
aioclient_mock.get('http://1.1.1.1', exc=exc)
scanner = ssdp.Scanner(hass)

with patch('netdisco.ssdp.scan', return_value=[
Mock(st="mock-st", location='http://1.1.1.1')
]):
await scanner.async_scan(None)


async def test_scan_description_parse_fail(hass, aioclient_mock):
"""Test invalid XML."""
aioclient_mock.get('http://1.1.1.1', text="""
<root>INVALIDXML
""")
scanner = ssdp.Scanner(hass)

with patch('netdisco.ssdp.scan', return_value=[
Mock(st="mock-st", location='http://1.1.1.1')
]):
await scanner.async_scan(None)

0 comments on commit 0269dd9

Please sign in to comment.