Skip to content

Commit 76f8b68

Browse files
authored
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.
1 parent fdc25ce commit 76f8b68

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

custom_components/rpi_gpio/hub.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,24 @@ def verify_gpiochip(self, path):
8989
_LOGGER.debug(f"verify_gpiochip gpiodevice: {path} has pinctrl")
9090
return True
9191

92+
def verify_port_ready(self, port: int):
93+
info = self._chip.get_line_info(port)
94+
_LOGGER.debug(f"original port info: {info}")
95+
if info.used and info.consumer != DOMAIN:
96+
_LOGGER.error(f"Port {port} already in use by {info.consumer}")
97+
raise HomeAssistantError(f"Port {port} already in use by {info.consumer}")
98+
99+
92100
async def startup(self, _):
93101
"""Stuff to do after starting."""
94102
_LOGGER.debug(f"startup {DOMAIN} hub")
95103
if not self._online:
104+
_LOGGER.debug(f"integration is not online")
96105
return
97-
106+
if not self._config:
107+
_LOGGER.debug(f"gpiod config is empty")
108+
return
109+
98110
# setup lines
99111
try:
100112
self.update_lines()
@@ -125,16 +137,12 @@ def hub_id(self) -> str:
125137
return self._id
126138

127139
def update_lines(self) -> None:
128-
if not self._online:
129-
_LOGGER.debug(f"gpiod hub not online {self._path}")
130-
if not self._config:
131-
_LOGGER.debug(f"gpiod config is empty")
132140
if self._lines:
133141
self._lines.release()
134142

135143
_LOGGER.debug(f"updating lines: {self._config}")
136144
self._lines = self._chip.request_lines(
137-
consumer = "rpi_gpio",
145+
consumer = DOMAIN,
138146
config = self._config
139147
)
140148
_LOGGER.debug(f"update_lines new lines: {self._lines}")
@@ -146,9 +154,8 @@ def handle_events(self):
146154

147155
def add_switch(self, entity, port, active_low, bias, drive_mode, init_output_value = True) -> None:
148156
_LOGGER.debug(f"in add_switch {port}")
149-
150-
info = self._chip.get_line_info(port)
151-
_LOGGER.debug(f"original line info: {info}")
157+
self.verify_online()
158+
self.verify_port_ready(port)
152159

153160
self._entities[port] = entity
154161
self._config[port] = gpiod.LineSettings(
@@ -171,9 +178,8 @@ def turn_off(self, port) -> None:
171178

172179
def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
173180
_LOGGER.debug(f"in add_sensor {port}")
174-
175-
info = self._chip.get_line_info(port)
176-
_LOGGER.debug(f"original line info: {info}")
181+
self.verify_online()
182+
self.verify_port_ready(port)
177183

178184
# read current status of the sensor
179185
line = self._chip.request_lines({ port: {} })

custom_components/rpi_gpio/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"integration_type": "hub",
77
"iot_class": "local_push",
88
"issue_tracker": "https://github.com/thecode/ha-rpi_gpio/issues",
9-
"requirements": [ "gpiod>=2.0.2" ],
9+
"requirements": [ "gpiod>=2.2.1" ],
1010
"version": "2024.10.2"
1111
}

0 commit comments

Comments
 (0)