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
|
import pytest
from _testutils import MockHidapiDevice, Report
from liquidctl.driver.hwmon import HwmonDevice
from liquidctl.driver.smart_device import SmartDevice
SAMPLE_RESPONSES = [
'043e00056e00000b5b000301000007200002001e00',
'04400005b500000b5b000201000007020002001e00',
'044000053800000b5b000201000007120102001e00',
]
@pytest.fixture
def mockSmartDevice():
device = MockHidapiDevice(vendor_id=0x1e71, product_id=0x1714, address='addr')
return SmartDevice(device, 'mock NZXT Smart Device V1', speed_channel_count=3, color_channel_count=1)
# class methods
def test_smart_device_constructor(mockSmartDevice):
assert mockSmartDevice._speed_channels == {
'fan1': (0, 0, 100),
'fan2': (1, 0, 100),
'fan3': (2, 0, 100),
}
assert mockSmartDevice._color_channels == {'led': (0), }
def test_smart_device_not_totally_broken(mockSmartDevice):
dev = mockSmartDevice
for i in range(4):
dev.device.preload_read(Report(0, bytes(63)))
dev.initialize()
dev.get_status()
dev.set_color(channel='led', mode='breathing', colors=iter([[142, 24, 68]]),
speed='fastest')
dev.set_fixed_speed(channel='fan3', duty=50)
@pytest.mark.parametrize('has_hwmon,direct_access', [(False, False), (True, True), (True, False)])
def test_smart_device_initializes(mockSmartDevice, has_hwmon, direct_access, tmp_path):
dev = mockSmartDevice
if has_hwmon:
dev._hwmon = HwmonDevice('mock_module', tmp_path)
for _, capdata in enumerate(SAMPLE_RESPONSES):
capdata = bytes.fromhex(capdata)
dev.device.preload_read(Report(capdata[0], capdata[1:]))
expected = [
('Firmware version', '1.7', ''),
('LED accessories', 2, ''),
('LED accessory type', 'HUE+ Strip', ''),
('LED count (total)', 20, ''),
]
got = dev.initialize(direct_access=direct_access)
assert expected == got
writes = len(dev.device.sent)
if not has_hwmon or direct_access:
assert writes == 2
else:
assert writes == 0
@pytest.mark.parametrize('has_hwmon,direct_access', [(False, False), (True, True)])
def test_smart_device_reads_status_directly(mockSmartDevice, has_hwmon, direct_access):
dev = mockSmartDevice
if has_hwmon:
dev._hwmon = HwmonDevice(None, None)
for _, capdata in enumerate(SAMPLE_RESPONSES):
capdata = bytes.fromhex(capdata)
dev.device.preload_read(Report(capdata[0], capdata[1:]))
# skip initialize for now, we're not emulating the behavior precisely
# enough to require it here
expected = [
('Fan 1 speed', 1461, 'rpm'),
('Fan 1 voltage', 11.91, 'V'),
('Fan 1 current', 0.02, 'A'),
('Fan 1 control mode', 'PWM', ''),
('Fan 2 speed', 1336, 'rpm'),
('Fan 2 voltage', 11.91, 'V'),
('Fan 2 current', 0.02, 'A'),
('Fan 2 control mode', 'PWM', ''),
('Fan 3 speed', 1390, 'rpm'),
('Fan 3 voltage', 11.91, 'V'),
('Fan 3 current', 0.03, 'A'),
('Fan 3 control mode', None, ''),
('Noise level', 63, 'dB')
]
got = dev.get_status(direct_access=direct_access)
assert expected == got
def test_smart_device_reads_status_from_hwmon(mockSmartDevice, tmp_path):
dev = mockSmartDevice
dev._hwmon = HwmonDevice('mock_module', tmp_path)
(tmp_path / 'fan1_input').write_text('1461\n')
(tmp_path / 'in0_input').write_text('11910\n')
(tmp_path / 'curr1_input').write_text('20\n')
(tmp_path / 'pwm1_mode').write_text('1\n')
(tmp_path / 'fan2_input').write_text('1336\n')
(tmp_path / 'in1_input').write_text('11910\n')
(tmp_path / 'curr2_input').write_text('20\n')
(tmp_path / 'pwm2_mode').write_text('0\n')
(tmp_path / 'fan3_input').write_text('1390\n')
(tmp_path / 'in2_input').write_text('11910\n')
(tmp_path / 'curr3_input').write_text('30\n')
(tmp_path / 'pwm3_mode').write_text('1\n')
# skip initialize for now, we're not emulating the behavior precisely
# enough to require it here
expected = [
('Fan 1 speed', 1461, 'rpm'),
('Fan 1 voltage', 11.91, 'V'),
('Fan 1 current', 0.02, 'A'),
('Fan 1 control mode', 'PWM', ''),
('Fan 2 speed', 1336, 'rpm'),
('Fan 2 voltage', 11.91, 'V'),
('Fan 2 current', 0.02, 'A'),
('Fan 2 control mode', 'DC', ''),
('Fan 3 speed', 1390, 'rpm'),
('Fan 3 voltage', 11.91, 'V'),
('Fan 3 current', 0.03, 'A'),
('Fan 3 control mode', 'PWM', ''),
]
got = dev.get_status()
assert expected == got
|