diff options
author | Devaev Maxim <[email protected]> | 2020-09-08 05:05:40 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-09-08 05:05:40 +0300 |
commit | 4cc60e4d528c669af0c1f7478160ff57a4691af7 (patch) | |
tree | 00778880ef828ac7a71c86348c155d3e59be2854 /kvmd/plugins/ugpio | |
parent | 1353ca2e974a66e7797dadba0a89fcbe470c7c7c (diff) |
refactoring
Diffstat (limited to 'kvmd/plugins/ugpio')
-rw-r--r-- | kvmd/plugins/ugpio/__init__.py | 16 | ||||
-rw-r--r-- | kvmd/plugins/ugpio/gpio.py | 23 |
2 files changed, 29 insertions, 10 deletions
diff --git a/kvmd/plugins/ugpio/__init__.py b/kvmd/plugins/ugpio/__init__.py index 9dbc0166..0cc8662a 100644 --- a/kvmd/plugins/ugpio/__init__.py +++ b/kvmd/plugins/ugpio/__init__.py @@ -22,6 +22,7 @@ from typing import Type from typing import Optional +from typing import Any from ...errors import OperationError @@ -42,13 +43,20 @@ class GpioOperationError(OperationError, GpioError): class GpioDriverOfflineError(GpioOperationError): def __init__(self, driver: "BaseUserGpioDriver") -> None: - super().__init__(f"GPIO driver {driver.get_instance_name()!r} is offline") + super().__init__(f"GPIO driver {driver} is offline") # ===== class BaseUserGpioDriver(BasePlugin): - def get_instance_name(self) -> str: - raise NotImplementedError + def __init__( # pylint: disable=super-init-not-called + self, + instance_name: str, + notifier: aiotools.AioNotifier, + **_: Any, + ) -> None: + + self._instance_name = instance_name + self._notifier = notifier def register_input(self, pin: int) -> None: raise NotImplementedError @@ -56,7 +64,7 @@ class BaseUserGpioDriver(BasePlugin): def register_output(self, pin: int, initial: Optional[bool]) -> None: raise NotImplementedError - def prepare(self, notifier: aiotools.AioNotifier) -> None: + def prepare(self) -> None: raise NotImplementedError async def run(self) -> None: diff --git a/kvmd/plugins/ugpio/gpio.py b/kvmd/plugins/ugpio/gpio.py index 526397e1..96c3eee7 100644 --- a/kvmd/plugins/ugpio/gpio.py +++ b/kvmd/plugins/ugpio/gpio.py @@ -36,7 +36,16 @@ from . import BaseUserGpioDriver # ===== class Plugin(BaseUserGpioDriver): - def __init__(self, state_poll: float) -> None: # pylint: disable=super-init-not-called + def __init__( + self, + instance_name: str, + notifier: aiotools.AioNotifier, + + state_poll: float, + ) -> None: + + super().__init__(instance_name, notifier) + self.__state_poll = state_poll self.__input_pins: Set[int] = set() @@ -50,16 +59,13 @@ class Plugin(BaseUserGpioDriver): "state_poll": Option(0.1, type=valid_float_f01), } - def get_instance_name(self) -> str: - return "gpio" - def register_input(self, pin: int) -> None: self.__input_pins.add(pin) def register_output(self, pin: int, initial: Optional[bool]) -> None: self.__output_pins[pin] = initial - def prepare(self, notifier: aiotools.AioNotifier) -> None: + def prepare(self) -> None: assert self.__reader is None self.__reader = gpio.BatchReader( pins=set([ @@ -70,7 +76,7 @@ class Plugin(BaseUserGpioDriver): ], ]), interval=self.__state_poll, - notifier=notifier, + notifier=self._notifier, ) async def run(self) -> None: @@ -82,3 +88,8 @@ class Plugin(BaseUserGpioDriver): def write(self, pin: int, state: bool) -> None: gpio.write(pin, state) + + def __str__(self) -> str: + return f"GPIO({self._instance_name})" + + __repr__ = __str__ |