From 41768d96166d892dc632f327ba3b58f043d17d5a Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:24:07 +0100 Subject: [PATCH] Test receiver notification info --- lib/logitech_receiver/receiver.py | 4 ++- tests/logitech_receiver/test_receiver.py | 31 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index 97f391fe17..8a360da7f7 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -41,6 +41,8 @@ if typing.TYPE_CHECKING: from logitech_receiver import common + from .base import HIDPPNotification + logger = logging.getLogger(__name__) _hidpp10 = hidpp10.Hidpp10() @@ -198,7 +200,7 @@ def notify_devices(self): if not self.write_register(Registers.RECEIVER_CONNECTION, 0x02): logger.warning("%s: failed to trigger device link notifications", self) - def notification_information(self, number, notification): + def notification_information(self, number, notification: HIDPPNotification) -> tuple[bool, bool, typing.Any, str]: """Extract information from unifying-style notification""" assert notification.address != 0x02 online = not bool(notification.data[0] & 0x40) diff --git a/tests/logitech_receiver/test_receiver.py b/tests/logitech_receiver/test_receiver.py index 4ea3c1bdfb..37bff645a2 100644 --- a/tests/logitech_receiver/test_receiver.py +++ b/tests/logitech_receiver/test_receiver.py @@ -12,6 +12,13 @@ from . import fake_hidpp +@pytest.fixture +def nano_recv(): + device_info = DeviceInfo("12", product_id=0xC534) + mock_low_level = LowLevelInterfaceFake(responses_lacking) + yield receiver.create_receiver(mock_low_level, device_info, lambda x: x) + + class LowLevelInterfaceFake: def __init__(self, responses=None): self.responses = responses @@ -189,3 +196,27 @@ def test_receiver_factory_no_device(device_info, responses): with pytest.raises(exceptions.NoSuchDevice): r.device_pairing_information(1) + + +@pytest.mark.parametrize( + "address, data, expected_online, expected_encrypted", + [ + (0x03, b"\x01\x02\x03", True, False), + (0x10, b"\x61\x02\x03", False, True), + ], +) +def test_notification_information_nano_receiver(nano_recv, address, data, expected_online, expected_encrypted): + _number = 0 + notification = base.HIDPPNotification( + report_id=0x01, + devnumber=0x52C, + sub_id=0, + address=address, + data=data, + ) + online, encrypted, wpid, kind = nano_recv.notification_information(_number, notification) + + assert online == expected_online + assert encrypted == expected_encrypted + assert wpid == "0302" + assert kind == "keyboard"