Add support for additional per pin event options

remotes/origin/extra-event-options
Phil Howard 2020-03-25 10:47:49 +00:00
parent e40219c254
commit e5a18757ba
2 changed files with 32 additions and 5 deletions

View File

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

View File

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