orvibo updates (#3006)

🐬
pull/3132/head
happyleavesaoc 2016-09-02 00:37:09 -04:00 committed by Teagan Glenn
parent 451f0cb3f1
commit 24d3cbdfe9
1 changed files with 36 additions and 14 deletions

View File

@ -6,40 +6,62 @@ https://home-assistant.io/components/switch.orvibo/
""" """
import logging import logging
from homeassistant.components.switch import SwitchDevice import voluptuous as vol
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
DEFAULT_NAME = "Orvibo S20 Switch"
REQUIREMENTS = ['orvibo==1.1.1'] REQUIREMENTS = ['orvibo==1.1.1']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_SWITCHES = 'switches'
CONF_HOST = 'host'
CONF_NAME = 'name'
CONF_MAC = 'mac'
CONF_DISCOVERY = 'discovery'
DEFAULT_NAME = 'Orvibo S20 Switch'
DEFAULT_DISCOVERY = True
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY): cv.boolean,
vol.Required(CONF_SWITCHES, default=[]):
vol.All(cv.ensure_list, [{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_MAC): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string
}])
})
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Find and return S20 switches.""" """Find and return S20 switches."""
from orvibo.s20 import S20, S20Exception from orvibo.s20 import discover, S20, S20Exception
switch_data = {}
switches = [] switches = []
switch_conf = config.get('switches', [config]) switch_conf = config.get(CONF_SWITCHES, [config])
if config.get(CONF_DISCOVERY):
_LOGGER.info("Discovering S20 switches ...")
switch_data.update(discover())
for switch in switch_conf: for switch in switch_conf:
if switch.get('host') is None: switch_data[switch.get(CONF_HOST)] = switch
_LOGGER.error("Missing required variable: host")
continue for host, data in switch_data.items():
host = switch.get('host')
mac = switch.get('mac')
try: try:
switches.append(S20Switch(switch.get('name', DEFAULT_NAME), switches.append(S20Switch(data.get(CONF_NAME, DEFAULT_NAME),
S20(host, mac=mac))) S20(host, mac=data.get(CONF_MAC))))
_LOGGER.info("Initialized S20 at %s", host) _LOGGER.info("Initialized S20 at %s", host)
except S20Exception: except S20Exception:
_LOGGER.exception("S20 at %s couldn't be initialized", _LOGGER.error("S20 at %s couldn't be initialized", host)
host)
add_devices_callback(switches) add_devices_callback(switches)
class S20Switch(SwitchDevice): class S20Switch(SwitchDevice):
"""Representsation of an S20 switch.""" """Representation of an S20 switch."""
def __init__(self, name, s20): def __init__(self, name, s20):
"""Initialize the S20 device.""" """Initialize the S20 device."""