diff --git a/lib/logitech_receiver/common.py b/lib/logitech_receiver/common.py index 6fce9c603f..6471570210 100644 --- a/lib/logitech_receiver/common.py +++ b/lib/logitech_receiver/common.py @@ -619,3 +619,13 @@ class Alert(IntEnum): SHOW_WINDOW = 0x02 ATTENTION = 0x04 ALL = 0xFF + + +class Notification(IntEnum): + NO_OPERATION = 0x00 + CONNECT_DISCONNECT = 0x40 + DJ_PAIRING = 0x41 + CONNECTED = 0x42 + RAW_INPUT = 0x49 + PAIRING_LOCK = 0x4A + POWER = 0x4B diff --git a/lib/logitech_receiver/notifications.py b/lib/logitech_receiver/notifications.py index 0f8c68bada..5d4afa9ed1 100644 --- a/lib/logitech_receiver/notifications.py +++ b/lib/logitech_receiver/notifications.py @@ -33,6 +33,7 @@ from . import settings_templates from .common import Alert from .common import BatteryStatus +from .common import Notification logger = logging.getLogger(__name__) @@ -58,7 +59,7 @@ def _process_receiver_notification(receiver, n): # supposedly only 0x4x notifications arrive for the receiver assert n.sub_id & 0x40 == 0x40 - if n.sub_id == 0x4A: # pairing lock notification + if n.sub_id == Notification.PAIRING_LOCK: receiver.pairing.lock_open = bool(n.address & 0x01) reason = _("pairing lock is open") if receiver.pairing.lock_open else _("pairing lock is closed") if logger.isEnabledFor(logging.INFO): @@ -147,7 +148,8 @@ def _process_device_notification(device, n): # incoming packets with SubId >= 0x80 are supposedly replies from HID++ 1.0 requests, should never get here assert n.sub_id & 0x80 == 0 - if n.sub_id == 00: # no-op feature notification, dispose of it quickly + if n.sub_id == Notification.NO_OPERATION: + # dispose it return False # Allow the device object to handle the notification using custom per-device state. @@ -188,19 +190,19 @@ def _process_dj_notification(device, n): if logger.isEnabledFor(logging.DEBUG): logger.debug("%s (%s) DJ %s", device, device.protocol, n) - if n.sub_id == 0x40: + if n.sub_id == Notification.CONNECT_DISCONNECT: # do all DJ paired notifications also show up as HID++ 1.0 notifications? if logger.isEnabledFor(logging.INFO): logger.info("%s: ignoring DJ unpaired: %s", device, n) return True - if n.sub_id == 0x41: + if n.sub_id == Notification.DJ_PAIRING: # do all DJ paired notifications also show up as HID++ 1.0 notifications? if logger.isEnabledFor(logging.INFO): logger.info("%s: ignoring DJ paired: %s", device, n) return True - if n.sub_id == 0x42: + if n.sub_id == Notification.CONNECTED: connected = not n.address & 0x01 if logger.isEnabledFor(logging.INFO): logger.info("%s: DJ connection: %s %s", device, connected, n) @@ -224,7 +226,7 @@ def _process_hidpp10_custom_notification(device, n): def _process_hidpp10_notification(device, n): - if n.sub_id == 0x40: # device unpairing + if n.sub_id == Notification.CONNECT_DISCONNECT: # device unpairing if n.address == 0x02: # device un-paired device.wpid = None @@ -236,7 +238,7 @@ def _process_hidpp10_notification(device, n): logger.warning("%s: disconnection with unknown type %02X: %s", device, n.address, n) return True - if n.sub_id == 0x41: # device connection (and disconnection) + if n.sub_id == Notification.DJ_PAIRING: # device connection (and disconnection) flags = ord(n.data[:1]) & 0xF0 if n.address == 0x02: # very old 27 MHz protocol wpid = "00" + common.strhex(n.data[2:3]) @@ -267,13 +269,13 @@ def _process_hidpp10_notification(device, n): device.changed(active=link_established) return True - if n.sub_id == 0x49: + if n.sub_id == Notification.RAW_INPUT: # raw input event? just ignore it # if n.address == 0x01, no idea what it is, but they keep on coming # if n.address == 0x03, appears to be an actual input event, because they only come when input happents return True - if n.sub_id == 0x4B: # power notification + if n.sub_id == Notification.POWER: if n.address == 0x01: if logger.isEnabledFor(logging.DEBUG): logger.debug("%s: device powered on", device) diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index b5b91ad60e..c8b8eed47d 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -35,6 +35,7 @@ from . import hidpp10 from . import hidpp10_constants from .common import Alert +from .common import Notification from .device import Device logger = logging.getLogger(__name__) @@ -230,7 +231,7 @@ def register_new_device(self, number, notification=None): raise IndexError(f"{self}: device number {int(number)} already registered") assert notification is None or notification.devnumber == number - assert notification is None or notification.sub_id == 0x41 + assert notification is None or notification.sub_id == Notification.DJ_PAIRING try: time.sleep(0.05) # let receiver settle