From 571cdb5f2d5dc0b08e104e7be4af66d6377b85bd Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Sat, 16 Nov 2024 15:27:53 +0100 Subject: [PATCH] Prepare refactoring of NotificationFlag Ensure behavior stays the same. Related #2273 --- lib/logitech_receiver/hidpp10_constants.py | 12 ++++++++++++ lib/solaar/ui/window.py | 6 ++---- tests/logitech_receiver/test_hidpp10.py | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/logitech_receiver/hidpp10_constants.py b/lib/logitech_receiver/hidpp10_constants.py index 6541014a..92b78863 100644 --- a/lib/logitech_receiver/hidpp10_constants.py +++ b/lib/logitech_receiver/hidpp10_constants.py @@ -14,6 +14,8 @@ ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from __future__ import annotations + from enum import IntEnum from .common import NamedInts @@ -89,6 +91,16 @@ NOTIFICATION_FLAG = NamedInts( ) +def flags_to_str(flag_bits: int | None, fallback: str) -> str: + flag_names = [] + if flag_bits is not None: + if flag_bits == 0: + flag_names = (fallback,) + else: + flag_names = list(NOTIFICATION_FLAG.flag_names(flag_bits)) + return f"\n{' ':15}".join(flag_names) + + class ErrorCode(IntEnum): INVALID_SUB_ID_COMMAND = 0x01 INVALID_ADDRESS = 0x02 diff --git a/lib/solaar/ui/window.py b/lib/solaar/ui/window.py index b76a717d..bc911a5a 100644 --- a/lib/solaar/ui/window.py +++ b/lib/solaar/ui/window.py @@ -551,10 +551,8 @@ def _update_details(button): flag_bits = device.notification_flags if flag_bits is not None: - flag_names = ( - (f"({_('none')})",) if flag_bits == 0 else hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits) - ) - yield _("Notifications"), f"\n{' ':15}".join(flag_names) + flag_names = hidpp10_constants.flags_to_names(flag_bits, fallback=f"({_('none')})") + yield _("Notifications"), flag_names def _set_details(text): _details._text.set_markup(text) diff --git a/tests/logitech_receiver/test_hidpp10.py b/tests/logitech_receiver/test_hidpp10.py index 707d521e..6280ae27 100644 --- a/tests/logitech_receiver/test_hidpp10.py +++ b/tests/logitech_receiver/test_hidpp10.py @@ -274,6 +274,25 @@ def test_set_notification_flags_bad(mocker): assert result is None +@pytest.mark.parametrize( + "flag_bits, expected_names", + [ + (None, ""), + (0x0, "none"), + (0x009020, "multi touch\n unknown:008020"), + (0x080000, "mouse extra buttons"), + ( + 0x080000 + 0x000400, + ("link quality\n mouse extra buttons"), + ), + ], +) +def test_notification_flag_str(flag_bits, expected_names): + flag_names = hidpp10_constants.flags_to_str(flag_bits, fallback="none") + + assert flag_names == expected_names + + def test_get_device_features(): result = _hidpp10.get_device_features(device_standard)