-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathtests.py
More file actions
executable file
·316 lines (235 loc) · 11.6 KB
/
tests.py
File metadata and controls
executable file
·316 lines (235 loc) · 11.6 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/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
import json
import struct
# Enable info logging to see version information
log = logging.getLogger('tinytuya')
logging.basicConfig() # TODO include function name/line numbers in log
log.setLevel(level=logging.INFO)
log.setLevel(level=logging.DEBUG) # Debug hack!
import base64
import tinytuya
from tinytuya.Contrib.RFRemoteControlDevice import RFRemoteControlDevice
LOCAL_KEY = '0123456789abcdef'
mock_byte_encoding = 'utf-8'
def get_results_from_mock(d):
result_message_payload = d._send_receive.call_args[0][0]
result_cmd = result_message_payload.cmd
result_payload = json.loads(result_message_payload.payload.decode(mock_byte_encoding))
result_payload["t"] = "" # clear "t"
return result_cmd, result_payload
def build_mock_bulb(bulb):
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
if bulb == 'A':
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False, "2":"none", "3": -1, "4": -1, "5":"00000000000000"}} # tell it which commands to support and which DPs need updating
elif bulb == 'B':
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"20": False, "21":"none", "22": -1, "23": -1, "24":"000000000000"}} # tell it which commands to support and DPs need updating
elif bulb == 'C':
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False, "2": -1}} # tell it which commands to support and which DPs need updating
else:
raise ValueError("Unknown bulb type %r" % bulb)
#d.set_bulb_type(bulb) # tell it which commands to support
d.detect_bulb(d.status()) # tell it which commands to support
d.set_version(3.1)
d._send_receive = MagicMock(return_value={})
return d
class TestXenonDevice(unittest.TestCase):
def test_set_timer(self):
# arrange
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.set_version(3.1)
mock_response = {"devId":"DEVICE_ID","dps":{"1":False,"2":0}}
d._send_receive = MagicMock(return_value=mock_response)
# act
d.set_timer(6666)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"uid":"DEVICE_ID_HERE","devId":"DEVICE_ID_HERE","t":"","dps":{"2":6666}}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_status(self):
# arrange
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.set_version(3.1)
d._send_receive = MagicMock(return_value={"devId":"DEVICE_ID","dps":{"1":False,"2":0}})
# act
d.set_status(True, 1)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"dps":{"1":True},"uid":"DEVICE_ID_HERE","t":"","devId":"DEVICE_ID_HERE"}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_status(self):
# arrange
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.set_version(3.1)
d._send_receive = MagicMock(return_value={"devId":"DEVICE_ID","dps":{"1":False,"2":0}})
# act
d.status()
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.DP_QUERY
expected_payload = {"devId": "DEVICE_ID_HERE", "gwId": "DEVICE_ID_HERE", "uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_colour_A(self):
# arrange
d = build_mock_bulb('A')
# act
d.set_colour(255,127,63)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
print(result_cmd, result_payload)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"colour", "5":"ffffff000000ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{"1": True, "2":"colour", "5":"ff7f3f0014c0ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_colour_B(self):
# arrange
d = build_mock_bulb('B')
# act
d.set_colour(255,127,63)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
print(result_cmd, result_payload)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"colour", "5":"ffffff000000ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{"20": True, "21":"colour", "24":"001402f003e8"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_white_A(self):
# arrange
d = build_mock_bulb('A')
# act
d.set_white_percentage(100,100)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
print(result_cmd, result_payload)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"colour", "5":"ffffff000000ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{'1': True, '2': 'white', '3': 255, '4': 255}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_white_B(self):
# arrange
d = build_mock_bulb('B')
# act
d.set_white_percentage(100,100)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"white", "3": 255, "4": 255}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{'20': True, "21":"white", "22": 1000, "23": 1000}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_brightness_C(self):
# arrange
d = build_mock_bulb('C')
# act
d.set_brightness_percentage(100)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"white", "3": 255, "4": 255}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{'1': True, "2": 255}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_set_bulb_type(self):
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False, "2": 90}} # tell it which commands to support and which DPs need updating
d.set_bulb_type('C') # tell it which commands to support
d.set_version(3.1)
d._send_receive = MagicMock(return_value={})
# act
d.turn_on()
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def test_not_a_bulb(self):
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False}} # tell it which commands to support and which DPs need updating
#d.set_bulb_type('C') # tell it which commands to support
d.set_version(3.1)
d._send_receive = MagicMock(return_value={})
# act
d.turn_on()
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
def build_mock_rf():
d = RFRemoteControlDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY, control_type=1)
d.set_version(3.3)
d.set_value = MagicMock()
return d
class TestRFRemoteControlDevice(unittest.TestCase):
def test_rf_decode_button_returns_dict(self):
# Bug 1: rf_decode_button was missing the () call on base64.b64decode,
# causing it to always return None instead of the decoded JSON dict.
sample = {"study_feq": "433", "ver": "2"}
encoded = base64.b64encode(json.dumps(sample).encode()).decode()
result = RFRemoteControlDevice.rf_decode_button(encoded)
self.assertIsNotNone(result, "rf_decode_button returned None — function was not called")
self.assertDictEqual(result, sample)
def test_rf_send_button_payload_structure(self):
# Bug 2: send_command('rfstudy_send', ...) was building the wrong payload:
# - used 'study_feq' (string) instead of 'feq' (int)
# - omitted 'mode' and 'rate' fields
# - omitted 'ver' inside each key dict
# Bug 3: rf_send_button was forwarding study_feq from the decoded button into
# feq, but feq must always be 0 so the device uses the frequency embedded in
# the code. Passing the actual frequency value selects a different chip path.
d = build_mock_rf()
# Use a button that has study_feq set to a non-zero value to confirm it is
# NOT forwarded into the payload's feq field.
button_data = {"study_feq": "433", "ver": "2"}
base64_code = base64.b64encode(json.dumps(button_data).encode()).decode()
d.rf_send_button(base64_code)
call_args = d.set_value.call_args
dp = call_args[0][0]
payload = json.loads(call_args[0][1])
self.assertEqual(dp, RFRemoteControlDevice.DP_SEND_IR)
self.assertEqual(payload['control'], 'rfstudy_send')
self.assertIn('feq', payload, "payload missing 'feq' (was 'study_feq')")
self.assertNotIn('study_feq', payload, "payload must not contain 'study_feq' for rfstudy_send")
self.assertIsInstance(payload['feq'], int, "'feq' must be int, not string")
self.assertEqual(payload['feq'], 0, "feq must be 0 so the device uses the frequency embedded in the code")
self.assertIn('mode', payload, "payload missing 'mode'")
self.assertIn('rate', payload, "payload missing 'rate'")
self.assertIn('key1', payload)
self.assertIn('ver', payload['key1'], "key1 missing 'ver'")
if __name__ == '__main__':
unittest.main()