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

Add retries to discovery requests #754

Merged
merged 1 commit into from
Jul 9, 2020
Merged
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
24 changes: 17 additions & 7 deletions miio/miioprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,31 @@ def __init__(
self.__id = start_id
self._device_id = None

def send_handshake(self) -> Message:
"""Send a handshake to the device,
which can be used to the device type and serial.
def send_handshake(self, *, retry_count=3) -> Message:
"""Send a handshake to the device.

This returns some information, such as device type and serial,
as well as device's timestamp in response.

The handshake must also be done regularly to enable communication
with the device.

:rtype: Message
:raises DeviceException: if the device could not be discovered after retries.
"""
try:
m = MiIOProtocol.discover(self.ip)
except DeviceException as ex:
if retry_count > 0:
return self.send_handshake(retry_count=retry_count - 1)

raise ex

:raises DeviceException: if the device could not be discovered."""
m = MiIOProtocol.discover(self.ip)
if m is not None:
header = m.header.value
self._device_id = header.device_id
self._device_ts = header.ts
self._discovered = True

if self.debug > 1:
_LOGGER.debug(m)
_LOGGER.debug(
Expand All @@ -73,7 +83,7 @@ def send_handshake(self) -> Message:
codecs.encode(m.checksum, "hex"),
)
else:
_LOGGER.error("Unable to discover a device at address %s", self.ip)
_LOGGER.debug("Unable to discover a device at address %s", self.ip)
raise DeviceException("Unable to discover the device %s" % self.ip)

return m
Expand Down