| 1 |
import orjson |
| 2 |
import pytest |
| 3 |
from unittest.mock import patch |
| 4 |
import logging |
| 5 |
from pyvesync import VeSync |
| 6 |
from pyvesync.devices.vesyncswitch import VeSyncWallSwitch |
| 7 |
from pyvesync.utils.helpers import Helpers as Helpers |
| 8 |
import call_json |
| 9 |
import call_json_switches |
| 10 |
from base_test_cases import TestBase |
| 11 |
|
| 12 |
DEVICE_TYPE = 'ESWL01' |
| 13 |
|
| 14 |
DEV_LIST_DETAIL = call_json.DeviceList.device_list_item(DEVICE_TYPE) |
| 15 |
|
| 16 |
CORRECT_WS_LIST = call_json.DeviceList.device_list_response(DEVICE_TYPE) |
| 17 |
|
| 18 |
CORRECT_WS_DETAILS = call_json_switches.DETAILS_RESPONSES[DEVICE_TYPE] |
| 19 |
|
| 20 |
BAD_LIST = call_json.DETAILS_BADCODE |
| 21 |
|
| 22 |
DEFAULTS = call_json.TestDefaults |
| 23 |
|
| 24 |
|
| 25 |
class TestVesyncWallSwitch(TestBase): |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
def test_ws_conf(self): |
| 42 |
"""Tests that Wall Switch is instantiated properly""" |
| 43 |
self.mock_api.return_value = CORRECT_WS_LIST |
| 44 |
self.run_in_loop(self.manager.get_devices) |
| 45 |
switch = self.manager.switches |
| 46 |
assert len(switch) == 1 |
| 47 |
wswitch = switch[0] |
| 48 |
assert isinstance(wswitch, VeSyncWallSwitch) |
| 49 |
assert wswitch.device_name == DEFAULTS.name(DEVICE_TYPE) |
| 50 |
assert wswitch.device_type == DEVICE_TYPE |
| 51 |
assert wswitch.cid == DEFAULTS.cid(DEVICE_TYPE) |
| 52 |
assert wswitch.uuid == DEFAULTS.uuid(DEVICE_TYPE) |
| 53 |
|
| 54 |
def test_ws_details(self): |
| 55 |
"""Test WS get_details() """ |
| 56 |
resp_dict, status = CORRECT_WS_DETAILS |
| 57 |
self.mock_api.return_value = orjson.dumps(resp_dict), status |
| 58 |
wswitch = VeSyncWallSwitch(DEV_LIST_DETAIL, self.manager) |
| 59 |
self.run_in_loop(wswitch.get_details) |
| 60 |
dev_details = wswitch.details |
| 61 |
assert wswitch.device_status == 'on' |
| 62 |
assert isinstance(dev_details, dict) |
| 63 |
assert dev_details['active_time'] == 1 |
| 64 |
assert wswitch.connection_status == 'online' |
| 65 |
|
| 66 |
def test_ws_details_fail(self, caplog): |
| 67 |
"""Test WS get_details with Code>0""" |
| 68 |
resp_dict, status = BAD_LIST |
| 69 |
self.mock_api.return_value = orjson.dumps(resp_dict), status |
| 70 |
vswitch15a = VeSyncWallSwitch(DEV_LIST_DETAIL, self.manager) |
| 71 |
self.run_in_loop(vswitch15a.get_details) |
| 72 |
assert len(caplog.records) == 1 |
| 73 |
assert 'details' in caplog.text |
| 74 |
|
| 75 |
def test_ws_onoff(self, caplog): |
| 76 |
"""Test 15A Device On/Off Methods""" |
| 77 |
self.mock_api.return_value = (orjson.dumps({'code': 0}), 200) |
| 78 |
wswitch = VeSyncWallSwitch(DEV_LIST_DETAIL, self.manager) |
| 79 |
head = Helpers.req_legacy_headers(self.manager) |
| 80 |
body = Helpers.req_body(self.manager, 'devicestatus') |
| 81 |
|
| 82 |
body['status'] = 'on' |
| 83 |
body['uuid'] = wswitch.uuid |
| 84 |
on = self.run_in_loop(wswitch.turn_on) |
| 85 |
self.mock_api.assert_called_with( |
| 86 |
'/inwallswitch/v1/device/devicestatus', 'put', headers=head, json_object=body |
| 87 |
) |
| 88 |
assert on |
| 89 |
off = self.run_in_loop(wswitch.turn_off) |
| 90 |
body['status'] = 'off' |
| 91 |
self.mock_api.assert_called_with( |
| 92 |
'/inwallswitch/v1/device/devicestatus', 'put', headers=head, json_object=body |
| 93 |
) |
| 94 |
assert off |
| 95 |
|
| 96 |
def test_ws_onoff_fail(self): |
| 97 |
"""Test ws On/Off Fail with Code>0""" |
| 98 |
self.mock_api.return_value = (orjson.dumps({'code': 1}), 400) |
| 99 |
vswitch15a = VeSyncWallSwitch(DEV_LIST_DETAIL, self.manager) |
| 100 |
assert not self.run_in_loop(vswitch15a.turn_on) |
| 101 |
assert not self.run_in_loop(vswitch15a.turn_off) |