base: Simplify receiver info retrieval

- Remove comments with unused receivers
- Simplify receiver hardcoded info
This commit is contained in:
MattHag 2024-09-15 17:57:41 +02:00 committed by Peter F. Patel-Schneider
parent 2aa462532a
commit 2442299539
4 changed files with 27 additions and 24 deletions

View File

@ -103,14 +103,12 @@ def other_device_check(bus_id: int, vendor_id: int, product_id: int):
return _bluetooth_device(product_id) return _bluetooth_device(product_id)
def product_information(usb_id: int | str) -> dict: def product_information(usb_id: int) -> dict[str, Any]:
if isinstance(usb_id, str): """Returns hardcoded information from USB receiver."""
usb_id = int(usb_id, 16) for receiver in base_usb.KNOWN_RECEIVER:
if usb_id == receiver.get("product_id"):
for r in base_usb.ALL: return receiver
if usb_id == r.get("product_id"): raise ValueError(f"Unknown receiver type: 0x{usb_id:02X}")
return r
return {}
_SHORT_MESSAGE_SIZE = 7 _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): 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 and if so return the receiver record for further checking""" """Check that this product is a Logitech receiver
for record in base_usb.ALL: # known receivers
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): if match(record, bus_id, vendor_id, product_id):
return record return record
if vendor_id == LOGITECH_VENDOR_ID and 0xC500 <= product_id <= 0xC5FF: # unknown receiver if vendor_id == LOGITECH_VENDOR_ID and 0xC500 <= product_id <= 0xC5FF: # unknown receiver

View File

@ -170,7 +170,6 @@ NANO_RECEIVER_C531 = _nano_receiver(0xC531)
NANO_RECEIVER_C534 = _nano_receiver_max2(0xC534) NANO_RECEIVER_C534 = _nano_receiver_max2(0xC534)
NANO_RECEIVER_C535 = _nano_receiver(0xC535) # branded as Dell NANO_RECEIVER_C535 = _nano_receiver(0xC535) # branded as Dell
NANO_RECEIVER_C537 = _nano_receiver(0xC537) NANO_RECEIVER_C537 = _nano_receiver(0xC537)
# NANO_RECEIVER_C542 = _nano_receiver(0xc542) # does not use HID++
NANO_RECEIVER_6042 = _lenovo_receiver(0x6042) NANO_RECEIVER_6042 = _lenovo_receiver(0x6042)
# Lightspeed receivers (usually sold with gaming devices) # Lightspeed receivers (usually sold with gaming devices)
@ -183,11 +182,9 @@ LIGHTSPEED_RECEIVER_C545 = _lightspeed_receiver(0xC545)
LIGHTSPEED_RECEIVER_C547 = _lightspeed_receiver(0xC547) LIGHTSPEED_RECEIVER_C547 = _lightspeed_receiver(0xC547)
# EX100 old style receiver pre-unifying protocol # 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_C517 = _ex100_receiver(0xC517)
# EX100_27MHZ_RECEIVER_C51B = _ex100_receiver(0xc51B) # in hid/hid-ids.h
ALL = ( KNOWN_RECEIVER = (
BOLT_RECEIVER_C548, BOLT_RECEIVER_C548,
UNIFYING_RECEIVER_C52B, UNIFYING_RECEIVER_C52B,
UNIFYING_RECEIVER_C532, UNIFYING_RECEIVER_C532,
@ -203,7 +200,6 @@ ALL = (
NANO_RECEIVER_C534, NANO_RECEIVER_C534,
NANO_RECEIVER_C535, NANO_RECEIVER_C535,
NANO_RECEIVER_C537, NANO_RECEIVER_C537,
# NANO_RECEIVER_C542, # does not use HID++
NANO_RECEIVER_6042, NANO_RECEIVER_6042,
LIGHTSPEED_RECEIVER_C539, LIGHTSPEED_RECEIVER_C539,
LIGHTSPEED_RECEIVER_C53A, LIGHTSPEED_RECEIVER_C53A,

View File

@ -513,9 +513,12 @@ class ReceiverFactory:
try: try:
handle = base.open_path(device_info.path) handle = base.open_path(device_info.path)
if handle: if handle:
product_info = base.product_information(device_info.product_id) usb_id = device_info.product_id
if not product_info: if isinstance(usb_id, str):
logger.warning("Unknown receiver type: %s", device_info.product_id) usb_id = int(usb_id, 16)
try:
product_info = base.product_information(usb_id)
except ValueError:
product_info = {} product_info = {}
kind = product_info.get("receiver_kind", "unknown") kind = product_info.get("receiver_kind", "unknown")
rclass = receiver_class_mapping.get(kind, Receiver) rclass = receiver_class_mapping.get(kind, Receiver)

View File

@ -6,16 +6,19 @@ from logitech_receiver import base
@pytest.mark.parametrize( @pytest.mark.parametrize(
"usb_id, expected_name, expected_receiver_kind", "usb_id, expected_name, expected_receiver_kind",
[ [
("0xC548", "Bolt Receiver", "bolt"), (0xC548, "Bolt Receiver", "bolt"),
("0xC52B", "Unifying Receiver", "unifying"), (0xC52B, "Unifying Receiver", "unifying"),
("0xC531", "Nano Receiver", "nano"), (0xC531, "Nano Receiver", "nano"),
("0xC53F", "Lightspeed Receiver", None), (0xC53F, "Lightspeed Receiver", None),
("0xC517", "EX100 Receiver 27 Mhz", "27Mhz"), (0xC517, "EX100 Receiver 27 Mhz", "27Mhz"),
], ],
) )
def test_product_information(usb_id, expected_name, expected_receiver_kind): def test_product_information(usb_id, expected_name, expected_receiver_kind):
res = base.product_information(usb_id) res = base.product_information(usb_id)
assert res["name"] == expected_name assert res["name"] == expected_name
assert isinstance(res["vendor_id"], int)
assert isinstance(res["product_id"], int)
if expected_receiver_kind: if expected_receiver_kind:
assert res["receiver_kind"] == expected_receiver_kind assert res["receiver_kind"] == expected_receiver_kind