diff --git a/moonraker/components/button.py b/moonraker/components/button.py index 0016900..3f5baee 100644 --- a/moonraker/components/button.py +++ b/moonraker/components/button.py @@ -14,7 +14,6 @@ from typing import ( ) if TYPE_CHECKING: from ..confighelper import ConfigHelper - from .gpio import GpioFactory from ..app import InternalTransport as ITransport @@ -48,10 +47,7 @@ class GpioButton: self.name = config.get_name().split()[-1] self.itransport: ITransport = self.server.lookup_component("internal_transport") self.mutex = asyncio.Lock() - gpio: GpioFactory = self.server.load_component(config, "gpio") - self.gpio_event = gpio.register_gpio_event( - config.get('pin'), self._on_gpio_event - ) + self.gpio_event = config.getgpioevent("pin", self._on_gpio_event) self.min_event_time = config.getfloat("minimum_event_time", 0, minval=0.0) debounce_period = config.getfloat("debounce_period", .05, minval=0.01) self.gpio_event.setup_debounce(debounce_period, self._on_gpio_error) diff --git a/moonraker/confighelper.py b/moonraker/confighelper.py index e42c46b..aa7137d 100644 --- a/moonraker/confighelper.py +++ b/moonraker/confighelper.py @@ -36,7 +36,12 @@ from typing import ( ) if TYPE_CHECKING: from .server import Server - from .components.gpio import GpioFactory, GpioOutputPin + from .components.gpio import ( + GpioFactory, + GpioOutputPin, + GpioEvent, + GpioEventCallback + ) from .components.template import TemplateFactory _T = TypeVar("_T") ConfigVal = Union[None, int, float, bool, str, dict, list] @@ -141,10 +146,10 @@ class ConfigHelper: raise ConfigError(str(e)) from None val = default section = self.section - except Exception: + except Exception as e: raise ConfigError( - f"Error parsing option ({option}) from " - f"section [{self.section}]") + f"Error parsing option ({option}) from section [{self.section}]" + ) from e else: if deprecate: self.server.add_warning( @@ -360,7 +365,7 @@ class ConfigHelper: deprecate: bool = False ) -> Union[GpioOutputPin, _T]: try: - gpio: GpioFactory = self.server.load_component(self, 'gpio') + gpio: GpioFactory = self.server.load_component(self, "gpio") except Exception: raise ConfigError( f"Section [{self.section}], option '{option}', " @@ -372,6 +377,28 @@ class ConfigHelper: return self._get_option(getgpio_wrapper, option, default, deprecate=deprecate) + def getgpioevent( + self, + option: str, + event_callback: GpioEventCallback, + default: Union[Sentinel, _T] = Sentinel.MISSING, + deprecate: bool = False + ) -> Union[GpioEvent, _T]: + try: + gpio: GpioFactory = self.server.load_component(self, "gpio") + except Exception: + raise ConfigError( + f"Section [{self.section}], option '{option}', " + "GPIO Component not available" + ) + + def getgpioevent_wrapper(sec: str, opt: str) -> GpioEvent: + val = self.config.get(sec, opt) + return gpio.register_gpio_event(val, event_callback) + return self._get_option( + getgpioevent_wrapper, option, default, deprecate=deprecate + ) + def gettemplate(self, option: str, default: Union[Sentinel, _T] = Sentinel.MISSING,