forked from clach04/python-tuya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·193 lines (147 loc) · 7.24 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
import logging
import unittest
try:
from unittest.mock import MagicMock # Python 3
except ImportError:
from mock import MagicMock # py2 use https://pypi.python.org/pypi/mock
from hashlib import md5
import json
import logging
import struct
# Enable info logging to see version information
log = logging.getLogger('pytuya')
logging.basicConfig() # TODO include function name/line numbers in log
log.setLevel(level=logging.INFO)
#log.setLevel(level=logging.DEBUG) # Debug hack!
import pytuya
LOCAL_KEY = '0123456789abcdef'
mock_byte_encoding = 'utf-8'
def compare_json_strings(json1, json2, ignoring_keys=None):
json1 = json.loads(json1)
json2 = json.loads(json2)
if ignoring_keys is not None:
for key in ignoring_keys:
json1[key] = json2[key]
return json.dumps(json1, sort_keys=True) == json.dumps(json2, sort_keys=True)
def check_data_frame(data, expected_prefix, encrypted=True):
prefix = data[:15]
suffix = data[-8:]
if encrypted:
payload_len = struct.unpack(">B",data[15:16])[0] # big-endian, unsigned char
version = data[16:19]
checksum = data[19:35]
encrypted_json = data[35:-8]
json_data = pytuya.AESCipher(LOCAL_KEY.encode(mock_byte_encoding)).decrypt(encrypted_json)
else:
json_data = data[16:-8].decode(mock_byte_encoding)
frame_ok = True
if prefix != pytuya.hex2bin(expected_prefix):
frame_ok = False
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):
frame_ok = False
elif version != b"3.1":
frame_ok = False
return json_data, frame_ok
def mock_send_receive_set_timer(data):
if mock_send_receive_set_timer.call_counter == 0:
ret = 20*chr(0x0) + '{"devId":"DEVICE_ID","dps":{"1":false,"2":0}}' + 8*chr(0x0)
elif mock_send_receive_set_timer.call_counter == 1:
expected = '{"uid":"DEVICE_ID_HERE","devId":"DEVICE_ID_HERE","t":"","dps":{"2":6666}}'
json_data, frame_ok = check_data_frame(data, "000055aa0000000000000007000000")
if frame_ok and compare_json_strings(json_data, expected, ['t']):
ret = '{"test_result":"SUCCESS"}'
else:
ret = '{"test_result":"FAIL"}'
ret = ret.encode(mock_byte_encoding)
mock_send_receive_set_timer.call_counter += 1
return ret
def mock_send_receive_set_status(data):
expected = '{"dps":{"1":true},"uid":"DEVICE_ID_HERE","t":"1516117564","devId":"DEVICE_ID_HERE"}'
json_data, frame_ok = check_data_frame(data, "000055aa0000000000000007000000")
if frame_ok and compare_json_strings(json_data, expected, ['t']):
ret = '{"test_result":"SUCCESS"}'
else:
logging.error("json data not the same: {} != {}".format(json_data, expected))
ret = '{"test_result":"FAIL"}'
ret = ret.encode(mock_byte_encoding)
return ret
def mock_send_receive_status(data):
expected = '{"devId":"DEVICE_ID_HERE","gwId":"DEVICE_ID_HERE"}'
json_data, frame_ok = check_data_frame(data, "000055aa000000000000000a000000", False)
# FIXME dead code block
if frame_ok and compare_json_strings(json_data, expected):
ret = '{"test_result":"SUCCESS"}'
else:
logging.error("json data not the same: {} != {}".format(json_data, expected))
ret = '{"test_result":"FAIL"}'
ret = 20*chr(0) + ret + 8*chr(0)
ret = ret.encode(mock_byte_encoding)
return ret
def mock_send_receive_set_colour(data):
expected = '{"dps":{"2":"colour", "5":"ffffff000000ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t":"1516117564"}'
json_data, frame_ok = check_data_frame(data, "000055aa0000000000000007000000")
if frame_ok and compare_json_strings(json_data, expected, ['t']):
ret = '{"test_result":"SUCCESS"}'
else:
logging.error("json data not the same: {} != {}".format(json_data, expected))
ret = '{"test_result":"FAIL"}'
ret = ret.encode(mock_byte_encoding)
return ret
def mock_send_receive_set_white(data):
expected = '{"dps":{"2":"white", "3":255, "4":255}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t":"1516117564"}'
json_data, frame_ok = check_data_frame(data, "000055aa0000000000000007000000")
if frame_ok and compare_json_strings(json_data, expected, ['t']):
ret = '{"test_result":"SUCCESS"}'
else:
logging.error("json data not the same: {} != {}".format(json_data, expected))
ret = '{"test_result":"FAIL"}'
ret = ret.encode(mock_byte_encoding)
return ret
class TestXenonDevice(unittest.TestCase):
def test_set_timer(self):
d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d._send_receive = MagicMock(side_effect=mock_send_receive_set_timer)
# Reset call_counter and start test
mock_send_receive_set_timer.call_counter = 0
result = d.set_timer(6666)
result = result[result.find(b'{'):result.rfind(b'}')+1]
result = result.decode(mock_byte_encoding) # Python 3 (3.5.4 and earlier) workaround to json stdlib "behavior" https://docs.python.org/3/whatsnew/3.6.html#json
result = json.loads(result)
# Make sure mock_send_receive_set_timer() has been called twice with correct parameters
self.assertEqual(result['test_result'], "SUCCESS")
def test_set_status(self):
d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d._send_receive = MagicMock(side_effect=mock_send_receive_set_status)
result = d.set_status(True, 1)
result = result.decode(mock_byte_encoding) # Python 3 (3.5.4 and earlier) workaround to json stdlib "behavior" https://docs.python.org/3/whatsnew/3.6.html#json
result = json.loads(result)
# Make sure mock_send_receive_set_timer() has been called twice with correct parameters
self.assertEqual(result['test_result'], "SUCCESS")
def test_status(self):
d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d._send_receive = MagicMock(side_effect=mock_send_receive_status)
result = d.status()
# Make sure mock_send_receive_set_timer() has been called twice with correct parameters
self.assertEqual(result['test_result'], "SUCCESS")
def test_set_colour(self):
d = pytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d._send_receive = MagicMock(side_effect=mock_send_receive_set_colour)
result = d.set_colour(255,255,255)
result = result.decode(mock_byte_encoding)
result = json.loads(result)
self.assertEqual(result['test_result'], "SUCCESS")
def test_set_white(self):
d = pytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d._send_receive = MagicMock(side_effect=mock_send_receive_set_white)
result = d.set_white(255, 255)
result = result.decode(mock_byte_encoding)
result = json.loads(result)
self.assertEqual(result['test_result'], "SUCCESS")
if __name__ == '__main__':
unittest.main()