core/homeassistant/components/switch/wemo.py

205 lines
6.4 KiB
Python
Raw Normal View History

2015-05-13 17:18:30 +00:00
"""
homeassistant.components.switch.wemo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
2015-12-28 00:04:30 +00:00
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON, STATE_STANDBY)
2016-02-18 19:32:30 +00:00
REQUIREMENTS = ['pywemo==0.3.12']
_LOGGER = logging.getLogger(__name__)
2015-08-09 04:22:34 +00:00
2015-12-23 15:46:18 +00:00
_WEMO_SUBSCRIPTION_REGISTRY = None
ATTR_SENSOR_STATE = "sensor_state"
ATTR_SWITCH_MODE = "switch_mode"
MAKER_SWITCH_MOMENTARY = "momentary"
MAKER_SWITCH_TOGGLE = "toggle"
2015-12-23 15:57:51 +00:00
2016-01-20 11:25:20 +00:00
def _find_manual_wemos(pywemo, static_config):
for address in static_config:
port = pywemo.ouimeaux_device.probe_wemo(address)
if not port:
_LOGGER.warning('Unable to probe wemo at %s', address)
continue
_LOGGER.info('Adding static wemo at %s:%i', address, port)
url = 'http://%s:%i/setup.xml' % (address, port)
yield pywemo.discovery.device_from_description(url, None)
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):
2015-05-13 17:18:30 +00:00
""" Find and return WeMo switches. """
import pywemo
import pywemo.discovery as discovery
2015-03-01 09:35:58 +00:00
2015-12-23 15:46:18 +00:00
global _WEMO_SUBSCRIPTION_REGISTRY
2015-12-23 15:57:51 +00:00
if _WEMO_SUBSCRIPTION_REGISTRY is None:
2015-12-23 15:46:18 +00:00
_WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry()
_WEMO_SUBSCRIPTION_REGISTRY.start()
2015-12-27 23:31:48 +00:00
def stop_wemo(event):
""" Shutdown Wemo subscriptions and subscription thread on exit"""
_LOGGER.info("Shutting down subscriptions.")
_WEMO_SUBSCRIPTION_REGISTRY.stop()
2015-12-27 23:56:18 +00:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo)
2015-12-23 15:46:18 +00:00
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 and isinstance(device, pywemo.Switch):
2015-03-01 18:43:08 +00:00
add_devices_callback([WemoSwitch(device)])
2015-03-01 09:35:58 +00:00
return
_LOGGER.info("Scanning for WeMo devices.")
2015-03-01 09:35:58 +00:00
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)])
# Add manually-defined wemo devices
if discovery_info is None and 'static' in config:
add_devices_callback(
[WemoSwitch(wemo)
for wemo in _find_manual_wemos(pywemo, config['static'])])
2015-06-13 21:56:20 +00:00
class WemoSwitch(SwitchDevice):
""" Represents a WeMo switch. """
def __init__(self, wemo):
self.wemo = wemo
2015-06-13 21:56:20 +00:00
self.insight_params = None
self.maker_params = None
2015-12-23 15:46:18 +00:00
_WEMO_SUBSCRIPTION_REGISTRY.register(wemo)
2015-12-23 15:57:51 +00:00
_WEMO_SUBSCRIPTION_REGISTRY.on(
wemo, None, self._update_callback)
2015-12-23 15:46:18 +00:00
def _update_callback(self, _device, _params):
2015-12-23 15:57:51 +00:00
""" Called by the wemo device callback to update state. """
_LOGGER.info(
'Subscription update for %s',
_device)
self.update_ha_state(True)
2015-12-23 15:46:18 +00:00
@property
def should_poll(self):
""" No polling needed with subscriptions """
return False
2015-12-23 15:46:18 +00:00
@property
def unique_id(self):
""" Returns the id of this WeMo switch """
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
@property
def name(self):
""" Returns the name of the switch if any. """
return self.wemo.name
@property
def device_state_attributes(self):
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
return attr
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
@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']
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']
@property
2015-08-31 10:07:52 +00:00
def is_standby(self):
""" 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
@property
def is_on(self):
""" True if switch is on. """
2015-01-16 05:25:24 +00:00
return self.wemo.get_state()
@property
def available(self):
""" True if switch is available. """
if (self.wemo.model_name == 'Insight' and
self.insight_params is None):
return False
if (self.wemo.model_name == 'Maker' and
self.maker_params is None):
return False
return True
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. """
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:
_LOGGER.warning('Could not update status for %s', self.name)