diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index a0c97c7810..81002ef2e1 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -192,7 +192,7 @@ def device_pairing_information(self, n: int) -> dict: if not wpid: logger.error("Unable to get wpid from udev for device %d of %s", n, self) raise exceptions.NoSuchDevice(number=n, receiver=self, error="Not present 27Mhz device") - kind = hidpp10_constants.DEVICE_KIND[self.get_kind_from_index(n)] + kind = hidpp10_constants.DEVICE_KIND[get_kind_from_index(receiver=self, index=n)] elif not self.receiver_kind == "unifying": # may be an old Nano receiver device_info = self.read_register(_R.receiver_info, 0x04) if device_info: @@ -211,22 +211,6 @@ def device_pairing_information(self, n: int) -> dict: serial = pair_info[1:5].hex().upper() return {"wpid": wpid, "kind": kind, "polling": polling_rate, "serial": serial, "power_switch": power_switch} - def get_kind_from_index(self, index): - """Get device kind from 27Mhz device index""" - # From drivers/hid/hid-logitech-dj.c - if index == 1: # mouse - kind = 2 - elif index == 2: # mouse - kind = 2 - elif index == 3: # keyboard - kind = 1 - elif index == 4: # numpad - kind = 3 - else: # unknown device number on 27Mhz receiver - logger.error("failed to calculate device kind for device %d of %s", index, self) - raise exceptions.NoSuchDevice(number=index, receiver=self, error="Unknown 27Mhz device number") - return kind - def notify_devices(self): """Scan all devices.""" if self.handle: @@ -396,3 +380,23 @@ def __str__(self): __repr__ = __str__ __bool__ = __nonzero__ = lambda self: self.handle is not None + + +def get_kind_from_index( + index: int, + receiver: Receiver, +) -> int: + """Get device kind from 27Mhz device index""" + # From drivers/hid/hid-logitech-dj.c + if index == 1: # mouse + kind = 2 + elif index == 2: # mouse + kind = 2 + elif index == 3: # keyboard + kind = 1 + elif index == 4: # numpad + kind = 3 + else: # unknown device number on 27Mhz receiver + logger.error("failed to calculate device kind for device %d of %s", index, receiver) + raise exceptions.NoSuchDevice(number=index, receiver=receiver, error="Unknown 27Mhz device number") + return kind diff --git a/tests/logitech_receiver/test_receiver.py b/tests/logitech_receiver/test_receiver.py new file mode 100644 index 0000000000..563c4a59d7 --- /dev/null +++ b/tests/logitech_receiver/test_receiver.py @@ -0,0 +1,26 @@ +from unittest import mock + +import pytest + +from logitech_receiver import exceptions, receiver + + +@pytest.mark.parametrize( + "index, expected_kind", + [ + (0, None), + (1, 2), # mouse + (2, 2), # mouse + (3, 1), # keyboard + (4, 3), # numpad + (5, None), + ], +) +def test_get_kind_from_index(index, expected_kind): + mock_receiver = mock.Mock() + + if expected_kind: + assert receiver.get_kind_from_index(index, mock_receiver) == expected_kind + else: + with pytest.raises(exceptions.NoSuchDevice): + receiver.get_kind_from_index(index, mock_receiver)