Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Fix unit tests - we were expecting zeroes where the CRC now goes. #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
PROTOCOL_VERSION_BYTES_31 = b'3.1'
PROTOCOL_VERSION_BYTES_33 = b'3.3'

PROTOCOL_VERSION_3_1 = 3.1
PROTOCOL_VERSION_3_3 = 3.3

IS_PY2 = sys.version_info[0] == 2

class AESCipher(object):
Expand Down Expand Up @@ -181,6 +184,8 @@ def _send_receive(self, payload):
return data

def set_version(self, version):
if (version != PROTOCOL_VERSION_3_1) and (version != PROTOCOL_VERSION_3_3):
raise ValueError("Unsupported verison")
self.version = version

def generate_payload(self, command, data=None):
Expand Down Expand Up @@ -214,7 +219,7 @@ def generate_payload(self, command, data=None):
json_payload = json_payload.encode('utf-8')
log.debug('json_payload=%r', json_payload)

if self.version == 3.3:
if self.version == PROTOCOL_VERSION_3_3:
self.cipher = AESCipher(self.local_key) # expect to connect and then disconnect to set new
json_payload = self.cipher.encrypt(json_payload, False)
self.cipher = None
Expand Down Expand Up @@ -302,7 +307,7 @@ def status(self):
if not isinstance(result, str):
result = result.decode()
result = json.loads(result)
elif self.version == 3.3:
elif self.version == PROTOCOL_VERSION_3_3:
cipher = AESCipher(self.local_key)
result = cipher.decrypt(result, False)
log.debug('decrypted result=%r', result)
Expand Down
4 changes: 3 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def check_data_frame(data, expected_prefix, encrypted=True):
frame_ok = True
if prefix != pytuya.hex2bin(expected_prefix):
frame_ok = False
elif suffix != pytuya.hex2bin("000000000000aa55"):
elif suffix[-4:] != pytuya.hex2bin("0000aa55"):
# We only check for the trailing byte signature
# We could extend the test to also check the CRC if we wanted.
frame_ok = False
elif encrypted:
if payload_len != len(version) + len(checksum) + len(encrypted_json) + len(suffix):
Expand Down