-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathtests.py
executable file
·143 lines (106 loc) · 4.86 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
#!/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 tinytuya
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
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(self):
# arrange
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = MagicMock(return_value={}) # set_version calls this to figure out which commands it supports
d.set_version(3.1)
d._send_receive = MagicMock(return_value={"devId":"DEVICE_ID","dps":{"2":"colour", "5":"ffffff000000ff"}})
# act
d.set_colour(255,255,255)
# gather results
result_cmd, result_payload = get_results_from_mock(d)
# expectations
expected_cmd = tinytuya.CONTROL
# expected_payload = {"dps":{"2":"colour", "5":"ffffff000000ff"}, "devId":"DEVICE_ID_HERE","uid":"DEVICE_ID_HERE", "t": ""}
expected_payload = {"dps":{"21":"colour", "24":"0000000003e8"}, "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(self):
# arrange
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = MagicMock(return_value={}) # set_version calls this to figure out which commands it supports
d.set_version(3.1)
d._send_receive = MagicMock(return_value={"devId":"DEVICE_ID","dps":{"1":False,"2":0}})
# act
d.set_white(255, 255)
# 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":{"21":"white", "22": 255, "23": 255}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)
if __name__ == '__main__':
unittest.main()