Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
139 changes: 81 additions & 58 deletions homeassistant/components/wemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import voluptuous as vol

from homeassistant.components.discovery import SERVICE_WEMO
from homeassistant.helpers import discovery
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.helpers.event import async_track_point_in_utc_time
import homeassistant.util.dt as dt_util

from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)

REQUIREMENTS = ['pywemo==0.4.34']

Expand Down Expand Up @@ -73,23 +77,44 @@ def coerce_host_port(value):
}, extra=vol.ALLOW_EXTRA)


def setup(hass, config):
async def async_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 devices
devices = []

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

@callback
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)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo)

async def setup_url_for_device(device):
"""Determine setup.xml url for given device."""
return 'http://{}:{}/setup.xml'.format(device.host, device.port)

async def setup_url_for_address(host, port):
"""Determine setup.xml url for given host and port pair."""
if not port:
port = pywemo.ouimeaux_device.probe_wemo(host)
Comment thread
MartinHjelmare marked this conversation as resolved.

if not port:
return None

return 'http://{}:{}/setup.xml'.format(host, port)

def discovery_dispatch(service, discovery_info):
"""Dispatcher for WeMo discovery events."""
async def discovery_dispatch(service, discovery_info):
"""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,69 +124,67 @@ 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')

discovery.load_platform(hass, component, DOMAIN, discovery_info,
config)
await discovery.async_load_platform(hass, component, DOMAIN,
discovery_info, config)

discovery.listen(hass, SERVICE_WEMO, discovery_dispatch)
discovery.async_listen(hass, SERVICE_WEMO, discovery_dispatch)

def setup_url_for_device(device):
"""Determine setup.xml url for given device."""
return 'http://{}:{}/setup.xml'.format(device.host, device.port)
async def discover_wemo_devices(now):
"""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 = await setup_url_for_address(host, port)

def setup_url_for_address(host, port):
"""Determine setup.xml url for given host and port pair."""
if not port:
port = pywemo.ouimeaux_device.probe_wemo(host)
if not url:
_LOGGER.error(
'Unable to get description url for WeMo at: %s',
'{}:{}'.format(host, port) if port else host)
continue

if not port:
return None
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

return 'http://{}:{}/setup.xml'.format(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():
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((await setup_url_for_device(device), device))
Comment thread
sqldiablo marked this conversation as resolved.
Outdated

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,
}

await discovery.async_discover(hass, SERVICE_WEMO, discovery_info)

@callback
def schedule_first_discovery(event):
"""Schedule the first discovery when Home Assistant starts up."""
async_track_point_in_utc_time(hass, discover_wemo_devices, dt_util.utcnow())
Comment thread
sqldiablo marked this conversation as resolved.
Outdated

for url, device in devices:
_LOGGER.debug('Adding WeMo device at %s:%i', device.host, device.port)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, schedule_first_discovery)
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