From de5878d34e29d27115e0f3a6a4c37292a0b6ae42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Fri, 14 Jan 2022 08:22:03 -0300 Subject: [PATCH] settings: also accept "Toggle" to be consistent with CLI --- lib/logitech_receiver/settings.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/logitech_receiver/settings.py b/lib/logitech_receiver/settings.py index 1a1f4584..1e872e0d 100644 --- a/lib/logitech_receiver/settings.py +++ b/lib/logitech_receiver/settings.py @@ -42,6 +42,22 @@ SENSITIVITY_IGNORE = 'ignore' KIND = _NamedInts(toggle=0x01, choice=0x02, range=0x04, map_choice=0x0A, multiple_toggle=0x10, multiple_range=0x40) +def bool_or_toggle(current, new): + if isinstance(new, bool): + return new + try: + return bool(int(new)) + except (TypeError, ValueError): + new = str(new).lower() + if new in ('true', 'yes', 'on', 't', 'y'): + return True + if new in ('false', 'no', 'off', 'f', 'n'): + return False + if new in ('~', 'toggle'): + return not current + return None + + class Setting: """A setting descriptor. Needs to be instantiated for each specific device.""" @@ -726,8 +742,9 @@ class BooleanValidator: def acceptable(self, args, current): if len(args) != 1: return None - val = [args[0]] if type(args[0]) == bool else [not current] if args[0] == '~' else None - return val + + val = bool_or_toggle(current, args[0]) + return [val] if val is not None else None class BitFieldValidator: @@ -770,7 +787,7 @@ class BitFieldValidator: key = next((key for key in self.options if key == args[0]), None) if key is None: return None - val = args[1] if type(args[1]) == bool else not current[str(int(key))] if args[1] == '~' else None + val = bool_or_toggle(current[str(int(key))], args[1]) return None if val is None else [str(int(key)), val] @@ -861,7 +878,7 @@ class BitFieldWithOffsetAndMaskValidator: key = next((option.id for option in self.options if option.as_int() == args[0]), None) if key is None: return None - val = args[1] if type(args[1]) == bool else not current[str(int(key))] if args[1] == '~' else None + val = bool_or_toggle(current[str(int(key))], args[1]) return None if val is None else [str(key), val]