-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
library_test.py
136 lines (119 loc) · 3.86 KB
/
library_test.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
131
132
133
134
135
136
"""Test script for aiovodafone library."""
import asyncio
import json
import logging
import sys
from argparse import ArgumentParser, Namespace
from pathlib import Path
import aiohttp
from aiovodafone.api import (
VodafoneStationCommonApi,
VodafoneStationSercommApi,
VodafoneStationTechnicolorApi,
)
from aiovodafone.const import DeviceType
from aiovodafone.exceptions import (
AlreadyLogged,
CannotAuthenticate,
CannotConnect,
GenericLoginError,
ModelNotSupported,
VodafoneError,
)
def get_arguments() -> tuple[ArgumentParser, Namespace]:
"""Get parsed passed in arguments."""
parser = ArgumentParser(description="aiovodafone library test")
parser.add_argument(
"--router",
"-r",
type=str,
default="192.168.1.1",
help="Set router IP address",
)
parser.add_argument(
"--username",
"-u",
type=str,
default="vodafone",
help="Set router username",
)
parser.add_argument("--password", "-p", type=str, help="Set router password")
parser.add_argument(
"--configfile",
"-cf",
type=str,
help="Load options from JSON config file. \
Command line options override those in the file.",
)
arguments = parser.parse_args()
# Re-parse the command line
# taking the options in the optional JSON file as a basis
if arguments.configfile and Path(arguments.configfile).exists():
with Path.open(arguments.configfile) as f:
arguments = parser.parse_args(namespace=Namespace(**json.load(f)))
return parser, arguments
async def main() -> None:
"""Run main."""
parser, args = get_arguments()
if not args.password:
print("You have to specify a password")
parser.print_help()
sys.exit(1)
print("Determining device type")
async with aiohttp.ClientSession() as session:
device_type = await VodafoneStationCommonApi.get_device_type(
args.router,
session,
)
print(device_type)
print("-" * 20)
api: VodafoneStationCommonApi
if device_type == DeviceType.TECHNICOLOR:
api = VodafoneStationTechnicolorApi(args.router, args.username, args.password)
elif device_type == DeviceType.SERCOMM:
api = VodafoneStationSercommApi(args.router, args.username, args.password)
else:
print("The device is not a supported Vodafone Station.")
sys.exit(1)
try:
try:
await api.login()
except ModelNotSupported:
print("Model is not supported yet for router", api.host)
raise
except CannotAuthenticate:
print("Cannot authenticate to router", api.host)
raise
except CannotConnect:
print("Cannot connect to router", api.host)
raise
except AlreadyLogged:
print("Only one user at a time can connect to router", api.host)
raise
except GenericLoginError:
print("Unable to login to router", api.host)
raise
except VodafoneError:
await api.close()
sys.exit(1)
print("Logged-in.")
print("-" * 20)
devices = await api.get_devices_data()
print("Devices:", devices)
print("-" * 20)
data = await api.get_sensor_data()
print("Data:", data)
print("-" * 20)
print("Serial #:", data["sys_serial_number"])
print("Firmware:", data["sys_firmware_version"])
print("Hardware:", data["sys_hardware_version"])
print("Uptime :", api.convert_uptime(data["sys_uptime"]))
print("-" * 20)
print("Logout & close session")
await api.logout()
await api.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("asyncio").setLevel(logging.INFO)
logging.getLogger("charset_normalizer").setLevel(logging.INFO)
asyncio.run(main())