Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subnet to be specified for broadcast #965

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions miio/miioprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -90,18 +90,22 @@ 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:
rytilahti marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def discover(addr: str = None, is_broadcast: bool = True, timeout: int = 5) -> Any:
def discover(addr: str = None, timeout: int = 5, *, is_broadcast: bool = True) -> Any:

Let's move the is_broadcast to be the last parameter, and make it kwarg-only for clarity.

"""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
"""
is_broadcast = addr is None
seen_addrs = [] # type: List[str]
if is_broadcast:
addr = "<broadcast>"
if not addr:
addr = "<broadcast>"
rytilahti marked this conversation as resolved.
Show resolved Hide resolved
is_broadcast = True
_LOGGER.info("Sending discovery to %s with timeout of %ss..", addr, timeout)
# magic, length 32
Expand Down
5 changes: 3 additions & 2 deletions miio/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<broadcast>")
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()

Expand Down