2019-11-21 20:06:13 +01:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from mopidy import config
|
|
|
|
from mopidy.config import types
|
|
|
|
|
|
|
|
|
|
|
|
class ValidList(list):
|
|
|
|
def __format__(self, format_string=None):
|
|
|
|
if format_string is None:
|
|
|
|
format_string = ", "
|
|
|
|
return format_string.join(self)
|
|
|
|
|
|
|
|
|
|
|
|
class PinConfig(config.ConfigValue):
|
2020-03-25 12:08:10 +01:00
|
|
|
tuple_pinconfig = namedtuple(
|
|
|
|
"PinConfig", ("event", "active", "bouncetime", "options")
|
|
|
|
)
|
2019-11-21 20:06:13 +01:00
|
|
|
|
2020-01-23 13:46:18 +01:00
|
|
|
valid_events = ValidList(
|
2020-07-15 06:37:24 +02:00
|
|
|
["play_pause", "play_stop", "prev", "next", "volume_up", "volume_down"]
|
2020-01-23 13:46:18 +01:00
|
|
|
)
|
2019-11-21 20:06:13 +01:00
|
|
|
|
|
|
|
valid_modes = ValidList(["active_low", "active_high"])
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def deserialize(self, value):
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
value = types.decode(value).strip()
|
|
|
|
|
2020-03-25 11:47:49 +01:00
|
|
|
value = value.split(",")
|
|
|
|
|
2020-03-25 12:08:10 +01:00
|
|
|
# At least Event, Active and Bouncetime settings required
|
|
|
|
if len(value) < 3:
|
2019-11-21 20:06:13 +01:00
|
|
|
return None
|
|
|
|
|
2020-03-25 11:47:49 +01:00
|
|
|
event, active, bouncetime = value[0:3]
|
|
|
|
|
2019-11-21 20:06:13 +01:00
|
|
|
if event not in self.valid_events:
|
|
|
|
raise ValueError(
|
2019-11-21 20:54:46 +01:00
|
|
|
f"invalid event for pin config {event} (Must be {self.valid_events})"
|
2019-11-21 20:06:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if active not in self.valid_modes:
|
|
|
|
raise ValueError(
|
2019-11-21 20:54:46 +01:00
|
|
|
f"invalid event for pin config {active} (Must be one of {self.valid_modes})"
|
2019-11-21 20:06:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
bouncetime = int(bouncetime)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(
|
|
|
|
f"invalid bouncetime value for pin config {bouncetime}"
|
|
|
|
)
|
|
|
|
|
2020-03-25 11:47:49 +01:00
|
|
|
options = {}
|
|
|
|
|
|
|
|
for option in value[3:]:
|
|
|
|
key, value = option.split("=")
|
|
|
|
options[key] = value
|
|
|
|
|
|
|
|
return self.tuple_pinconfig(event, active, bouncetime, options)
|
2019-11-21 20:06:13 +01:00
|
|
|
|
|
|
|
def serialize(self, value, display=False):
|
|
|
|
if value is None:
|
|
|
|
return ""
|
2020-03-25 12:08:10 +01:00
|
|
|
options = ",".join({f"{k}={v}" for k, v in value.options.items()})
|
|
|
|
value = f"{value.event},{value.active},{value.bouncetime},{options}"
|
2019-11-21 20:06:13 +01:00
|
|
|
return types.encode(value)
|