From 59cad0f6ef0f9789e7e5d617f7bd8035b9fccb7a Mon Sep 17 00:00:00 2001 From: snagytx Date: Tue, 24 Jan 2017 23:35:12 -0600 Subject: [PATCH] add a small sleep before reading the rpi-gpio sensor (#5446) * add a small sleep before reading the sensor The read of the sensor might be incorrect if it's read too soon after the setup_input call. It might be isolated to my case where I rely on the the PI internal PULL, but once I added this sleep I never get false initial read. If you think this change is appropriate please merge it. * Update rpi_gpio.py * Update rpi_gpio.py * Update rpi_gpio.py --- homeassistant/components/binary_sensor/rpi_gpio.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/binary_sensor/rpi_gpio.py b/homeassistant/components/binary_sensor/rpi_gpio.py index 03978ac625b..eaf9ee737e5 100644 --- a/homeassistant/components/binary_sensor/rpi_gpio.py +++ b/homeassistant/components/binary_sensor/rpi_gpio.py @@ -51,7 +51,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for port_num, port_name in ports.items(): binary_sensors.append(RPiGPIOBinarySensor( port_name, port_num, pull_mode, bouncetime, invert_logic)) - add_devices(binary_sensors) + add_devices(binary_sensors, True) class RPiGPIOBinarySensor(BinarySensorDevice): @@ -65,9 +65,9 @@ class RPiGPIOBinarySensor(BinarySensorDevice): self._pull_mode = pull_mode self._bouncetime = bouncetime self._invert_logic = invert_logic + self._state = None rpi_gpio.setup_input(self._port, self._pull_mode) - self._state = rpi_gpio.read_input(self._port) def read_gpio(port): """Read state from GPIO.""" @@ -90,3 +90,7 @@ class RPiGPIOBinarySensor(BinarySensorDevice): def is_on(self): """Return the state of the entity.""" return self._state != self._invert_logic + + def update(self): + """Update the GPIO state.""" + self._state = rpi_gpio.read_input(self._port)