core/homeassistant/components/switch/wemo.py

174 lines
5.2 KiB
Python
Raw Normal View History

2015-05-13 17:18:30 +00:00
"""
Support for WeMo switches.
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
"""
import logging
2015-06-13 21:56:20 +00:00
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN)
from homeassistant.loader import get_component
DEPENDENCIES = ['wemo']
2015-08-09 04:22:34 +00:00
_LOGGER = logging.getLogger(__name__)
ATTR_SENSOR_STATE = "sensor_state"
ATTR_SWITCH_MODE = "switch_mode"
ATTR_CURRENT_STATE_DETAIL = 'state_detail'
MAKER_SWITCH_MOMENTARY = "momentary"
MAKER_SWITCH_TOGGLE = "toggle"
2015-12-23 15:57:51 +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."""
import pywemo.discovery as discovery
2015-03-01 09:35:58 +00:00
if discovery_info is not None:
location = discovery_info[2]
mac = discovery_info[3]
device = discovery.device_from_description(location, mac)
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
2015-06-13 21:56:20 +00:00
class WemoSwitch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a WeMo switch."""
def __init__(self, device):
2016-03-08 12:35:39 +00:00
"""Initialize the WeMo switch."""
self.wemo = device
2015-06-13 21:56:20 +00:00
self.insight_params = None
self.maker_params = None
self._state = None
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(
'Subscription update for %s',
_device)
self.update()
2016-03-14 10:29:12 +00:00
if not hasattr(self, 'hass'):
return
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."""
return False
2015-12-23 15:46:18 +00:00
@property
def unique_id(self):
2016-03-08 12:35:39 +00:00
"""Return the ID of this WeMo switch."""
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch if any."""
return self.wemo.name
@property
def device_state_attributes(self):
2016-03-08 12:35:39 +00:00
"""Return the state attributes of the device."""
attr = {}
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']:
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
if self.insight_params:
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
return attr
2015-08-31 10:07:52 +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']
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']
@property
def detail_state(self):
2016-03-08 12:35:39 +00:00
"""Return the state of the device."""
if self.insight_params:
standby_state = int(self.insight_params['state'])
if standby_state == WEMO_ON:
2016-02-26 08:28:17 +00:00
return STATE_ON
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:
return STATE_UNKNOWN
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if switch is on. Standby is on."""
return self._state
@property
def available(self):
"""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:
return False
2016-03-08 17:05:01 +00:00
if self.wemo.model_name == 'Maker' and self.maker_params is None:
return False
return True
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch on."""
self._state = WEMO_ON
self.wemo.on()
self.schedule_update_ha_state()
def turn_off(self):
2016-03-08 12:35:39 +00:00
"""Turn the switch off."""
self._state = WEMO_OFF
self.wemo.off()
self.schedule_update_ha_state()
2015-01-16 05:25:24 +00:00
def update(self):
"""Update WeMo state."""
try:
self._state = 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:
_LOGGER.warning('Could not update status for %s', self.name)