Use voluptuous for RPi GPIO (#3371)

* Migrate to voluptuous

* Remove the check for lists
This commit is contained in:
Fabian Affolter 2016-09-18 08:28:37 +02:00 committed by Paulus Schoutsen
parent 2b7d1fe20d
commit 04d31e4ef4
4 changed files with 85 additions and 40 deletions

View File

@ -6,16 +6,37 @@ https://home-assistant.io/components/binary_sensor.rpi_gpio/
""" """
import logging import logging
import homeassistant.components.rpi_gpio as rpi_gpio import voluptuous as vol
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.const import DEVICE_DEFAULT_NAME import homeassistant.components.rpi_gpio as rpi_gpio
from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.const import DEVICE_DEFAULT_NAME
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
CONF_BOUNCETIME = 'bouncetime'
CONF_INVERT_LOGIC = 'invert_logic'
CONF_PORTS = 'ports'
CONF_PULL_MODE = 'pull_mode'
DEFAULT_PULL_MODE = "UP"
DEFAULT_BOUNCETIME = 50 DEFAULT_BOUNCETIME = 50
DEFAULT_INVERT_LOGIC = False DEFAULT_INVERT_LOGIC = False
DEFAULT_PULL_MODE = 'UP'
DEPENDENCIES = ['rpi_gpio'] DEPENDENCIES = ['rpi_gpio']
_LOGGER = logging.getLogger(__name__)
_SENSORS_SCHEMA = vol.Schema({
cv.positive_int: cv.string,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_PORTS): _SENSORS_SCHEMA,
vol.Optional(CONF_BOUNCETIME, default=DEFAULT_BOUNCETIME): cv.positive_int,
vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean,
vol.Optional(CONF_PULL_MODE, default=DEFAULT_PULL_MODE): cv.string,
})
# pylint: disable=unused-argument # pylint: disable=unused-argument

View File

@ -7,64 +7,69 @@ https://github.com/andrewshilliday/garage-door-controller
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/cover.rpi_gpio/ https://home-assistant.io/components/cover.rpi_gpio/
""" """
import logging import logging
from time import sleep from time import sleep
import voluptuous as vol import voluptuous as vol
from homeassistant.components.cover import CoverDevice from homeassistant.components.cover import CoverDevice, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
import homeassistant.components.rpi_gpio as rpi_gpio import homeassistant.components.rpi_gpio as rpi_gpio
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
RELAY_TIME = 'relay_time'
STATE_PULL_MODE = 'state_pull_mode'
DEFAULT_PULL_MODE = 'UP'
DEFAULT_RELAY_TIME = .2
DEPENDENCIES = ['rpi_gpio']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_COVERS = 'covers'
CONF_RELAY_PIN = 'relay_pin'
CONF_RELAY_TIME = 'relay_time'
CONF_STATE_PIN = 'state_pin'
CONF_STATE_PULL_MODE = 'state_pull_mode'
DEFAULT_RELAY_TIME = .2
DEFAULT_STATE_PULL_MODE = 'UP'
DEPENDENCIES = ['rpi_gpio']
_COVERS_SCHEMA = vol.All( _COVERS_SCHEMA = vol.All(
cv.ensure_list, cv.ensure_list,
[ [
vol.Schema({ vol.Schema({
'name': str, CONF_NAME: cv.string,
'relay_pin': int, CONF_RELAY_PIN: cv.positive_int,
'state_pin': int, CONF_STATE_PIN: cv.positive_int,
}) })
] ]
) )
PLATFORM_SCHEMA = vol.Schema({
'platform': str, PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required('covers'): _COVERS_SCHEMA, vol.Required(CONF_COVERS): _COVERS_SCHEMA,
vol.Optional(STATE_PULL_MODE, default=DEFAULT_PULL_MODE): cv.string, vol.Optional(CONF_STATE_PULL_MODE, default=DEFAULT_STATE_PULL_MODE):
vol.Optional(RELAY_TIME, default=DEFAULT_RELAY_TIME): vol.Coerce(int), cv.string,
vol.Optional(CONF_RELAY_TIME, default=DEFAULT_RELAY_TIME): cv.positive_int,
}) })
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the cover platform.""" """Setup the RPi cover platform."""
relay_time = config.get(RELAY_TIME) relay_time = config.get(CONF_RELAY_TIME)
state_pull_mode = config.get(STATE_PULL_MODE) state_pull_mode = config.get(CONF_STATE_PULL_MODE)
covers = [] covers = []
covers_conf = config.get('covers') covers_conf = config.get(CONF_COVERS)
for cover in covers_conf: for cover in covers_conf:
covers.append(RPiGPIOCover(cover['name'], cover['relay_pin'], covers.append(RPiGPIOCover(
cover['state_pin'], cover[CONF_NAME], cover[CONF_RELAY_PIN], cover[CONF_STATE_PIN],
state_pull_mode, state_pull_mode, relay_time))
relay_time))
add_devices(covers) add_devices(covers)
# pylint: disable=abstract-method # pylint: disable=abstract-method
class RPiGPIOCover(CoverDevice): class RPiGPIOCover(CoverDevice):
"""Representation of a Raspberry cover.""" """Representation of a Raspberry GPIO cover."""
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, name, relay_pin, state_pin, def __init__(self, name, relay_pin, state_pin, state_pull_mode,
state_pull_mode, relay_time): relay_time):
"""Initialize the cover.""" """Initialize the cover."""
self._name = name self._name = name
self._state = False self._state = False
@ -79,7 +84,7 @@ class RPiGPIOCover(CoverDevice):
@property @property
def unique_id(self): def unique_id(self):
"""Return the ID of this cover.""" """Return the ID of this cover."""
return "{}.{}".format(self.__class__, self._name) return '{}.{}'.format(self.__class__, self._name)
@property @property
def name(self): def name(self):

View File

@ -1,7 +1,7 @@
""" """
Support for controlling GPIO pins of a Raspberry Pi. Support for controlling GPIO pins of a Raspberry Pi.
For more details about this platform, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/rpi_gpio/ https://home-assistant.io/components/rpi_gpio/
""" """
# pylint: disable=import-error # pylint: disable=import-error
@ -11,9 +11,11 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
REQUIREMENTS = ['RPi.GPIO==0.6.1'] REQUIREMENTS = ['RPi.GPIO==0.6.1']
DOMAIN = "rpi_gpio"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = 'rpi_gpio'
# pylint: disable=no-member # pylint: disable=no-member
def setup(hass, config): def setup(hass, config):

View File

@ -4,26 +4,43 @@ Allows to configure a switch using RPi GPIO.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.rpi_gpio/ https://home-assistant.io/components/switch.rpi_gpio/
""" """
import logging import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA
import homeassistant.components.rpi_gpio as rpi_gpio import homeassistant.components.rpi_gpio as rpi_gpio
from homeassistant.const import DEVICE_DEFAULT_NAME from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['rpi_gpio']
CONF_PULL_MODE = 'pull_mode'
CONF_PORTS = 'ports'
CONF_INVERT_LOGIC = 'invert_logic'
DEFAULT_INVERT_LOGIC = False DEFAULT_INVERT_LOGIC = False
DEPENDENCIES = ['rpi_gpio'] _SWITCHES_SCHEMA = vol.Schema({
_LOGGER = logging.getLogger(__name__) cv.positive_int: cv.string,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_PORTS): _SWITCHES_SCHEMA,
vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean,
})
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Raspberry PI GPIO devices.""" """Setup the Raspberry PI GPIO devices."""
invert_logic = config.get('invert_logic', DEFAULT_INVERT_LOGIC) invert_logic = config.get(CONF_INVERT_LOGIC)
switches = [] switches = []
ports = config.get('ports') ports = config.get(CONF_PORTS)
for port, name in ports.items(): for port, name in ports.items():
switches.append(RPiGPIOSwitch(name, port, invert_logic)) switches.append(RPiGPIOSwitch(name, port, invert_logic))
add_devices(switches) add_devices(switches)