2015-05-13 17:18:30 +00:00
|
|
|
"""
|
|
|
|
Support for WeMo switches.
|
2015-10-23 20:53:58 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/switch.wemo/
|
2015-05-13 17:18:30 +00:00
|
|
|
"""
|
2014-11-12 05:39:17 +00:00
|
|
|
import logging
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2016-02-25 22:46:14 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN)
|
2016-02-19 06:57:32 +00:00
|
|
|
from homeassistant.loader import get_component
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
DEPENDENCIES = ['wemo']
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-01-20 11:06:08 +00:00
|
|
|
ATTR_SENSOR_STATE = "sensor_state"
|
|
|
|
ATTR_SWITCH_MODE = "switch_mode"
|
2016-02-25 22:46:14 +00:00
|
|
|
ATTR_CURRENT_STATE_DETAIL = 'state_detail'
|
2016-01-20 11:06:08 +00:00
|
|
|
|
|
|
|
MAKER_SWITCH_MOMENTARY = "momentary"
|
|
|
|
MAKER_SWITCH_TOGGLE = "toggle"
|
2015-12-23 15:57:51 +00:00
|
|
|
|
2016-02-25 22:46:14 +00:00
|
|
|
MAKER_SWITCH_MOMENTARY = "momentary"
|
|
|
|
MAKER_SWITCH_TOGGLE = "toggle"
|
|
|
|
|
|
|
|
WEMO_ON = 1
|
|
|
|
WEMO_OFF = 0
|
|
|
|
WEMO_STANDBY = 8
|
|
|
|
|
2016-01-20 11:25:20 +00:00
|
|
|
|
2015-10-27 23:18:03 +00:00
|
|
|
# pylint: disable=unused-argument, too-many-function-args
|
2015-03-01 09:35:58 +00:00
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Setup discovered WeMo switches."""
|
2015-07-20 07:08:00 +00:00
|
|
|
import pywemo.discovery as discovery
|
2015-03-01 09:35:58 +00:00
|
|
|
|
|
|
|
if discovery_info is not None:
|
2015-10-26 09:03:57 +00:00
|
|
|
location = discovery_info[2]
|
|
|
|
mac = discovery_info[3]
|
|
|
|
device = discovery.device_from_description(location, mac)
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2016-02-19 06:57:32 +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
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
class WemoSwitch(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a WeMo switch."""
|
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
def __init__(self, device):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the WeMo switch."""
|
2016-02-19 06:57:32 +00:00
|
|
|
self.wemo = device
|
2015-06-13 21:56:20 +00:00
|
|
|
self.insight_params = None
|
2015-08-28 22:11:55 +00:00
|
|
|
self.maker_params = None
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = None
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-02-19 06:57:32 +00:00
|
|
|
wemo = get_component('wemo')
|
|
|
|
wemo.SUBSCRIPTION_REGISTRY.register(self.wemo)
|
|
|
|
wemo.SUBSCRIPTION_REGISTRY.on(self.wemo, None, self._update_callback)
|
2015-12-23 15:46:18 +00:00
|
|
|
|
|
|
|
def _update_callback(self, _device, _params):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Called by the Wemo device callback to update state."""
|
2015-12-23 15:57:51 +00:00
|
|
|
_LOGGER.info(
|
2016-01-08 16:00:26 +00:00
|
|
|
'Subscription update for %s',
|
|
|
|
_device)
|
2016-11-17 15:34:46 +00:00
|
|
|
self.update()
|
2016-03-14 10:29:12 +00:00
|
|
|
if not hasattr(self, 'hass'):
|
|
|
|
return
|
2016-11-17 15:34:46 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-12-23 15:46:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""No polling needed with subscriptions."""
|
2016-01-08 16:00:26 +00:00
|
|
|
return False
|
2015-12-23 15:46:18 +00:00
|
|
|
|
2015-01-10 18:34:56 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the ID of this WeMo switch."""
|
2015-01-10 18:34:56 +00:00
|
|
|
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch if any."""
|
2014-11-12 05:39:17 +00:00
|
|
|
return self.wemo.name
|
|
|
|
|
2016-01-20 11:06:08 +00:00
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2016-02-07 06:28:29 +00:00
|
|
|
attr = {}
|
2016-01-20 11:06:08 +00:00
|
|
|
if self.maker_params:
|
|
|
|
# Is the maker sensor on or off.
|
|
|
|
if self.maker_params['hassensor']:
|
|
|
|
# Note a state of 1 matches the WeMo app 'not triggered'!
|
2016-01-20 19:13:29 +00:00
|
|
|
if self.maker_params['sensorstate']:
|
2016-01-20 11:06:08 +00:00
|
|
|
attr[ATTR_SENSOR_STATE] = STATE_OFF
|
|
|
|
else:
|
|
|
|
attr[ATTR_SENSOR_STATE] = STATE_ON
|
|
|
|
|
|
|
|
# Is the maker switch configured as toggle(0) or momentary (1).
|
|
|
|
if self.maker_params['switchmode']:
|
2016-01-20 19:13:29 +00:00
|
|
|
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_MOMENTARY
|
|
|
|
else:
|
|
|
|
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE
|
2016-01-20 11:06:08 +00:00
|
|
|
|
2016-02-25 22:46:14 +00:00
|
|
|
if self.insight_params:
|
|
|
|
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
|
2016-01-20 11:06:08 +00:00
|
|
|
|
2016-02-25 22:46:14 +00:00
|
|
|
return attr
|
2015-08-31 10:07:52 +00:00
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
2015-06-13 21:56:20 +00:00
|
|
|
def current_power_mwh(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Current power usage in mWh."""
|
2015-06-13 21:56:20 +00:00
|
|
|
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):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Today total power usage in mW."""
|
2015-06-13 21:56:20 +00:00
|
|
|
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
|
2016-02-25 22:46:14 +00:00
|
|
|
def detail_state(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state of the device."""
|
2015-08-28 22:11:55 +00:00
|
|
|
if self.insight_params:
|
2016-02-26 00:16:33 +00:00
|
|
|
standby_state = int(self.insight_params['state'])
|
2016-02-25 22:46:14 +00:00
|
|
|
if standby_state == WEMO_ON:
|
2016-02-26 08:28:17 +00:00
|
|
|
return STATE_ON
|
2016-02-25 22:46:14 +00:00
|
|
|
elif standby_state == WEMO_OFF:
|
|
|
|
return STATE_OFF
|
|
|
|
elif standby_state == WEMO_STANDBY:
|
|
|
|
return STATE_STANDBY
|
2015-08-31 11:13:53 +00:00
|
|
|
else:
|
2016-02-25 22:46:14 +00:00
|
|
|
return STATE_UNKNOWN
|
2015-08-28 22:11:55 +00:00
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on. Standby is on."""
|
2016-02-25 22:46:14 +00:00
|
|
|
return self._state
|
2015-01-11 17:20:41 +00:00
|
|
|
|
2016-02-06 08:18:12 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
2016-02-19 06:57:32 +00:00
|
|
|
"""True if switch is available."""
|
2016-03-08 17:05:01 +00:00
|
|
|
if self.wemo.model_name == 'Insight' and self.insight_params is None:
|
2016-02-06 08:18:12 +00:00
|
|
|
return False
|
2016-03-08 17:05:01 +00:00
|
|
|
if self.wemo.model_name == 'Maker' and self.maker_params is None:
|
2016-02-06 08:18:12 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = WEMO_ON
|
2015-01-11 17:20:41 +00:00
|
|
|
self.wemo.on()
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-01-11 17:20:41 +00:00
|
|
|
|
|
|
|
def turn_off(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = WEMO_OFF
|
2015-01-11 17:20:41 +00:00
|
|
|
self.wemo.off()
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-01-16 05:25:24 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-19 06:57:32 +00:00
|
|
|
"""Update WeMo state."""
|
2015-09-25 10:49:57 +00:00
|
|
|
try:
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = self.wemo.get_state(True)
|
2015-09-25 10:49:57 +00:00
|
|
|
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:
|
2015-10-23 20:53:58 +00:00
|
|
|
_LOGGER.warning('Could not update status for %s', self.name)
|