Skip to content

Commit

Permalink
centralized sensor init
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeneef committed Nov 9, 2024
1 parent f441d5b commit 39a27a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
18 changes: 8 additions & 10 deletions custom_components/gpiod/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions custom_components/gpiod/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 39a27a2

Please sign in to comment.