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,5 +1,5 @@
|
|||
"""
|
||||
Component to create an interface to a Pilight daemon (https://pilight.org/).
|
||||
Component to create an interface to a Pilight daemon.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/pilight/
|
||||
|
@ -12,40 +12,38 @@ import voluptuous as vol
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_HOST, CONF_PORT,
|
||||
CONF_WHITELIST)
|
||||
CONF_WHITELIST, CONF_PROTOCOL)
|
||||
|
||||
REQUIREMENTS = ['pilight==0.1.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_PROTOCOL = 'protocol'
|
||||
|
||||
DEFAULT_HOST = '127.0.0.1'
|
||||
DEFAULT_PORT = 5000
|
||||
DOMAIN = 'pilight'
|
||||
|
||||
EVENT = 'pilight_received'
|
||||
|
||||
# The pilight code schema depends on the protocol
|
||||
# Thus only require to have the protocol information
|
||||
# Ensure that protocol is in a list otherwise segfault in pilight-daemon
|
||||
# https://github.com/pilight/pilight/issues/296
|
||||
RF_CODE_SCHEMA = vol.Schema({vol.Required(ATTR_PROTOCOL):
|
||||
vol.All(cv.ensure_list, [cv.string])},
|
||||
extra=vol.ALLOW_EXTRA)
|
||||
# The Pilight code schema depends on the protocol. Thus only require to have
|
||||
# the protocol information. Ensure that protocol is in a list otherwise
|
||||
# segfault in pilight-daemon, https://github.com/pilight/pilight/issues/296
|
||||
RF_CODE_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_PROTOCOL): vol.All(cv.ensure_list, [cv.string]),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
SERVICE_NAME = 'send'
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_WHITELIST, default={}): {cv.string: [cv.string]}
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Setup the pilight component."""
|
||||
"""Setup the Pilight component."""
|
||||
from pilight import pilight
|
||||
|
||||
host = config[DOMAIN][CONF_HOST]
|
||||
|
@ -58,23 +56,22 @@ def setup(hass, config):
|
|||
host, port, err)
|
||||
return False
|
||||
|
||||
# Start / stop pilight-daemon connection with HA start/stop
|
||||
def start_pilight_client(_):
|
||||
"""Called once when home assistant starts."""
|
||||
"""Called once when Home Assistant starts."""
|
||||
pilight_client.start()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_pilight_client)
|
||||
|
||||
def stop_pilight_client(_):
|
||||
"""Called once when home assistant stops."""
|
||||
"""Called once when Home Assistant stops."""
|
||||
pilight_client.stop()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_pilight_client)
|
||||
|
||||
def send_code(call):
|
||||
"""Send RF code to the pilight-daemon."""
|
||||
# Change type to dict from mappingproxy
|
||||
# since data has to be JSON serializable
|
||||
# Change type to dict from mappingproxy since data has to be JSON
|
||||
# serializable
|
||||
message_data = dict(call.data)
|
||||
|
||||
try:
|
||||
|
@ -82,8 +79,8 @@ def setup(hass, config):
|
|||
except IOError:
|
||||
_LOGGER.error('Pilight send failed for %s', str(message_data))
|
||||
|
||||
hass.services.register(DOMAIN, SERVICE_NAME,
|
||||
send_code, schema=RF_CODE_SCHEMA)
|
||||
hass.services.register(
|
||||
DOMAIN, SERVICE_NAME, send_code, schema=RF_CODE_SCHEMA)
|
||||
|
||||
# Publish received codes on the HA event bus
|
||||
# A whitelist of codes to be published in the event bus
|
||||
|
@ -93,10 +90,8 @@ def setup(hass, config):
|
|||
"""Called when RF codes are received."""
|
||||
# Unravel dict of dicts to make event_data cut in automation rule
|
||||
# possible
|
||||
data = dict(
|
||||
{'protocol': data['protocol'],
|
||||
'uuid': data['uuid']},
|
||||
**data['message'])
|
||||
data = dict({'protocol': data['protocol'], 'uuid': data['uuid']},
|
||||
**data['message'])
|
||||
|
||||
# No whitelist defined, put data on event bus
|
||||
if not whitelist:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue