2015-05-13 19:18:30 +02:00
|
|
|
"""
|
|
|
|
Support for WeMo switches.
|
2015-10-23 22:53:58 +02:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/switch.wemo/
|
2015-05-13 19:18:30 +02:00
|
|
|
"""
|
2014-11-11 21:39:17 -08:00
|
|
|
import logging
|
2017-02-15 15:34:42 -05:00
|
|
|
from datetime import datetime, timedelta
|
2014-11-11 21:39:17 -08:00
|
|
|
|
2015-06-13 14:56:20 -07: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 01:57:32 -05:00
|
|
|
from homeassistant.loader import get_component
|
2014-11-11 21:39:17 -08:00
|
|
|
|
2016-02-19 01:57:32 -05:00
|
|
|
DEPENDENCIES = ['wemo']
|
2015-08-08 21:22:34 -07:00
|
|
|
|
2016-02-19 01:57:32 -05:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-11-11 21:39:17 -08: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'
|
2017-01-25 01:10:10 -05:00
|
|
|
ATTR_COFFEMAKER_MODE = "coffeemaker_mode"
|
2015-12-23 15:57:51 +00:00
|
|
|
|
2017-02-14 18:29:23 -05:00
|
|
|
# Wemo Insight
|
|
|
|
ATTR_POWER_CURRENT_W = 'power_current_w'
|
|
|
|
# ATTR_POWER_AVG_W = 'power_average_w'
|
|
|
|
ATTR_POWER_TODAY_MW_MIN = 'power_today_mW_min'
|
|
|
|
ATTR_POWER_TOTAL_MW_MIN = 'power_total_mW_min'
|
|
|
|
ATTR_ON_FOR_TIME = 'on_time_most_recent'
|
|
|
|
ATTR_ON_TODAY_TIME = 'on_time_today'
|
|
|
|
ATTR_ON_TOTAL_TIME = 'on_time_total'
|
|
|
|
ATTR_POWER_THRESHOLD = 'power_threshold_w'
|
|
|
|
|
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-28 00:18:03 +01:00
|
|
|
# pylint: disable=unused-argument, too-many-function-args
|
2015-03-01 01:35:58 -08:00
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Setup discovered WeMo switches."""
|
2015-07-20 00:08:00 -07:00
|
|
|
import pywemo.discovery as discovery
|
2015-03-01 01:35:58 -08: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 01:35:58 -08:00
|
|
|
|
2016-02-19 01:57:32 -05:00
|
|
|
if device:
|
2015-03-01 10:43:08 -08:00
|
|
|
add_devices_callback([WemoSwitch(device)])
|
2015-03-01 01:35:58 -08:00
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
class WemoSwitch(SwitchDevice):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a WeMo switch."""
|
|
|
|
|
2016-02-19 01:57:32 -05:00
|
|
|
def __init__(self, device):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the WeMo switch."""
|
2016-02-19 01:57:32 -05:00
|
|
|
self.wemo = device
|
2015-06-13 14:56:20 -07:00
|
|
|
self.insight_params = None
|
2015-08-28 23:11:55 +01:00
|
|
|
self.maker_params = None
|
2017-01-25 01:10:10 -05:00
|
|
|
self.coffeemaker_mode = None
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = None
|
2017-01-25 01:10:10 -05:00
|
|
|
# look up model name once as it incurs network traffic
|
|
|
|
self._model_name = self.wemo.model_name
|
2014-11-11 21:39:17 -08:00
|
|
|
|
2016-02-19 01:57:32 -05: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 13:35:39 +01: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)
|
2017-01-25 01:10:10 -05:00
|
|
|
if self._model_name == 'CoffeeMaker':
|
|
|
|
self.wemo.subscription_callback(_params)
|
|
|
|
self._update(force_update=False)
|
|
|
|
else:
|
|
|
|
self.update()
|
2016-03-14 10:29:12 +00:00
|
|
|
if not hasattr(self, 'hass'):
|
|
|
|
return
|
2016-11-17 07:34:46 -08:00
|
|
|
self.schedule_update_ha_state()
|
2015-12-23 15:46:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-08 13:35:39 +01: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 10:34:56 -08:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the ID of this WeMo switch."""
|
2015-01-10 10:34:56 -08:00
|
|
|
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the name of the switch if any."""
|
2014-11-11 21:39:17 -08:00
|
|
|
return self.wemo.name
|
|
|
|
|
2016-01-20 11:06:08 +00:00
|
|
|
@property
|
2016-02-06 22:28:29 -08:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the state attributes of the device."""
|
2016-02-06 22:28:29 -08: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
|
|
|
|
2017-01-25 01:10:10 -05:00
|
|
|
if self.insight_params or (self.coffeemaker_mode is not None):
|
2016-02-25 22:46:14 +00:00
|
|
|
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
|
2017-02-14 18:29:23 -05:00
|
|
|
attr[ATTR_POWER_CURRENT_W] = self.power_current_watt
|
|
|
|
# attr[ATTR_POWER_AVG_W] = self.power_average_watt
|
|
|
|
attr[ATTR_POWER_TODAY_MW_MIN] = self.power_today_mw_min
|
|
|
|
attr[ATTR_POWER_TOTAL_MW_MIN] = self.power_total_mw_min
|
|
|
|
attr[ATTR_ON_FOR_TIME] = self.on_for
|
|
|
|
attr[ATTR_ON_TODAY_TIME] = self.on_today
|
|
|
|
attr[ATTR_ON_TOTAL_TIME] = self.on_total
|
|
|
|
attr[ATTR_POWER_THRESHOLD] = self.power_threshold
|
2016-01-20 11:06:08 +00:00
|
|
|
|
2017-01-25 01:10:10 -05:00
|
|
|
if self.coffeemaker_mode is not None:
|
|
|
|
attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode
|
|
|
|
|
2016-02-25 22:46:14 +00:00
|
|
|
return attr
|
2015-08-31 11:07:52 +01:00
|
|
|
|
2017-02-14 18:29:23 -05:00
|
|
|
# @property
|
|
|
|
def _current_power_mw(self):
|
|
|
|
"""Current power usage in mW."""
|
|
|
|
if self.insight_params:
|
2017-02-15 15:34:42 -05:00
|
|
|
return self.insight_params['currentpower']
|
2017-02-14 18:29:23 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def power_current_watt(self):
|
|
|
|
"""Current power usage in W."""
|
|
|
|
if self.insight_params:
|
|
|
|
try:
|
2017-02-15 15:34:42 -05:00
|
|
|
return self._current_power_mw() / 1000
|
2017-02-14 18:29:23 -05:00
|
|
|
except:
|
2017-02-15 15:34:42 -05:00
|
|
|
return None
|
2017-02-14 18:29:23 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def power_threshold(self):
|
|
|
|
if self.insight_params:
|
2017-02-15 15:34:42 -05:00
|
|
|
return self.insight_params['powerthreshold'] / 1000
|
2017-02-14 18:29:23 -05:00
|
|
|
|
|
|
|
def _as_uptime(self, _seconds):
|
|
|
|
d = datetime(1,1,1) + timedelta(seconds=_seconds)
|
|
|
|
return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(d.day-1,
|
2017-02-15 15:38:41 -05:00
|
|
|
d.hour,
|
2017-02-15 15:40:02 -05:00
|
|
|
d.minute,
|
2017-02-15 15:38:41 -05:00
|
|
|
d.second)
|
|
|
|
|
2017-02-14 18:29:23 -05:00
|
|
|
@property
|
|
|
|
def on_for(self):
|
|
|
|
"""On time in seconds."""
|
|
|
|
if self.insight_params:
|
|
|
|
return self._as_uptime(self.insight_params['onfor'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def on_today(self):
|
|
|
|
"""On time in seconds."""
|
|
|
|
if self.insight_params:
|
|
|
|
return self._as_uptime(self.insight_params['ontoday'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def on_total(self):
|
|
|
|
"""On time in seconds."""
|
|
|
|
if self.insight_params:
|
|
|
|
return self._as_uptime(self.insight_params['ontotal'])
|
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
@property
|
2017-02-14 18:29:23 -05:00
|
|
|
def power_total_mw_min(self):
|
|
|
|
"""This is a total of average mW per minute."""
|
2015-06-13 14:56:20 -07:00
|
|
|
if self.insight_params:
|
2017-02-14 18:29:23 -05:00
|
|
|
try:
|
2017-02-15 15:34:42 -05:00
|
|
|
return self.insight_params['totalmw']
|
2017-02-14 18:29:23 -05:00
|
|
|
except:
|
2017-02-15 15:34:42 -05:00
|
|
|
return None
|
2014-12-15 19:14:31 -08:00
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
@property
|
2017-02-14 18:29:23 -05:00
|
|
|
def power_today_mw_min(self):
|
|
|
|
"""This is the total consumption today in mW per minute."""
|
2015-06-13 14:56:20 -07:00
|
|
|
if self.insight_params:
|
2017-02-14 18:29:23 -05:00
|
|
|
try:
|
2017-02-15 15:34:42 -05:00
|
|
|
return self.insight_params['todaymw']
|
2017-02-14 18:29:23 -05:00
|
|
|
except:
|
2017-02-15 15:34:42 -05:00
|
|
|
return None
|
2015-01-11 09:20:41 -08:00
|
|
|
|
2015-08-28 23:11:55 +01:00
|
|
|
@property
|
2016-02-25 22:46:14 +00:00
|
|
|
def detail_state(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the state of the device."""
|
2017-01-25 01:10:10 -05:00
|
|
|
if self.coffeemaker_mode is not None:
|
|
|
|
return self.wemo.mode_string
|
2015-08-28 23:11:55 +01: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 12:13:53 +01:00
|
|
|
else:
|
2016-02-25 22:46:14 +00:00
|
|
|
return STATE_UNKNOWN
|
2015-08-28 23:11:55 +01:00
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if switch is on. Standby is on."""
|
2016-02-25 22:46:14 +00:00
|
|
|
return self._state
|
2015-01-11 09:20:41 -08:00
|
|
|
|
2016-02-06 09:18:12 +01:00
|
|
|
@property
|
|
|
|
def available(self):
|
2016-02-19 01:57:32 -05:00
|
|
|
"""True if switch is available."""
|
2017-01-25 01:10:10 -05:00
|
|
|
if self._model_name == 'Insight' and self.insight_params is None:
|
2016-02-06 09:18:12 +01:00
|
|
|
return False
|
2017-01-25 01:10:10 -05:00
|
|
|
if self._model_name == 'Maker' and self.maker_params is None:
|
|
|
|
return False
|
|
|
|
if self._model_name == 'CoffeeMaker' and self.coffeemaker_mode is None:
|
2016-02-06 09:18:12 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2017-01-25 01:10:10 -05:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon of device based on its type."""
|
|
|
|
if self._model_name == 'CoffeeMaker':
|
|
|
|
return 'mdi:coffee'
|
|
|
|
else:
|
|
|
|
return super().icon
|
|
|
|
|
2015-01-11 09:20:41 -08:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the switch on."""
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = WEMO_ON
|
2015-01-11 09:20:41 -08:00
|
|
|
self.wemo.on()
|
2016-11-16 08:26:29 -08:00
|
|
|
self.schedule_update_ha_state()
|
2015-01-11 09:20:41 -08:00
|
|
|
|
|
|
|
def turn_off(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the switch off."""
|
2016-02-25 22:46:14 +00:00
|
|
|
self._state = WEMO_OFF
|
2015-01-11 09:20:41 -08:00
|
|
|
self.wemo.off()
|
2016-11-16 08:26:29 -08:00
|
|
|
self.schedule_update_ha_state()
|
2015-01-15 21:25:24 -08:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-19 01:57:32 -05:00
|
|
|
"""Update WeMo state."""
|
2017-01-25 01:10:10 -05:00
|
|
|
self._update(force_update=True)
|
|
|
|
|
|
|
|
def _update(self, force_update=True):
|
2015-09-25 11:49:57 +01:00
|
|
|
try:
|
2017-01-25 01:10:10 -05:00
|
|
|
self._state = self.wemo.get_state(force_update)
|
|
|
|
if self._model_name == 'Insight':
|
2015-09-25 11:49:57 +01:00
|
|
|
self.insight_params = self.wemo.insight_params
|
|
|
|
self.insight_params['standby_state'] = (
|
|
|
|
self.wemo.get_standby_state)
|
2017-01-25 01:10:10 -05:00
|
|
|
elif self._model_name == 'Maker':
|
2015-09-25 11:49:57 +01:00
|
|
|
self.maker_params = self.wemo.maker_params
|
2017-01-25 01:10:10 -05:00
|
|
|
elif self._model_name == 'CoffeeMaker':
|
|
|
|
self.coffeemaker_mode = self.wemo.mode
|
2015-09-25 11:49:57 +01:00
|
|
|
except AttributeError:
|
2015-10-23 22:53:58 +02:00
|
|
|
_LOGGER.warning('Could not update status for %s', self.name)
|