forked from SevenW/Plugwise-2-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugwise_util
executable file
·167 lines (130 loc) · 5.26 KB
/
plugwise_util
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# Copyright (C) 2015 SevenWatt - sevenwatt.com
# Use of this source code is governed by the MIT license found in the LICENSE file.
from pprint import pprint
import optparse
import logging
from serial.serialutil import SerialException
from plugwise import *
from swutil.util import *
from plugwise.api import *
log_comm(False)
init_logger("plugwise_util.log")
log_level(logging.INFO)
DEFAULT_SERIAL_PORT = "/dev/ttyUSB0"
#default settings for attr-field of cirle
conf = {"mac": "000D6F0001000000", "category": "misc", "name": "circle_n", "loginterval": "60", "always_on": "False", "production": "False", "location": "misc"}
parser = optparse.OptionParser()
parser.add_option("-m", "--mac", dest="mac", help="MAC address")
parser.add_option("-d", "--device", dest="device",
help="Serial port device")
parser.add_option("-p", "--power", action="store_true",
help="Get current power usage")
parser.add_option("-t", "--time", dest="time",
help="""Set circle's internal clock to given time.
Format is 'YYYY-MM-DD hh:mm:ss' use the special value 'sync' if you want to set Circles clock to the same time as your computer""")
parser.add_option("-C", "--counter", action="store_true",
help="Print out values of the pulse counters")
parser.add_option("-c", "--continuous", type="int",
help="Perform the requested action in an endless loop, sleeping for the given number of seconds in between.")
parser.add_option("-s", "--switch", dest="switch",
help="Switch power on/off. Possible values: 1,on,0,off")
parser.add_option("-l", "--log", dest="log",
help="""Read power usage history from the log buffers of the Circle.
Argument should be 'cur' or 'current' if you want to read the log buffer that is currently being written.
It can also be a numeric log buffer index if you want to read an arbitrary log buffer.
""")
parser.add_option("-i", "--info", action="store_true", dest="info",
help="Perform the info request")
parser.add_option("-q", "--query", dest="query",
help="""Query data. Possible values are: time, pulses, last_logaddr, relay_state""")
parser.add_option("-v", "--verbose", dest="verbose",
help="""Verbose mode. Argument should be a number representing verboseness.
Currently all the debug is logged at the same level so it doesn't really matter which number you use.""")
options, args = parser.parse_args()
device = DEFAULT_SERIAL_PORT
if options.device:
device = options.device
if not options.mac:
print("you have to specify mac with -m")
parser.print_help()
sys.exit(-1)
if options.verbose:
plugwise.util.DEBUG_PROTOCOL = True
def print_pulse_counters(c):
try:
print("%d %d %d %d" % c.get_pulse_counters())
except ValueError:
print("Error: Failed to read pulse counters")
def handle_query(c, query):
if query == 'time':
print(c.get_clock().strftime("%H:%M:%S"))
elif query == 'pulses':
print_pulse_counters(c)
elif query in ('last_logaddr', 'relay_state'):
print(c.get_info()[query])
def handle_log(c, log_opt):
if log_opt in ('cur', 'current'):
log_idx = None
else:
try:
log_idx = int(log_opt)
except ValueError:
print("log option argument should be either number or string current")
return False
print("power usage log:")
for dt, watt, watt_hours in c.get_power_usage_history(log_idx):
if dt is None:
ts_str, watt, watt_hours = "N/A", "N/A", "N/A"
else:
ts_str = dt.strftime("%Y-%m-%d %H:%M")
watt = "%7.2f" % (watt,)
watt_hours = "%7.2f" % (watt_hours,)
print("\t%s %s W %s Wh" % (ts_str, watt, watt_hours))
return True
def set_time(c, time_opt):
if time_opt == 'sync':
set_ts = datetime.datetime.now()
else:
try:
set_ts = datetime.datetime.strptime(time_opt, "%Y-%m-%d %H:%M:%S")
except ValueError as reason:
print("Error: Could not parse the time value: %s" % (str(reason),))
sys.exit(-1)
c.set_clock(set_ts)
try:
device = Stick(device)
conf['mac'] = options.mac
c = Circle(options.mac, device, conf)
if options.time:
set_time(c, options.time)
if options.switch:
sw_direction = options.switch.lower()
if sw_direction in ('on', '1'):
c.switch_on()
elif sw_direction in ('off', '0'):
c.switch_off()
else:
print("Error: Unknown switch direction: "+sw_direction)
sys.exit(-1)
while 1:
if options.power:
try:
print("power usage: 1s=%7.2f W 8s=%7.2f W 1h=%7.2f W 1h=%7.2f W(production)" % c.get_power_usage())
except ValueError:
print("Error: Failed to read power usage")
if options.log != None:
handle_log(c, options.log)
if options.info:
print("info:")
pprint(c.get_info())
if options.counter:
print_pulse_counters(c)
if options.query:
handle_query(c, options.query)
if options.continuous is None:
break
else:
time.sleep(options.continuous)
except (TimeoutException, SerialException) as reason:
print("Error: %s" % (reason,))