mopidy-raspberrypi-gpio/mopidy_raspberry_gpio/pinconfig.py

51 lines
1.4 KiB
Python
Raw Normal View History

2019-10-10 14:16:37 +02:00
from collections import namedtuple
from mopidy import config
2019-10-10 14:49:20 +02:00
class PinConfig(config.ConfigValue):
2019-11-21 19:13:31 +01:00
tuple_pinconfig = namedtuple("PinConfig", ("event", "active", "bouncetime"))
2019-10-10 14:16:37 +02:00
valid_events = "play_pause", "prev", "next", "volume_up", "volume_down"
valid_modes = "active_low", "active_high"
def __init__(self):
2019-10-10 14:49:20 +02:00
pass
2019-10-10 14:16:37 +02:00
def deserialize(self, value):
2019-10-10 14:49:20 +02:00
if value is None:
return None
value = config.decode(value).strip()
try:
2019-11-21 19:13:31 +01:00
event, active, bouncetime = value.split(",")
2019-10-10 14:49:20 +02:00
except ValueError:
return None
2019-10-10 14:16:37 +02:00
if event not in self.valid_events:
raise ValueError(
f"invalid event for pin config {event} (Must be {', '.join(self.valid_events)})"
2019-10-10 14:16:37 +02:00
)
if active not in self.valid_modes:
raise ValueError(
f"invalid event for pin config {active} (Must be {', '.join(self.valid_modes)})"
2019-10-10 14:16:37 +02:00
)
try:
bouncetime = int(bouncetime)
except ValueError:
raise ValueError(
2019-11-21 19:11:43 +01:00
f"invalid bouncetime value for pin config {bouncetime}"
2019-10-10 14:16:37 +02:00
)
return self.tuple_pinconfig(event, active, bouncetime)
def serialize(self, value, display=False):
2019-10-10 14:49:20 +02:00
if value is None:
return ""
value = f"{value.event},{value.active},{value.bouncetime}"
2019-10-10 14:49:20 +02:00
return config.encode(value)