From 2442299539970e0b6cb2a1a7d3e1f1c7d16687c8 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Sun, 15 Sep 2024 17:57:41 +0200 Subject: [PATCH] base: Simplify receiver info retrieval - Remove comments with unused receivers - Simplify receiver hardcoded info --- lib/logitech_receiver/base.py | 23 ++++++++++++----------- lib/logitech_receiver/base_usb.py | 6 +----- lib/logitech_receiver/receiver.py | 9 ++++++--- tests/logitech_receiver/test_base.py | 13 ++++++++----- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/logitech_receiver/base.py b/lib/logitech_receiver/base.py index 7685c37f..88f593eb 100644 --- a/lib/logitech_receiver/base.py +++ b/lib/logitech_receiver/base.py @@ -103,14 +103,12 @@ def other_device_check(bus_id: int, vendor_id: int, product_id: int): return _bluetooth_device(product_id) -def product_information(usb_id: int | str) -> dict: - if isinstance(usb_id, str): - usb_id = int(usb_id, 16) - - for r in base_usb.ALL: - if usb_id == r.get("product_id"): - return r - return {} +def product_information(usb_id: int) -> dict[str, Any]: + """Returns hardcoded information from USB receiver.""" + for receiver in base_usb.KNOWN_RECEIVER: + if usb_id == receiver.get("product_id"): + return receiver + raise ValueError(f"Unknown receiver type: 0x{usb_id:02X}") _SHORT_MESSAGE_SIZE = 7 @@ -147,9 +145,12 @@ def match(record, bus_id, vendor_id, product_id): ) -def filter_receivers(bus_id, vendor_id, product_id, hidpp_short=False, hidpp_long=False): - """Check that this product is a Logitech receiver and if so return the receiver record for further checking""" - for record in base_usb.ALL: # known receivers +def filter_receivers(bus_id: int, vendor_id: int, product_id: int, hidpp_short=False, hidpp_long=False): + """Check that this product is a Logitech receiver + + If so return the receiver record for further checking. + """ + for record in base_usb.KNOWN_RECEIVER: # known receivers if match(record, bus_id, vendor_id, product_id): return record if vendor_id == LOGITECH_VENDOR_ID and 0xC500 <= product_id <= 0xC5FF: # unknown receiver diff --git a/lib/logitech_receiver/base_usb.py b/lib/logitech_receiver/base_usb.py index c5f0246e..48865d8b 100644 --- a/lib/logitech_receiver/base_usb.py +++ b/lib/logitech_receiver/base_usb.py @@ -170,7 +170,6 @@ NANO_RECEIVER_C531 = _nano_receiver(0xC531) NANO_RECEIVER_C534 = _nano_receiver_max2(0xC534) NANO_RECEIVER_C535 = _nano_receiver(0xC535) # branded as Dell NANO_RECEIVER_C537 = _nano_receiver(0xC537) -# NANO_RECEIVER_C542 = _nano_receiver(0xc542) # does not use HID++ NANO_RECEIVER_6042 = _lenovo_receiver(0x6042) # Lightspeed receivers (usually sold with gaming devices) @@ -183,11 +182,9 @@ LIGHTSPEED_RECEIVER_C545 = _lightspeed_receiver(0xC545) LIGHTSPEED_RECEIVER_C547 = _lightspeed_receiver(0xC547) # EX100 old style receiver pre-unifying protocol -# EX100_27MHZ_RECEIVER_C50C = _ex100_receiver(0xc50C) # in hid/hid-ids.h EX100_27MHZ_RECEIVER_C517 = _ex100_receiver(0xC517) -# EX100_27MHZ_RECEIVER_C51B = _ex100_receiver(0xc51B) # in hid/hid-ids.h -ALL = ( +KNOWN_RECEIVER = ( BOLT_RECEIVER_C548, UNIFYING_RECEIVER_C52B, UNIFYING_RECEIVER_C532, @@ -203,7 +200,6 @@ ALL = ( NANO_RECEIVER_C534, NANO_RECEIVER_C535, NANO_RECEIVER_C537, - # NANO_RECEIVER_C542, # does not use HID++ NANO_RECEIVER_6042, LIGHTSPEED_RECEIVER_C539, LIGHTSPEED_RECEIVER_C53A, diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index b63085d8..f260807b 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -513,9 +513,12 @@ class ReceiverFactory: try: handle = base.open_path(device_info.path) if handle: - product_info = base.product_information(device_info.product_id) - if not product_info: - logger.warning("Unknown receiver type: %s", device_info.product_id) + usb_id = device_info.product_id + if isinstance(usb_id, str): + usb_id = int(usb_id, 16) + try: + product_info = base.product_information(usb_id) + except ValueError: product_info = {} kind = product_info.get("receiver_kind", "unknown") rclass = receiver_class_mapping.get(kind, Receiver) diff --git a/tests/logitech_receiver/test_base.py b/tests/logitech_receiver/test_base.py index 1df0da8e..a8a79152 100644 --- a/tests/logitech_receiver/test_base.py +++ b/tests/logitech_receiver/test_base.py @@ -6,16 +6,19 @@ from logitech_receiver import base @pytest.mark.parametrize( "usb_id, expected_name, expected_receiver_kind", [ - ("0xC548", "Bolt Receiver", "bolt"), - ("0xC52B", "Unifying Receiver", "unifying"), - ("0xC531", "Nano Receiver", "nano"), - ("0xC53F", "Lightspeed Receiver", None), - ("0xC517", "EX100 Receiver 27 Mhz", "27Mhz"), + (0xC548, "Bolt Receiver", "bolt"), + (0xC52B, "Unifying Receiver", "unifying"), + (0xC531, "Nano Receiver", "nano"), + (0xC53F, "Lightspeed Receiver", None), + (0xC517, "EX100 Receiver 27 Mhz", "27Mhz"), ], ) def test_product_information(usb_id, expected_name, expected_receiver_kind): res = base.product_information(usb_id) assert res["name"] == expected_name + assert isinstance(res["vendor_id"], int) + assert isinstance(res["product_id"], int) + if expected_receiver_kind: assert res["receiver_kind"] == expected_receiver_kind