From 29c7987453720683d4165c4dbb1fdb45c638419b Mon Sep 17 00:00:00 2001 From: jumpkick Date: Tue, 14 Feb 2017 18:29:23 -0500 Subject: [PATCH 01/10] Improvements for WeMo Insight switches * Changes current power units to watts * Adds power on times and additional totals --- homeassistant/components/switch/wemo.py | 83 ++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 3af93d08fc8..985dd8418eb 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -10,6 +10,7 @@ from homeassistant.components.switch import SwitchDevice from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN) from homeassistant.loader import get_component +from datetime import datetime, timedelta DEPENDENCIES = ['wemo'] @@ -20,6 +21,16 @@ ATTR_SWITCH_MODE = "switch_mode" ATTR_CURRENT_STATE_DETAIL = 'state_detail' ATTR_COFFEMAKER_MODE = "coffeemaker_mode" +# 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' + MAKER_SWITCH_MOMENTARY = "momentary" MAKER_SWITCH_TOGGLE = "toggle" @@ -109,23 +120,81 @@ class WemoSwitch(SwitchDevice): if self.insight_params or (self.coffeemaker_mode is not None): attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state + 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 if self.coffeemaker_mode is not None: attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode return attr - @property - def current_power_mwh(self): - """Current power usage in mWh.""" +# @property + def _current_power_mw(self): + """Current power usage in mW.""" if self.insight_params: - return self.insight_params['currentpower'] + return self.insight_params['currentpower'] @property - def today_power_mw(self): - """Today total power usage in mW.""" + def power_current_watt(self): + """Current power usage in W.""" if self.insight_params: - return self.insight_params['todaymw'] + try: + return self._current_power_mw() / 1000 + except: + return None + + @property + def power_threshold(self): + if self.insight_params: + return self.insight_params['powerthreshold'] / 1000 + + 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, + d.hour, + d.minute, d.second) + + @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']) + + @property + def power_total_mw_min(self): + """This is a total of average mW per minute.""" + if self.insight_params: + try: + return self.insight_params['totalmw'] + except: + return None + + @property + def power_today_mw_min(self): + """This is the total consumption today in mW per minute.""" + if self.insight_params: + try: + return self.insight_params['todaymw'] + except: + return None @property def detail_state(self): From c404fb7142e97be0176c9ea58bcc161fab6f6006 Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 15:34:42 -0500 Subject: [PATCH 02/10] Update wemo.py * Reordered datetime import * Spaces by 4 --- homeassistant/components/switch/wemo.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 985dd8418eb..1576a203b35 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -5,12 +5,12 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/switch.wemo/ """ import logging +from datetime import datetime, timedelta from homeassistant.components.switch import SwitchDevice from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN) from homeassistant.loader import get_component -from datetime import datetime, timedelta DEPENDENCIES = ['wemo'] @@ -138,21 +138,21 @@ class WemoSwitch(SwitchDevice): def _current_power_mw(self): """Current power usage in mW.""" if self.insight_params: - return self.insight_params['currentpower'] + return self.insight_params['currentpower'] @property def power_current_watt(self): """Current power usage in W.""" if self.insight_params: try: - return self._current_power_mw() / 1000 + return self._current_power_mw() / 1000 except: - return None + return None @property def power_threshold(self): if self.insight_params: - return self.insight_params['powerthreshold'] / 1000 + return self.insight_params['powerthreshold'] / 1000 def _as_uptime(self, _seconds): d = datetime(1,1,1) + timedelta(seconds=_seconds) @@ -183,18 +183,18 @@ class WemoSwitch(SwitchDevice): """This is a total of average mW per minute.""" if self.insight_params: try: - return self.insight_params['totalmw'] + return self.insight_params['totalmw'] except: - return None + return None @property def power_today_mw_min(self): """This is the total consumption today in mW per minute.""" if self.insight_params: try: - return self.insight_params['todaymw'] + return self.insight_params['todaymw'] except: - return None + return None @property def detail_state(self): From 44d274e4286516a48f6d61c5ed5a42d7dd1ef4fc Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 15:38:41 -0500 Subject: [PATCH 03/10] Update wemo.py * continuation line under-indented for visual indent --- 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 1576a203b35..b4d619c9882 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -157,9 +157,10 @@ class WemoSwitch(SwitchDevice): 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, - d.hour, - d.minute, d.second) - + d.hour, + d.minute, + d.second) + @property def on_for(self): """On time in seconds.""" From a718e92708e6d80d03645087e5ebeb4bd376280b Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 15:40:02 -0500 Subject: [PATCH 04/10] Update wemo.py trailing whitespace... (argh... the bot should just trim it) --- 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 b4d619c9882..4d436aa348e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -158,7 +158,7 @@ class WemoSwitch(SwitchDevice): 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, d.hour, - d.minute, + d.minute, d.second) @property From b163544e3c5dca8370afebf2b9a11992c4761a44 Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 16:47:02 -0500 Subject: [PATCH 05/10] Back to you travis.... --- homeassistant/components/switch/wemo.py | 41 +++++++++++++------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 4d436aa348e..89bddb8ecc2 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -138,63 +138,66 @@ class WemoSwitch(SwitchDevice): def _current_power_mw(self): """Current power usage in mW.""" if self.insight_params: - return self.insight_params['currentpower'] + return self.insight_params['currentpower'] @property def power_current_watt(self): """Current power usage in W.""" if self.insight_params: try: - return self._current_power_mw() / 1000 - except: - return None + return self._current_power_mw() / 1000 + except Exception: + return None @property def power_threshold(self): + """Threshold of W at which Insight will indicate it's load is ON.""" if self.insight_params: - return self.insight_params['powerthreshold'] / 1000 + return self.insight_params['powerthreshold'] / 1000 + + @staticmethod + def as_uptime(_seconds): + """Format seconds in to uptime string in the format: 00d 00h 00m 00s """ + uptime = datetime(1, 1, 1) + timedelta(seconds=_seconds) + return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(uptime.day-1, + uptime.hour, + uptime.minute, + uptime.second) - 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, - d.hour, - d.minute, - d.second) - @property def on_for(self): """On time in seconds.""" if self.insight_params: - return self._as_uptime(self.insight_params['onfor']) + return 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']) + return 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']) + return as_uptime(self.insight_params['ontotal']) @property def power_total_mw_min(self): - """This is a total of average mW per minute.""" + """Total of average mW per minute.""" if self.insight_params: try: return self.insight_params['totalmw'] - except: + except Exception: return None @property def power_today_mw_min(self): - """This is the total consumption today in mW per minute.""" + """Total consumption today in mW per minute.""" if self.insight_params: try: return self.insight_params['todaymw'] - except: + except Exception: return None @property From e221c8a37d5f13a14adf4ddd925ee65126d307b8 Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 16:57:16 -0500 Subject: [PATCH 06/10] Update wemo.py --- homeassistant/components/switch/wemo.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 89bddb8ecc2..7fe8de7b699 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -168,19 +168,19 @@ class WemoSwitch(SwitchDevice): def on_for(self): """On time in seconds.""" if self.insight_params: - return as_uptime(self.insight_params['onfor']) + return WemoSwitch.as_uptime(self.insight_params['onfor']) @property def on_today(self): """On time in seconds.""" if self.insight_params: - return as_uptime(self.insight_params['ontoday']) + return WemoSwitch.as_uptime(self.insight_params['ontoday']) @property def on_total(self): """On time in seconds.""" if self.insight_params: - return as_uptime(self.insight_params['ontotal']) + return WemoSwitch.as_uptime(self.insight_params['ontotal']) @property def power_total_mw_min(self): From e9cf5f6f42680f5ef4105b5a37eb62a45835ccad Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 16:58:11 -0500 Subject: [PATCH 07/10] Update wemo.py --- 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 7fe8de7b699..3c7a3c565c0 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -157,7 +157,7 @@ class WemoSwitch(SwitchDevice): @staticmethod def as_uptime(_seconds): - """Format seconds in to uptime string in the format: 00d 00h 00m 00s """ + """Format seconds into uptime string in the format: 00d 00h 00m 00s""" uptime = datetime(1, 1, 1) + timedelta(seconds=_seconds) return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(uptime.day-1, uptime.hour, From f6e46aecf54a6f81dd254e9e61d0d1b3ebb65f46 Mon Sep 17 00:00:00 2001 From: jumpkick Date: Wed, 15 Feb 2017 17:32:45 -0500 Subject: [PATCH 08/10] Update wemo.py --- homeassistant/components/switch/wemo.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 3c7a3c565c0..afa3a8a0237 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -138,7 +138,10 @@ class WemoSwitch(SwitchDevice): def _current_power_mw(self): """Current power usage in mW.""" if self.insight_params: - return self.insight_params['currentpower'] + try: + return self.insight_params['currentpower'] + except KeyError: + return None @property def power_current_watt(self): @@ -146,7 +149,7 @@ class WemoSwitch(SwitchDevice): if self.insight_params: try: return self._current_power_mw() / 1000 - except Exception: + except TypeError: return None @property @@ -157,7 +160,7 @@ class WemoSwitch(SwitchDevice): @staticmethod def as_uptime(_seconds): - """Format seconds into uptime string in the format: 00d 00h 00m 00s""" + """Format seconds into uptime string in the format: 00d 00h 00m 00s.""" uptime = datetime(1, 1, 1) + timedelta(seconds=_seconds) return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(uptime.day-1, uptime.hour, @@ -188,7 +191,7 @@ class WemoSwitch(SwitchDevice): if self.insight_params: try: return self.insight_params['totalmw'] - except Exception: + except KeyError: return None @property @@ -197,7 +200,7 @@ class WemoSwitch(SwitchDevice): if self.insight_params: try: return self.insight_params['todaymw'] - except Exception: + except KeyError: return None @property From ef87d4dad47404d7c251ca617e127c5d7f2688aa Mon Sep 17 00:00:00 2001 From: jumpkick Date: Thu, 23 Feb 2017 04:54:09 -0500 Subject: [PATCH 09/10] Update device_state_attributes only This gets rid of the other stuff and just updates device_state_attributes, leaving the default properties alone. --- homeassistant/components/switch/wemo.py | 90 +++++-------------------- 1 file changed, 18 insertions(+), 72 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index afa3a8a0237..0768b62062d 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -21,16 +21,6 @@ ATTR_SWITCH_MODE = "switch_mode" ATTR_CURRENT_STATE_DETAIL = 'state_detail' ATTR_COFFEMAKER_MODE = "coffeemaker_mode" -# 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' - MAKER_SWITCH_MOMENTARY = "momentary" MAKER_SWITCH_TOGGLE = "toggle" @@ -120,44 +110,24 @@ class WemoSwitch(SwitchDevice): if self.insight_params or (self.coffeemaker_mode is not None): attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state - 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 + attr['current_power_w'] = \ + self.insight_params['currentpower'] / 1000 + attr['today_power_mW_min'] = self.insight_params['todaymw'] + attr['total_power_mW_min'] = self.insight_params['totalmw'] + attr['on_time_most_recent'] = \ + WemoSwitch.as_uptime(self.insight_params['onfor']) + attr['on_time_today'] = \ + WemoSwitch.as_uptime(self.insight_params['ontoday']) + attr['on_time_total'] = \ + WemoSwitch.as_uptime(self.insight_params['ontotal']) + attr['power_threshold_w'] = \ + self.insight_params['powerthreshold'] / 1000 if self.coffeemaker_mode is not None: attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode return attr -# @property - def _current_power_mw(self): - """Current power usage in mW.""" - if self.insight_params: - try: - return self.insight_params['currentpower'] - except KeyError: - return None - - @property - def power_current_watt(self): - """Current power usage in W.""" - if self.insight_params: - try: - return self._current_power_mw() / 1000 - except TypeError: - return None - - @property - def power_threshold(self): - """Threshold of W at which Insight will indicate it's load is ON.""" - if self.insight_params: - return self.insight_params['powerthreshold'] / 1000 - @staticmethod def as_uptime(_seconds): """Format seconds into uptime string in the format: 00d 00h 00m 00s.""" @@ -168,40 +138,16 @@ class WemoSwitch(SwitchDevice): uptime.second) @property - def on_for(self): - """On time in seconds.""" + def current_power_mwh(self): + """Current power usage in mWh.""" if self.insight_params: - return WemoSwitch.as_uptime(self.insight_params['onfor']) + return self.insight_params['currentpower'] @property - def on_today(self): - """On time in seconds.""" + def today_power_mw(self): + """Today total power usage in mW.""" if self.insight_params: - return WemoSwitch.as_uptime(self.insight_params['ontoday']) - - @property - def on_total(self): - """On time in seconds.""" - if self.insight_params: - return WemoSwitch.as_uptime(self.insight_params['ontotal']) - - @property - def power_total_mw_min(self): - """Total of average mW per minute.""" - if self.insight_params: - try: - return self.insight_params['totalmw'] - except KeyError: - return None - - @property - def power_today_mw_min(self): - """Total consumption today in mW per minute.""" - if self.insight_params: - try: - return self.insight_params['todaymw'] - except KeyError: - return None + return self.insight_params['todaymw'] @property def detail_state(self): From fc5e25a07bab728452936833053f7b96a25e6df7 Mon Sep 17 00:00:00 2001 From: jumpkick Date: Thu, 23 Feb 2017 18:03:49 -0500 Subject: [PATCH 10/10] Incorporate comment suggestions - Separate attribs from coffeemaker condition - Set power units for threshold to mW to be consistent with others - Adjust on-time labels to be more clear --- homeassistant/components/switch/wemo.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 0768b62062d..1c9cbe15a33 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -110,18 +110,16 @@ class WemoSwitch(SwitchDevice): if self.insight_params or (self.coffeemaker_mode is not None): attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state - attr['current_power_w'] = \ - self.insight_params['currentpower'] / 1000 - attr['today_power_mW_min'] = self.insight_params['todaymw'] - attr['total_power_mW_min'] = self.insight_params['totalmw'] - attr['on_time_most_recent'] = \ + + if self.insight_params: + attr['on_latest_time'] = \ WemoSwitch.as_uptime(self.insight_params['onfor']) - attr['on_time_today'] = \ + attr['on_today_time'] = \ WemoSwitch.as_uptime(self.insight_params['ontoday']) - attr['on_time_total'] = \ + attr['on_total_time'] = \ WemoSwitch.as_uptime(self.insight_params['ontotal']) - attr['power_threshold_w'] = \ - self.insight_params['powerthreshold'] / 1000 + attr['power_threshold_mw'] = \ + self.insight_params['powerthreshold'] if self.coffeemaker_mode is not None: attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode