Skip to content

Commit

Permalink
receiver: Handle unknown power switch locations again
Browse files Browse the repository at this point in the history
Ensure functionality via unit test.
  • Loading branch information
pfps committed Feb 22, 2025
1 parent d8f321a commit 9b5e416
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/logitech_receiver/hidpp10_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@


class PowerSwitchLocation(IntEnum):
UNKNOWN = 0x00
BASE = 0x01
TOP_CASE = 0x02
EDGE_OF_TOP_RIGHT_CORNER = 0x03
Expand All @@ -59,6 +60,13 @@ class PowerSwitchLocation(IntEnum):
LEFT_EDGE = 0x0B
BOTTOM_EDGE = 0x0C

@classmethod
def location(cls, loc: int) -> PowerSwitchLocation:
try:
return cls(loc)
except ValueError:
return cls.UNKNOWN


class NotificationFlag(Flag):
"""Some flags are used both by devices and receivers.
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def extract_codename(response: bytes) -> str:
def extract_power_switch_location(response: bytes) -> str:
"""Extracts power switch location from response."""
index = response[9] & 0x0F
return hidpp10_constants.PowerSwitchLocation(index).name.lower()
return hidpp10_constants.PowerSwitchLocation.location(index).name.lower()


def extract_connection_count(response: bytes) -> int:
Expand Down
16 changes: 13 additions & 3 deletions tests/logitech_receiver/test_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,22 @@ def test_extract_codename():
assert codename == "K520"


def test_extract_power_switch_location():
response = b"0\x19\x8e>\xb8\x06\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
@pytest.mark.parametrize(
"power_switch_byte, expected_location",
[
(b"\x01", "base"),
(b"\x09", "top_edge"),
(b"\x0c", "bottom_edge"),
(b"\x00", "unknown"),
(b"\x0f", "unknown"),
],
)
def test_extract_power_switch_location(power_switch_byte, expected_location):
response = b"\x19\x8e>\xb8\x06\x00\x00\x00\x00" + power_switch_byte + b"\x00\x00\x00\x00\x00"

ps_location = receiver.extract_power_switch_location(response)

assert ps_location == "base"
assert ps_location == expected_location


def test_extract_connection_count():
Expand Down

0 comments on commit 9b5e416

Please sign in to comment.