-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
67 lines (52 loc) · 1.73 KB
/
example.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
#!/usr/bin/env python
"""Basic usage example and testing of pysenechome."""
import argparse
import asyncio
import logging
import signal
import sys
import aiohttp
import pysenechome
# This module will work with Python 3.5+
# Python 3.4+ "@asyncio.coroutine" decorator
# Python 3.5+ uses "async def f()" syntax
_LOGGER = logging.getLogger(__name__)
VAR = {}
def print_table(sensors):
print()
for sen in sensors:
if sen.value is None:
print("{:>30}".format(sen.name))
else:
print("{:>30}{:>15} {}".format(sen.name, str(sen.value), sen.unit))
async def main_loop(loop, ip): # pylint: disable=invalid-name
"""Main loop."""
async with aiohttp.ClientSession(loop=loop) as session:
VAR["senec"] = pysenechome.SENEC(session, ip)
VAR["running"] = True
cnt = 5
while VAR.get("running"):
sensors = await VAR["senec"].read()
print_table(sensors)
cnt -= 1
if cnt == 0:
break
await asyncio.sleep(1)
def main():
"""Main example."""
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
parser = argparse.ArgumentParser(description="Test the SENEC web API library.")
parser.add_argument("ip", type=str, help="IP address of the battery system")
args = parser.parse_args()
loop = asyncio.get_event_loop()
def _shutdown(*_):
VAR["running"] = False
# asyncio.ensure_future(senec.close_session(), loop=loop)
signal.signal(signal.SIGINT, _shutdown)
# loop.add_signal_handler(signal.SIGINT, shutdown)
# signal.signal(signal.SIGINT, signal.SIG_DFL)
loop.run_until_complete(
main_loop(loop, ip=args.ip)
)
if __name__ == "__main__":
main()