Minor error handing and tracing improvements (#308)

* 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
This commit is contained in:
Tomer 2024-10-25 17:37:16 +03:00 committed by GitHub
parent 7088db0334
commit f36b427748
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 12 deletions

View File

@ -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
--- | ---
@ -27,7 +27,7 @@ RPi5 | `/dev/gpiochip4`
```yaml
# setup gpiod chip; mostly not required
gpiod:
rpi_gpio:
path: '/dev/gpiochip0'
```

View File

@ -85,5 +85,5 @@ class GPIODBinarySensor(BinarySensorEntity):
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)

View File

@ -114,7 +114,7 @@ class GPIODCover(CoverEntity):
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):

View File

@ -46,11 +46,13 @@ class Hub:
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}"
@ -59,9 +61,7 @@ class Hub:
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}")
@ -69,6 +69,11 @@ class Hub:
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")
@ -148,10 +153,12 @@ class Hub:
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:
@ -175,7 +182,7 @@ class Hub:
)
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,
@ -183,5 +190,4 @@ class Hub:
_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()

View File

@ -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
@ -93,7 +93,7 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
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()
@ -109,5 +109,5 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
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)