Skip to content

Commit

Permalink
refactor(logitech_receiver/hidpp20_constants.py): replace ERROR Named…
Browse files Browse the repository at this point in the history
…Int by IntEnum.
  • Loading branch information
rloutrel committed Oct 26, 2024
1 parent f9cd676 commit a1fb813
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/logitech_receiver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from .common import LOGITECH_VENDOR_ID
from .common import BusID
from .hidpp10_constants import Error as Hidpp10Error
from .hidpp20_constants import ERROR as Hidpp20ERROR
from .hidpp20_constants import Error as Hidpp20Error

if typing.TYPE_CHECKING:
import gi
Expand Down Expand Up @@ -595,7 +595,7 @@ def request(
devnumber,
request_id,
error,
Hidpp20ERROR[error],
Hidpp20Error(error),
)
raise exceptions.FeatureCallError(
number=devnumber,
Expand Down
6 changes: 3 additions & 3 deletions lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
from .hidpp20_constants import CHARGE_STATUS
from .hidpp20_constants import CHARGE_TYPE
from .hidpp20_constants import DEVICE_KIND
from .hidpp20_constants import ERROR
from .hidpp20_constants import GESTURE
from .hidpp20_constants import Error
from .hidpp20_constants import SupportedFeature

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1823,14 +1823,14 @@ def decipher_battery_status(report: FixedBytes5) -> Tuple[Any, Battery]:
def decipher_battery_voltage(report):
voltage, flags = struct.unpack(">HB", report[:3])
status = BatteryStatus.DISCHARGING
charge_sts = ERROR.unknown
charge_sts = Error.unknown
charge_lvl = CHARGE_LEVEL.average
charge_type = CHARGE_TYPE.standard
if flags & (1 << 7):
status = BatteryStatus.RECHARGING
charge_sts = CHARGE_STATUS[flags & 0x03]
if charge_sts is None:
charge_sts = ERROR.unknown
charge_sts = Error.unknown
elif charge_sts == CHARGE_STATUS.full:
charge_lvl = CHARGE_LEVEL.full
status = BatteryStatus.FULL
Expand Down
23 changes: 12 additions & 11 deletions lib/logitech_receiver/hidpp20_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,18 @@ class FeatureFlag(IntFlag):

CHARGE_TYPE = NamedInts(standard=0x00, fast=0x01, slow=0x02)

ERROR = NamedInts(
unknown=0x01,
invalid_argument=0x02,
out_of_range=0x03,
hardware_error=0x04,
logitech_internal=0x05,
invalid_feature_index=0x06,
invalid_function=0x07,
busy=0x08,
unsupported=0x09,
)

class Error(IntEnum):
unknown = (0x01,)
invalid_argument = (0x02,)
out_of_range = (0x03,)
hardware_error = (0x04,)
logitech_internal = (0x05,)
invalid_feature_index = (0x06,)
invalid_function = (0x07,)
busy = (0x08,)
unsupported = (0x09,)


# Gesture Ids for feature GESTURE_2
GESTURE = NamedInts(
Expand Down
21 changes: 11 additions & 10 deletions tests/logitech_receiver/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from logitech_receiver import exceptions
from logitech_receiver.base import HIDPP_SHORT_MESSAGE_ID
from logitech_receiver.base import request
from logitech_receiver.hidpp10_constants import Error
from logitech_receiver.hidpp10_constants import Error as Hidpp10Error
from logitech_receiver.hidpp20_constants import Error as Hidpp20Error


@pytest.mark.parametrize(
Expand Down Expand Up @@ -125,12 +126,12 @@ def test_get_next_sw_id():
@pytest.mark.parametrize(
"prefix, error_code, return_error, raise_exception",
[
(b"\x8f", Error.invalid_SubID__command, False, False),
(b"\x8f", Error.invalid_SubID__command, True, False),
(b"\xff", Error.invalid_SubID__command, False, True),
(b"\x8f", Hidpp10Error.invalid_SubID__command, False, False),
(b"\x8f", Hidpp10Error.invalid_SubID__command, True, False),
(b"\xff", Hidpp20Error.unknown, False, True),
],
)
def test_request_errors(prefix: bytes, error_code: Error, return_error: bool, raise_exception: bool):
def test_request_errors(prefix: bytes, error_code: Hidpp10Error | Hidpp20Error, return_error: bool, raise_exception: bool):
handle = 0
device_number = 66

Expand Down Expand Up @@ -160,13 +161,13 @@ def test_request_errors(prefix: bytes, error_code: Error, return_error: bool, ra
@pytest.mark.parametrize(
"simulated_error, expected_result",
[
(Error.invalid_SubID__command, 1.0),
(Error.resource_error, None),
(Error.connection_request_failed, None),
(Error.unknown_device, exceptions.NoSuchDevice),
(Hidpp10Error.invalid_SubID__command, 1.0),
(Hidpp10Error.resource_error, None),
(Hidpp10Error.connection_request_failed, None),
(Hidpp10Error.unknown_device, exceptions.NoSuchDevice),
],
)
def test_ping_errors(simulated_error: Error, expected_result):
def test_ping_errors(simulated_error: Hidpp10Error, expected_result):
handle = 1
device_number = 1

Expand Down

0 comments on commit a1fb813

Please sign in to comment.