Fix discovery cli to print devices not printed during discovery timeout (#670)

* Fix discovery cli to print devices not printed during discovery

* Fix tests

* Fix print exceptions not being propagated

* Fix tests

* Reduce test discover_send time

* Simplify wait logic

* Add tests

* Remove sleep loop and make auth failed a list
This commit is contained in:
Steven B
2024-02-05 17:53:09 +00:00
committed by GitHub
parent 0d119e63d0
commit 215b8d4e4f
5 changed files with 142 additions and 63 deletions

View File

@@ -4,7 +4,7 @@ import binascii
import ipaddress
import logging
import socket
from typing import Awaitable, Callable, Dict, Optional, Set, Type, cast
from typing import Awaitable, Callable, Dict, List, Optional, Set, Type, cast
# When support for cpython older than 3.11 is dropped
# async_timeout can be replaced with asyncio.timeout
@@ -46,6 +46,8 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
This is internal class, use :func:`Discover.discover`: instead.
"""
DISCOVERY_START_TIMEOUT = 1
discovered_devices: DeviceDict
def __init__(
@@ -60,7 +62,6 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
Callable[[UnsupportedDeviceException], Awaitable[None]]
] = None,
port: Optional[int] = None,
discovered_event: Optional[asyncio.Event] = None,
credentials: Optional[Credentials] = None,
timeout: Optional[int] = None,
) -> None:
@@ -79,12 +80,32 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
self.unsupported_device_exceptions: Dict = {}
self.invalid_device_exceptions: Dict = {}
self.on_unsupported = on_unsupported
self.discovered_event = discovered_event
self.credentials = credentials
self.timeout = timeout
self.discovery_timeout = discovery_timeout
self.seen_hosts: Set[str] = set()
self.discover_task: Optional[asyncio.Task] = None
self.callback_tasks: List[asyncio.Task] = []
self.target_discovered: bool = False
self._started_event = asyncio.Event()
def _run_callback_task(self, coro):
task = asyncio.create_task(coro)
self.callback_tasks.append(task)
async def wait_for_discovery_to_complete(self):
"""Wait for the discovery task to complete."""
# Give some time for connection_made event to be received
async with asyncio_timeout(self.DISCOVERY_START_TIMEOUT):
await self._started_event.wait()
try:
await self.discover_task
except asyncio.CancelledError:
# if target_discovered then cancel was called internally
if not self.target_discovered:
raise
# Wait for any pending callbacks to complete
await asyncio.gather(*self.callback_tasks)
def connection_made(self, transport) -> None:
"""Set socket options for broadcasting."""
@@ -103,6 +124,7 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
)
self.discover_task = asyncio.create_task(self.do_discover())
self._started_event.set()
async def do_discover(self) -> None:
"""Send number of discovery datagrams."""
@@ -110,13 +132,12 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
_LOGGER.debug("[DISCOVERY] %s >> %s", self.target, Discover.DISCOVERY_QUERY)
encrypted_req = XorEncryption.encrypt(req)
sleep_between_packets = self.discovery_timeout / self.discovery_packets
for i in range(self.discovery_packets):
for _ in range(self.discovery_packets):
if self.target in self.seen_hosts: # Stop sending for discover_single
break
self.transport.sendto(encrypted_req[4:], self.target_1) # type: ignore
self.transport.sendto(Discover.DISCOVERY_QUERY_2, self.target_2) # type: ignore
if i < self.discovery_packets - 1:
await asyncio.sleep(sleep_between_packets)
await asyncio.sleep(sleep_between_packets)
def datagram_received(self, data, addr) -> None:
"""Handle discovery responses."""
@@ -145,7 +166,7 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
_LOGGER.debug("Unsupported device found at %s << %s", ip, udex)
self.unsupported_device_exceptions[ip] = udex
if self.on_unsupported is not None:
asyncio.ensure_future(self.on_unsupported(udex))
self._run_callback_task(self.on_unsupported(udex))
self._handle_discovered_event()
return
except SmartDeviceException as ex:
@@ -157,16 +178,16 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
self.discovered_devices[ip] = device
if self.on_discovered is not None:
asyncio.ensure_future(self.on_discovered(device))
self._run_callback_task(self.on_discovered(device))
self._handle_discovered_event()
def _handle_discovered_event(self):
"""If discovered_event is available set it and cancel discover_task."""
if self.discovered_event is not None:
"""If target is in seen_hosts cancel discover_task."""
if self.target in self.seen_hosts:
self.target_discovered = True
if self.discover_task:
self.discover_task.cancel()
self.discovered_event.set()
def error_received(self, ex):
"""Handle asyncio.Protocol errors."""
@@ -289,7 +310,11 @@ class Discover:
try:
_LOGGER.debug("Waiting %s seconds for responses...", discovery_timeout)
await asyncio.sleep(discovery_timeout)
await protocol.wait_for_discovery_to_complete()
except SmartDeviceException as ex:
for device in protocol.discovered_devices.values():
await device.protocol.close()
raise ex
finally:
transport.close()
@@ -322,7 +347,6 @@ class Discover:
:return: Object for querying/controlling found device.
"""
loop = asyncio.get_event_loop()
event = asyncio.Event()
try:
ipaddress.ip_address(host)
@@ -352,7 +376,6 @@ class Discover:
lambda: _DiscoverProtocol(
target=ip,
port=port,
discovered_event=event,
credentials=credentials,
timeout=timeout,
discovery_timeout=discovery_timeout,
@@ -365,13 +388,7 @@ class Discover:
_LOGGER.debug(
"Waiting a total of %s seconds for responses...", discovery_timeout
)
async with asyncio_timeout(discovery_timeout):
await event.wait()
except asyncio.TimeoutError as ex:
raise TimeoutException(
f"Timed out getting discovery response for {host}"
) from ex
await protocol.wait_for_discovery_to_complete()
finally:
transport.close()
@@ -384,7 +401,7 @@ class Discover:
elif ip in protocol.invalid_device_exceptions:
raise protocol.invalid_device_exceptions[ip]
else:
raise SmartDeviceException(f"Unable to get discovery response for {host}")
raise TimeoutException(f"Timed out getting discovery response for {host}")
@staticmethod
def _get_device_class(info: dict) -> Type[Device]: