Beaglebone black GPIO control by switch v2 (#4908)

* Added support for BBB GPIO

* Requirements updated

* unnecessary pylint statement removed

* Changed according arduino switch

* typo corrected

* Hound errors solved

* lint error

* Update bbb_gpio.py
pull/4879/merge
Martin Vacula 2017-01-05 20:53:48 +01:00 committed by Paulus Schoutsen
parent cbda516af9
commit 6b682d0d81
4 changed files with 174 additions and 0 deletions

View File

@ -13,6 +13,9 @@ omit =
homeassistant/components/arduino.py
homeassistant/components/*/arduino.py
homeassistant/components/bbb_gpio.py
homeassistant/components/*/bbb_gpio.py
homeassistant/components/bloomsky.py
homeassistant/components/*/bloomsky.py

View File

@ -0,0 +1,68 @@
"""
Support for controlling GPIO pins of a Beaglebone Black.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/bbb_gpio/
"""
import logging
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
REQUIREMENTS = ['Adafruit_BBIO==1.0.0']
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'bbb_gpio'
# pylint: disable=no-member
def setup(hass, config):
"""Setup the Beaglebone black GPIO component."""
import Adafruit_BBIO.GPIO as GPIO
def cleanup_gpio(event):
"""Stuff to do before stopping."""
GPIO.cleanup()
def prepare_gpio(event):
"""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)
return True
def setup_output(pin):
"""Setup a GPIO as output."""
import Adafruit_BBIO.GPIO as GPIO
GPIO.setup(pin, GPIO.OUT)
def setup_input(pin, pull_mode):
"""Setup a GPIO as input."""
import Adafruit_BBIO.GPIO as GPIO
GPIO.setup(pin, GPIO.IN,
GPIO.PUD_DOWN if pull_mode == 'DOWN' else GPIO.PUD_UP)
def write_output(pin, value):
"""Write a value to a GPIO."""
import Adafruit_BBIO.GPIO as GPIO
GPIO.output(pin, value)
def read_input(pin):
"""Read a value from a GPIO."""
import Adafruit_BBIO.GPIO as GPIO
return GPIO.input(pin)
def edge_detect(pin, event_callback, bounce):
"""Add detection for RISING and FALLING events."""
import Adafruit_BBIO.GPIO as GPIO
GPIO.add_event_detect(
pin,
GPIO.BOTH,
callback=event_callback,
bouncetime=bounce)

View File

@ -0,0 +1,100 @@
"""
Allows to configure a switch using BBB GPIO.
Switch example for two GPIOs pins P9_12 and P9_42
Allowed GPIO pin name is GPIOxxx or Px_x
switch:
- platform: bbb_gpio
pins:
GPIO0_7:
name: LED Red
P9_12:
name: LED Green
initial: true
invert_logic: true
"""
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA
import homeassistant.components.bbb_gpio as bbb_gpio
from homeassistant.const import (DEVICE_DEFAULT_NAME, CONF_NAME)
from homeassistant.helpers.entity import ToggleEntity
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['bbb_gpio']
CONF_PINS = 'pins'
CONF_INITIAL = 'initial'
CONF_INVERT_LOGIC = 'invert_logic'
PIN_SCHEMA = vol.Schema({
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
vol.Optional(CONF_INVERT_LOGIC, default=False): cv.boolean,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_PINS, default={}):
vol.Schema({cv.string: PIN_SCHEMA}),
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Beaglebone GPIO devices."""
pins = config.get(CONF_PINS)
switches = []
for pin, params in pins.items():
switches.append(BBBGPIOSwitch(pin, params))
add_devices(switches)
class BBBGPIOSwitch(ToggleEntity):
"""Representation of a Beaglebone GPIO."""
def __init__(self, pin, params):
"""Initialize the pin."""
self._pin = pin
self._name = params.get(CONF_NAME) or DEVICE_DEFAULT_NAME
self._state = params.get(CONF_INITIAL)
self._invert_logic = params.get(CONF_INVERT_LOGIC)
bbb_gpio.setup_output(self._pin)
if self._state is False:
bbb_gpio.write_output(self._pin, 1 if self._invert_logic else 0)
else:
bbb_gpio.write_output(self._pin, 0 if self._invert_logic else 1)
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def is_on(self):
"""Return true if device is on."""
return self._state
def turn_on(self):
"""Turn the device on."""
bbb_gpio.write_output(self._pin, 0 if self._invert_logic else 1)
self._state = True
self.schedule_update_ha_state()
def turn_off(self):
"""Turn the device off."""
bbb_gpio.write_output(self._pin, 1 if self._invert_logic else 0)
self._state = False
self.schedule_update_ha_state()

View File

@ -12,6 +12,9 @@ async_timeout==1.1.0
# homeassistant.components.nuimo_controller
--only-binary=all http://github.com/getSenic/nuimo-linux-python/archive/29fc42987f74d8090d0e2382e8f248ff5990b8c9.zip#nuimo==1.0.0
# homeassistant.components.bbb_gpio
Adafruit_BBIO==1.0.0
# homeassistant.components.isy994
PyISY==1.0.7