confighelper: add getgpioevent method
Use the ConfigHelper as an intermediary to register GPIO event pins. This allows for parsing exceptions to be captured and properly re-raised as Config Errors. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
e620d2dcd7
commit
ad1666bb2c
|
@ -14,7 +14,6 @@ from typing import (
|
||||||
)
|
)
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ..confighelper import ConfigHelper
|
from ..confighelper import ConfigHelper
|
||||||
from .gpio import GpioFactory
|
|
||||||
from ..app import InternalTransport as ITransport
|
from ..app import InternalTransport as ITransport
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,10 +47,7 @@ class GpioButton:
|
||||||
self.name = config.get_name().split()[-1]
|
self.name = config.get_name().split()[-1]
|
||||||
self.itransport: ITransport = self.server.lookup_component("internal_transport")
|
self.itransport: ITransport = self.server.lookup_component("internal_transport")
|
||||||
self.mutex = asyncio.Lock()
|
self.mutex = asyncio.Lock()
|
||||||
gpio: GpioFactory = self.server.load_component(config, "gpio")
|
self.gpio_event = config.getgpioevent("pin", self._on_gpio_event)
|
||||||
self.gpio_event = gpio.register_gpio_event(
|
|
||||||
config.get('pin'), self._on_gpio_event
|
|
||||||
)
|
|
||||||
self.min_event_time = config.getfloat("minimum_event_time", 0, minval=0.0)
|
self.min_event_time = config.getfloat("minimum_event_time", 0, minval=0.0)
|
||||||
debounce_period = config.getfloat("debounce_period", .05, minval=0.01)
|
debounce_period = config.getfloat("debounce_period", .05, minval=0.01)
|
||||||
self.gpio_event.setup_debounce(debounce_period, self._on_gpio_error)
|
self.gpio_event.setup_debounce(debounce_period, self._on_gpio_error)
|
||||||
|
|
|
@ -36,7 +36,12 @@ from typing import (
|
||||||
)
|
)
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .server import Server
|
from .server import Server
|
||||||
from .components.gpio import GpioFactory, GpioOutputPin
|
from .components.gpio import (
|
||||||
|
GpioFactory,
|
||||||
|
GpioOutputPin,
|
||||||
|
GpioEvent,
|
||||||
|
GpioEventCallback
|
||||||
|
)
|
||||||
from .components.template import TemplateFactory
|
from .components.template import TemplateFactory
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
ConfigVal = Union[None, int, float, bool, str, dict, list]
|
ConfigVal = Union[None, int, float, bool, str, dict, list]
|
||||||
|
@ -141,10 +146,10 @@ class ConfigHelper:
|
||||||
raise ConfigError(str(e)) from None
|
raise ConfigError(str(e)) from None
|
||||||
val = default
|
val = default
|
||||||
section = self.section
|
section = self.section
|
||||||
except Exception:
|
except Exception as e:
|
||||||
raise ConfigError(
|
raise ConfigError(
|
||||||
f"Error parsing option ({option}) from "
|
f"Error parsing option ({option}) from section [{self.section}]"
|
||||||
f"section [{self.section}]")
|
) from e
|
||||||
else:
|
else:
|
||||||
if deprecate:
|
if deprecate:
|
||||||
self.server.add_warning(
|
self.server.add_warning(
|
||||||
|
@ -360,7 +365,7 @@ class ConfigHelper:
|
||||||
deprecate: bool = False
|
deprecate: bool = False
|
||||||
) -> Union[GpioOutputPin, _T]:
|
) -> Union[GpioOutputPin, _T]:
|
||||||
try:
|
try:
|
||||||
gpio: GpioFactory = self.server.load_component(self, 'gpio')
|
gpio: GpioFactory = self.server.load_component(self, "gpio")
|
||||||
except Exception:
|
except Exception:
|
||||||
raise ConfigError(
|
raise ConfigError(
|
||||||
f"Section [{self.section}], option '{option}', "
|
f"Section [{self.section}], option '{option}', "
|
||||||
|
@ -372,6 +377,28 @@ class ConfigHelper:
|
||||||
return self._get_option(getgpio_wrapper, option, default,
|
return self._get_option(getgpio_wrapper, option, default,
|
||||||
deprecate=deprecate)
|
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,
|
def gettemplate(self,
|
||||||
option: str,
|
option: str,
|
||||||
default: Union[Sentinel, _T] = Sentinel.MISSING,
|
default: Union[Sentinel, _T] = Sentinel.MISSING,
|
||||||
|
|
Loading…
Reference in New Issue