mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Migrate to voluptuous (#3817)
This commit is contained in:
parent
d4dc2707a1
commit
e135691bd6
@ -4,51 +4,65 @@ Allows to configure a switch using a 433MHz module via GPIO on a Raspberry Pi.
|
|||||||
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_rf/
|
https://home-assistant.io/components/switch.rpi_rf/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDevice
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||||
|
from homeassistant.const import (CONF_NAME, CONF_SWITCHES)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['rpi-rf==0.9.5']
|
REQUIREMENTS = ['rpi-rf==0.9.5']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
CONF_CODE_OFF = 'code_off'
|
||||||
|
CONF_CODE_ON = 'code_on'
|
||||||
|
CONF_GPIO = 'gpio'
|
||||||
|
CONF_PROTOCOL = 'protocol'
|
||||||
|
CONF_PULSELENGTH = 'pulselength'
|
||||||
|
|
||||||
|
DEFAULT_PROTOCOL = 1
|
||||||
|
|
||||||
|
SWITCH_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(CONF_CODE_OFF): cv.positive_int,
|
||||||
|
vol.Required(CONF_CODE_ON): cv.positive_int,
|
||||||
|
vol.Optional(CONF_PULSELENGTH): cv.positive_int,
|
||||||
|
vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_GPIO): cv.positive_int,
|
||||||
|
vol.Required(CONF_SWITCHES): vol.Schema({cv.string: SWITCH_SCHEMA}),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument, import-error
|
# pylint: disable=unused-argument, import-error
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Find and return switches controlled by a generic RF device via GPIO."""
|
"""Find and return switches controlled by a generic RF device via GPIO."""
|
||||||
import rpi_rf
|
import rpi_rf
|
||||||
|
|
||||||
gpio = config.get('gpio')
|
gpio = config.get(CONF_GPIO)
|
||||||
if not gpio:
|
|
||||||
_LOGGER.error("No GPIO specified")
|
|
||||||
return False
|
|
||||||
|
|
||||||
rfdevice = rpi_rf.RFDevice(gpio)
|
rfdevice = rpi_rf.RFDevice(gpio)
|
||||||
|
switches = config.get(CONF_SWITCHES)
|
||||||
|
|
||||||
switches = config.get('switches', {})
|
|
||||||
devices = []
|
devices = []
|
||||||
for dev_name, properties in switches.items():
|
for dev_name, properties in switches.items():
|
||||||
if not properties.get('code_on'):
|
|
||||||
_LOGGER.error("%s: code_on not specified", dev_name)
|
|
||||||
continue
|
|
||||||
if not properties.get('code_off'):
|
|
||||||
_LOGGER.error("%s: code_off not specified", dev_name)
|
|
||||||
continue
|
|
||||||
|
|
||||||
devices.append(
|
devices.append(
|
||||||
RPiRFSwitch(
|
RPiRFSwitch(
|
||||||
hass,
|
hass,
|
||||||
properties.get('name', dev_name),
|
properties.get(CONF_NAME, dev_name),
|
||||||
rfdevice,
|
rfdevice,
|
||||||
properties.get('protocol', None),
|
properties.get(CONF_PROTOCOL),
|
||||||
properties.get('pulselength', None),
|
properties.get(CONF_PULSELENGTH),
|
||||||
properties.get('code_on'),
|
properties.get(CONF_CODE_ON),
|
||||||
properties.get('code_off')))
|
properties.get(CONF_CODE_OFF)
|
||||||
|
)
|
||||||
|
)
|
||||||
if devices:
|
if devices:
|
||||||
rfdevice.enable_tx()
|
rfdevice.enable_tx()
|
||||||
|
|
||||||
add_devices_callback(devices)
|
add_devices(devices)
|
||||||
|
|
||||||
|
|
||||||
class RPiRFSwitch(SwitchDevice):
|
class RPiRFSwitch(SwitchDevice):
|
||||||
@ -84,10 +98,10 @@ class RPiRFSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def _send_code(self, code, protocol, pulselength):
|
def _send_code(self, code, protocol, pulselength):
|
||||||
"""Send the code with a specified pulselength."""
|
"""Send the code with a specified pulselength."""
|
||||||
_LOGGER.info('Sending code: %s', code)
|
_LOGGER.info("Sending code: %s", code)
|
||||||
res = self._rfdevice.tx_code(code, protocol, pulselength)
|
res = self._rfdevice.tx_code(code, protocol, pulselength)
|
||||||
if not res:
|
if not res:
|
||||||
_LOGGER.error('Sending code %s failed', code)
|
_LOGGER.error("Sending code %s failed", code)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def turn_on(self):
|
def turn_on(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user