Skip to content

Commit 9b5e416

Browse files
committed
receiver: Handle unknown power switch locations again
Ensure functionality via unit test.
1 parent d8f321a commit 9b5e416

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/logitech_receiver/hidpp10_constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848

4949
class PowerSwitchLocation(IntEnum):
50+
UNKNOWN = 0x00
5051
BASE = 0x01
5152
TOP_CASE = 0x02
5253
EDGE_OF_TOP_RIGHT_CORNER = 0x03
@@ -59,6 +60,13 @@ class PowerSwitchLocation(IntEnum):
5960
LEFT_EDGE = 0x0B
6061
BOTTOM_EDGE = 0x0C
6162

63+
@classmethod
64+
def location(cls, loc: int) -> PowerSwitchLocation:
65+
try:
66+
return cls(loc)
67+
except ValueError:
68+
return cls.UNKNOWN
69+
6270

6371
class NotificationFlag(Flag):
6472
"""Some flags are used both by devices and receivers.

lib/logitech_receiver/receiver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def extract_codename(response: bytes) -> str:
108108
def extract_power_switch_location(response: bytes) -> str:
109109
"""Extracts power switch location from response."""
110110
index = response[9] & 0x0F
111-
return hidpp10_constants.PowerSwitchLocation(index).name.lower()
111+
return hidpp10_constants.PowerSwitchLocation.location(index).name.lower()
112112

113113

114114
def extract_connection_count(response: bytes) -> int:

tests/logitech_receiver/test_receiver.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,22 @@ def test_extract_codename():
259259
assert codename == "K520"
260260

261261

262-
def test_extract_power_switch_location():
263-
response = b"0\x19\x8e>\xb8\x06\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
262+
@pytest.mark.parametrize(
263+
"power_switch_byte, expected_location",
264+
[
265+
(b"\x01", "base"),
266+
(b"\x09", "top_edge"),
267+
(b"\x0c", "bottom_edge"),
268+
(b"\x00", "unknown"),
269+
(b"\x0f", "unknown"),
270+
],
271+
)
272+
def test_extract_power_switch_location(power_switch_byte, expected_location):
273+
response = b"\x19\x8e>\xb8\x06\x00\x00\x00\x00" + power_switch_byte + b"\x00\x00\x00\x00\x00"
264274

265275
ps_location = receiver.extract_power_switch_location(response)
266276

267-
assert ps_location == "base"
277+
assert ps_location == expected_location
268278

269279

270280
def test_extract_connection_count():

0 commit comments

Comments
 (0)