Improve base module

Use clearer names and type hints.
This commit is contained in:
MattHag 2024-05-28 02:47:51 +02:00 committed by Peter F. Patel-Schneider
parent e316ed1bc2
commit 9726b93a78
1 changed files with 19 additions and 11 deletions

View File

@ -27,6 +27,7 @@ import threading
from contextlib import contextmanager from contextlib import contextmanager
from random import getrandbits from random import getrandbits
from time import time from time import time
from typing import Any
import hidapi import hidapi
@ -56,34 +57,41 @@ class HIDPPNotification:
return f"Notification({self.report_id:02x},{self.devnumber},{self.sub_id:02X},{self.address:02X},{text_as_hex})" return f"Notification({self.report_id:02x},{self.devnumber},{self.sub_id:02X},{self.address:02X},{text_as_hex})"
def _wired_device(product_id, interface): def _usb_device(product_id: int, usb_interface: int) -> dict[str, Any]:
return {"vendor_id": 1133, "product_id": product_id, "bus_id": 3, "usb_interface": interface, "isDevice": True} return {
"vendor_id": 1133,
"product_id": product_id,
"bus_id": 3,
"usb_interface": usb_interface,
"isDevice": True,
}
def _bt_device(product_id): def _bluetooth_device(product_id: int) -> dict[str, Any]:
return {"vendor_id": 1133, "product_id": product_id, "bus_id": 5, "isDevice": True} return {"vendor_id": 1133, "product_id": product_id, "bus_id": 5, "isDevice": True}
DEVICE_IDS = [] KNOWN_DEVICE_IDS = []
for _ignore, d in descriptors.DEVICES.items(): for _ignore, d in descriptors.DEVICES.items():
if d.usbid: if d.usbid:
DEVICE_IDS.append(_wired_device(d.usbid, d.interface if d.interface else 2)) usb_interface = d.interface if d.interface else 2
KNOWN_DEVICE_IDS.append(_usb_device(d.usbid, usb_interface))
if d.btid: if d.btid:
DEVICE_IDS.append(_bt_device(d.btid)) KNOWN_DEVICE_IDS.append(_bluetooth_device(d.btid))
def other_device_check(bus_id, vendor_id, product_id): def other_device_check(bus_id: int, vendor_id: int, product_id: int):
"""Check whether product is a Logitech USB-connected or Bluetooth device based on bus, vendor, and product IDs """Check whether product is a Logitech USB-connected or Bluetooth device based on bus, vendor, and product IDs
This allows Solaar to support receiverless HID++ 2.0 devices that it knows nothing about""" This allows Solaar to support receiverless HID++ 2.0 devices that it knows nothing about"""
if vendor_id != 0x46D: # Logitech if vendor_id != 0x46D: # Logitech
return return
if bus_id == 0x3: # USB if bus_id == 0x3: # USB
if product_id >= 0xC07D and product_id <= 0xC094 or product_id >= 0xC32B and product_id <= 0xC344: if product_id >= 0xC07D and product_id <= 0xC094 or product_id >= 0xC32B and product_id <= 0xC344:
return _wired_device(product_id, 2) return _usb_device(product_id, 2)
elif bus_id == 0x5: # Bluetooth elif bus_id == 0x5: # Bluetooth
if product_id >= 0xB012 and product_id <= 0xB0FF or product_id >= 0xB317 and product_id <= 0xB3FF: if product_id >= 0xB012 and product_id <= 0xB0FF or product_id >= 0xB317 and product_id <= 0xB3FF:
return _bt_device(product_id) return _bluetooth_device(product_id)
def product_information(usb_id: int | str) -> dict: def product_information(usb_id: int | str) -> dict:
@ -149,7 +157,7 @@ def filter(bus_id, vendor_id, product_id, hidpp_short=False, hidpp_long=False):
record = filter_receivers(bus_id, vendor_id, product_id, hidpp_short, hidpp_long) record = filter_receivers(bus_id, vendor_id, product_id, hidpp_short, hidpp_long)
if record: # known or unknown receiver if record: # known or unknown receiver
return record return record
for record in DEVICE_IDS: # known devices for record in KNOWN_DEVICE_IDS:
if match(record, bus_id, vendor_id, product_id): if match(record, bus_id, vendor_id, product_id):
return record return record
if hidpp_short or hidpp_long: # unknown devices that use HID++ if hidpp_short or hidpp_long: # unknown devices that use HID++
@ -340,7 +348,7 @@ def _skip_incoming(handle, ihandle, notifications_hook):
def make_notification(report_id, devnumber, data) -> HIDPPNotification | None: def make_notification(report_id, devnumber, data) -> HIDPPNotification | None:
"""Guess if this is a notification (and not just a request reply), and """Guess if this is a notification (and not just a request reply), and
return a Notification tuple if it is.""" return a Notification if it is."""
sub_id = ord(data[:1]) sub_id = ord(data[:1])
if sub_id & 0x80 == 0x80: if sub_id & 0x80 == 0x80: