Skip to content

Commit

Permalink
Prepare refactoring of NotificationFlag
Browse files Browse the repository at this point in the history
Ensure behavior stays the same.

Related #2273
  • Loading branch information
MattHag committed Nov 16, 2024
1 parent 18cfa45 commit f71ddaf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/logitech_receiver/hidpp10_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +91,16 @@ class PowerSwitchLocation(IntEnum):
)


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
Expand Down
6 changes: 2 additions & 4 deletions lib/solaar/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,8 @@ def _details_items(device, read_all=False):

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)
Expand Down
19 changes: 19 additions & 0 deletions tests/logitech_receiver/test_hidpp10.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit f71ddaf

Please sign in to comment.