From dfae1a44a632ecdf13994208d29beb611d8ada90 Mon Sep 17 00:00:00 2001 From: pavoni Date: Fri, 28 Aug 2015 23:11:55 +0100 Subject: [PATCH 01/10] Add standby state to WeMo Insight Switch, and add WeMo Maker --- homeassistant/components/switch/__init__.py | 8 ++--- homeassistant/components/switch/wemo.py | 34 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index b141c36f7b5..60411b2815e 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -24,6 +24,8 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}' ATTR_TODAY_MWH = "today_mwh" ATTR_CURRENT_POWER_MWH = "current_power_mwh" +ATTR_STANDBY_STATE = "standby_state" +ATTR_SENSOR_STATE = "sensor_state" MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) @@ -37,6 +39,8 @@ DISCOVERY_PLATFORMS = { PROP_TO_ATTR = { 'current_power_mwh': ATTR_CURRENT_POWER_MWH, 'today_power_mw': ATTR_TODAY_MWH, + 'standby_state': ATTR_STANDBY_STATE, + 'sensor_state': ATTR_SENSOR_STATE } _LOGGER = logging.getLogger(__name__) @@ -45,21 +49,18 @@ _LOGGER = logging.getLogger(__name__) def is_on(hass, entity_id=None): """ Returns if the switch is on based on the statemachine. """ entity_id = entity_id or ENTITY_ID_ALL_SWITCHES - return hass.states.is_state(entity_id, STATE_ON) def turn_on(hass, entity_id=None): """ Turns all or specified switch on. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None - hass.services.call(DOMAIN, SERVICE_TURN_ON, data) def turn_off(hass, entity_id=None): """ Turns all or specified switch off. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None - hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) @@ -84,7 +85,6 @@ def setup(hass, config): switch.update_ha_state(True) hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service) - hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service) return True diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 33f5f03799b..f6fb0c266fe 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -37,6 +37,7 @@ class WemoSwitch(SwitchDevice): def __init__(self, wemo): self.wemo = wemo self.insight_params = None + self.maker_params = None @property def unique_id(self): @@ -60,6 +61,34 @@ class WemoSwitch(SwitchDevice): if self.insight_params: return self.insight_params['todaymw'] + @property + def standby_state(self): + """ Is the device on - or in standby. """ + if self.insight_params: + return self.insight_params['standby_state'] + + @property + def sensor_state(self): + """ Is the sensor on or off. """ + if self.maker_params and self.has_sensor: + # Note a state of 1 matches the WeMo app 'not triggered'! + if self.maker_params['sensorstate']: + return 'off' + else: + return 'on' + + @property + def switch_mode(self): + """ Is the switch configured as toggle(0) or momentary (1). """ + if self.maker_params: + return self.maker_params['switchmode'] + + @property + def has_sensor(self): + """ Is the sensor present? """ + if self.maker_params: + return self.maker_params['hassensor'] + @property def is_on(self): """ True if switch is on. """ @@ -76,5 +105,8 @@ class WemoSwitch(SwitchDevice): def update(self): """ Update WeMo state. """ self.wemo.get_state(True) - if self.wemo.model.startswith('Belkin Insight'): + 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 From 2a0d459722a0f3e112ff9f9c384920d0d53da330 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sat, 29 Aug 2015 00:27:28 +0100 Subject: [PATCH 02/10] Fix blank line regression --- homeassistant/components/switch/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 60411b2815e..4f83b8aa24f 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -49,18 +49,21 @@ _LOGGER = logging.getLogger(__name__) def is_on(hass, entity_id=None): """ Returns if the switch is on based on the statemachine. """ entity_id = entity_id or ENTITY_ID_ALL_SWITCHES + return hass.states.is_state(entity_id, STATE_ON) def turn_on(hass, entity_id=None): """ Turns all or specified switch on. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None + hass.services.call(DOMAIN, SERVICE_TURN_ON, data) def turn_off(hass, entity_id=None): """ Turns all or specified switch off. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None + hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) @@ -85,6 +88,7 @@ def setup(hass, config): switch.update_ha_state(True) hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service) + hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service) return True From c3a9db0a3747181a186b1fe6d514d7d0e3e1a07d Mon Sep 17 00:00:00 2001 From: pavoni Date: Sat, 29 Aug 2015 20:32:46 +0100 Subject: [PATCH 03/10] Add defult methods to base class for switch_state and sensor_state --- homeassistant/components/switch/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 4f83b8aa24f..f245406fb28 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -108,6 +108,16 @@ class SwitchDevice(ToggleEntity): """ Today total power usage in mw. """ return None + @property + def standby_state(self): + """ Is the device on - or in standby. """ + return None + + @property + def sensor_state(self): + """ Is the sensor on or off. """ + return None + @property def device_state_attributes(self): """ Returns device specific state attributes. """ From b9cca82a45697a794dbd9d92534644129162130d Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 30 Aug 2015 22:48:48 +0100 Subject: [PATCH 04/10] Update pywemo to 0.3 --- requirements_all.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_all.txt b/requirements_all.txt index 9798a95f030..ce74a8475cd 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -89,7 +89,7 @@ pynetgear==0.3 netdisco==0.3 # Wemo (switch.wemo) -pywemo==0.2 +pywemo==0.3 # Wink (*.wink) https://github.com/balloob/python-wink/archive/c2b700e8ca866159566ecf5e644d9c297f69f257.zip From 028551784a0bf4341f2161146c9e1184cab0f395 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 30 Aug 2015 22:50:08 +0100 Subject: [PATCH 05/10] Update pywemo to 0,3 --- homeassistant/components/switch/wemo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index cf1aed40d52..45e2fb19fd6 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -8,7 +8,7 @@ import logging from homeassistant.components.switch import SwitchDevice -REQUIREMENTS = ['pywemo==0.2'] +REQUIREMENTS = ['pywemo==0.3'] # pylint: disable=unused-argument From 5ed69870677a7a4366c69e37b0077b9c8b455107 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 31 Aug 2015 11:07:52 +0100 Subject: [PATCH 06/10] Refactor wemo standby state slightly --- homeassistant/components/switch/__init__.py | 6 ++---- homeassistant/components/switch/wemo.py | 19 +++++++++++++++++-- homeassistant/const.py | 1 + 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 48d3e31e9b5..b6dd31b48c2 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -24,7 +24,6 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}' ATTR_TODAY_MWH = "today_mwh" ATTR_CURRENT_POWER_MWH = "current_power_mwh" -ATTR_STANDBY_STATE = "standby_state" ATTR_SENSOR_STATE = "sensor_state" MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) @@ -40,7 +39,6 @@ DISCOVERY_PLATFORMS = { PROP_TO_ATTR = { 'current_power_mwh': ATTR_CURRENT_POWER_MWH, 'today_power_mw': ATTR_TODAY_MWH, - 'standby_state': ATTR_STANDBY_STATE, 'sensor_state': ATTR_SENSOR_STATE } @@ -106,8 +104,8 @@ class SwitchDevice(ToggleEntity): return None @property - def standby_state(self): - """ Is the device on - or in standby. """ + def is_standby(self): + """ Is the device in standby. """ return None @property diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 45e2fb19fd6..4c6cd2ed4d8 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -51,6 +51,16 @@ class WemoSwitch(SwitchDevice): """ Returns the name of the switch if any. """ return self.wemo.name + @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 def current_power_mwh(self): """ Current power usage in mwh. """ @@ -64,10 +74,15 @@ class WemoSwitch(SwitchDevice): return self.insight_params['todaymw'] @property - def standby_state(self): + def is_standby(self): """ Is the device on - or in standby. """ if self.insight_params: - return self.insight_params['standby_state'] + standby_state = self.insight_params['standby_state'] + # Standby is actually '8' but seems more defensive to check for the On and Off states + if standby_state == '1' or standby_state == '0' + return False + else + return True @property def sensor_state(self): diff --git a/homeassistant/const.py b/homeassistant/const.py index a3b9cc8d396..4ed110dcd93 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -46,6 +46,7 @@ STATE_CLOSED = 'closed' STATE_PLAYING = 'playing' STATE_PAUSED = 'paused' STATE_IDLE = 'idle' +STATE_STANDBY = 'standby' # #### STATE AND EVENT ATTRIBUTES #### # Contains current time for a TIME_CHANGED event From 54dd09df29cfb2e15fd1956fc0bb0430713d9239 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 31 Aug 2015 12:13:53 +0100 Subject: [PATCH 07/10] Fix typos --- homeassistant/components/switch/wemo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 4c6cd2ed4d8..026b20f4c5a 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -79,9 +79,9 @@ class WemoSwitch(SwitchDevice): if self.insight_params: standby_state = self.insight_params['standby_state'] # Standby is actually '8' but seems more defensive to check for the On and Off states - if standby_state == '1' or standby_state == '0' + if standby_state == '1' or standby_state == '0': return False - else + else: return True @property From 325655267515ed819f264e28746a703ed5d3f869 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 31 Aug 2015 13:12:17 +0100 Subject: [PATCH 08/10] Fix bug, use constants for states. --- homeassistant/components/switch/wemo.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 026b20f4c5a..86c5853d727 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -7,6 +7,7 @@ Support for WeMo switches. import logging from homeassistant.components.switch import SwitchDevice +from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY REQUIREMENTS = ['pywemo==0.3'] @@ -77,7 +78,7 @@ class WemoSwitch(SwitchDevice): def is_standby(self): """ Is the device on - or in standby. """ if self.insight_params: - standby_state = self.insight_params['standby_state'] + standby_state = self.insight_params['state'] # Standby is actually '8' but seems more defensive to check for the On and Off states if standby_state == '1' or standby_state == '0': return False @@ -90,9 +91,9 @@ class WemoSwitch(SwitchDevice): if self.maker_params and self.has_sensor: # Note a state of 1 matches the WeMo app 'not triggered'! if self.maker_params['sensorstate']: - return 'off' + return STATE_OFF else: - return 'on' + return STATE_ON @property def switch_mode(self): From 93cd7bfc5df9458ec743c57db5a4e77fc586635d Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 31 Aug 2015 15:19:04 +0100 Subject: [PATCH 09/10] Split comment to keep line short --- homeassistant/components/switch/wemo.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 86c5853d727..6bb694c1adf 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -59,6 +59,9 @@ class WemoSwitch(SwitchDevice): if not is_on: return STATE_OFF elif self.is_standby: + print(STATE_STANDBY) + print(self.wemo) + # import pdb; pdb.set_trace() return STATE_STANDBY return STATE_ON @@ -79,7 +82,8 @@ class WemoSwitch(SwitchDevice): """ Is the device on - or in standby. """ if self.insight_params: standby_state = self.insight_params['state'] - # Standby is actually '8' but seems more defensive to check for the On and Off states + # Standby is actually '8' but seems more defensive + # to check for the On and Off states if standby_state == '1' or standby_state == '0': return False else: From 794a11db21c825de718f14f00049cb04c28e97a3 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 31 Aug 2015 16:33:12 +0100 Subject: [PATCH 10/10] Remove debug statements! --- homeassistant/components/switch/wemo.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 6bb694c1adf..98c6ed5aa01 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -59,9 +59,6 @@ class WemoSwitch(SwitchDevice): if not is_on: return STATE_OFF elif self.is_standby: - print(STATE_STANDBY) - print(self.wemo) - # import pdb; pdb.set_trace() return STATE_STANDBY return STATE_ON