From 968f8e1c6515fff8d6f544c3cfbb189c1daa99d1 Mon Sep 17 00:00:00 2001 From: Petr Kotek Date: Sat, 11 Jan 2020 22:33:38 +1100 Subject: [PATCH] Device: make protocol field private; don't deprecate Device.send method to keep everything compatible. Protocol: update docstring to reflect new code. Vacuum: expose raw_id from the Protocol. --- miio/device.py | 20 +++++++++----------- miio/protocol.py | 12 +++++++----- miio/tests/dummies.py | 2 +- miio/vacuum.py | 6 +++++- miio/vacuum_cli.py | 2 +- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/miio/device.py b/miio/device.py index 54aed678c..82e8b5e47 100644 --- a/miio/device.py +++ b/miio/device.py @@ -6,7 +6,6 @@ from .click_common import DeviceGroupMeta, LiteralParamType, command, format_output from .protocol import Protocol -from .utils import deprecated _LOGGER = logging.getLogger(__name__) @@ -119,14 +118,13 @@ def __init__( ) -> None: self.ip = ip self.token = token - self.protocol = Protocol(ip, token, start_id, debug, lazy_discover) + self._protocol = Protocol(ip, token, start_id, debug, lazy_discover) - @deprecated(reason="Use self.raw_command() instead") def send(self, command: str, parameters: Any = None, retry_count=3) -> Any: - return self.protocol.send(command, parameters, retry_count) + return self._protocol.send(command, parameters, retry_count) def send_handshake(self): - return self.protocol.send_handshake() + return self._protocol.send_handshake() @command( click.argument("command", type=str, required=True), @@ -139,7 +137,7 @@ def raw_command(self, command, parameters): :param str command: Command to send :param dict parameters: Parameters to send""" - return self.protocol.send(command, parameters) + return self._protocol.send(command, parameters) @command( default_output=format_output( @@ -155,7 +153,7 @@ def info(self) -> DeviceInfo: """Get miIO protocol information from the device. This includes information about connected wlan network, and hardware and software versions.""" - return DeviceInfo(self.protocol.send("miIO.info")) + return DeviceInfo(self._protocol.send("miIO.info")) def update(self, url: str, md5: str): """Start an OTA update.""" @@ -166,15 +164,15 @@ def update(self, url: str, md5: str): "file_md5": md5, "proc": "dnld install", } - return self.protocol.send("miIO.ota", payload)[0] == "ok" + return self._protocol.send("miIO.ota", payload)[0] == "ok" def update_progress(self) -> int: """Return current update progress [0-100].""" - return self.protocol.send("miIO.get_ota_progress")[0] + return self._protocol.send("miIO.get_ota_progress")[0] def update_state(self): """Return current update state.""" - return UpdateState(self.protocol.send("miIO.get_ota_state")[0]) + return UpdateState(self._protocol.send("miIO.get_ota_state")[0]) def configure_wifi(self, ssid, password, uid=0, extra_params=None): """Configure the wifi settings.""" @@ -182,4 +180,4 @@ def configure_wifi(self, ssid, password, uid=0, extra_params=None): extra_params = {} params = {"ssid": ssid, "passwd": password, "uid": uid, **extra_params} - return self.protocol.send("miIO.config_router", params)[0] + return self._protocol.send("miIO.config_router", params)[0] diff --git a/miio/protocol.py b/miio/protocol.py index 068c1e19e..468183955 100644 --- a/miio/protocol.py +++ b/miio/protocol.py @@ -1,14 +1,16 @@ """miIO protocol implementation This module contains the implementation of the routines to encrypt and decrypt -miIO payloads with a device-specific token. +miIO payloads with a device-specific token (Utils) and implementation of +routines to send handshakes, send commands and discover devices (Protocol). -The payloads to be encrypted (to be passed to a device) are expected to be -JSON objects, the same applies for decryption where they are converted -automatically to JSON objects. +The payloads to be encrypted (to be passed to a device) are expected to be JSON +objects, the same applies for decryption where they are converted automatically +to JSON objects. If the decryption fails, raw bytes as returned by the device are returned. -An usage example can be seen in the source of :func:`miio.Device.send`. +An usage example of encryption/decryption (using the Message struct) be seen in +the source of :func:`miio.Protocol.send`. """ import binascii import calendar diff --git a/miio/tests/dummies.py b/miio/tests/dummies.py index 9d5cf515c..07feaa01d 100644 --- a/miio/tests/dummies.py +++ b/miio/tests/dummies.py @@ -38,7 +38,7 @@ class DummyDevice: def __init__(self, *args, **kwargs): self.start_state = self.state.copy() - self.protocol = DummyProtocol(self) + self._protocol = DummyProtocol(self) def _reset_state(self): """Revert back to the original state.""" diff --git a/miio/vacuum.py b/miio/vacuum.py index 3111d3daa..92c8fbde2 100644 --- a/miio/vacuum.py +++ b/miio/vacuum.py @@ -546,6 +546,10 @@ def get_segment_status(self): """Get the status of a segment.""" return self.send("get_segment_status") + @property + def raw_id(self): + return self._protocol.raw_id + def name_segment(self): raise NotImplementedError("unknown parameters") # return self.send("name_segment") @@ -600,7 +604,7 @@ def cleanup(vac: Vacuum, *args, **kwargs): if vac.ip is None: # dummy Device for discovery, skip teardown return id_file = kwargs["id_file"] - seqs = {"seq": vac.protocol.raw_id, "manual_seq": vac.manual_seqnum} + seqs = {"seq": vac._protocol.raw_id, "manual_seq": vac.manual_seqnum} _LOGGER.debug("Writing %s to %s", seqs, id_file) path_obj = pathlib.Path(id_file) cache_dir = path_obj.parents[0] diff --git a/miio/vacuum_cli.py b/miio/vacuum_cli.py index c395d62e1..b30081311 100644 --- a/miio/vacuum_cli.py +++ b/miio/vacuum_cli.py @@ -83,7 +83,7 @@ def cleanup(vac: miio.Vacuum, *args, **kwargs): if vac.ip is None: # dummy Device for discovery, skip teardown return id_file = kwargs["id_file"] - seqs = {"seq": vac.protocol.raw_id, "manual_seq": vac.manual_seqnum} + seqs = {"seq": vac.raw_id, "manual_seq": vac.manual_seqnum} _LOGGER.debug("Writing %s to %s", seqs, id_file) path_obj = pathlib.Path(id_file) dir = path_obj.parents[0]