2016-02-19 06:57:32 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for WeMo device discovery.
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/wemo/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-08-20 12:03:57 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.components.discovery import SERVICE_WEMO
|
|
|
|
from homeassistant.helpers import discovery
|
2016-08-20 12:03:57 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|
|
|
|
2017-01-11 23:10:24 +00:00
|
|
|
REQUIREMENTS = ['pywemo==0.4.9']
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
DOMAIN = 'wemo'
|
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
# Mapping from Wemo model_name to component.
|
2016-02-19 06:57:32 +00:00
|
|
|
WEMO_MODEL_DISPATCH = {
|
2016-06-12 00:43:13 +00:00
|
|
|
'Bridge': 'light',
|
|
|
|
'Insight': 'switch',
|
|
|
|
'Maker': 'switch',
|
|
|
|
'Sensor': 'binary_sensor',
|
|
|
|
'Socket': 'switch',
|
|
|
|
'LightSwitch': 'switch'
|
2016-02-19 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBSCRIPTION_REGISTRY = None
|
|
|
|
KNOWN_DEVICES = []
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-20 12:03:57 +00:00
|
|
|
CONF_STATIC = 'static'
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(CONF_STATIC, default=[]): vol.Schema([cv.string])
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument, too-many-function-args
|
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Common setup for WeMo devices."""
|
2016-02-19 06:57:32 +00:00
|
|
|
import pywemo
|
|
|
|
|
|
|
|
global SUBSCRIPTION_REGISTRY
|
|
|
|
SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry()
|
|
|
|
SUBSCRIPTION_REGISTRY.start()
|
|
|
|
|
|
|
|
def stop_wemo(event):
|
|
|
|
"""Shutdown Wemo subscriptions and subscription thread on exit."""
|
|
|
|
_LOGGER.info("Shutting down subscriptions.")
|
|
|
|
SUBSCRIPTION_REGISTRY.stop()
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo)
|
|
|
|
|
|
|
|
def discovery_dispatch(service, discovery_info):
|
|
|
|
"""Dispatcher for WeMo discovery events."""
|
|
|
|
# name, model, location, mac
|
2016-03-01 18:49:38 +00:00
|
|
|
_, model_name, _, _, serial = discovery_info
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
# Only register a device once
|
2016-03-01 18:49:38 +00:00
|
|
|
if serial in KNOWN_DEVICES:
|
2016-02-19 06:57:32 +00:00
|
|
|
return
|
2016-03-01 18:49:38 +00:00
|
|
|
_LOGGER.debug('Discovered unique device %s', serial)
|
|
|
|
KNOWN_DEVICES.append(serial)
|
2016-02-19 06:57:32 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
component = WEMO_MODEL_DISPATCH.get(model_name, 'switch')
|
2016-02-19 06:57:32 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, discovery_info,
|
|
|
|
config)
|
2016-02-19 06:57:32 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
discovery.listen(hass, SERVICE_WEMO, discovery_dispatch)
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
_LOGGER.info("Scanning for WeMo devices.")
|
|
|
|
devices = [(device.host, device) for device in pywemo.discover_devices()]
|
|
|
|
|
2016-03-07 17:49:31 +00:00
|
|
|
# Add static devices from the config file.
|
2016-02-28 04:11:32 +00:00
|
|
|
devices.extend((address, None)
|
2016-08-29 13:45:48 +00:00
|
|
|
for address in config.get(DOMAIN, {}).get(CONF_STATIC, []))
|
2016-02-19 06:57:32 +00:00
|
|
|
|
|
|
|
for address, device in devices:
|
|
|
|
port = pywemo.ouimeaux_device.probe_wemo(address)
|
|
|
|
if not port:
|
|
|
|
_LOGGER.warning('Unable to probe wemo at %s', address)
|
|
|
|
continue
|
|
|
|
_LOGGER.info('Adding wemo at %s:%i', address, port)
|
|
|
|
|
|
|
|
url = 'http://%s:%i/setup.xml' % (address, port)
|
|
|
|
if device is None:
|
|
|
|
device = pywemo.discovery.device_from_description(url, None)
|
|
|
|
|
2016-03-01 18:49:38 +00:00
|
|
|
discovery_info = (device.name, device.model_name, url, device.mac,
|
|
|
|
device.serialnumber)
|
2016-06-12 00:43:13 +00:00
|
|
|
discovery.discover(hass, SERVICE_WEMO, discovery_info)
|
2016-02-19 06:57:32 +00:00
|
|
|
return True
|