-
Notifications
You must be signed in to change notification settings - Fork 0
/
printhistory.py
executable file
·57 lines (46 loc) · 2.07 KB
/
printhistory.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
#!/usr/bin/env python3
"""Get the Zappi history and import into InfluxDB.
This script will query the first zappi found to obtain a number of days history at the hourly level
It will then import a summary of this information into an influxdb database.
"""
import argparse
import logging
from datetime import datetime
import myenergi
from utilities import get_env, get_logger
def get_options():
"""Get the required options using argparse or from a dotenv file."""
env = get_env()
parser = argparse.ArgumentParser(description='Gets history from the Zappi and imports to influxdb.')
if "myenergi_serial" not in env:
parser.add_argument('-s', '--serial', required=True, help='myenergi hub serial number')
if "myenergi_password" not in env:
parser.add_argument('-p', '--password', required=True, help='myenergi password')
parser.add_argument('-t', '--start', required=False, type=int, default=1, help='starting number of days ago')
parser.add_argument('-e', '--end', required=False, type=int, default=4, help='ending number of days ago')
args = parser.parse_args()
if "myenergi_serial" in env:
args.serial = env['myenergi_serial']
if "myenergi_password" in env:
args.password = env['myenergi_password']
return args
def main():
"""Get the Zappi history and print it."""
args = get_options()
# Set the logging level for the myenergi api client
logging.getLogger('myenergi.api').setLevel(logging.INFO)
# Setup the local logger
logger = get_logger(destination = "stdout")
with myenergi.API(args.serial, args.password) as mye:
zappiserials = mye.get_serials(myenergi.const.MyenergiType.ZAPPI)
for serial in zappiserials:
logger.info("querying Zappi: %s", serial)
thetime = datetime.now()
datestring = thetime.strftime("%Y-%m-%d")
days = 7
history = mye.get_zappi_daily_total(serial, datestring, days)
for entry in history.history_data:
print(entry)
exit(0)
if __name__ == "__main__":
main()