Skip to content

Commit

Permalink
construct-code uptodate
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Feb 16, 2018
1 parent 26fc798 commit f636c8a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions miio/chuangmi_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ def play(self, command: str):


class ProntoPulseAdapter(Adapter):
def _decode(self, obj, context, *args, **kwargs):
def _decode(self, obj, context, path):
return int(obj * context._.modulation_period)

def _encode(self, obj, context, *args, **kwargs):
def _encode(self, obj, context, path):
raise RuntimeError('Not implemented')


Expand Down
5 changes: 3 additions & 2 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import socket
import logging
import construct
import binascii
from typing import Any, List, Optional # noqa: F401

from .protocol import Message
Expand Down Expand Up @@ -133,7 +134,7 @@ 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._device_id = m.header.value.device_id
self._device_id = binascii.hexlify(m.header.value.device_id)
self._device_ts = m.header.value.ts
self._discovered = True
if self.debug > 1:
Expand Down Expand Up @@ -184,7 +185,7 @@ def discover(addr: str=None) -> Any:
if addr[0] not in seen_addrs:
_LOGGER.info(" IP %s (ID: %s) - token: %s",
addr[0],
m.header.value.device_id.decode(),
binascii.hexlify(m.header.value.device_id).decode(),
codecs.encode(m.checksum, 'hex'))
seen_addrs.append(addr[0])
except socket.timeout:
Expand Down
13 changes: 5 additions & 8 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

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

# for debugging parsing
# from construct import Probe
Default, Pointer, Hex, Probe)

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -130,24 +127,24 @@ def is_hello(x) -> bool:

class TimeAdapter(Adapter):
"""Adapter for timestamp conversion."""
def _encode(self, obj, context, *args, **kwargs):
def _encode(self, obj, context, path):
return calendar.timegm(obj.timetuple())

def _decode(self, obj, context, *args, **kwargs):
def _decode(self, obj, context, path):
return datetime.datetime.utcfromtimestamp(obj)


class EncryptionAdapter(Adapter):
"""Adapter to handle communication encryption."""
def _encode(self, obj, context, *args, **kwargs):
def _encode(self, obj, context, path):
"""Encrypt the given payload with the token stored in the context.
:param obj: JSON object to encrypt"""
# pp(context)
return Utils.encrypt(json.dumps(obj).encode('utf-8') + b'\x00',
context['_']['token'])

def _decode(self, obj, context, *args, **kwargs):
def _decode(self, obj, context, path):
"""Decrypts the given payload with the token stored in the context.
:return str: JSON object"""
Expand Down

0 comments on commit f636c8a

Please sign in to comment.