From 8a67858368bafd305e651d6ae0da9455d977b24b Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 12 Feb 2018 13:26:34 +0100 Subject: [PATCH 1/4] Discover once vs. always. Just for testing. --- miio/device.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/miio/device.py b/miio/device.py index 490bd12f5..7bd42186f 100644 --- a/miio/device.py +++ b/miio/device.py @@ -116,6 +116,7 @@ def __init__(self, ip: str = None, token: str = None, self.debug = debug self._timeout = 5 + self._discovered = False self._device_ts = None # type: datetime.datetime self.__id = start_id self._device_id = None @@ -139,6 +140,7 @@ def do_discover(self) -> Message: self._device_id, self._device_ts, codecs.encode(m.checksum, 'hex')) + self._discovered = True else: _LOGGER.error("Unable to discover a device at address %s", self.ip) raise DeviceException("Unable to discover the device %s" % self.ip) @@ -201,7 +203,9 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any: :param dict parameters: Parameters to send, or an empty list FIXME :param retry_count: How many times to retry in case of failure :raises DeviceException: if an error has occured during communication.""" - self.do_discover() + + if not self._discovered: + self.do_discover() cmd = { "id": self._id, From 9ea7ad8c31006a2450c58472b037eb3365a24a5a Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 12 Feb 2018 13:38:58 +0100 Subject: [PATCH 2/4] Discover the device again on failure. A discovery is required after power loss for example. --- miio/device.py | 1 + 1 file changed, 1 insertion(+) diff --git a/miio/device.py b/miio/device.py index 7bd42186f..09047f85e 100644 --- a/miio/device.py +++ b/miio/device.py @@ -268,6 +268,7 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any: _LOGGER.warning("Retrying with incremented id, " "retries left: %s", retry_count) self.__id += 100 + self._discovered = False return self.send(command, parameters, retry_count - 1) raise DeviceException("No response from the device") from ex From d0901c424bef8f337091b7392705f2876cca9e74 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 12 Feb 2018 14:03:59 +0100 Subject: [PATCH 3/4] Lazy discovery added. --- miio/device.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/miio/device.py b/miio/device.py index 09047f85e..647844201 100644 --- a/miio/device.py +++ b/miio/device.py @@ -99,7 +99,7 @@ class Device: This class should not be initialized directly but a device-specific class inheriting it should be used instead of it.""" def __init__(self, ip: str = None, token: str = None, - start_id: int=0, debug: int=0) -> None: + start_id: int=0, debug: int=0, lazy_discover: bool=False) -> None: """ Create a :class:`Device` instance. :param ip: IP address or a hostname for the device @@ -114,6 +114,7 @@ def __init__(self, ip: str = None, token: str = None, if token is not None: self.token = bytes.fromhex(token) self.debug = debug + self.lazy_discover = lazy_discover self._timeout = 5 self._discovered = False @@ -134,13 +135,13 @@ def do_discover(self) -> Message: if m is not None: self._device_id = m.header.value.device_id self._device_ts = m.header.value.ts + self._discovered = True if self.debug > 1: _LOGGER.debug(m) _LOGGER.debug("Discovered %s with ts: %s, token: %s", self._device_id, self._device_ts, codecs.encode(m.checksum, 'hex')) - self._discovered = True else: _LOGGER.error("Unable to discover a device at address %s", self.ip) raise DeviceException("Unable to discover the device %s" % self.ip) @@ -204,7 +205,7 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any: :param retry_count: How many times to retry in case of failure :raises DeviceException: if an error has occured during communication.""" - if not self._discovered: + if not self.lazy_discover or not self._discovered: self.do_discover() cmd = { From 9efe931b5af8cfaa974db3026f6817d6ca0d600a Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Tue, 13 Feb 2018 11:55:02 +0100 Subject: [PATCH 4/4] Lazy discovery enabled per default. --- miio/device.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miio/device.py b/miio/device.py index 647844201..4cd27fdd1 100644 --- a/miio/device.py +++ b/miio/device.py @@ -99,7 +99,7 @@ class Device: This class should not be initialized directly but a device-specific class inheriting it should be used instead of it.""" def __init__(self, ip: str = None, token: str = None, - start_id: int=0, debug: int=0, lazy_discover: bool=False) -> None: + start_id: int=0, debug: int=0, lazy_discover: bool=True) -> None: """ Create a :class:`Device` instance. :param ip: IP address or a hostname for the device