2015-05-13 19:18:30 +02:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch.wemo
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Support for WeMo switches.
|
|
|
|
"""
|
2014-11-11 21:39:17 -08:00
|
|
|
import logging
|
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2014-11-11 21:39:17 -08:00
|
|
|
|
|
|
|
|
2015-03-01 01:35:58 -08:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2015-05-13 19:18:30 +02:00
|
|
|
""" Find and return WeMo switches. """
|
2015-07-20 00:08:00 -07:00
|
|
|
import pywemo
|
|
|
|
import pywemo.discovery as discovery
|
2015-03-01 01:35:58 -08:00
|
|
|
|
|
|
|
if discovery_info is not None:
|
|
|
|
device = discovery.device_from_description(discovery_info)
|
|
|
|
|
|
|
|
if device:
|
2015-03-01 10:43:08 -08:00
|
|
|
add_devices_callback([WemoSwitch(device)])
|
2015-03-01 01:35:58 -08:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
logging.getLogger(__name__).info("Scanning for WeMo devices")
|
|
|
|
switches = pywemo.discover_devices()
|
|
|
|
|
|
|
|
# Filter out the switches and wrap in WemoSwitch object
|
|
|
|
add_devices_callback(
|
|
|
|
[WemoSwitch(switch) for switch in switches
|
|
|
|
if isinstance(switch, pywemo.Switch)])
|
2015-01-09 00:07:58 -08:00
|
|
|
|
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
class WemoSwitch(SwitchDevice):
|
2015-05-13 19:18:30 +02:00
|
|
|
""" Represents a WeMo switch within Home Assistant. """
|
2014-11-11 21:39:17 -08:00
|
|
|
def __init__(self, wemo):
|
|
|
|
self.wemo = wemo
|
2015-06-13 14:56:20 -07:00
|
|
|
self.insight_params = None
|
2014-11-11 21:39:17 -08:00
|
|
|
|
2015-01-10 10:34:56 -08:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
""" Returns the id of this WeMo switch """
|
|
|
|
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
@property
|
|
|
|
def name(self):
|
2014-11-11 21:39:17 -08:00
|
|
|
""" Returns the name of the switch if any. """
|
|
|
|
return self.wemo.name
|
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
@property
|
2015-06-13 14:56:20 -07:00
|
|
|
def current_power_mwh(self):
|
|
|
|
""" Current power usage in mwh. """
|
|
|
|
if self.insight_params:
|
|
|
|
return self.insight_params['currentpower']
|
2014-12-15 19:14:31 -08:00
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
@property
|
|
|
|
def today_power_mw(self):
|
|
|
|
""" Today total power usage in mw. """
|
|
|
|
if self.insight_params:
|
|
|
|
return self.insight_params['todaymw']
|
2015-01-11 09:20:41 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if switch is on. """
|
2015-01-15 21:25:24 -08:00
|
|
|
return self.wemo.get_state()
|
2015-01-11 09:20:41 -08: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-15 21:25:24 -08:00
|
|
|
|
|
|
|
def update(self):
|
2015-05-13 19:18:30 +02:00
|
|
|
""" Update WeMo state. """
|
2015-01-15 21:25:24 -08:00
|
|
|
self.wemo.get_state(True)
|
2015-06-13 14:56:20 -07:00
|
|
|
if self.wemo.model.startswith('Belkin Insight'):
|
|
|
|
self.insight_params = self.wemo.insight_params
|