receiver: Handle unknown power switch locations again

Ensure functionality via unit test.
This commit is contained in:
Peter F. Patel-Schneider 2025-02-19 05:52:30 -05:00
parent d8f321a5e9
commit 9b5e416755
3 changed files with 22 additions and 4 deletions

View File

@ -47,6 +47,7 @@ DEVICE_KIND = NamedInts(
class PowerSwitchLocation(IntEnum):
UNKNOWN = 0x00
BASE = 0x01
TOP_CASE = 0x02
EDGE_OF_TOP_RIGHT_CORNER = 0x03
@ -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.

View File

@ -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:

View File

@ -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():