Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions homeassistant/components/switch/wemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
https://home-assistant.io/components/switch.wemo/
"""
import logging
from datetime import datetime, timedelta

from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
Expand Down Expand Up @@ -110,11 +111,30 @@ def device_state_attributes(self):
if self.insight_params or (self.coffeemaker_mode is not None):
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.

WemoSwitch.as_uptime(self.insight_params['onfor'])
attr['on_today_time'] = \
WemoSwitch.as_uptime(self.insight_params['ontoday'])
attr['on_total_time'] = \
WemoSwitch.as_uptime(self.insight_params['ontotal'])
attr['power_threshold_mw'] = \
self.insight_params['powerthreshold']

if self.coffeemaker_mode is not None:
attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode

return attr

@staticmethod
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.

uptime.hour,
uptime.minute,
uptime.second)

@property
def current_power_mwh(self):
"""Current power usage in mWh."""
Expand Down