Improvements for WeMo Insight switches#6001
Conversation
* Changes current power units to watts * Adds power on times and additional totals
| try: | ||
| return self.insight_params['todaymw'] | ||
| except: | ||
| return None |
There was a problem hiding this comment.
indentation is not a multiple of four
| if self.insight_params: | ||
| return self.insight_params['todaymw'] | ||
| try: | ||
| return self.insight_params['todaymw'] |
There was a problem hiding this comment.
indentation is not a multiple of four
| try: | ||
| return self.insight_params['totalmw'] | ||
| except: | ||
| return None |
There was a problem hiding this comment.
indentation is not a multiple of four
| if self.insight_params: | ||
| return self.insight_params['currentpower'] | ||
| try: | ||
| return self.insight_params['totalmw'] |
There was a problem hiding this comment.
indentation is not a multiple of four
| 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) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| return self.insight_params['powerthreshold'] / 1000 | ||
|
|
||
| def _as_uptime(self, _seconds): | ||
| d = datetime(1,1,1) + timedelta(seconds=_seconds) |
There was a problem hiding this comment.
indentation is not a multiple of four
missing whitespace after ','
| @property | ||
| def power_threshold(self): | ||
| if self.insight_params: | ||
| return self.insight_params['powerthreshold'] / 1000 |
There was a problem hiding this comment.
indentation is not a multiple of four
| try: | ||
| return self._current_power_mw() / 1000 | ||
| except: | ||
| return None |
There was a problem hiding this comment.
indentation is not a multiple of four
| """Current power usage in W.""" | ||
| if self.insight_params: | ||
| try: | ||
| return self._current_power_mw() / 1000 |
There was a problem hiding this comment.
indentation is not a multiple of four
| def _current_power_mw(self): | ||
| """Current power usage in mW.""" | ||
| if self.insight_params: | ||
| return self.insight_params['currentpower'] |
There was a problem hiding this comment.
indentation is not a multiple of four
* Reordered datetime import * Spaces by 4
* continuation line under-indented for visual indent
| d.hour, | ||
| d.minute, | ||
| d.second) | ||
|
|
| 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, |
trailing whitespace... (argh... the bot should just trim it)
| def on_total(self): | ||
| """On time in seconds.""" | ||
| if self.insight_params: | ||
| return as_uptime(self.insight_params['ontotal']) |
| def on_today(self): | ||
| """On time in seconds.""" | ||
| if self.insight_params: | ||
| return as_uptime(self.insight_params['ontoday']) |
| def on_for(self): | ||
| """On time in seconds.""" | ||
| if self.insight_params: | ||
| return as_uptime(self.insight_params['onfor']) |
|
|
||
| @staticmethod | ||
| def as_uptime(_seconds): | ||
| """Format seconds in to uptime string in the format: 00d 00h 00m 00s """ |
There was a problem hiding this comment.
line too long (80 > 79 characters)
| def _current_power_mw(self): | ||
| """Current power usage in mW.""" | ||
| if self.insight_params: | ||
| try: |
There was a problem hiding this comment.
Instead of try … catch, use self.insight_param.get('current_power')
There was a problem hiding this comment.
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.
|
|
||
| return attr | ||
|
|
||
| # @property |
There was a problem hiding this comment.
Yeah, not needed, was from originally when that method was currrent_power_mwh
| except TypeError: | ||
| return None | ||
|
|
||
| @property |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
| @property | ||
| def current_power_mwh(self): | ||
| """Current power usage in mWh.""" | ||
| def power_total_mw_min(self): |
There was a problem hiding this comment.
You can't just rename overrides of official properties. Please check out the switch ABC and see what is supported and what is not.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
I've released your |
|
|
||
| # Wemo Insight | ||
| ATTR_POWER_CURRENT_W = 'power_current_w' | ||
| # ATTR_POWER_AVG_W = 'power_average_w' |
|
@jumpkick Let me know if you need some help on this. My suggestion would be to leave the overridden switch attributes ( 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. |
This gets rid of the other stuff and just updates device_state_attributes, leaving the default properties alone.
|
|
||
| if self.insight_params or (self.coffeemaker_mode is not None): | ||
| attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state | ||
| attr['current_power_w'] = \ |
There was a problem hiding this comment.
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.
| 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'] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Will remove in favour of the incorrectly labeled existing ones
|
|
||
| if self.insight_params or (self.coffeemaker_mode is not None): | ||
| attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state | ||
| attr['current_power_w'] = \ |
There was a problem hiding this comment.
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
|
👍 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'] = \ |
There was a problem hiding this comment.
Can we move these to ATTR_* constants as above?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Why not use the timedelta object directly? It supports days, hours etc https://docs.python.org/3/library/datetime.html#datetime.timedelta
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
Oh you're right, I was wrong. Looks like timedelta converts it all to seconds.
|
Thanks! 🐬 🍪 💯 |
Description:
Related issue (if applicable): fixes #