Skip to content

Commit

Permalink
receiver: Test extraction of serial and max. devices
Browse files Browse the repository at this point in the history
Related #2273
  • Loading branch information
MattHag committed Dec 1, 2024
1 parent 2157fdb commit 8c67fbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ class Pairing:
error: Optional[any] = None


def extract_serial(response: bytes) -> str:
"""Extracts serial number from received response."""
serial_number = response[1:5].hex().upper()
return serial_number


def extract_max_devices(response: bytes) -> int:
"""Extracts maximum number of supported devices from response."""
max_devices = response[6]
return int(max_devices)


class Receiver:
"""A generic Receiver instance, mostly implementing the interface used on Unifying, Nano, and LightSpeed receivers"
The paired devices are available through the sequence interface.
Expand Down Expand Up @@ -126,9 +138,9 @@ def initialize(self, product_info: dict):
# read the receiver information subregister, so we can find out max_devices
serial_reply = self.read_register(Registers.RECEIVER_INFO, _IR.receiver_information)
if serial_reply:
self.serial = serial_reply[1:5].hex().upper()
self.max_devices = serial_reply[6]
if self.max_devices <= 0 or self.max_devices > 6:
self.serial = extract_serial(serial_reply)
self.max_devices = extract_max_devices(serial_reply)
if not (1 <= self.max_devices <= 6):
self.max_devices = product_info.get("max_devices", 1)
else: # handle receivers that don't have a serial number specially (i.e., c534)
self.serial = None
Expand Down
16 changes: 16 additions & 0 deletions tests/logitech_receiver/test_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,19 @@ def test_receiver_factory_no_device(device_info, responses):

with pytest.raises(exceptions.NoSuchDevice):
r.device_pairing_information(1)


def test_extract_serial_number():
response = b'\x03\x16\xcc\x9c\xb4\x05\x06"\x00\x00\x00\x00\x00\x00\x00\x00'

serial_number = receiver.extract_serial(response)

assert serial_number == "16CC9CB4"


def test_extract_max_devices():
response = b'\x03\x16\xcc\x9c\xb4\x05\x06"\x00\x00\x00\x00\x00\x00\x00\x00'

max_devices = receiver.extract_max_devices(response)

assert max_devices == 6

0 comments on commit 8c67fbd

Please sign in to comment.