Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for controlling GPIO pins of a Raspberry Pi."""
2 
3 from gpiozero import LED, DigitalInputDevice
4 from gpiozero.pins.pigpio import PiGPIOFactory
5 
6 CONF_BOUNCETIME = "bouncetime"
7 CONF_INVERT_LOGIC = "invert_logic"
8 CONF_PULL_MODE = "pull_mode"
9 
10 DEFAULT_BOUNCETIME = 50
11 DEFAULT_INVERT_LOGIC = False
12 DEFAULT_PULL_MODE = "UP"
13 
14 DOMAIN = "remote_rpi_gpio"
15 
16 
17 def setup_output(address, port, invert_logic):
18  """Set up a GPIO as output."""
19 
20  try:
21  return LED(
22  port, active_high=not invert_logic, pin_factory=PiGPIOFactory(address)
23  )
24  except (ValueError, IndexError, KeyError):
25  return None
26 
27 
28 def setup_input(address, port, pull_mode, bouncetime):
29  """Set up a GPIO as input."""
30 
31  if pull_mode == "UP":
32  pull_gpio_up = True
33  elif pull_mode == "DOWN":
34  pull_gpio_up = False
35 
36  try:
37  return DigitalInputDevice(
38  port,
39  pull_up=pull_gpio_up,
40  bounce_time=bouncetime,
41  pin_factory=PiGPIOFactory(address),
42  )
43  except (ValueError, IndexError, KeyError, OSError):
44  return None
45 
46 
47 def write_output(switch, value):
48  """Write a value to a GPIO."""
49  if value == 1:
50  switch.on()
51  if value == 0:
52  switch.off()
53 
54 
55 def read_input(sensor):
56  """Read a value from a GPIO."""
57  return sensor.value
def setup_output(address, port, invert_logic)
Definition: __init__.py:17
def setup_input(address, port, pull_mode, bouncetime)
Definition: __init__.py:28