2014-11-12 05:39:17 +00:00
|
|
|
""" Support for WeMo switchces. """
|
|
|
|
import logging
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.helpers import ToggleDevice
|
2014-12-16 03:14:31 +00:00
|
|
|
from homeassistant.components.switch import (
|
|
|
|
ATTR_TODAY_MWH, ATTR_CURRENT_POWER_MWH)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
def get_devices(hass, config):
|
2014-11-12 05:39:17 +00:00
|
|
|
""" Find and return WeMo switches. """
|
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
pywemo, _ = get_pywemo()
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
if pywemo is None:
|
2014-11-12 05:39:17 +00:00
|
|
|
return []
|
|
|
|
|
2015-01-06 05:35:22 +00:00
|
|
|
logging.getLogger(__name__).info("Scanning for WeMo devices")
|
|
|
|
switches = pywemo.discover_devices()
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
# Filter out the switches and wrap in WemoSwitch object
|
|
|
|
return [WemoSwitch(switch) for switch in switches
|
|
|
|
if isinstance(switch, pywemo.Switch)]
|
|
|
|
|
|
|
|
|
2015-01-11 07:47:23 +00:00
|
|
|
def devices_discovered(hass, config, info):
|
2015-01-09 08:07:58 +00:00
|
|
|
""" Called when a device is discovered. """
|
|
|
|
_, discovery = get_pywemo()
|
|
|
|
|
|
|
|
if discovery is None:
|
2015-01-11 07:47:23 +00:00
|
|
|
return []
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
device = discovery.device_from_description(info)
|
|
|
|
|
2015-01-11 07:47:23 +00:00
|
|
|
return [] if device is None else [WemoSwitch(device)]
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_pywemo():
|
|
|
|
""" Tries to import PyWemo. """
|
|
|
|
try:
|
|
|
|
# pylint: disable=no-name-in-module, import-error
|
|
|
|
import homeassistant.external.pywemo.pywemo as pywemo
|
|
|
|
import homeassistant.external.pywemo.pywemo.discovery as discovery
|
|
|
|
|
|
|
|
return pywemo, discovery
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
logging.getLogger(__name__).exception((
|
|
|
|
"Failed to import pywemo. "
|
|
|
|
"Did you maybe not run `git submodule init` "
|
|
|
|
"and `git submodule update`?"))
|
|
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
class WemoSwitch(ToggleDevice):
|
|
|
|
""" represents a WeMo switch within home assistant. """
|
|
|
|
def __init__(self, wemo):
|
|
|
|
self.wemo = wemo
|
|
|
|
|
2015-01-10 18:34:56 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
""" Returns the id of this WeMo switch """
|
|
|
|
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2014-11-12 05:39:17 +00:00
|
|
|
""" Returns the name of the switch if any. """
|
|
|
|
return self.wemo.name
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2014-11-12 05:39:17 +00:00
|
|
|
""" Returns optional state attributes. """
|
2014-12-16 03:14:31 +00:00
|
|
|
if self.wemo.model.startswith('Belkin Insight'):
|
|
|
|
cur_info = self.wemo.insight_params
|
|
|
|
|
|
|
|
return {
|
|
|
|
ATTR_CURRENT_POWER_MWH: cur_info['currentpower'],
|
|
|
|
ATTR_TODAY_MWH: cur_info['todaymw']
|
|
|
|
}
|
2015-01-11 17:20:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if switch is on. """
|
2015-01-16 05:25:24 +00:00
|
|
|
return self.wemo.get_state()
|
2015-01-11 17:20:41 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
""" Turns the switch on. """
|
|
|
|
self.wemo.on()
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
""" Turns the switch off. """
|
|
|
|
self.wemo.off()
|
2015-01-16 05:25:24 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" Update Wemo state. """
|
|
|
|
self.wemo.get_state(True)
|