xair-api-python/xair_api/config.py

201 lines
5.3 KiB
Python
Raw Permalink Normal View History

2022-04-05 20:05:55 +01:00
import abc
2024-02-08 13:44:55 +00:00
from . import kinds, util
from .errors import XAirRemoteError
2022-04-05 20:05:55 +01:00
from .meta import bool_prop
class IConfig(abc.ABC):
"""Abstract Base Class for config"""
def __init__(self, remote):
self._remote = remote
def getter(self, param: str):
return self._remote.query(f"{self.address}/{param}")
2022-04-05 20:05:55 +01:00
def setter(self, param: str, val: int):
self._remote.send(f"{self.address}/{param}", val)
@abc.abstractmethod
def address(self):
pass
class Config(IConfig):
"""Concrete class for config"""
@classmethod
def make(cls, remote):
"""
Factory function for Config
Returns a Config class of a kind.
"""
LINKS_cls = _make_links_mixins[remote.kind.id_]
MUTEGROUP_cls = type(f"MuteGroup", (Config.MuteGroup, cls), {})
2022-04-05 20:05:55 +01:00
MONITOR_cls = type(f"ConfigMonitor", (Config.Monitor, cls), {})
CONFIG_cls = type(
f"Config{remote.kind}",
2022-04-05 20:05:55 +01:00
(cls, LINKS_cls),
{
"mute_group": tuple(MUTEGROUP_cls(remote, i) for i in range(4)),
"monitor": MONITOR_cls(remote),
},
2022-04-05 20:05:55 +01:00
)
return CONFIG_cls(remote)
@property
def address(self) -> str:
return f"/config"
@property
def amixenable(self) -> bool:
return self.getter("mute")[0] == 1
@amixenable.setter
def amixenable(self, val: bool):
self.setter("amixenable", 1 if val else 0)
@property
def amixlock(self) -> bool:
return self.getter("amixlock")[0] == 1
@amixlock.setter
def amixlock(self, val: bool):
self.setter("amixlock", 1 if val else 0)
class MuteGroup:
def __init__(self, remote, i):
super(Config.MuteGroup, self).__init__(remote)
self.i = i + 1
2022-04-05 20:05:55 +01:00
@property
def address(self) -> str:
root = super(Config.MuteGroup, self).address
return f"{root}/mute"
@property
def on(self) -> bool:
return self.getter(f"{self.i}")[0] == 1
@on.setter
def on(self, val: bool):
self.setter(f"{self.i}", 1 if val else 0)
2022-04-05 20:05:55 +01:00
class Monitor:
@property
def address(self) -> str:
root = super(Config.Monitor, self).address
return f"{root}/solo"
@property
2024-02-08 15:27:15 +00:00
@util.db_from
2022-04-05 20:05:55 +01:00
def level(self) -> float:
2024-02-08 13:44:55 +00:00
return self.getter("level")[0]
2022-04-05 20:05:55 +01:00
@level.setter
2024-02-08 15:27:15 +00:00
@util.db_to
2022-04-05 20:05:55 +01:00
def level(self, val: float):
2024-02-08 13:44:55 +00:00
self.setter("level", val)
2022-04-05 20:05:55 +01:00
@property
def source(self) -> int:
return int(self.getter("source")[0])
@source.setter
def source(self, val: int):
self.setter(f"source", val)
@property
def sourcetrim(self) -> float:
2024-02-08 13:44:55 +00:00
return round(util.lin_get(-18, 18, self.getter("sourcetrim")[0]), 1)
2022-04-05 20:05:55 +01:00
@sourcetrim.setter
def sourcetrim(self, val: float):
if not -18 <= val <= 18:
raise XAirRemoteError("expected value in range -18.0 to 18.0")
2024-02-08 13:44:55 +00:00
self.setter("sourcetrim", util.lin_set(-18, 18, val))
2022-04-05 20:05:55 +01:00
@property
def chmode(self) -> bool:
return self.getter("chmode")[0] == 1
@chmode.setter
def chmode(self, val: bool):
self.setter("chmode", 1 if val else 0)
@property
def busmode(self) -> bool:
return self.getter("busmode")[0] == 1
@busmode.setter
def busmode(self, val: bool):
self.setter("busmode", 1 if val else 0)
@property
def dimgain(self) -> int:
2024-02-08 13:44:55 +00:00
return int(util.lin_get(-40, 0, self.getter("dimatt")[0]))
2022-04-05 20:05:55 +01:00
@dimgain.setter
def dimgain(self, val: int):
if not -40 <= val <= 0:
raise XAirRemoteError("expected value in range -40 to 0")
2024-02-08 13:44:55 +00:00
self.setter("dimatt", util.lin_set(-40, 0, val))
2022-04-05 20:05:55 +01:00
@property
def dim(self) -> bool:
return self.getter("dim")[0] == 1
@dim.setter
def dim(self, val: bool):
self.setter("dim", 1 if val else 0)
@property
def mono(self) -> bool:
return self.getter("mono")[0] == 1
@mono.setter
def mono(self, val: bool):
self.setter("mono", 1 if val else 0)
@property
def mute(self) -> bool:
return self.getter("mute")[0] == 1
@mute.setter
def mute(self, val: bool):
self.setter("mute", 1 if val else 0)
@property
def dimfpl(self) -> bool:
return self.getter("dimfpl")[0] == 1
@dimfpl.setter
def dimfpl(self, val: bool):
self.setter("dimfpl", 1 if val else 0)
def _make_links_mixin(kind):
"""Creates a links mixin"""
return type(
f"Links{kind}",
2022-04-05 20:05:55 +01:00
(),
{
"link_eq": bool_prop("linkcfg/eq"),
"link_dyn": bool_prop("linkcfg/dyn"),
"link_fader_mute": bool_prop("linkcfg/fdrmute"),
**{
f"chlink{i}_{i+1}": bool_prop(f"chlink/{i}-{i+1}")
for i in range(1, kind.num_strip, 2)
},
**{
f"buslink{i}_{i+1}": bool_prop(f"buslink/{i}-{i+1}")
for i in range(1, kind.num_bus, 2)
},
},
)
_make_links_mixins = {kind.id_: _make_links_mixin(kind) for kind in kinds.all}