Skip to content

Improvements for WeMo Insight switches#6001

Merged
robbiet480 merged 10 commits into
home-assistant:devfrom
deftdawg:patch-2
Mar 10, 2017
Merged

Improvements for WeMo Insight switches#6001
robbiet480 merged 10 commits into
home-assistant:devfrom
deftdawg:patch-2

Conversation

@deftdawg
Copy link
Copy Markdown
Contributor

Description:

  • Changes current power units to watts
  • Adds power on times and additional totals

Related issue (if applicable): fixes #

* Changes current power units to watts
* Adds power on times and additional totals
@mention-bot
Copy link
Copy Markdown

@jumpkick, thanks for your PR! By analyzing the history of the files in this pull request, we identified @pavoni, @stu-gott and @balloob to be potential reviewers.

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @jumpkick,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Comment thread homeassistant/components/switch/wemo.py Outdated
try:
return self.insight_params['todaymw']
except:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
if self.insight_params:
return self.insight_params['todaymw']
try:
return self.insight_params['todaymw']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
try:
return self.insight_params['totalmw']
except:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
if self.insight_params:
return self.insight_params['currentpower']
try:
return self.insight_params['totalmw']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

Comment thread homeassistant/components/switch/wemo.py Outdated
return self.insight_params['powerthreshold'] / 1000

def _as_uptime(self, _seconds):
d = datetime(1,1,1) + timedelta(seconds=_seconds)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four
missing whitespace after ','

Comment thread homeassistant/components/switch/wemo.py Outdated
@property
def power_threshold(self):
if self.insight_params:
return self.insight_params['powerthreshold'] / 1000
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
try:
return self._current_power_mw() / 1000
except:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
"""Current power usage in W."""
if self.insight_params:
try:
return self._current_power_mw() / 1000
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

Comment thread homeassistant/components/switch/wemo.py Outdated
def _current_power_mw(self):
"""Current power usage in mW."""
if self.insight_params:
return self.insight_params['currentpower']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not a multiple of four

* Reordered datetime import
* Spaces by 4
* continuation line under-indented for visual indent
Comment thread homeassistant/components/switch/wemo.py Outdated
d.hour,
d.minute,
d.second)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

Comment thread homeassistant/components/switch/wemo.py Outdated
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,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

trailing whitespace... (argh... the bot should just trim it)
Comment thread homeassistant/components/switch/wemo.py Outdated
def on_total(self):
"""On time in seconds."""
if self.insight_params:
return as_uptime(self.insight_params['ontotal'])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'as_uptime'

Comment thread homeassistant/components/switch/wemo.py Outdated
def on_today(self):
"""On time in seconds."""
if self.insight_params:
return as_uptime(self.insight_params['ontoday'])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'as_uptime'

Comment thread homeassistant/components/switch/wemo.py Outdated
def on_for(self):
"""On time in seconds."""
if self.insight_params:
return as_uptime(self.insight_params['onfor'])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'as_uptime'

Comment thread homeassistant/components/switch/wemo.py Outdated

@staticmethod
def as_uptime(_seconds):
"""Format seconds in to uptime string in the format: 00d 00h 00m 00s """
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (80 > 79 characters)

Comment thread homeassistant/components/switch/wemo.py Outdated
def _current_power_mw(self):
"""Current power usage in mW."""
if self.insight_params:
try:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of try … catch, use self.insight_param.get('current_power')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess I could do if self.insight_params and self.insight_param.get('current_power'):, guessing the original code if self.insight_params condition because non-Insight switches don't have those attribs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly right.

Comment thread homeassistant/components/switch/wemo.py Outdated

return attr

# @property
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not needed, was from originally when that method was currrent_power_mwh

Comment thread homeassistant/components/switch/wemo.py Outdated
except TypeError:
return None

@property
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are somethings properties and other methods ? Also, why are they properties/method to begin with and not just all part of device_state_attributes ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Started out cutting and pasting what was there (I'm not a python guy, nor do I know HA components)... If we don't need the methods, I can just load everything into the state attribs assignments.

Comment thread homeassistant/components/switch/wemo.py Outdated
@property
def current_power_mwh(self):
"""Current power usage in mWh."""
def power_total_mw_min(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't just rename overrides of official properties. Please check out the switch ABC and see what is supported and what is not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Insight returns average milliwatt power consumed for each minute. So for 12W it will increment this value by 12,000 each minute. So it's mW/min.

mWh is average milliwatts per hour... The Insight doesn't provide that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point I hope to refactor some of the HA power/energy code - but this requires changing some of the base components properties - and any other components that override then.

Until then I think you need to leave the official properties as they are - and adddevice_state_properties for the correct / new attributes.

@pavoni
Copy link
Copy Markdown
Contributor

pavoni commented Feb 18, 2017

I've released your pywemo changes as 0.4.13 ready for when you have time to wrap up this PR.

Comment thread homeassistant/components/switch/wemo.py Outdated

# Wemo Insight
ATTR_POWER_CURRENT_W = 'power_current_w'
# ATTR_POWER_AVG_W = 'power_average_w'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required?

@pavoni
Copy link
Copy Markdown
Contributor

pavoni commented Feb 22, 2017

@jumpkick Let me know if you need some help on this.

My suggestion would be to leave the overridden switch attributes (current_power_mwh and today_power_mw) as they are for this PR.

Some of the issues you've found with these properties should be reworked as a broader PR to change all these attributes across all devices (#3828)).

The other attributes you want to include can just be added as device_state_attributes.

@deftdawg
Copy link
Copy Markdown
Contributor Author

@pavoni yeah, I'm thinking of starting over on this patch... leave the stuff that's there and just load up device_state_attributes w/ the additional attributes. I agree with #3828, but not sure how to incorporate it.

This gets rid of the other stuff and just updates device_state_attributes, leaving the default properties alone.
Comment thread homeassistant/components/switch/wemo.py Outdated

if self.insight_params or (self.coffeemaker_mode is not None):
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
attr['current_power_w'] = \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't expect the coffee machine will support these new attributes - so I'd change this so that you only retrieve these attributes for the insight device.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call

Comment thread homeassistant/components/switch/wemo.py Outdated
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']
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is duplicated - but it's also a duplicate of the existing current_power_mwh property of the switch - so you can just remove these - and depend on the switch attributes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove in favour of the incorrectly labeled existing ones

Comment thread homeassistant/components/switch/wemo.py Outdated

if self.insight_params or (self.coffeemaker_mode is not None):
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
attr['current_power_w'] = \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just the existing switch attribute divided by 1000.

At some point we'll change the switch attribute to be W rather than mW anyway. So this is only really helpful until we do that.

My suggestion would be to remove it

- 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
@pavoni
Copy link
Copy Markdown
Contributor

pavoni commented Feb 24, 2017

👍 Looks good to me.

@balloob are you happy for me to merge?

attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state

if self.insight_params:
attr['on_latest_time'] = \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these to ATTR_* constants as above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put them in ATTR_ constants, but they are probably all going to be renamed and content replaced as part of addressing #3828... more places to change, so should I do it anyway?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only one you'd need to rename is the threshold in mw.

Perhaps switch it to `W’ and break out the attributes in constants. Then nothing will need to be changed if my PR gets merged.

def as_uptime(_seconds):
"""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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the timedelta object directly? It supports days, hours etc https://docs.python.org/3/library/datetime.html#datetime.timedelta

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably doing something stupid (python noob), but when I try that I get this:

>>> from datetime import datetime, timedelta
>>> uptime = timedelta(days=0,seconds=86401,hours=0,minutes=0)
>>> print ("{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(uptime.days, uptime.hours, uptime.minutes, uptime.seconds))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.timedelta' object has no attribute 'hours'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right, I was wrong. Looks like timedelta converts it all to seconds.

@robbiet480
Copy link
Copy Markdown
Contributor

Thanks! 🐬 🍪 💯

@robbiet480 robbiet480 merged commit 11c4d38 into home-assistant:dev Mar 10, 2017
@deftdawg deftdawg deleted the patch-2 branch March 11, 2017 06:27
@home-assistant home-assistant locked and limited conversation to collaborators Jul 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants