-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
library_test.py
152 lines (129 loc) · 4.47 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Test script for midea-local library."""
import asyncio
import json
import logging
import sys
from argparse import ArgumentParser, Namespace
from pathlib import Path
import aiohttp
from colorlog import ColoredFormatter
from midealocal.cloud import SUPPORTED_CLOUDS, get_midea_cloud
from midealocal.devices import device_selector
from midealocal.discover import discover
_LOGGER = logging.getLogger("library_test")
def get_arguments() -> tuple[ArgumentParser, Namespace]:
"""Get parsed passed in arguments."""
parser = ArgumentParser(description="midea-local library test")
parser.add_argument(
"--username",
"-u",
type=str,
help="Set Cloud username",
)
parser.add_argument("--password", "-p", type=str, help="Set Cloud password")
parser.add_argument(
"--cloud_name",
"-cn",
type=str,
help="Set Cloud name, options are: " + ", ".join(SUPPORTED_CLOUDS.keys()),
)
parser.add_argument(
"--configfile",
"-cf",
type=str,
help="Load options from JSON config file. \
Command line options override those in the file.",
)
parser.add_argument("--ip", "-i", type=str, help="Device or broadcast IP Address.")
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(arguments.configfile).open(encoding="utf-8") 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 or not args.username or not args.cloud_name:
_LOGGER.error("All parameters needed: username, password and Cloud name")
parser.print_help()
sys.exit(1)
_LOGGER.info("Starting network discovery...")
devices = discover(ip_address=args.ip)
_LOGGER.info("Devices: %s", devices)
first_device = next(iter(devices.values()))
_LOGGER.info("First device: %s", first_device)
# The device type is in hexadecimal as in midealocal/devices/TYPE
type_code = hex(first_device["type"])[2:]
_LOGGER.info("First device type: %s", type_code)
session = aiohttp.ClientSession()
cloud = get_midea_cloud(
session=session,
cloud_name=args.cloud_name,
account=args.username,
password=args.password,
)
cloud_keys = {}
if cloud:
if not await cloud.login():
msg = f"Cannot login into device {first_device['device_id']} \
[{first_device['ip_address']}]"
_LOGGER.error(msg)
await session.close()
sys.exit(2)
cloud_keys = await cloud.get_cloud_keys(first_device["device_id"])
_LOGGER.info("Fist device Cloud info: %s", cloud_keys)
token = ""
key = ""
for v in cloud_keys.values():
token = v["token"]
key = v["key"]
_LOGGER.info("Fist device Cloud token: %s", token)
_LOGGER.info("Fist device Cloud key: %s", key)
# Select the device
ac = device_selector(
name=type_code,
device_id=first_device["device_id"],
device_type=first_device["type"],
ip_address=first_device["ip_address"],
port=first_device["port"],
token=token,
key=key,
device_protocol=first_device["protocol"],
model=first_device["model"],
subtype=0,
customize="",
)
# Connect and authenticate
ac.connect()
# Getting the attributes
_LOGGER.info("First device attributes: %s", ac.attributes)
# Close session
await session.close()
def set_logging() -> None:
"""Set logging levels."""
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("asyncio").setLevel(logging.INFO)
logging.getLogger("charset_normalizer").setLevel(logging.INFO)
fmt = (
"%(asctime)s.%(msecs)03d %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
)
colorfmt = f"%(log_color)s{fmt}%(reset)s"
logging.getLogger().handlers[0].setFormatter(
ColoredFormatter(
colorfmt,
datefmt="%Y-%m-%d %H:%M:%S",
reset=True,
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red",
},
),
)
if __name__ == "__main__":
set_logging()
asyncio.run(main())