mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
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
a8ea214f2e
commit
bacb6c6b14
@ -1,5 +1,5 @@
|
|||||||
"""Support for controlling GPIO pins of a Raspberry Pi."""
|
"""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
|
from gpiozero.pins.pigpio import PiGPIOFactory
|
||||||
|
|
||||||
CONF_BOUNCETIME = "bouncetime"
|
CONF_BOUNCETIME = "bouncetime"
|
||||||
@ -38,7 +38,7 @@ def setup_input(address, port, pull_mode, bouncetime):
|
|||||||
pull_gpio_up = False
|
pull_gpio_up = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return Button(
|
return DigitalInputDevice(
|
||||||
port,
|
port,
|
||||||
pull_up=pull_gpio_up,
|
pull_up=pull_gpio_up,
|
||||||
bounce_time=bouncetime,
|
bounce_time=bouncetime,
|
||||||
@ -56,6 +56,6 @@ def write_output(switch, value):
|
|||||||
switch.off()
|
switch.off()
|
||||||
|
|
||||||
|
|
||||||
def read_input(button):
|
def read_input(sensor):
|
||||||
"""Read a value from a GPIO."""
|
"""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 = []
|
devices = []
|
||||||
for port_num, port_name in ports.items():
|
for port_num, port_name in ports.items():
|
||||||
try:
|
try:
|
||||||
button = remote_rpi_gpio.setup_input(
|
remote_sensor = remote_rpi_gpio.setup_input(
|
||||||
address, port_num, pull_mode, bouncetime
|
address, port_num, pull_mode, bouncetime
|
||||||
)
|
)
|
||||||
except (ValueError, IndexError, KeyError, OSError):
|
except (ValueError, IndexError, KeyError, OSError):
|
||||||
return
|
return
|
||||||
new_sensor = RemoteRPiGPIOBinarySensor(port_name, button, invert_logic)
|
new_sensor = RemoteRPiGPIOBinarySensor(port_name, remote_sensor, invert_logic)
|
||||||
devices.append(new_sensor)
|
devices.append(new_sensor)
|
||||||
|
|
||||||
add_entities(devices, True)
|
add_entities(devices, True)
|
||||||
@ -56,23 +56,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
|
class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
|
||||||
"""Represent a binary sensor that uses a Remote Raspberry Pi GPIO."""
|
"""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."""
|
"""Initialize the RPi binary sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._invert_logic = invert_logic
|
self._invert_logic = invert_logic
|
||||||
self._state = False
|
self._state = False
|
||||||
self._button = button
|
self._sensor = sensor
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Run when entity about to be added to hass."""
|
"""Run when entity about to be added to hass."""
|
||||||
|
|
||||||
def read_gpio():
|
def read_gpio():
|
||||||
"""Read state from 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.schedule_update_ha_state()
|
||||||
|
|
||||||
self._button.when_released = read_gpio
|
self._sensor.when_deactivated = read_gpio
|
||||||
self._button.when_pressed = read_gpio
|
self._sensor.when_activated = read_gpio
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
@ -97,6 +97,6 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
|
|||||||
def update(self):
|
def update(self):
|
||||||
"""Update the GPIO state."""
|
"""Update the GPIO state."""
|
||||||
try:
|
try:
|
||||||
self._state = remote_rpi_gpio.read_input(self._button)
|
self._state = remote_rpi_gpio.read_input(self._sensor)
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user