Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/binary_sensor/wemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities_callback, discovery_info=None):
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Register discovered WeMo binary sensors."""
from pywemo import discovery

Expand All @@ -34,7 +34,7 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
raise PlatformNotReady

if device:
add_entities_callback([WemoBinarySensor(hass, device)])
add_entities([WemoBinarySensor(hass, device)])


class WemoBinarySensor(BinarySensorDevice):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/switch/wemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
WEMO_STANDBY = 8


def setup_platform(hass, config, add_entities_callback, discovery_info=None):
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up discovered WeMo switches."""
from pywemo import discovery

Expand All @@ -51,7 +51,7 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
raise PlatformNotReady

if device:
add_entities_callback([WemoSwitch(device)])
add_entities([WemoSwitch(device)])


class WemoSwitch(SwitchDevice):
Expand Down
86 changes: 49 additions & 37 deletions homeassistant/components/wemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,24 @@ def coerce_host_port(value):

def setup(hass, config):
"""Set up for WeMo devices."""
_LOGGER.debug("Beginning setup of WeMo component...")
Comment thread
sqldiablo marked this conversation as resolved.
Outdated

import pywemo

# Keep track of WeMo device subscriptions for push updates
global SUBSCRIPTION_REGISTRY
SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry()
SUBSCRIPTION_REGISTRY.start()

def stop_wemo(event):
"""Shutdown Wemo subscriptions and subscription thread on exit."""
_LOGGER.debug("Shutting down subscriptions.")
_LOGGER.debug("Shutting down WeMo event subscriptions.")
Comment thread
sqldiablo marked this conversation as resolved.
Outdated
SUBSCRIPTION_REGISTRY.stop()

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo)

def discovery_dispatch(service, discovery_info):
"""Dispatcher for WeMo discovery events."""
"""Dispatcher for incoming WeMo discovery events."""
# name, model, location, mac
model_name = discovery_info.get('model_name')
serial = discovery_info.get('serial')
Expand All @@ -99,7 +102,8 @@ def discovery_dispatch(service, discovery_info):
_LOGGER.debug('Ignoring known device %s %s',
service, discovery_info)
return
_LOGGER.debug('Discovered unique device %s', serial)

_LOGGER.debug('Discovered unique WeMo device: %s', serial)
KNOWN_DEVICES.append(serial)

component = WEMO_MODEL_DISPATCH.get(model_name, 'switch')
Expand All @@ -125,43 +129,51 @@ def setup_url_for_address(host, port):

devices = []

_LOGGER.debug("Scanning statically configured WeMo 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)
continue

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)
continue

if not [d[1] for d in devices
if d[1].serialnumber == device.serialnumber]:
devices.append((url, device))

if config.get(DOMAIN, {}).get(CONF_DISCOVERY):
_LOGGER.debug("Scanning for WeMo devices...")
for device in pywemo.discover_devices():
async def async_discover_wemo_devices(hass, config):
"""Run discovery in an async context so setup can complete quickly."""
Comment thread
sqldiablo marked this conversation as resolved.
Outdated
_LOGGER.debug("Scanning statically configured WeMo devices...")
for host, port in config.get(DOMAIN, {}).get(CONF_STATIC, []):
url = setup_url_for_address(host, port)
Comment thread
sqldiablo marked this conversation as resolved.

if not url:
_LOGGER.error(
'Unable to get description url for WeMo at: %s',
'{}:{}'.format(host, port) if port else host)
continue

try:
device = pywemo.discovery.device_from_description(url, None)
Comment thread
sqldiablo marked this conversation as resolved.
except (requests.exceptions.ConnectionError,
requests.exceptions.Timeout) as err:
_LOGGER.error('Unable to access WeMo at %s (%s)', url, err)
continue

if not [d[1] for d in devices
if d[1].serialnumber == device.serialnumber]:
devices.append((setup_url_for_device(device), device))
devices.append((url, device))

if config.get(DOMAIN, {}).get(CONF_DISCOVERY):
_LOGGER.debug("Scanning for WeMo devices...")
for device in pywemo.discover_devices():
Comment thread
sqldiablo marked this conversation as resolved.
if not [d[1] for d in devices
if d[1].serialnumber == device.serialnumber]:
devices.append((setup_url_for_device(device), device))

for url, device in devices:
_LOGGER.debug('Adding WeMo device at %s:%i',
device.host, device.port)

discovery_info = {
'model_name': device.model_name,
'serial': device.serialnumber,
'mac_address': device.mac,
'ssdp_description': url,
}

discovery.discover(hass, SERVICE_WEMO, discovery_info)
Comment thread
sqldiablo marked this conversation as resolved.

for url, device in devices:
_LOGGER.debug('Adding WeMo device at %s:%i', device.host, device.port)
hass.add_job(async_discover_wemo_devices, hass, config)
Comment thread
sqldiablo marked this conversation as resolved.
Outdated

discovery_info = {
'model_name': device.model_name,
'serial': device.serialnumber,
'mac_address': device.mac,
'ssdp_description': url,
}
_LOGGER.debug("Setup of WeMo component has finished.")
Comment thread
sqldiablo marked this conversation as resolved.
Outdated

discovery.discover(hass, SERVICE_WEMO, discovery_info)
return True