Debian Codegraph @

src/tests/xtest_x_vesync_devices.py

raw symbols SCIP
1 """Test VeSync manager methods."""
2
3 import pytest
4 import asyncio
5 from unittest.mock import patch
6 import pyvesync
7 import logging
8 import copy
9 import time
10 import orjson
11 from itertools import chain
12 from pyvesync.vesyncfan import *
13 from pyvesync.devices.vesyncbulb import *
14 from pyvesync.devices.vesyncoutlet import *
15 from pyvesync.devices.vesyncpurifier import VeSyncAir131
16 from pyvesync.devices.vesyncswitch import *
17 import call_json as json_vals
18 from call_json_switches import SWITCHES_NUM
19 from call_json_outlets import OUTLETS_NUM
20 from call_json_fans import FANS_NUM
21 from call_json_bulbs import BULBS_NUM
22
23 BAD_DEV_LIST = {
24 'result': {
25 'total': 5,
26 'pageNo': 1,
27 'pageSize': 50,
28 'list': [{'NoConfigKeys': None}],
29 },
30 'code': 0,
31 }
32
33
34 class TestDeviceList(object):
35 """Test getting and populating device lists."""
36
37 @pytest.fixture(scope='function')
38 def api_mock(self, caplog):
39 """Mock call_api and initialize VeSync object."""
40 self.mock_api_call = patch('pyvesync.vesync.VeSync.async_call_api')
41 self.mock_api = self.mock_api_call.start()
42 self.loop = asyncio.new_event_loop()
43 self.mock_api.return_value.ok = True
44 self.vesync_obj = pyvesync.vesync.VeSync('[email protected]', 'pass', debug=True)
45 self.vesync_obj.enabled = True
46 self.vesync_obj.token = 'sample_tk'
47 self.vesync_obj.account_id = 'sample_id'
48 self.vesync_obj.in_process = False
49 caplog.set_level(logging.DEBUG)
50 yield
51 self.mock_api_call.stop()
52
53 async def run_coro(self, coro):
54 """Run a coroutine in the event loop."""
55 return await coro
56
57 def run_in_loop(self, func, *args, **kwargs):
58 """Run a function in the event loop."""
59 return self.loop.run_until_complete(self.run_coro(func(*args, **kwargs)))
60
61 def test_device_api(self, caplog, api_mock):
62 """Tests to ensure call_api is being called correctly."""
63 head = json_vals.DEFAULT_HEADER_BYPASS
64 self.mock_api.return_value = (orjson.dumps({'V': 2}), 200)
65 self.run_in_loop(self.vesync_obj.get_devices)
66 call_list = self.mock_api.call_args_list
67 call_p1 = call_list[0][0]
68 call_p2 = call_list[0][1]
69 assert call_p1[0] == '/cloud/v1/deviceManaged/devices'
70 assert call_p1[1] == 'post'
71 assert call_p2['headers'] == head
72 assert self.vesync_obj.enabled
73
74 @patch('pyvesync.vesyncoutlet.VeSyncOutlet7A')
75 @patch('pyvesync.vesyncoutlet.VeSyncOutlet15A')
76 @patch('pyvesync.vesyncoutlet.VeSyncOutlet10A')
77 @patch('pyvesync.vesyncswitch.VeSyncWallSwitch')
78 @patch('pyvesync.vesyncswitch.VeSyncDimmerSwitch')
79 @patch('pyvesync.vesyncfan.VeSyncAir131')
80 def test_getdevs_vsfact(
81 self, air_patch, wsdim_patch, ws_patch, out10a_patch, out15a_patch, out7a_patch, api_mock
82 ):
83 """Test the get_devices, process_devices and VSFactory methods.
84 Build list with device objects from details
85 Test for all 6 known devices - 4 outlets, 2 switches, 1 fan.
86 """
87
88 device_list = json_vals.DeviceList.device_list_response()
89
90 self.mock_api.return_value = device_list
91
92 self.run_in_loop(self.vesync_obj.get_devices)
93
94 assert len(self.vesync_obj.outlets) == OUTLETS_NUM
95 assert len(self.vesync_obj.switches) == SWITCHES_NUM
96 assert len(self.vesync_obj.fans) == FANS_NUM
97 assert len(self.vesync_obj.bulbs) == BULBS_NUM
98
99 def test_lv600(
100 self, api_mock
101 ):
102 """Test the get_devices, process_devices and VSFactory methods.
103 Build list with device objects from details
104 Test for all 6 known devices - 4 outlets, 2 switches, 1 fan.
105 """
106
107 resp_dict, status = json_vals.DeviceList.FAN_TEST
108
109 self.mock_api.return_value = orjson.dumps(resp_dict), status
110
111 self.run_in_loop(self.vesync_obj.get_devices)
112
113 assert len(self.vesync_obj.fans) == 3
114
115 def test_dual200s(
116 self, api_mock
117 ):
118 """Test the get_devices, process_devices and VSFactory methods.
119 Build list with device objects from details
120 Test for all 6 known devices - 4 outlets, 2 switches, 1 fan.
121 """
122
123 self.mock_api.return_value = json_vals.DeviceList.device_list_response('Dual200S')
124 self.vesync_obj.debug = True
125 self.run_in_loop(self.vesync_obj.get_devices)
126
127 assert len(self.vesync_obj.fans) == 1
128
129 def test_getdevs_code(self, caplog, api_mock):
130 """Test get_devices with code > 0 returned."""
131 device_list = (orjson.dumps({'code': 1, 'msg': 'gibberish'}), 200)
132
133 self.mock_api.return_value = device_list
134 self.run_in_loop(self.vesync_obj.get_devices)
135
136 assert 'Error retrieving device list' in caplog.text
137
138 def test_get_devices_resp_changes(self, caplog, api_mock):
139 """Test if structure of device list response has changed."""
140 resp_dict, status = (
141 {
142 'code': 0,
143 'NOTresult': {
144 'NOTlist': [
145 {
146 'deviceType': 'wifi-switch-1.3',
147 'type': 'wifi-switch',
148 'cid': 'cid1',
149 }
150 ]
151 },
152 },
153 200,
154 )
155 self.mock_api.return_value = orjson.dumps(resp_dict), status
156 self.run_in_loop(self.vesync_obj.get_devices)
157 assert len(caplog.records) == 1
158 assert 'Device list in response not found' in caplog.text
159
160 def test_7a_bad_conf(self, caplog, api_mock):
161 """Test bad device list response."""
162 self.mock_api.return_value = (orjson.dumps(BAD_DEV_LIST), 200)
163 self.run_in_loop(self.vesync_obj.get_devices)
164 assert len(caplog.records) == 2
165
166 def test_7a_no_dev_list(self, caplog, api_mock):
167 """Test if empty device list is handled correctly."""
168 empty_list = []
169
170 self.vesync_obj.process_devices(empty_list)
171
172 assert len(caplog.records) == 1
173
174 def test_get_devices_devicetype_error(self, caplog, api_mock):
175 """Test result and list keys exist but deviceType not in list."""
176 resp_dict, status = (
177 {'code': 0, 'result': {'list': [{'type': 'wifi-switch', 'cid': 'cid1'}]}},
178 200,
179 )
180 self.mock_api.return_value = orjson.dumps(resp_dict), status
181 self.run_in_loop(self.vesync_obj.get_devices)
182 assert len(caplog.records) == 2
183 assert 'Error adding device' in caplog.text
184
185 def test_unknown_device(self, caplog, api_mock):
186 """Test unknown device type is handled correctly."""
187 unknown_dev = json_vals.DeviceList.LIST_CONF_7A
188
189 unknown_dev['devType'] = 'UNKNOWN-DEVTYPE'
190
191 pyvesync.vesync.object_factory('unknown_device', unknown_dev, self.vesync_obj)
192
193 assert len(caplog.records) == 1
194 assert 'Unknown' in caplog.text
195
196 def NO_test_time_check(self, api_mock):
197 """Test device details update throttle."""
198 # The throttle feature has been removed
199 time_check = self.vesync_obj.device_time_check()
200 assert time_check is True
201 self.vesync_obj.last_update_ts = time.time()
202 time_check = self.vesync_obj.device_time_check()
203 assert time_check is False
204 self.vesync_obj.last_update_ts = (
205 time.time() - self.vesync_obj.update_interval - 1
206 )
207 time_check = self.vesync_obj.device_time_check()
208 assert time_check is True
209
210 @patch('pyvesync.vesyncoutlet.VeSyncOutlet7A', autospec=True)
211 def test_remove_device(self, outlet_patch, caplog, api_mock):
212 """Test remove device test."""
213 device = copy.deepcopy(json_vals.DeviceList.LIST_CONF_7A)
214
215 outlet_test = outlet_patch.return_value
216 outlet_test.cid = '7A-CID'
217 outlet_test.device_type = 'wifi-switch-1.3'
218 outlet_test.device_name = '7A Device'
219
220 new_list = [device]
221 self.vesync_obj.outlets = [outlet_test]
222 self.vesync_obj._remove_stale_devices(new_list) # pylint: disable=protected-access
223
224 assert self.vesync_obj.outlets == [outlet_test]
225
226 device['cid'] = '7A-CID2'
227
228 self.vesync_obj._remove_stale_devices([device]) # pylint: disable=protected-access
229
230 assert not self.vesync_obj.outlets
231
232 assert len(caplog.records) == 2
233
234 @patch('pyvesync.vesyncoutlet.VeSyncOutdoorPlug', autospec=True)
235 def test_add_dev_test(self, outdoor_patch, caplog, api_mock):
236 """Test that new devices will not be added to instance."""
237 outdoor_inst = VeSyncOutdoorPlug(
238 json_vals.DeviceList.LIST_CONF_OUTDOOR_2, self.vesync_obj
239 )
240 self.vesync_obj.outlets = [outdoor_inst]
241
242 add_test = self.vesync_obj._find_new_devices([json_vals.DeviceList.LIST_CONF_OUTDOOR_1])
243
244 assert not add_test
245
246 def test_display_func(self, caplog, api_mock):
247 """Test display function outputs text."""
248 self.vesync_obj.outlets.append(
249 VeSyncOutdoorPlug(json_vals.DeviceList.device_list_item('ESO15-TB', 0), self.vesync_obj)
250 )
251 self.vesync_obj.outlets.append(
252 VeSyncOutlet10A(json_vals.DeviceList.device_list_item('ESW01-EU'), self.vesync_obj)
253 )
254 self.vesync_obj.outlets.append(
255 VeSyncOutlet15A(json_vals.DeviceList.device_list_item('ESW15-USA'), self.vesync_obj)
256 )
257 self.vesync_obj.outlets.append(
258 VeSyncOutlet7A(json_vals.DeviceList.LIST_CONF_7A, self.vesync_obj)
259 )
260 self.vesync_obj.switches.append(
261 VeSyncWallSwitch(json_vals.DeviceList.LIST_CONF_WS, self.vesync_obj)
262 )
263 self.vesync_obj.fans.append(
264 VeSyncAir131(json_vals.DeviceList.LIST_CONF_AIR, self.vesync_obj)
265 )
266 self.vesync_obj.bulbs.append(
267 VeSyncBulbESL100(json_vals.DeviceList.LIST_CONF_ESL100, self.vesync_obj)
268 )
269
270 dev_list = [
271 self.vesync_obj.outlets,
272 self.vesync_obj.switches,
273 self.vesync_obj.fans,
274 self.vesync_obj.bulbs,
275 ]
276
277 for device in chain(*dev_list):
278 device.display()
279
280 assert len(caplog.records) == 0
281
282 @patch('pyvesync.vesyncoutlet.VeSyncOutlet7A', autospec=True)
283 @patch('pyvesync.vesyncoutlet.VeSyncOutlet15A', autospec=True)
284 @patch('pyvesync.vesyncoutlet.VeSyncOutlet10A', autospec=True)
285 @patch('pyvesync.vesyncoutlet.VeSyncOutdoorPlug', autospec=True)
286 @patch('pyvesync.vesyncbulb.VeSyncBulbESL100', autospec=True)
287 @patch('pyvesync.vesyncswitch.VeSyncWallSwitch', autospec=True)
288 @patch('pyvesync.vesyncfan.VeSyncAir131', autospec=True)
289 def test_resolve_updates(
290 self,
291 air_patch,
292 ws_patch,
293 esl100_patch,
294 outdoor_patch,
295 out10a_patch,
296 out15a_patch,
297 out7a_patch,
298 caplog,
299 api_mock,
300 ):
301 """Test process_devices() with all devices.
302 Creates vesync object with all devices and returns
303 device list with new set of all devices.
304 """
305 out10a_patch.cid = '10A-CID1'
306 out10a_patch.device_type = 'ESW10-EU'
307 out10a_patch.device_name = '10A Removed'
308
309 out15a_patch.cid = '15A-CID1'
310 out15a_patch.device_type = 'ESW15-USA'
311 out15a_patch.device_name = '15A Removed'
312
313 out7a_patch.cid = '7A-CID1'
314 out7a_patch.device_type = 'wifi-switch-1.3'
315 out7a_patch.device_name = '7A Removed'
316
317 outdoor_patch.cid = 'OUTDOOR-CID1'
318 outdoor_patch.device_type = 'ESO15-TB'
319 outdoor_patch.device_name = 'Outdoor Removed'
320
321 esl100_patch.cid = 'BULB-CID1'
322 esl100_patch.device_type = 'ESL100'
323 esl100_patch.device_name = 'Bulb Removed'
324
325 ws_patch.cid = 'WS-CID2'
326 ws_patch.device_name = 'Switch Removed'
327 ws_patch.device_type = 'ESWL01'
328
329 air_patch.cid = 'AirCID2'
330 air_patch.device_type = 'LV-PUR131S'
331 air_patch.device_name = 'fan Removed'
332
333 json_ret = json_vals.DeviceList.FULL_DEV_LIST
334
335 self.vesync_obj.outlets.extend(
336 [out7a_patch, out10a_patch, outdoor_patch]
337 )
338 self.vesync_obj.switches.extend([ws_patch])
339 self.vesync_obj.fans.extend([air_patch])
340 self.vesync_obj.bulbs.extend([esl100_patch])
341
342 self.vesync_obj.process_devices(json_ret)
343
344 assert len(self.vesync_obj.outlets) == 6
345 assert len(self.vesync_obj.switches) == 2
346 assert len(self.vesync_obj.fans) == 4
347 assert len(self.vesync_obj.bulbs) == 4