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

Unbreak the communication when using construct v2.8.17 #142

Merged
merged 1 commit into from
Dec 15, 2017
Merged
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
19 changes: 8 additions & 11 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def __init__(self, ip: str = None, token: str = None,
self._timeout = 5
self._device_ts = None # type: datetime.datetime
self.__id = start_id
self._devtype = None
self._serial = None
self._device_id = None

def do_discover(self) -> Message:
"""Send a handshake to the device,
Expand All @@ -126,14 +125,12 @@ def do_discover(self) -> Message:
:raises DeviceException: if the device could not be discovered."""
m = Device.discover(self.ip)
if m is not None:
self._devtype = m.header.value.devtype
self._serial = m.header.value.serial
self._device_id = m.header.value.device_id
self._device_ts = m.header.value.ts
if self.debug > 1:
_LOGGER.debug(m)
_LOGGER.debug("Discovered %s %s with ts: %s, token: %s",
self._devtype,
self._serial,
_LOGGER.debug("Discovered %s with ts: %s, token: %s",
self._device_id,
self._device_ts,
codecs.encode(m.checksum, 'hex'))
else:
Expand Down Expand Up @@ -176,9 +173,9 @@ def discover(addr: str=None) -> Any:
return m

if addr[0] not in seen_addrs:
_LOGGER.info(" IP %s: %s - token: %s",
_LOGGER.info(" IP %s (ID: %s) - token: %s",
addr[0],
m.header.value.devtype,
m.header.value.device_id.decode(),
codecs.encode(m.checksum, 'hex'))
seen_addrs.append(addr[0])
except socket.timeout:
Expand Down Expand Up @@ -210,8 +207,7 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:

send_ts = self._device_ts + datetime.timedelta(seconds=1)
header = {'length': 0, 'unknown': 0x00000000,
'devtype': self._devtype, 'serial': self._serial,
'ts': send_ts}
'device_id': self._device_id, 'ts': send_ts}

msg = {'data': {'value': cmd},
'header': {'value': header},
Expand All @@ -236,6 +232,7 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
data, addr = s.recvfrom(1024)
m = Message.parse(data, ctx)
self._device_ts = m.header.value.ts
self._device_id = m.header.value.device_id
if self.debug > 1:
_LOGGER.debug("recv from %s: %s", addr[0], m)

Expand Down
17 changes: 2 additions & 15 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from construct import (Struct, Bytes, Const, Int16ub, Int32ub, GreedyBytes,
Adapter, Checksum, RawCopy, Rebuild, IfThenElse,
Default, Pointer, Pass, Enum)
Default, Pointer, Hex)

# for debugging parsing
# from construct import Probe
Expand All @@ -31,17 +31,6 @@

_LOGGER = logging.getLogger(__name__)

# Map of device ids
xiaomi_devices_reverse = {0x02c1: "Xiaomi Mi Smart WiFi Socket",
0x02f2: "Xiaomi Mi Robot Vacuum",
0x00c4: "Xiaomi Smart Mi Air Purifier",
0x031a: "Xiaomi Smart home gateway",
0x0330: "Yeelight color bulb",
0x0374: "Xiaomi Philips LED Ceiling Lamp",
0x02f9: "Xiaomi Philips Eyecare Smart Lamp 2"
}
xiaomi_devices = {y: x for x, y in xiaomi_devices_reverse.items()}


class Utils:
""" This class is adapted from the original xpn.py code by gst666 """
Expand Down Expand Up @@ -190,9 +179,7 @@ def _decode(self, obj, context):
Const(Int16ub, 0x2131),
"length" / Rebuild(Int16ub, Utils.get_length),
"unknown" / Default(Int32ub, 0x00000000),
"devtype" / Enum(Default(Int16ub, 0x02f2),
default=Pass, **xiaomi_devices),
"serial" / Default(Int16ub, 0xa40d),
"device_id" / Hex(Bytes(4)),
"ts" / TimeAdapter(Default(Int32ub, datetime.datetime.utcnow()))
)),
"checksum" / IfThenElse(
Expand Down