Multiple improvments (#313)

Multiple improvments:
1. Bump gpiod min version to 2.2.1 which is the latest.
2. Based on the learning from the used ports, I added detection for those cases which clear and useful errors.
3. Make it easier to load the integration twice on the same machine for testing.
This commit is contained in:
Tomer 2024-10-28 21:01:00 +02:00 committed by GitHub
parent fdc25ce9a2
commit 76f8b68077
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View File

@ -89,10 +89,22 @@ class Hub:
_LOGGER.debug(f"verify_gpiochip gpiodevice: {path} has pinctrl") _LOGGER.debug(f"verify_gpiochip gpiodevice: {path} has pinctrl")
return True return True
def verify_port_ready(self, port: int):
info = self._chip.get_line_info(port)
_LOGGER.debug(f"original port info: {info}")
if info.used and info.consumer != DOMAIN:
_LOGGER.error(f"Port {port} already in use by {info.consumer}")
raise HomeAssistantError(f"Port {port} already in use by {info.consumer}")
async def startup(self, _): async def startup(self, _):
"""Stuff to do after starting.""" """Stuff to do after starting."""
_LOGGER.debug(f"startup {DOMAIN} hub") _LOGGER.debug(f"startup {DOMAIN} hub")
if not self._online: if not self._online:
_LOGGER.debug(f"integration is not online")
return
if not self._config:
_LOGGER.debug(f"gpiod config is empty")
return return
# setup lines # setup lines
@ -125,16 +137,12 @@ class Hub:
return self._id return self._id
def update_lines(self) -> None: def update_lines(self) -> None:
if not self._online:
_LOGGER.debug(f"gpiod hub not online {self._path}")
if not self._config:
_LOGGER.debug(f"gpiod config is empty")
if self._lines: if self._lines:
self._lines.release() self._lines.release()
_LOGGER.debug(f"updating lines: {self._config}") _LOGGER.debug(f"updating lines: {self._config}")
self._lines = self._chip.request_lines( self._lines = self._chip.request_lines(
consumer = "rpi_gpio", consumer = DOMAIN,
config = self._config config = self._config
) )
_LOGGER.debug(f"update_lines new lines: {self._lines}") _LOGGER.debug(f"update_lines new lines: {self._lines}")
@ -146,9 +154,8 @@ class Hub:
def add_switch(self, entity, port, active_low, bias, drive_mode, init_output_value = True) -> None: def add_switch(self, entity, port, active_low, bias, drive_mode, init_output_value = True) -> None:
_LOGGER.debug(f"in add_switch {port}") _LOGGER.debug(f"in add_switch {port}")
self.verify_online()
info = self._chip.get_line_info(port) self.verify_port_ready(port)
_LOGGER.debug(f"original line info: {info}")
self._entities[port] = entity self._entities[port] = entity
self._config[port] = gpiod.LineSettings( self._config[port] = gpiod.LineSettings(
@ -171,9 +178,8 @@ class Hub:
def add_sensor(self, entity, port, active_low, bias, debounce) -> None: def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
_LOGGER.debug(f"in add_sensor {port}") _LOGGER.debug(f"in add_sensor {port}")
self.verify_online()
info = self._chip.get_line_info(port) self.verify_port_ready(port)
_LOGGER.debug(f"original line info: {info}")
# read current status of the sensor # read current status of the sensor
line = self._chip.request_lines({ port: {} }) line = self._chip.request_lines({ port: {} })

View File

@ -6,6 +6,6 @@
"integration_type": "hub", "integration_type": "hub",
"iot_class": "local_push", "iot_class": "local_push",
"issue_tracker": "https://github.com/thecode/ha-rpi_gpio/issues", "issue_tracker": "https://github.com/thecode/ha-rpi_gpio/issues",
"requirements": [ "gpiod>=2.0.2" ], "requirements": [ "gpiod>=2.2.1" ],
"version": "2024.10.2" "version": "2024.10.2"
} }