diff --git a/homeassistant/components/binary_sensor/wemo.py b/homeassistant/components/binary_sensor/wemo.py index a589ab4e8c8..bde6accf421 100644 --- a/homeassistant/components/binary_sensor/wemo.py +++ b/homeassistant/components/binary_sensor/wemo.py @@ -5,8 +5,10 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/binary_sensor.wemo/ """ import logging +import requests from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.exceptions import PlatformNotReady DEPENDENCIES = ['wemo'] @@ -20,7 +22,13 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if discovery_info is not None: location = discovery_info['ssdp_description'] mac = discovery_info['mac_address'] - device = discovery.device_from_description(location, mac) + + try: + device = discovery.device_from_description(location, mac) + except (requests.exceptions.ConnectionError, + requests.exceptions.Timeout) as err: + _LOGGER.error('Unable to access %s (%s)', location, err) + raise PlatformNotReady if device: add_devices_callback([WemoBinarySensor(hass, device)]) diff --git a/homeassistant/components/light/wemo.py b/homeassistant/components/light/wemo.py index 4c912d60fb7..78dedb12718 100644 --- a/homeassistant/components/light/wemo.py +++ b/homeassistant/components/light/wemo.py @@ -7,11 +7,13 @@ https://home-assistant.io/components/light.wemo/ import asyncio import logging from datetime import timedelta +import requests from homeassistant import util from homeassistant.components.light import ( Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, ATTR_TRANSITION, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_COLOR, SUPPORT_TRANSITION) +from homeassistant.exceptions import PlatformNotReady import homeassistant.util.color as color_util DEPENDENCIES = ['wemo'] @@ -32,7 +34,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is not None: location = discovery_info['ssdp_description'] mac = discovery_info['mac_address'] - device = discovery.device_from_description(location, mac) + + try: + device = discovery.device_from_description(location, mac) + except (requests.exceptions.ConnectionError, + requests.exceptions.Timeout) as err: + _LOGGER.error('Unable to access %s (%s)', location, err) + raise PlatformNotReady if device.model_name == 'Dimmer': add_devices([WemoDimmer(device)]) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 35ea435bf48..594f290273e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -7,10 +7,12 @@ https://home-assistant.io/components/switch.wemo/ import asyncio import logging from datetime import datetime, timedelta +import requests import async_timeout from homeassistant.components.switch import SwitchDevice +from homeassistant.exceptions import PlatformNotReady from homeassistant.util import convert from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN) @@ -40,7 +42,13 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if discovery_info is not None: location = discovery_info['ssdp_description'] mac = discovery_info['mac_address'] - device = discovery.device_from_description(location, mac) + + try: + device = discovery.device_from_description(location, mac) + except (requests.exceptions.ConnectionError, + requests.exceptions.Timeout) as err: + _LOGGER.error('Unable to access %s (%s)', location, err) + raise PlatformNotReady if device: add_devices_callback([WemoSwitch(device)]) diff --git a/homeassistant/components/wemo.py b/homeassistant/components/wemo.py index 27027cc9eb4..2ce2ff475a2 100644 --- a/homeassistant/components/wemo.py +++ b/homeassistant/components/wemo.py @@ -6,6 +6,7 @@ https://home-assistant.io/components/wemo/ """ import logging +import requests import voluptuous as vol from homeassistant.components.discovery import SERVICE_WEMO @@ -36,11 +37,32 @@ KNOWN_DEVICES = [] _LOGGER = logging.getLogger(__name__) + +def coerce_host_port(value): + """Validate that provided value is either just host or host:port. + + Returns (host, None) or (host, port) respectively. + """ + host, _, port = value.partition(':') + + if not host: + raise vol.Invalid('host cannot be empty') + + if port: + port = cv.port(port) + else: + port = None + + return host, port + + CONF_STATIC = 'static' CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ - vol.Optional(CONF_STATIC, default=[]): vol.Schema([cv.string]) + vol.Optional(CONF_STATIC, default=[]): vol.Schema([ + vol.All(cv.string, coerce_host_port) + ]) }), }, extra=vol.ALLOW_EXTRA) @@ -79,23 +101,47 @@ def setup(hass, config): discovery.listen(hass, SERVICE_WEMO, discovery_dispatch) - _LOGGER.info("Scanning for WeMo devices.") - devices = [(device.host, device) for device in pywemo.discover_devices()] + def setup_url_for_device(device): + """Determine setup.xml url for given device.""" + return 'http://{}:{}/setup.xml'.format(device.host, device.port) - # Add static devices from the config file. - devices.extend((address, None) - for address in config.get(DOMAIN, {}).get(CONF_STATIC, [])) - - for address, device in devices: - port = pywemo.ouimeaux_device.probe_wemo(address) + def setup_url_for_address(host, port): + """Determine setup.xml url for given host and port pair.""" if not port: - _LOGGER.warning('Unable to probe wemo at %s', address) - continue - _LOGGER.info('Adding wemo at %s:%i', address, port) + port = pywemo.ouimeaux_device.probe_wemo(host) - url = 'http://%s:%i/setup.xml' % (address, port) - if device is None: + if not port: + return None + + return 'http://{}:{}/setup.xml'.format(host, port) + + devices = [] + + for host, port in config.get(DOMAIN, {}).get(CONF_STATIC, []): + url = setup_url_for_address(host, port) + + if not url: + _LOGGER.error( + 'Unable to get description url for %s', + '{}:{}'.format(host, port) if port else host) + return False + + try: device = pywemo.discovery.device_from_description(url, None) + except (requests.exceptions.ConnectionError, + requests.exceptions.Timeout) as err: + _LOGGER.error('Unable to access %s (%s)', url, err) + return False + + devices.append((url, device)) + + _LOGGER.info("Scanning for WeMo devices.") + devices.extend( + (setup_url_for_device(device), device) + for device in pywemo.discover_devices()) + + for url, device in devices: + _LOGGER.info('Adding wemo at %s:%i', device.host, device.port) discovery_info = { 'model_name': device.model_name,