Skip to content

Commit

Permalink
Multiple improvments (#313)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomer-w authored Oct 28, 2024
1 parent fdc25ce commit 76f8b68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions custom_components/rpi_gpio/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,24 @@ def verify_gpiochip(self, path):
_LOGGER.debug(f"verify_gpiochip gpiodevice: {path} has pinctrl")
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, _):
"""Stuff to do after starting."""
_LOGGER.debug(f"startup {DOMAIN} hub")
if not self._online:
_LOGGER.debug(f"integration is not online")
return

if not self._config:
_LOGGER.debug(f"gpiod config is empty")
return

# setup lines
try:
self.update_lines()
Expand Down Expand Up @@ -125,16 +137,12 @@ def hub_id(self) -> str:
return self._id

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:
self._lines.release()

_LOGGER.debug(f"updating lines: {self._config}")
self._lines = self._chip.request_lines(
consumer = "rpi_gpio",
consumer = DOMAIN,
config = self._config
)
_LOGGER.debug(f"update_lines new lines: {self._lines}")
Expand All @@ -146,9 +154,8 @@ 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.verify_online()
self.verify_port_ready(port)

self._entities[port] = entity
self._config[port] = gpiod.LineSettings(
Expand All @@ -171,9 +178,8 @@ 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}")
self.verify_online()
self.verify_port_ready(port)

# read current status of the sensor
line = self._chip.request_lines({ port: {} })
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rpi_gpio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"integration_type": "hub",
"iot_class": "local_push",
"issue_tracker": "https://github.com/thecode/ha-rpi_gpio/issues",
"requirements": [ "gpiod>=2.0.2" ],
"requirements": [ "gpiod>=2.2.1" ],
"version": "2024.10.2"
}

0 comments on commit 76f8b68

Please sign in to comment.