Skip to content

Commit

Permalink
Minor error handing and tracing improvements (#308)
Browse files Browse the repository at this point in the history
* Update the minimal HA supported version for the RP5 release

* Improve tracing when hub is not online. Also rename the update function to make it more clear.

* Minor trace improvement

* Remove duplicate update_lines from cover.

* Fix README to have the right integration name
  • Loading branch information
tomer-w authored Oct 25, 2024
1 parent 7088db0 commit f36b427
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Copy the `rpi_gpio` folder and all of its contents into your Home Assistant's `c

# Usage

The `gpiod` platform will be initialized using the path to the gpio chip. When path is not in the config `/dev/gpiochip[0-5]` are tested for a gpiodevice having `pinctrl`, in sequence `[0,4,1,2,3,5]`. So with a raspberry pi you should be OK to leave the path empty.
The `rpi_gpio` platform will be initialized using the path to the gpio chip. When path is not in the config `/dev/gpiochip[0-5]` are tested for a gpiodevice having `pinctrl`, in sequence `[0,4,1,2,3,5]`. So with a raspberry pi you should be OK to leave the path empty.

Raspberry Pi | GPIO Device
--- | ---
Expand All @@ -27,7 +27,7 @@ RPi5 | `/dev/gpiochip4`

```yaml
# setup gpiod chip; mostly not required
gpiod:
rpi_gpio:
path: '/dev/gpiochip0'
```
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rpi_gpio/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ async def async_added_to_hass(self) -> None:
self.async_write_ha_state()

def handle_event(self):
self._attr_is_on = self._hub.update(self._port)
self._attr_is_on = self._hub.get_line_value(self._port)
self.schedule_update_ha_state(False)
2 changes: 1 addition & 1 deletion custom_components/rpi_gpio/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async def async_added_to_hass(self) -> None:
self.async_write_ha_state()

def handle_event(self):
self._attr_is_closed = self._hub.update(self._state_port)
self._attr_is_closed = self._hub.get_line_value(self._state_port)
self.schedule_update_ha_state(False)

def close_cover(self, **kwargs):
Expand Down
16 changes: 11 additions & 5 deletions custom_components/rpi_gpio/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ def __init__(self, hass: HomeAssistant, path: str) -> None:

if path:
# use config
_LOGGER.debug(f"trying to use configured device: {path}")
if self.verify_gpiochip(path):
self._online = True
self._path = path
else:
# discover
_LOGGER.debug(f"auto discovering gpio device")
for d in [0,4,1,2,3,5]:
# rpi3,4 using 0. rpi5 using 4
path = f"/dev/gpiochip{d}"
Expand All @@ -59,16 +61,19 @@ def __init__(self, hass: HomeAssistant, path: str) -> None:
self._path = path
break

if not self._online:
_LOGGER.error("No gpio device detected, bailing out")
raise HomeAssistantError("No gpio device detected")
self.verify_online()

_LOGGER.debug(f"using gpio_device: {self._path}")

# startup and shutdown triggers of hass
self._hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, self.startup)
self._hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.cleanup)

def verify_online(self):
if not self._online:
_LOGGER.error("No gpio device detected, bailing out")
raise HomeAssistantError("No gpio device detected")

def verify_gpiochip(self, path):
if not gpiod.is_gpiochip_device(path):
_LOGGER.debug(f"verify_gpiochip: {path} not a gpiochip_device")
Expand Down Expand Up @@ -148,10 +153,12 @@ def add_switch(self, entity, port, active_low, bias, drive_mode, init_output_val

def turn_on(self, port) -> None:
_LOGGER.debug(f"in turn_on {port}")
self.verify_online()
self._lines.set_value(port, Value.ACTIVE)

def turn_off(self, port) -> None:
_LOGGER.debug(f"in turn_off {port}")
self.verify_online()
self._lines.set_value(port, Value.INACTIVE)

def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
Expand All @@ -175,13 +182,12 @@ def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
)
self._edge_events = True

def update(self, port, **kwargs):
def get_line_value(self, port, **kwargs):
return self._lines.get_value(port) == Value.ACTIVE

def add_cover(self, entity, relay_port, relay_active_low, relay_bias, relay_drive,
state_port, state_bias, state_active_low) -> None:
_LOGGER.debug(f"in add_cover {relay_port} {state_port}")
self.add_switch(entity, relay_port, relay_active_low, relay_bias, relay_drive, init_output_value = False)
self.add_sensor(entity, state_port, state_active_low, state_bias, 50)
self.update_lines()

6 changes: 3 additions & 3 deletions custom_components/rpi_gpio/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
_attr_should_poll = False

def __init__(self, hub, name, port, unique_id, active_low, bias, drive, persistent):
_LOGGER.debug(f"GPIODSwitch init: {port} - {name} - {unique_id} - active_low: {active_low} - bias: {bias} - drive: {drive}")
_LOGGER.debug(f"GPIODSwitch init: {port} - {name} - {unique_id} - active_low: {active_low} - bias: {bias} - drive: {drive} - persistent: {persistent}")
self._hub = hub
self._attr_name = name
self._attr_unique_id = unique_id
Expand All @@ -93,7 +93,7 @@ async def async_added_to_hass(self) -> None:
if not state or not self._persistent:
self._attr_is_on = False
else:
_LOGGER.debug(f"GPIODSwitch async_added_to_has initial port: {self._port} persistent: {self._persistent} state: {state.state}")
_LOGGER.debug(f"setting initial persistent state for: {self._port}. state: {state.state}")
self._attr_is_on = True if state.state == STATE_ON else False
self._hub.add_switch(self, self._port, self._active_low, self._bias, self._drive_mode)
self.async_write_ha_state()
Expand All @@ -109,5 +109,5 @@ async def async_turn_off(self, **kwargs: Any) -> None:
self.async_write_ha_state()

def handle_event(self):
self._attr_is_on = self._hub.update(self._port)
self._attr_is_on = self._hub.get_line_value(self._port)
self.schedule_update_ha_state(False)

0 comments on commit f36b427

Please sign in to comment.