-
Notifications
You must be signed in to change notification settings - Fork 14
/
example.py
58 lines (44 loc) · 1.56 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
import argparse
import asyncio
import logging
import sys
import time
from iotawattpy.iotawatt import Iotawatt
from httpx import AsyncClient
import httpx
LOOP = asyncio.get_event_loop()
logging.basicConfig(level="DEBUG")
class Tester:
def __init__(self, ip_addr, username, password):
self._ip_addr = ip_addr
self._username = username
self._password = password
async def run(self):
self.session = AsyncClient()
self.iotawatt = Iotawatt(
"iotawatt", self._ip_addr, self.session, self._username, self._password
)
try:
await self.iotawatt.connect()
except httpx.HTTPStatusError as err:
logging.error("%s", err)
await self.session.aclose()
return
while True:
logging.info("=============================================")
await self.iotawatt.update()
logging.info("=============================================")
time.sleep(5)
def main(argv):
my_parser = argparse.ArgumentParser(description="Run the IoTaWatt tester")
my_parser.add_argument(
"IPAddress", metavar="IP Address", type=str, help="IP Address of IoTaWatt"
)
my_parser.add_argument("-u", metavar="Username", type=str, help="Username")
my_parser.add_argument("-p", metavar="Password", type=str, help="Password")
args = my_parser.parse_args()
logging.info("Started")
test = Tester(args.IPAddress, args.u, args.p)
LOOP.run_until_complete(test.run())
if __name__ == "__main__":
main(sys.argv[1:])