Catch exceptions raised on unknown devices during discovery (#240)

This commit is contained in:
Teemu R 2021-10-29 02:44:51 +02:00 committed by GitHub
parent 8a4068c623
commit 9cda529329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -73,7 +73,12 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
info = json.loads(TPLinkSmartHomeProtocol.decrypt(data)) info = json.loads(TPLinkSmartHomeProtocol.decrypt(data))
_LOGGER.debug("[DISCOVERY] %s << %s", ip, info) _LOGGER.debug("[DISCOVERY] %s << %s", ip, info)
device_class = Discover._get_device_class(info) try:
device_class = Discover._get_device_class(info)
except SmartDeviceException as ex:
_LOGGER.debug("Unable to find device type from %s: %s", info, ex)
return
device = device_class(ip) device = device_class(ip)
device.update_from_discover_info(info) device.update_from_discover_info(info)

View File

@ -85,9 +85,9 @@ async def test_discover_send(mocker):
proto = _DiscoverProtocol() proto = _DiscoverProtocol()
assert proto.discovery_packets == 3 assert proto.discovery_packets == 3
assert proto.target == ("255.255.255.255", 9999) assert proto.target == ("255.255.255.255", 9999)
sendto = mocker.patch.object(proto, "transport") transport = mocker.patch.object(proto, "transport")
proto.do_discover() proto.do_discover()
assert sendto.sendto.call_count == proto.discovery_packets assert transport.sendto.call_count == proto.discovery_packets
async def test_discover_datagram_received(mocker, discovery_data): async def test_discover_datagram_received(mocker, discovery_data):
@ -105,3 +105,15 @@ async def test_discover_datagram_received(mocker, discovery_data):
dev = proto.discovered_devices[addr] dev = proto.discovered_devices[addr]
assert issubclass(dev.__class__, SmartDevice) assert issubclass(dev.__class__, SmartDevice)
assert dev.host == addr assert dev.host == addr
@pytest.mark.parametrize("msg, data", INVALIDS)
async def test_discover_invalid_responses(msg, data, mocker):
"""Verify that we don't crash whole discovery if some devices in the network are sending unexpected data."""
proto = _DiscoverProtocol()
mocker.patch("json.loads", return_value=data)
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "encrypt")
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "decrypt")
proto.datagram_received(data, ("127.0.0.1", 1234))
assert len(proto.discovered_devices) == 0