_cmd() helper method added

apply() extended to handle nested dicts

module level logger added
This commit is contained in:
onyx-and-iris 2023-06-25 13:52:39 +01:00
parent a8bb9711af
commit cdccc603d1

View File

@ -1,7 +1,10 @@
import logging
import time import time
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
logger = logging.getLogger(__name__)
@dataclass @dataclass
class Modes: class Modes:
@ -26,9 +29,9 @@ class Modes:
_mask: hex = 0x000000F0 _mask: hex = 0x000000F0
_eq_on: hex = 0x00000100 _on: hex = 0x00000100 # eq.on
_cross: hex = 0x00000200 _cross: hex = 0x00000200
_eq_ab: hex = 0x00000800 _ab: hex = 0x00000800 # eq.ab
_busa: hex = 0x00001000 _busa: hex = 0x00001000
_busa1: hex = 0x00001000 _busa1: hex = 0x00001000
@ -85,10 +88,12 @@ class IRemote(metaclass=ABCMeta):
def __init__(self, remote, index=None): def __init__(self, remote, index=None):
self._remote = remote self._remote = remote
self.index = index self.index = index
self.logger = logger.getChild(self.__class__.__name__)
self._modes = Modes() self._modes = Modes()
def getter(self, param): def getter(self, param):
cmd = f"{self.identifier}.{param}" cmd = self._cmd(param)
self.logger.debug(f"getter: {cmd}")
if cmd in self._remote.cache: if cmd in self._remote.cache:
return self._remote.cache.pop(cmd) return self._remote.cache.pop(cmd)
if self._remote.sync: if self._remote.sync:
@ -96,7 +101,14 @@ class IRemote(metaclass=ABCMeta):
def setter(self, param, val): def setter(self, param, val):
"""Sends a string request RT packet.""" """Sends a string request RT packet."""
self._remote._set_rt(f"{self.identifier}", param, val) self.logger.debug(f"setter: {self._cmd(param)}={val}")
self._remote._set_rt(self.identifier, param, val)
def _cmd(self, param):
cmd = (self.identifier,)
if param:
cmd += (f".{param}",)
return "".join(cmd)
@abstractmethod @abstractmethod
def identifier(self): def identifier(self):
@ -113,20 +125,26 @@ class IRemote(metaclass=ABCMeta):
def fget(attr, val): def fget(attr, val):
if attr == "mode": if attr == "mode":
return (f"mode.{val}", 1) return (f"mode.{val}", 1)
elif attr == "knob":
return ("", val)
return (attr, val) return (attr, val)
script = str()
for attr, val in data.items(): for attr, val in data.items():
if hasattr(self, attr): if not isinstance(val, dict):
if attr in dir(self): # avoid calling getattr (with hasattr)
attr, val = fget(attr, val) attr, val = fget(attr, val)
if isinstance(val, bool): if isinstance(val, bool):
val = 1 if val else 0 val = 1 if val else 0
self._remote.cache[f"{self.identifier}.{attr}"] = val self._remote.cache[self._cmd(attr)] = val
script += f"{self.identifier}.{attr}={val};" self._remote._script += f"{self._cmd(attr)}={val};"
else:
self._remote.sendtext(script) target = getattr(self, attr)
target.apply(val)
return self return self
def then_wait(self): def then_wait(self):
self.logger.debug(self._remote._script)
self._remote.sendtext(self._remote._script)
self._remote._script = str()
time.sleep(self._remote.DELAY) time.sleep(self._remote.DELAY)