From 8c67fbd4b8fa141b5737002573585535e7e083e0 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Sun, 1 Dec 2024 20:22:09 +0100 Subject: [PATCH] receiver: Test extraction of serial and max. devices Related #2273 --- lib/logitech_receiver/receiver.py | 18 +++++++++++++++--- tests/logitech_receiver/test_receiver.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index 97f391fe1..a1a7415fc 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -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. @@ -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 diff --git a/tests/logitech_receiver/test_receiver.py b/tests/logitech_receiver/test_receiver.py index 4ea3c1bdf..f7a44e447 100644 --- a/tests/logitech_receiver/test_receiver.py +++ b/tests/logitech_receiver/test_receiver.py @@ -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