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
|
2015-01-06 05:35:22 +00:00
|
|
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
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
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
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-09 08:07:58 +00:00
|
|
|
def device_discovered(hass, config, info):
|
|
|
|
""" Called when a device is discovered. """
|
|
|
|
_, discovery = get_pywemo()
|
|
|
|
|
|
|
|
if discovery is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
device = discovery.device_from_description(info)
|
|
|
|
|
|
|
|
return None if device is None else WemoSwitch(device)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
""" Returns the name of the switch if any. """
|
|
|
|
return self.wemo.name
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
""" Turns the switch on. """
|
|
|
|
self.wemo.on()
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
""" Turns the switch off. """
|
|
|
|
self.wemo.off()
|
|
|
|
|
|
|
|
def is_on(self):
|
|
|
|
""" True if switch is on. """
|
|
|
|
return self.wemo.get_state(True)
|
|
|
|
|
|
|
|
def get_state_attributes(self):
|
|
|
|
""" 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_FRIENDLY_NAME: self.wemo.name,
|
|
|
|
ATTR_CURRENT_POWER_MWH: cur_info['currentpower'],
|
|
|
|
ATTR_TODAY_MWH: cur_info['todaymw']
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {ATTR_FRIENDLY_NAME: self.wemo.name}
|