/
insight.py
130 lines (105 loc) · 4 KB
/
insight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Representation of a WeMo Insight device."""
import logging
from datetime import datetime
from .switch import Switch
LOG = logging.getLogger(__name__)
class Insight(Switch):
"""Representation of a WeMo Insight device."""
def __init__(self, *args, **kwargs):
"""Create a WeMo Switch device."""
Switch.__init__(self, *args, **kwargs)
self.insight_params = {}
self.update_insight_params()
def __repr__(self):
"""Return a string representation of the device."""
return '<WeMo Insight "{name}">'.format(name=self.name)
def update_insight_params(self):
"""Get and parse the device attributes."""
# pylint: disable=maybe-no-member
params = self.insight.GetInsightParams().get('InsightParams')
self.insight_params = self.parse_insight_params(params)
def subscription_update(self, _type, _params):
"""Update the device attributes due to a subscription update event."""
LOG.debug("subscription_update %s %s", _type, _params)
if _type == "InsightParams":
self.insight_params = self.parse_insight_params(_params)
return True
return Switch.subscription_update(self, _type, _params)
def parse_insight_params(self, params):
"""Parse the Insight parameters."""
(
state, # 0 if off, 1 if on, 8 if on but load is off
lastchange,
onfor, # seconds
ontoday, # seconds
ontotal, # seconds
timeperiod, # pylint: disable=unused-variable
wifipower, # wifi rssi signal strength
currentmw,
todaymw,
totalmw,
powerthreshold
) = params.split('|')
return {'state': state,
'lastchange': datetime.fromtimestamp(int(lastchange)),
'onfor': int(onfor),
'ontoday': int(ontoday),
'ontotal': int(ontotal),
'todaymw': int(float(todaymw)),
'totalmw': int(float(totalmw)),
'currentpower': int(float(currentmw)),
'wifipower': int(float(wifipower)),
'powerthreshold': int(float(powerthreshold))}
def get_state(self, force_update=False):
"""Return the device state."""
if force_update or self._state is None:
self.update_insight_params()
return Switch.get_state(self, force_update)
@property
def device_type(self):
"""Return what kind of WeMo this device is."""
return "Insight"
@property
def today_kwh(self):
"""Return the kwh used today."""
return self.insight_params['todaymw'] * 1.6666667e-8
@property
def current_power(self):
"""Return the current power usage in mW."""
return self.insight_params['currentpower']
@property
def wifi_power(self):
"""Return the current rssi wifi signal."""
return self.insight_params['wifipower']
@property
def threshold_power(self):
"""
Return the threshold power.
Above this the device is on, below it is standby.
"""
return self.insight_params['powerthreshold']
@property
def today_on_time(self):
"""Return how long the device has been on today."""
return self.insight_params['ontoday']
@property
def on_for(self):
"""Return how long the device has been on."""
return self.insight_params['onfor']
@property
def last_change(self):
"""Return the last change datetime."""
return self.insight_params['lastchange']
@property
def today_standby_time(self):
"""Return how long the device has been in standby today."""
return self.insight_params['ontoday']
@property
def get_standby_state(self):
"""Return the standby state of the device."""
state = self.insight_params['state']
if state == '0':
return 'off'
if state == '1':
return 'on'
return 'standby'