mirror of
https://github.com/thecode/ha-rpi_gpio.git
synced 2025-07-24 19:26:40 +00:00
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:
parent
7088db0334
commit
f36b427748
@ -18,7 +18,7 @@ Copy the `rpi_gpio` folder and all of its contents into your Home Assistant's `c
|
|||||||
|
|
||||||
# Usage
|
# 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
|
Raspberry Pi | GPIO Device
|
||||||
--- | ---
|
--- | ---
|
||||||
@ -27,7 +27,7 @@ RPi5 | `/dev/gpiochip4`
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
# setup gpiod chip; mostly not required
|
# setup gpiod chip; mostly not required
|
||||||
gpiod:
|
rpi_gpio:
|
||||||
path: '/dev/gpiochip0'
|
path: '/dev/gpiochip0'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -85,5 +85,5 @@ class GPIODBinarySensor(BinarySensorEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def handle_event(self):
|
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)
|
self.schedule_update_ha_state(False)
|
||||||
|
@ -114,7 +114,7 @@ class GPIODCover(CoverEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def handle_event(self):
|
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)
|
self.schedule_update_ha_state(False)
|
||||||
|
|
||||||
def close_cover(self, **kwargs):
|
def close_cover(self, **kwargs):
|
||||||
|
@ -46,11 +46,13 @@ class Hub:
|
|||||||
|
|
||||||
if path:
|
if path:
|
||||||
# use config
|
# use config
|
||||||
|
_LOGGER.debug(f"trying to use configured device: {path}")
|
||||||
if self.verify_gpiochip(path):
|
if self.verify_gpiochip(path):
|
||||||
self._online = True
|
self._online = True
|
||||||
self._path = path
|
self._path = path
|
||||||
else:
|
else:
|
||||||
# discover
|
# discover
|
||||||
|
_LOGGER.debug(f"auto discovering gpio device")
|
||||||
for d in [0,4,1,2,3,5]:
|
for d in [0,4,1,2,3,5]:
|
||||||
# rpi3,4 using 0. rpi5 using 4
|
# rpi3,4 using 0. rpi5 using 4
|
||||||
path = f"/dev/gpiochip{d}"
|
path = f"/dev/gpiochip{d}"
|
||||||
@ -59,9 +61,7 @@ class Hub:
|
|||||||
self._path = path
|
self._path = path
|
||||||
break
|
break
|
||||||
|
|
||||||
if not self._online:
|
self.verify_online()
|
||||||
_LOGGER.error("No gpio device detected, bailing out")
|
|
||||||
raise HomeAssistantError("No gpio device detected")
|
|
||||||
|
|
||||||
_LOGGER.debug(f"using gpio_device: {self._path}")
|
_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_START, self.startup)
|
||||||
self._hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.cleanup)
|
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):
|
def verify_gpiochip(self, path):
|
||||||
if not gpiod.is_gpiochip_device(path):
|
if not gpiod.is_gpiochip_device(path):
|
||||||
_LOGGER.debug(f"verify_gpiochip: {path} not a gpiochip_device")
|
_LOGGER.debug(f"verify_gpiochip: {path} not a gpiochip_device")
|
||||||
@ -148,10 +153,12 @@ class Hub:
|
|||||||
|
|
||||||
def turn_on(self, port) -> None:
|
def turn_on(self, port) -> None:
|
||||||
_LOGGER.debug(f"in turn_on {port}")
|
_LOGGER.debug(f"in turn_on {port}")
|
||||||
|
self.verify_online()
|
||||||
self._lines.set_value(port, Value.ACTIVE)
|
self._lines.set_value(port, Value.ACTIVE)
|
||||||
|
|
||||||
def turn_off(self, port) -> None:
|
def turn_off(self, port) -> None:
|
||||||
_LOGGER.debug(f"in turn_off {port}")
|
_LOGGER.debug(f"in turn_off {port}")
|
||||||
|
self.verify_online()
|
||||||
self._lines.set_value(port, Value.INACTIVE)
|
self._lines.set_value(port, Value.INACTIVE)
|
||||||
|
|
||||||
def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
|
def add_sensor(self, entity, port, active_low, bias, debounce) -> None:
|
||||||
@ -175,7 +182,7 @@ class Hub:
|
|||||||
)
|
)
|
||||||
self._edge_events = True
|
self._edge_events = True
|
||||||
|
|
||||||
def update(self, port, **kwargs):
|
def get_line_value(self, port, **kwargs):
|
||||||
return self._lines.get_value(port) == Value.ACTIVE
|
return self._lines.get_value(port) == Value.ACTIVE
|
||||||
|
|
||||||
def add_cover(self, entity, relay_port, relay_active_low, relay_bias, relay_drive,
|
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}")
|
_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_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.add_sensor(entity, state_port, state_active_low, state_bias, 50)
|
||||||
self.update_lines()
|
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
|
|||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, hub, name, port, unique_id, active_low, bias, drive, persistent):
|
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._hub = hub
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
@ -93,7 +93,7 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
|
|||||||
if not state or not self._persistent:
|
if not state or not self._persistent:
|
||||||
self._attr_is_on = False
|
self._attr_is_on = False
|
||||||
else:
|
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._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._hub.add_switch(self, self._port, self._active_low, self._bias, self._drive_mode)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
@ -109,5 +109,5 @@ class GPIODSwitch(SwitchEntity, RestoreEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def handle_event(self):
|
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)
|
self.schedule_update_ha_state(False)
|
Loading…
x
Reference in New Issue
Block a user