Skip to content

Commit

Permalink
test: Extract get_kind_from_index function and test it
Browse files Browse the repository at this point in the history
Pull get_kind_from_index from class to module level and add tests.

Related #1097
  • Loading branch information
MattHag committed Mar 3, 2024
1 parent b9693a3 commit 4da9cbc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
26 changes: 26 additions & 0 deletions tests/logitech_receiver/test_receiver.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4da9cbc

Please sign in to comment.