mirror of
https://github.com/home-assistant/core
synced 2024-08-02 23:40:32 +02:00
Fix Raspi GPIO binary_sensor produces unreliable responses (#48170)
* Fix for issue #10498 Raspi GPIO binary_sensor produces unreliable responses ("Doorbell Scenario") Changes overtaken from PR#31788 which was somehow never finished * Fix for issue #10498 Raspi GPIO binary_sensor produces unreliable response. Changes taken over from PR31788 which was somehow never finished * Remove unused code (pylint warning)
This commit is contained in:
parent
3bc583607f
commit
ecec3c8ab9
@ -1,4 +1,7 @@
|
||||
"""Support for binary sensor using RPi GPIO."""
|
||||
|
||||
import asyncio
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import rpi_gpio
|
||||
@ -52,6 +55,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
class RPiGPIOBinarySensor(BinarySensorEntity):
|
||||
"""Represent a binary sensor that uses Raspberry Pi GPIO."""
|
||||
|
||||
async def async_read_gpio(self):
|
||||
"""Read state from GPIO."""
|
||||
await asyncio.sleep(float(self._bouncetime) / 1000)
|
||||
self._state = await self.hass.async_add_executor_job(
|
||||
rpi_gpio.read_input, self._port
|
||||
)
|
||||
self.async_write_ha_state()
|
||||
|
||||
def __init__(self, name, port, pull_mode, bouncetime, invert_logic):
|
||||
"""Initialize the RPi binary sensor."""
|
||||
self._name = name or DEVICE_DEFAULT_NAME
|
||||
@ -63,12 +74,11 @@ class RPiGPIOBinarySensor(BinarySensorEntity):
|
||||
|
||||
rpi_gpio.setup_input(self._port, self._pull_mode)
|
||||
|
||||
def read_gpio(port):
|
||||
"""Read state from GPIO."""
|
||||
self._state = rpi_gpio.read_input(self._port)
|
||||
self.schedule_update_ha_state()
|
||||
def edge_detected(port):
|
||||
"""Edge detection handler."""
|
||||
self.hass.add_job(self.async_read_gpio)
|
||||
|
||||
rpi_gpio.edge_detect(self._port, read_gpio, self._bouncetime)
|
||||
rpi_gpio.edge_detect(self._port, edge_detected, self._bouncetime)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
|
Loading…
Reference in New Issue
Block a user