diff --git a/README.md b/README.md index 1975bab..1e5dee5 100644 --- a/README.md +++ b/README.md @@ -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' ``` diff --git a/custom_components/rpi_gpio/binary_sensor.py b/custom_components/rpi_gpio/binary_sensor.py index 50191dd..a63d445 100644 --- a/custom_components/rpi_gpio/binary_sensor.py +++ b/custom_components/rpi_gpio/binary_sensor.py @@ -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) diff --git a/custom_components/rpi_gpio/cover.py b/custom_components/rpi_gpio/cover.py index 3b389ab..d3d32c5 100644 --- a/custom_components/rpi_gpio/cover.py +++ b/custom_components/rpi_gpio/cover.py @@ -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): diff --git a/custom_components/rpi_gpio/hub.py b/custom_components/rpi_gpio/hub.py index 2dc19af..477c3d1 100644 --- a/custom_components/rpi_gpio/hub.py +++ b/custom_components/rpi_gpio/hub.py @@ -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() diff --git a/custom_components/rpi_gpio/switch.py b/custom_components/rpi_gpio/switch.py index 76b8179..c7af800 100644 --- a/custom_components/rpi_gpio/switch.py +++ b/custom_components/rpi_gpio/switch.py @@ -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) \ No newline at end of file