Fix remote rpi gpio input type (#53108)
* Fix issue #45770 - Change sensor from Button to DigitalInput * Change references from button to sensor
This commit is contained in:
parent
8a009f1374
commit
1c05329b5d
2 changed files with 12 additions and 12 deletions
|
@ -1,5 +1,5 @@
|
|||
"""Support for controlling GPIO pins of a Raspberry Pi."""
|
||||
from gpiozero import LED, Button
|
||||
from gpiozero import LED, DigitalInputDevice
|
||||
from gpiozero.pins.pigpio import PiGPIOFactory
|
||||
|
||||
CONF_BOUNCETIME = "bouncetime"
|
||||
|
@ -38,7 +38,7 @@ def setup_input(address, port, pull_mode, bouncetime):
|
|||
pull_gpio_up = False
|
||||
|
||||
try:
|
||||
return Button(
|
||||
return DigitalInputDevice(
|
||||
port,
|
||||
pull_up=pull_gpio_up,
|
||||
bounce_time=bouncetime,
|
||||
|
@ -56,6 +56,6 @@ def write_output(switch, value):
|
|||
switch.off()
|
||||
|
||||
|
||||
def read_input(button):
|
||||
def read_input(sensor):
|
||||
"""Read a value from a GPIO."""
|
||||
return button.is_pressed
|
||||
return sensor.value
|
||||
|
|
|
@ -42,12 +42,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
devices = []
|
||||
for port_num, port_name in ports.items():
|
||||
try:
|
||||
button = remote_rpi_gpio.setup_input(
|
||||
remote_sensor = remote_rpi_gpio.setup_input(
|
||||
address, port_num, pull_mode, bouncetime
|
||||
)
|
||||
except (ValueError, IndexError, KeyError, OSError):
|
||||
return
|
||||
new_sensor = RemoteRPiGPIOBinarySensor(port_name, button, invert_logic)
|
||||
new_sensor = RemoteRPiGPIOBinarySensor(port_name, remote_sensor, invert_logic)
|
||||
devices.append(new_sensor)
|
||||
|
||||
add_entities(devices, True)
|
||||
|
@ -56,23 +56,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
|
||||
"""Represent a binary sensor that uses a Remote Raspberry Pi GPIO."""
|
||||
|
||||
def __init__(self, name, button, invert_logic):
|
||||
def __init__(self, name, sensor, invert_logic):
|
||||
"""Initialize the RPi binary sensor."""
|
||||
self._name = name
|
||||
self._invert_logic = invert_logic
|
||||
self._state = False
|
||||
self._button = button
|
||||
self._sensor = sensor
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added to hass."""
|
||||
|
||||
def read_gpio():
|
||||
"""Read state from GPIO."""
|
||||
self._state = remote_rpi_gpio.read_input(self._button)
|
||||
self._state = remote_rpi_gpio.read_input(self._sensor)
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
self._button.when_released = read_gpio
|
||||
self._button.when_pressed = read_gpio
|
||||
self._sensor.when_deactivated = read_gpio
|
||||
self._sensor.when_activated = read_gpio
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
|
@ -97,6 +97,6 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
|
|||
def update(self):
|
||||
"""Update the GPIO state."""
|
||||
try:
|
||||
self._state = remote_rpi_gpio.read_input(self._button)
|
||||
self._state = remote_rpi_gpio.read_input(self._sensor)
|
||||
except requests.exceptions.ConnectionError:
|
||||
return
|
||||
|
|
Loading…
Add table
Reference in a new issue