2015-05-13 17:18:30 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch.wemo
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Support for WeMo switches.
|
|
|
|
"""
|
2014-11-12 05:39:17 +00:00
|
|
|
import logging
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2015-08-31 12:12:17 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-09-30 07:12:00 +00:00
|
|
|
REQUIREMENTS = ['pywemo==0.3.1']
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-03-01 09:35:58 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2015-05-13 17:18:30 +00:00
|
|
|
""" Find and return WeMo switches. """
|
2015-07-20 07:08:00 +00:00
|
|
|
import pywemo
|
|
|
|
import pywemo.discovery as discovery
|
2015-03-01 09:35:58 +00:00
|
|
|
|
|
|
|
if discovery_info is not None:
|
2015-08-24 00:18:52 +00:00
|
|
|
device = discovery.device_from_description(discovery_info[2])
|
2015-03-01 09:35:58 +00:00
|
|
|
|
|
|
|
if device:
|
2015-03-01 18:43:08 +00:00
|
|
|
add_devices_callback([WemoSwitch(device)])
|
2015-03-01 09:35:58 +00: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 08:07:58 +00:00
|
|
|
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
class WemoSwitch(SwitchDevice):
|
2015-09-07 17:39:16 +00:00
|
|
|
""" Represents a WeMo switch. """
|
2014-11-12 05:39:17 +00:00
|
|
|
def __init__(self, wemo):
|
|
|
|
self.wemo = wemo
|
2015-06-13 21:56:20 +00:00
|
|
|
self.insight_params = None
|
2015-08-28 22:11:55 +00:00
|
|
|
self.maker_params = None
|
2014-11-12 05:39:17 +00:00
|
|
|
|
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-08-31 10:07:52 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state. """
|
|
|
|
is_on = self.is_on
|
|
|
|
if not is_on:
|
|
|
|
return STATE_OFF
|
|
|
|
elif self.is_standby:
|
|
|
|
return STATE_STANDBY
|
|
|
|
return STATE_ON
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
2015-06-13 21:56:20 +00:00
|
|
|
def current_power_mwh(self):
|
|
|
|
""" Current power usage in mwh. """
|
|
|
|
if self.insight_params:
|
|
|
|
return self.insight_params['currentpower']
|
2014-12-16 03:14:31 +00:00
|
|
|
|
2015-06-13 21:56:20 +00: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 17:20:41 +00:00
|
|
|
|
2015-08-28 22:11:55 +00:00
|
|
|
@property
|
2015-08-31 10:07:52 +00:00
|
|
|
def is_standby(self):
|
2015-08-28 22:11:55 +00:00
|
|
|
""" Is the device on - or in standby. """
|
|
|
|
if self.insight_params:
|
2015-08-31 12:12:17 +00:00
|
|
|
standby_state = self.insight_params['state']
|
2015-08-31 14:19:04 +00:00
|
|
|
# Standby is actually '8' but seems more defensive
|
|
|
|
# to check for the On and Off states
|
2015-08-31 11:13:53 +00:00
|
|
|
if standby_state == '1' or standby_state == '0':
|
2015-08-31 10:07:52 +00:00
|
|
|
return False
|
2015-08-31 11:13:53 +00:00
|
|
|
else:
|
2015-08-31 10:07:52 +00:00
|
|
|
return True
|
2015-08-28 22:11:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def sensor_state(self):
|
|
|
|
""" Is the sensor on or off. """
|
|
|
|
if self.maker_params and self.has_sensor:
|
|
|
|
# Note a state of 1 matches the WeMo app 'not triggered'!
|
|
|
|
if self.maker_params['sensorstate']:
|
2015-08-31 12:12:17 +00:00
|
|
|
return STATE_OFF
|
2015-08-28 22:11:55 +00:00
|
|
|
else:
|
2015-08-31 12:12:17 +00:00
|
|
|
return STATE_ON
|
2015-08-28 22:11:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def switch_mode(self):
|
|
|
|
""" Is the switch configured as toggle(0) or momentary (1). """
|
|
|
|
if self.maker_params:
|
|
|
|
return self.maker_params['switchmode']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_sensor(self):
|
|
|
|
""" Is the sensor present? """
|
|
|
|
if self.maker_params:
|
|
|
|
return self.maker_params['hassensor']
|
|
|
|
|
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):
|
2015-05-13 17:18:30 +00:00
|
|
|
""" Update WeMo state. """
|
2015-09-25 10:49:57 +00:00
|
|
|
try:
|
|
|
|
self.wemo.get_state(True)
|
|
|
|
if self.wemo.model_name == 'Insight':
|
|
|
|
self.insight_params = self.wemo.insight_params
|
|
|
|
self.insight_params['standby_state'] = (
|
|
|
|
self.wemo.get_standby_state)
|
|
|
|
elif self.wemo.model_name == 'Maker':
|
|
|
|
self.maker_params = self.wemo.maker_params
|
|
|
|
except AttributeError:
|
|
|
|
logging.getLogger(__name__).warning(
|
|
|
|
'Could not update status for %s', self.name)
|