From e5a18757baa8d68d3cfbedd8c8ae637ebe944aea Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 25 Mar 2020 10:47:49 +0000 Subject: [PATCH] Add support for additional per pin event options --- mopidy_raspberry_gpio/pinconfig.py | 19 ++++++++++++++----- tests/test_config.py | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/mopidy_raspberry_gpio/pinconfig.py b/mopidy_raspberry_gpio/pinconfig.py index f6c7270..112e50b 100644 --- a/mopidy_raspberry_gpio/pinconfig.py +++ b/mopidy_raspberry_gpio/pinconfig.py @@ -12,7 +12,7 @@ class ValidList(list): class PinConfig(config.ConfigValue): - tuple_pinconfig = namedtuple("PinConfig", ("event", "active", "bouncetime")) + tuple_pinconfig = namedtuple("PinConfig", ("event", "active", "bouncetime", "options")) valid_events = ValidList( ["play_pause", "prev", "next", "volume_up", "volume_down"] @@ -29,11 +29,13 @@ class PinConfig(config.ConfigValue): value = types.decode(value).strip() - try: - event, active, bouncetime = value.split(",") - except ValueError: + value = value.split(",") + + if len(value) < 3: # At least Event, Active and Bouncetime settings required return None + event, active, bouncetime = value[0:3] + if event not in self.valid_events: raise ValueError( f"invalid event for pin config {event} (Must be {self.valid_events})" @@ -51,10 +53,17 @@ class PinConfig(config.ConfigValue): f"invalid bouncetime value for pin config {bouncetime}" ) - return self.tuple_pinconfig(event, active, bouncetime) + options = {} + + for option in value[3:]: + key, value = option.split("=") + options[key] = value + + return self.tuple_pinconfig(event, active, bouncetime, options) def serialize(self, value, display=False): if value is None: return "" + options = ",".join({f'{k}={v}' for k, v in value.options.items()}) value = f"{value.event},{value.active},{value.bouncetime}" return types.encode(value) diff --git a/tests/test_config.py b/tests/test_config.py index 0ca5765..0eb966d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -59,3 +59,21 @@ def test_pinconfig_invalid_bouncetime_raises_valueerror(): with pytest.raises(ValueError): bcm1 = schema["bcm1"].deserialize("play_pause,active_low,tomato") del bcm1 + + +def test_pinconfig_additional_options(): + ext = Extension() + + schema = ext.get_config_schema() + + bcm1 = schema["bcm1"].deserialize("volume_up,active_low,30,steps=1") + del bcm1 + + +def test_pinconfig_serialize(): + ext = Extension() + + schema = ext.get_config_schema() + + bcm1 = schema["bcm1"].deserialize("volume_up,active_low,30,steps=1") + assert bcm1.serialize() == "volume_up,active_low,30,steps=1"