Use voluptuous for Pilight switch (#3819)
* Migrate to voluptuous * Add protocol * Update
This commit is contained in:
parent
a89e635bf3
commit
4484a7a94b
5 changed files with 96 additions and 74 deletions
|
@ -1,44 +1,77 @@
|
|||
"""
|
||||
Support for switching devices via pilight to on and off.
|
||||
Support for switching devices via Pilight to on and off.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.pilight/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.helpers.config_validation import ensure_list
|
||||
import homeassistant.components.pilight as pilight
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
import voluptuous as vol
|
||||
|
||||
DEPENDENCIES = ['pilight']
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.components.pilight as pilight
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (CONF_NAME, CONF_ID, CONF_SWITCHES, CONF_STATE)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_OFF_CODE = 'off_code'
|
||||
CONF_OFF_CODE_RECIEVE = 'off_code_receive'
|
||||
CONF_ON_CODE = 'on_code'
|
||||
CONF_ON_CODE_RECIEVE = 'on_code_receive'
|
||||
CONF_SYSTEMCODE = 'systemcode'
|
||||
CONF_UNIT = 'unit'
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Setup the pilight platform."""
|
||||
# Find and return switches controlled by pilight
|
||||
switches = config.get('switches', {})
|
||||
DEPENDENCIES = ['pilight']
|
||||
|
||||
COMMAND_SCHEMA = pilight.RF_CODE_SCHEMA.extend({
|
||||
vol.Optional('on'): cv.positive_int,
|
||||
vol.Optional('off'): cv.positive_int,
|
||||
vol.Optional(CONF_UNIT): cv.string,
|
||||
vol.Optional(CONF_ID): cv.positive_int,
|
||||
vol.Optional(CONF_STATE): cv.string,
|
||||
vol.Optional(CONF_SYSTEMCODE): cv.string,
|
||||
})
|
||||
|
||||
SWITCHES_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_ON_CODE): COMMAND_SCHEMA,
|
||||
vol.Required(CONF_OFF_CODE): COMMAND_SCHEMA,
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_OFF_CODE_RECIEVE): COMMAND_SCHEMA,
|
||||
vol.Optional(CONF_ON_CODE_RECIEVE): COMMAND_SCHEMA,
|
||||
})
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_SWITCHES):
|
||||
vol.Schema({cv.string: SWITCHES_SCHEMA}),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the Pilight platform."""
|
||||
switches = config.get(CONF_SWITCHES)
|
||||
devices = []
|
||||
|
||||
for dev_name, properties in switches.items():
|
||||
devices.append(
|
||||
PilightSwitch(
|
||||
hass,
|
||||
properties.get('name', dev_name),
|
||||
properties.get('on_code'),
|
||||
properties.get('off_code'),
|
||||
ensure_list(properties.get('on_code_receive', False)),
|
||||
ensure_list(properties.get('off_code_receive', False))))
|
||||
properties.get(CONF_NAME, dev_name),
|
||||
properties.get(CONF_ON_CODE),
|
||||
properties.get(CONF_OFF_CODE),
|
||||
properties.get(CONF_ON_CODE_RECIEVE),
|
||||
properties.get(CONF_OFF_CODE_RECIEVE)
|
||||
)
|
||||
)
|
||||
|
||||
add_devices_callback(devices)
|
||||
add_devices(devices)
|
||||
|
||||
|
||||
class PilightSwitch(SwitchDevice):
|
||||
"""Representation of a pilight switch."""
|
||||
"""Representation of a Pilight switch."""
|
||||
|
||||
def __init__(self, hass, name, code_on, code_off,
|
||||
code_on_receive, code_off_receive):
|
||||
def __init__(self, hass, name, code_on, code_off, code_on_receive,
|
||||
code_off_receive):
|
||||
"""Initialize the switch."""
|
||||
self._hass = hass
|
||||
self._name = name
|
||||
|
@ -69,29 +102,22 @@ class PilightSwitch(SwitchDevice):
|
|||
def _handle_code(self, call):
|
||||
"""Check if received code by the pilight-daemon.
|
||||
|
||||
If the code matches the receive on / off codes of this switch
|
||||
the switch state is changed accordingly.
|
||||
If the code matches the receive on/off codes of this switch the switch
|
||||
state is changed accordingly.
|
||||
"""
|
||||
# Check if a on code is defined to turn this switch on
|
||||
# - True if off_code/on_code is contained in received code dict, not
|
||||
# all items have to match.
|
||||
# - Call turn on/off only once, even if more than one code is received
|
||||
if any(self._code_on_receive):
|
||||
for on_code in self._code_on_receive: # Loop through codes
|
||||
# True if on_code is contained in received code dict, not
|
||||
# all items have to match
|
||||
for on_code in self._code_on_receive:
|
||||
if on_code.items() <= call.data.items():
|
||||
self.turn_on()
|
||||
# Call turn on only once, even when more than one on
|
||||
# code is received
|
||||
break
|
||||
|
||||
# Check if a off code is defined to turn this switch off
|
||||
if any(self._code_off_receive):
|
||||
for off_code in self._code_off_receive: # Loop through codes
|
||||
# True if off_code is contained in received code dict, not
|
||||
# all items have to match
|
||||
for off_code in self._code_off_receive:
|
||||
if off_code.items() <= call.data.items():
|
||||
self.turn_off()
|
||||
# Call turn off only once, even when more than one off
|
||||
# code is received
|
||||
break
|
||||
|
||||
def turn_on(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue