def _send_command(self, feature, command, data=None): # self.device.write expects buf[0] to be the report number or 0 if not used buf = bytearray(_REPORT_LENGTH + 1) buf[1] = _WRITE_PREFIX buf[2] = next(self._sequence) << 3 if feature is not None: buf[2] |= feature buf[3] = command start_at = 4 else: buf[2] |= command start_at = 3 if data: assert len(data) <= _REPORT_LENGTH - start_at, 'data does not fit' buf[start_at:start_at + len(data)] = data buf[-1] = compute_pec(buf[2:-1]) LOGGER.debug('write %s', buf.hex()) self.device.clear_enqueued_reports() self.device.write(buf) buf = bytes(self.device.read(_REPORT_LENGTH)) self.device.release() LOGGER.debug('received %s', buf.hex()) if compute_pec(buf[1:]): LOGGER.warning('response checksum does not match data') return buf
def test_command_format(self): self.device._data.store('sequence', None) self.device.initialize() self.device.get_status() self.device.set_fixed_speed(channel='fan', duty=100) self.device.set_speed_profile(channel='fan', profile=[]) self.device.set_color(channel='led', mode='off', colors=[]) self.assertEqual(len(self.mock_hid.sent), 6) for i, (report, data) in enumerate(self.mock_hid.sent): self.assertEqual(report, 0) self.assertEqual(len(data), 64) self.assertEqual(data[0], 0x3f) self.assertEqual(data[1] >> 3, i + 1) self.assertEqual(data[-1], compute_pec(data[1:-1]))
def read(self, length): pre = super().read(length) if pre: return pre buf = bytearray(64) buf[2] = self.fw_version[0] << 4 | self.fw_version[1] buf[3] = self.fw_version[2] buf[7] = int((self.temperature - int(self.temperature)) * 255) buf[8] = int(self.temperature) buf[15:17] = self.fan1_speed.to_bytes(length=2, byteorder='little') buf[22:24] = self.fan2_speed.to_bytes(length=2, byteorder='little') buf[29:31] = self.pump_speed.to_bytes(length=2, byteorder='little') buf[-1] = compute_pec(buf[1:-1]) return buf[:length]
def test_h115i_platinum_device_command_format(h115iPlatinumDevice): dev = h115iPlatinumDevice dev.initialize() dev.get_status() dev.set_fixed_speed(channel='fan', duty=100) dev.set_speed_profile(channel='fan', profile=[]) dev.set_color(channel='led', mode='off', colors=[]) assert len(dev.device.sent) == 9 for i, (report, data) in enumerate(dev.device.sent): assert report == 0 assert len(data) == 64 assert data[0] == 0x3f assert data[1] >> 3 == i + 1 assert data[-1] == compute_pec(data[1:-1])
def test_h115i_platinum_device_command_format_enabled(h115iPlatinumDevice): dev = h115iPlatinumDevice # test that the led enable messages are not sent if they are sent again dev.initialize() dev._data.store('leds_enabled', 1) dev.get_status() dev.set_fixed_speed(channel='fan', duty=100) dev.set_speed_profile(channel='fan', profile=[]) dev.set_color(channel='led', mode='off', colors=[]) assert len(dev.device.sent) == 6 for i, (report, data) in enumerate(dev.device.sent): assert report == 0 assert len(data) == 64 assert data[0] == 0x3f assert data[1] >> 3 == i + 1 assert data[-1] == compute_pec(data[1:-1])