Skip to content

Commit

Permalink
Device: make protocol field private; don't deprecate Device.send meth…
Browse files Browse the repository at this point in the history
…od to keep everything compatible.

Protocol: update docstring to reflect new code.
Vacuum: expose raw_id from the Protocol.
  • Loading branch information
Petr Kotek committed Jan 11, 2020
1 parent 8509d4d commit 968f8e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
20 changes: 9 additions & 11 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand All @@ -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."""
Expand All @@ -166,20 +164,20 @@ 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."""
if extra_params is 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]
12 changes: 7 additions & 5 deletions miio/protocol.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion miio/tests/dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
6 changes: 5 additions & 1 deletion miio/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion miio/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 968f8e1

Please sign in to comment.