From e1af7411e44fc3082eb396c9135fe22f2f825d48 Mon Sep 17 00:00:00 2001 From: Jared Sanson Date: Tue, 9 Mar 2021 22:09:09 +1300 Subject: [PATCH 1/2] Allow subnet to be specified for broadcast --- miio/miioprotocol.py | 8 ++++---- miio/vacuum_cli.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index 78553487f..82449ea88 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -62,7 +62,7 @@ def send_handshake(self, *, retry_count=3) -> Message: :raises DeviceException: if the device could not be discovered after retries. """ try: - m = MiIOProtocol.discover(self.ip) + m = MiIOProtocol.discover(self.ip, is_broadcast=False) except DeviceException as ex: if retry_count > 0: return self.send_handshake(retry_count=retry_count - 1) @@ -90,7 +90,7 @@ def send_handshake(self, *, retry_count=3) -> Message: return m @staticmethod - def discover(addr: str = None, timeout: int = 5) -> Any: + def discover(addr: str = None, is_broadcast: bool = True, timeout: int = 5) -> Any: """Scan for devices in the network. This method is used to discover supported devices by sending a handshake message to the broadcast address on port 54321. If the target IP address is given, the handshake will be send as an unicast @@ -98,10 +98,10 @@ def discover(addr: str = None, timeout: int = 5) -> Any: :param str addr: Target IP address """ - is_broadcast = addr is None seen_addrs = [] # type: List[str] if is_broadcast: - addr = "" + if not addr: + addr = "" is_broadcast = True _LOGGER.info("Sending discovery to %s with timeout of %ss..", addr, timeout) # magic, length 32 diff --git a/miio/vacuum_cli.py b/miio/vacuum_cli.py index 97cffae4f..b630c3235 100644 --- a/miio/vacuum_cli.py +++ b/miio/vacuum_cli.py @@ -97,10 +97,11 @@ def cleanup(vac: miio.Vacuum, *args, **kwargs): @cli.command() @click.option("--handshake", type=bool, default=False) -def discover(handshake): +@click.option("--subnet", type=str, default="") +def discover(handshake, subnet): """Search for robots in the network.""" if handshake: - MiIOProtocol.discover() + MiIOProtocol.discover(subnet, is_broadcast=True) else: miio.Discovery.discover_mdns() From 5ccaf476a057da71ed0ed7545565a08ab5f794d0 Mon Sep 17 00:00:00 2001 From: Jared Sanson Date: Tue, 16 Mar 2021 23:43:48 +1300 Subject: [PATCH 2/2] Update docstr --- miio/miioprotocol.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index 82449ea88..9a5a6c599 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -93,10 +93,14 @@ def send_handshake(self, *, retry_count=3) -> Message: def discover(addr: str = None, is_broadcast: bool = True, timeout: int = 5) -> Any: """Scan for devices in the network. This method is used to discover supported devices by sending a handshake message to the broadcast address on port 54321. - If the target IP address is given, the handshake will be send as an unicast - packet. - - :param str addr: Target IP address + If the target IP address is given and is_broadcast is False, the handshake + will be send as a unicast packet. + If is_broadcast is True, then addr can optionally specify the target network + to send the broadcast to. This is necessary for Windows platforms. + + :param str addr: Target IP address or network + :param bool is_broadcast: True to broadcast, False to unicast + :param int timeout: Time to wait for discovering new devices """ seen_addrs = [] # type: List[str] if is_broadcast: