From 53611ae1b17602abb869ea5603526e5cb955c534 Mon Sep 17 00:00:00 2001 From: Tomer <57483589+tomer-w@users.noreply.github.com> Date: Sat, 26 Oct 2024 07:18:37 +0300 Subject: [PATCH] Improve detection libgpiod issues (#309) 1. Catch unhandled exception from update_lines 2. Add some tracing which will show if the GPIO is used by other consumers. When we have more info we will be able to add specific checks. --- custom_components/rpi_gpio/hub.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/custom_components/rpi_gpio/hub.py b/custom_components/rpi_gpio/hub.py index 477c3d1..e79b593 100644 --- a/custom_components/rpi_gpio/hub.py +++ b/custom_components/rpi_gpio/hub.py @@ -96,7 +96,11 @@ async def startup(self, _): return # setup lines - self.update_lines() + try: + self.update_lines() + except Exception as e: + _LOGGER.error(f"Failed to update lines: {e}") + return if not self._edge_events: return @@ -129,11 +133,11 @@ def update_lines(self) -> None: self._lines.release() _LOGGER.debug(f"updating lines: {self._config}") - self._lines = gpiod.request_lines( - self._path, + self._lines = self._chip.request_lines( consumer = "rpi_gpio", config = self._config ) + _LOGGER.debug(f"update_lines new lines: {self._lines}") def handle_events(self): for event in self._lines.read_edge_events(): @@ -142,6 +146,10 @@ def handle_events(self): def add_switch(self, entity, port, active_low, bias, drive_mode, init_output_value = True) -> None: _LOGGER.debug(f"in add_switch {port}") + + info = self._chip.get_line_info(port) + _LOGGER.debug(f"original line info: {info}") + self._entities[port] = entity self._config[port] = gpiod.LineSettings( direction = Direction.OUTPUT, @@ -163,6 +171,10 @@ def turn_off(self, port) -> None: def add_sensor(self, entity, port, active_low, bias, debounce) -> None: _LOGGER.debug(f"in add_sensor {port}") + + info = self._chip.get_line_info(port) + _LOGGER.debug(f"original line info: {info}") + # read current status of the sensor line = self._chip.request_lines({ port: {} }) value = True if line.get_value(port) == Value.ACTIVE else False