hass-core/homeassistant/components/wemo.py
angel12 799e1f0469 Wemo Dimmer Support (#10882)
* Wemo Dimmer Support

Add support for the Wemo Dimmer Switch

* Add newline at end of file

Re: findings from hound

* Syntax for the hound

Sorry for the excess edits, new to python

* Change order of Models

Fixed order back to ABC order

* Changes as requested

I made the changes I was comfortable with at this point, the dimmer addition now very closely mirrors what is under the switch component, at least as far as the parts necessary for the dimmer.

Any changes past these with regards to the subscription registry / callback info is probably going to be over my head, but I will try to look deeper at them if required.

* Remove unnecessary lines

Removed self.schedule_update_ha_state() from turn off / turn on

* Remove update(self)

Removed update method

* Move subscription to async_added_to_hass

* Move subscription.
* Clean up.

* Wait until the job in the executor is done

* Run gen_requirements_all script

* Only update instance attributes via callback
2018-01-15 23:08:48 +01:00

108 lines
3.1 KiB
Python

"""
Support for WeMo device discovery.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/wemo/
"""
import logging
import voluptuous as vol
from homeassistant.components.discovery import SERVICE_WEMO
from homeassistant.helpers import discovery
from homeassistant.helpers import config_validation as cv
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
REQUIREMENTS = ['pywemo==0.4.25']
DOMAIN = 'wemo'
# Mapping from Wemo model_name to component.
WEMO_MODEL_DISPATCH = {
'Bridge': 'light',
'CoffeeMaker': 'switch',
'Dimmer': 'light',
'Insight': 'switch',
'LightSwitch': 'switch',
'Maker': 'switch',
'Sensor': 'binary_sensor',
'Socket': 'switch'
}
SUBSCRIPTION_REGISTRY = None
KNOWN_DEVICES = []
_LOGGER = logging.getLogger(__name__)
CONF_STATIC = 'static'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_STATIC, default=[]): vol.Schema([cv.string])
}),
}, extra=vol.ALLOW_EXTRA)
# pylint: disable=unused-argument, too-many-function-args
def setup(hass, config):
"""Set up for WeMo devices."""
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
model_name = discovery_info.get('model_name')
serial = discovery_info.get('serial')
# Only register a device once
if serial in KNOWN_DEVICES:
return
_LOGGER.debug('Discovered unique device %s', serial)
KNOWN_DEVICES.append(serial)
component = WEMO_MODEL_DISPATCH.get(model_name, 'switch')
discovery.load_platform(hass, component, DOMAIN, discovery_info,
config)
discovery.listen(hass, SERVICE_WEMO, discovery_dispatch)
_LOGGER.info("Scanning for WeMo devices.")
devices = [(device.host, device) for device in pywemo.discover_devices()]
# 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)
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)
discovery_info = {
'model_name': device.model_name,
'serial': device.serialnumber,
'mac_address': device.mac,
'ssdp_description': url,
}
discovery.discover(hass, SERVICE_WEMO, discovery_info)
return True