forked from clach04/python-tuya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·201 lines (148 loc) · 7.03 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
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import json
import logging
import struct
import pytuya
import unittest
try:
from unittest.mock import MagicMock # Python 3
except ImportError:
from mock import MagicMock # py2 use https://pypi.python.org/pypi/mock
# Enable info logging to see version information
log = logging.getLogger('pytuya')
log.setLevel(level=logging.INFO)
# log.setLevel(level=logging.DEBUG) # Debug hack!
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.utils.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.utils.hex2bin(expected_prefix):
frame_ok = False
elif suffix != pytuya.utils.hex2bin("000000000000aa55"):
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 not hasattr(mock_send_receive_set_timer, "call_counter"):
mock_send_receive_set_timer.call_counter = 0
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":"ID","devId":"ID","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"}'
else:
raise RuntimeError("unexpected counter of > 1")
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":"ID","t":"1516117564","devId":"ID"}'
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":"ID","gwId":"ID"}'
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":"ID","uid":"ID", "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":"ID","uid":"ID", "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('ID', '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('ID', '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('ID', '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('ID', '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('ID', '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()