diff --git a/custom_components/gpiod/hub.py b/custom_components/gpiod/hub.py index 3b1f28a..8fceaf0 100644 --- a/custom_components/gpiod/hub.py +++ b/custom_components/gpiod/hub.py @@ -161,16 +161,20 @@ def turn_off(self, port) -> None: _LOGGER.debug(f"in turn_off") self._lines.set_value(port, Value.INACTIVE) - def add_sensor(self, entity, port, active_low, bias, debounce) -> None: - _LOGGER.debug(f"in add_sensor {port}") + def _get_sensor_init_value(self, port, bias, active_low): line = self._chip.request_lines({ port: gpiod.LineSettings( direction = Direction.INPUT, bias = BIAS[bias], active_low = active_low ) }) line_value = line.get_value(port) - entity.is_on = True if line_value == Value.ACTIVE else False line.release() + return line_value + + def add_sensor(self, entity, port, active_low, bias, debounce) -> None: + _LOGGER.debug(f"in add_sensor {port}") + line_value = self._get_sensor_init_value(port, bias, active_low) + entity.is_on = True if line_value == Value.ACTIVE else False _LOGGER.debug(f"current value for port {port}: {line_value}") self._entities[port] = entity @@ -203,14 +207,8 @@ def add_cover(self, entity, relay_port, relay_active_low, relay_bias, relay_driv ) # add sensor - line = self._chip.request_lines({ state_port: gpiod.LineSettings( - direction = Direction.INPUT, - bias = BIAS[state_bias], - active_low = state_active_low - ) }) - line_value = line.get_value(state_port) + line_value = self._get_sensor_init_value(state_port, state_bias, state_active_low) entity.is_closed = True if line_value == Value.ACTIVE else False - line.release() _LOGGER.debug(f"current value for port {state_port}: {line_value}") self._entities[state_port] = entity diff --git a/custom_components/gpiod/switch.py b/custom_components/gpiod/switch.py index 6387ea5..f4f19ff 100644 --- a/custom_components/gpiod/switch.py +++ b/custom_components/gpiod/switch.py @@ -101,14 +101,10 @@ async def async_added_to_hass(self) -> None: async def async_turn_on(self, **kwargs: Any) -> None: self._hub.turn_on(self._port) self.is_on = True - self.async_write_ha_state() + self.schedule_update_ha_state(False) async def async_turn_off(self, **kwargs: Any) -> None: self._hub.turn_off(self._port) self.is_on = False - self.async_write_ha_state() - - def update(self): - self.is_on = self._hub.update(self._port) self.schedule_update_ha_state(False)