Use constants, update configuration check, and ordering (Pilight) (#3118)

* Use contants, update configuration check, and ordering

* Fix pylint issue
This commit is contained in:
Fabian Affolter 2016-09-02 06:28:52 +02:00 committed by Teagan Glenn
parent a571271c39
commit db7abc1cfe

View File

@ -4,7 +4,6 @@ Component to create an interface to a Pilight daemon (https://pilight.org/).
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/pilight/ https://home-assistant.io/components/pilight/
""" """
# pylint: disable=import-error
import logging import logging
import socket import socket
@ -12,46 +11,49 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import ensure_list from homeassistant.helpers.config_validation import ensure_list
from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.const import (
from homeassistant.const import EVENT_HOMEASSISTANT_STOP EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_HOST, CONF_PORT,
from homeassistant.const import CONF_HOST, CONF_PORT CONF_WHITELIST)
REQUIREMENTS = ['pilight==0.0.2'] REQUIREMENTS = ['pilight==0.0.2']
DOMAIN = "pilight" _LOGGER = logging.getLogger(__name__)
ATTR_PROTOCOL = 'protocol'
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 5000
DOMAIN = 'pilight'
EVENT = 'pilight_received' EVENT = 'pilight_received'
SERVICE_NAME = 'send'
CONF_WHITELIST = 'whitelist'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST, default='127.0.0.1'): cv.string,
vol.Required(CONF_PORT, default=5000): vol.Coerce(int),
vol.Optional(CONF_WHITELIST): {cv.string: [cv.string]}
}),
}, extra=vol.ALLOW_EXTRA)
# The pilight code schema depends on the protocol # The pilight code schema depends on the protocol
# Thus only require to have the protocol information # Thus only require to have the protocol information
ATTR_PROTOCOL = 'protocol'
RF_CODE_SCHEMA = vol.Schema({vol.Required(ATTR_PROTOCOL): cv.string}, RF_CODE_SCHEMA = vol.Schema({vol.Required(ATTR_PROTOCOL): cv.string},
extra=vol.ALLOW_EXTRA) extra=vol.ALLOW_EXTRA)
SERVICE_NAME = 'send'
_LOGGER = logging.getLogger(__name__) 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_WHITELIST, default={}): {cv.string: [cv.string]}
}),
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config): def setup(hass, config):
"""Setup the pilight component.""" """Setup the pilight component."""
from pilight import pilight from pilight import pilight
host = config[DOMAIN][CONF_HOST]
port = config[DOMAIN][CONF_PORT]
try: try:
pilight_client = pilight.Client(host=config[DOMAIN][CONF_HOST], pilight_client = pilight.Client(host=host, port=port)
port=config[DOMAIN][CONF_PORT])
except (socket.error, socket.timeout) as err: except (socket.error, socket.timeout) as err:
_LOGGER.error( _LOGGER.error("Unable to connect to %s on port %s: %s",
"Unable to connect to %s on port %s: %s", host, port, err)
config[CONF_HOST], config[CONF_PORT], err)
return False return False
# Start / stop pilight-daemon connection with HA start/stop # Start / stop pilight-daemon connection with HA start/stop
@ -74,7 +76,7 @@ def setup(hass, config):
# Patch data because of bug: # Patch data because of bug:
# https://github.com/pilight/pilight/issues/296 # https://github.com/pilight/pilight/issues/296
# Protocol has to be in a list otherwise segfault in pilight-daemon # Protocol has to be in a list otherwise segfault in pilight-daemon
message_data["protocol"] = ensure_list(message_data["protocol"]) message_data['protocol'] = ensure_list(message_data['protocol'])
try: try:
pilight_client.send_code(message_data) pilight_client.send_code(message_data)
@ -86,7 +88,7 @@ def setup(hass, config):
# Publish received codes on the HA event bus # Publish received codes on the HA event bus
# A whitelist of codes to be published in the event bus # A whitelist of codes to be published in the event bus
whitelist = config[DOMAIN].get('whitelist', False) whitelist = config[DOMAIN].get(CONF_WHITELIST)
def handle_received_code(data): def handle_received_code(data):
"""Called when RF codes are received.""" """Called when RF codes are received."""