core/homeassistant/components/rpi_gpio.py

71 lines
1.7 KiB
Python
Raw Normal View History

"""
2016-03-07 17:49:31 +00:00
Support for controlling GPIO pins of a Raspberry Pi.
For more details about this component, please refer to the documentation at
2016-01-15 12:35:06 +00:00
https://home-assistant.io/components/rpi_gpio/
"""
2016-01-18 00:36:25 +00:00
# pylint: disable=import-error
import logging
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
REQUIREMENTS = ['RPi.GPIO==0.6.1']
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'rpi_gpio'
# pylint: disable=no-member
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup the Raspberry PI GPIO component."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
def cleanup_gpio(event):
2016-03-07 17:49:31 +00:00
"""Stuff to do before stopping."""
GPIO.cleanup()
def prepare_gpio(event):
2016-03-07 17:49:31 +00:00
"""Stuff to do when home assistant starts."""
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, cleanup_gpio)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, prepare_gpio)
GPIO.setmode(GPIO.BCM)
return True
def setup_output(port):
2016-03-07 17:49:31 +00:00
"""Setup a GPIO as output."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
GPIO.setup(port, GPIO.OUT)
def setup_input(port, pull_mode):
2016-03-07 17:49:31 +00:00
"""Setup a GPIO as input."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
GPIO.setup(port, GPIO.IN,
GPIO.PUD_DOWN if pull_mode == 'DOWN' else GPIO.PUD_UP)
def write_output(port, value):
2016-03-07 17:49:31 +00:00
"""Write a value to a GPIO."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
GPIO.output(port, value)
def read_input(port):
2016-03-07 17:49:31 +00:00
"""Read a value from a GPIO."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
return GPIO.input(port)
def edge_detect(port, event_callback, bounce):
2016-03-08 16:55:57 +00:00
"""Add detection for RISING and FALLING events."""
2016-01-18 00:29:41 +00:00
import RPi.GPIO as GPIO
GPIO.add_event_detect(
port,
GPIO.BOTH,
callback=event_callback,
bouncetime=bounce)