Move standby from state to attribute, add optimistic switch status.

This commit is contained in:
pavoni 2016-02-25 22:46:14 +00:00
parent 35aa9aa863
commit 72144945b4

View file

@ -9,7 +9,8 @@ https://home-assistant.io/components/switch.wemo/
import logging import logging
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import SwitchDevice
from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY from homeassistant.const import (
STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN)
from homeassistant.loader import get_component from homeassistant.loader import get_component
DEPENDENCIES = ['wemo'] DEPENDENCIES = ['wemo']
@ -18,10 +19,18 @@ _LOGGER = logging.getLogger(__name__)
ATTR_SENSOR_STATE = "sensor_state" ATTR_SENSOR_STATE = "sensor_state"
ATTR_SWITCH_MODE = "switch_mode" ATTR_SWITCH_MODE = "switch_mode"
ATTR_CURRENT_STATE_DETAIL = 'state_detail'
MAKER_SWITCH_MOMENTARY = "momentary" MAKER_SWITCH_MOMENTARY = "momentary"
MAKER_SWITCH_TOGGLE = "toggle" MAKER_SWITCH_TOGGLE = "toggle"
MAKER_SWITCH_MOMENTARY = "momentary"
MAKER_SWITCH_TOGGLE = "toggle"
WEMO_ON = 1
WEMO_OFF = 0
WEMO_STANDBY = 8
# pylint: disable=unused-argument, too-many-function-args # pylint: disable=unused-argument, too-many-function-args
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
@ -43,6 +52,7 @@ class WemoSwitch(SwitchDevice):
self.wemo = device self.wemo = device
self.insight_params = None self.insight_params = None
self.maker_params = None self.maker_params = None
self._state = None
wemo = get_component('wemo') wemo = get_component('wemo')
wemo.SUBSCRIPTION_REGISTRY.register(self.wemo) wemo.SUBSCRIPTION_REGISTRY.register(self.wemo)
@ -88,17 +98,10 @@ class WemoSwitch(SwitchDevice):
else: else:
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE
return attr if self.insight_params:
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
@property return attr
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 @property
def current_power_mwh(self): def current_power_mwh(self):
@ -113,21 +116,25 @@ class WemoSwitch(SwitchDevice):
return self.insight_params['todaymw'] return self.insight_params['todaymw']
@property @property
def is_standby(self): def detail_state(self):
"""Is the device on - or in standby.""" """Is the device on - or in standby."""
if self.insight_params: if self.insight_params:
standby_state = self.insight_params['state'] standby_state = self.insight_params['state']
# Standby is actually '8' but seems more defensive # Standby is actually '8' but seems more defensive
# to check for the On and Off states # to check for the On and Off states
if standby_state == '1' or standby_state == '0': if standby_state == WEMO_ON:
return False return STATE_OFF
elif standby_state == WEMO_OFF:
return STATE_OFF
elif standby_state == WEMO_STANDBY:
return STATE_STANDBY
else: else:
return True return STATE_UNKNOWN
@property @property
def is_on(self): def is_on(self):
"""True if switch is on.""" """True if switch is on. Standby is on!"""
return self.wemo.get_state() return self._state
@property @property
def available(self): def available(self):
@ -143,16 +150,20 @@ class WemoSwitch(SwitchDevice):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turns the switch on.""" """Turns the switch on."""
self._state = WEMO_ON
self.update_ha_state()
self.wemo.on() self.wemo.on()
def turn_off(self): def turn_off(self):
"""Turns the switch off.""" """Turns the switch off."""
self._state = WEMO_OFF
self.update_ha_state()
self.wemo.off() self.wemo.off()
def update(self): def update(self):
"""Update WeMo state.""" """Update WeMo state."""
try: try:
self.wemo.get_state(True) self._state = self.wemo.get_state(True)
if self.wemo.model_name == 'Insight': if self.wemo.model_name == 'Insight':
self.insight_params = self.wemo.insight_params self.insight_params = self.wemo.insight_params
self.insight_params['standby_state'] = ( self.insight_params['standby_state'] = (