From db156a01bce36bb6ef0f4d4b22e931bb5bee6835 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Thu, 23 May 2024 22:58:56 +0000 Subject: [PATCH 1/2] chore: add library_test.py --- .gitignore | 5 +- library_test.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 library_test.py diff --git a/.gitignore b/.gitignore index d3c6fa13..2334a386 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,7 @@ dmypy.json reports/ # IDE -.idea/ \ No newline at end of file +.idea/ + +# Optional config file for library_test.py script +library_test.json diff --git a/library_test.py b/library_test.py new file mode 100644 index 00000000..8c7318d1 --- /dev/null +++ b/library_test.py @@ -0,0 +1,120 @@ +"""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 midealocal.cloud import get_midea_cloud +from midealocal.devices import device_selector +from midealocal.discover import discover + + +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") + 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, 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: + print("You have to specify all parameters: username, password and Cloud name") + parser.print_help() + sys.exit(1) + + print("-" * 20) + print("Starting network discovery...") + devices = discover() + print("-" * 20) + print("Devices: ", devices) + first_device = list(devices.values())[0] + print("-" * 20) + print("First device: ", first_device) + # The device type is in hexadecimal as in midealocal/devices/TYPE + type_code = hex(first_device["type"])[2:] + print("-" * 20) + print("First device type: ", type_code) + + session = aiohttp.ClientSession() + cloud = get_midea_cloud( + session=session, + cloud_name=args.cloud_name, + account=args.username, + password=args.password, + ) + cloud_keys = await cloud.get_keys(first_device["device_id"]) + print("-" * 20) + print("Fist device Cloud info: ", cloud_keys) + + token = "" + key = "" + for v in cloud_keys.values(): + token = v["token"] + key = v["key"] + + print("-" * 20) + print("Fist device Cloud token: ", token) + print("Fist device Cloud key: ", 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, + protocol=first_device["protocol"], + model=first_device["model"], + subtype=0, + customize="", + ) + + # Connect and authenticate + ac.connect() + ac.authenticate() + + # Getting the attributes + print("-" * 20) + print("First device attributes: ", ac.attributes) + + # Close session + session.close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + logging.getLogger("asyncio").setLevel(logging.INFO) + logging.getLogger("charset_normalizer").setLevel(logging.INFO) + asyncio.run(main()) From e4b5f013074955b9205799c6172bf90122781601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mind=C3=AAllo=20de=20Andrade?= Date: Fri, 24 May 2024 14:52:27 -0300 Subject: [PATCH 2/2] Apply suggestions from code review --- library_test.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/library_test.py b/library_test.py index 8c7318d1..c5a84503 100644 --- a/library_test.py +++ b/library_test.py @@ -9,7 +9,7 @@ import aiohttp -from midealocal.cloud import get_midea_cloud +from midealocal.cloud import get_midea_cloud, clouds from midealocal.devices import device_selector from midealocal.discover import discover @@ -24,7 +24,12 @@ def get_arguments() -> tuple[ArgumentParser, Namespace]: 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") + parser.add_argument( + "--cloud_name", + "-cn", + type=str, + help="Set Cloud name, options are: " + ", ".join(clouds.keys()), + ) parser.add_argument( "--configfile", "-cf", @@ -32,12 +37,13 @@ def get_arguments() -> tuple[ArgumentParser, Namespace]: 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.open(arguments.configfile, encoding="utf-8") as f: + with Path(arguments.configfile).open(encoding="utf-8") as f: arguments = parser.parse_args(namespace=Namespace(**json.load(f))) return parser, arguments @@ -54,7 +60,7 @@ async def main() -> None: print("-" * 20) print("Starting network discovery...") - devices = discover() + devices = discover(ip_address=args.ip) print("-" * 20) print("Devices: ", devices) first_device = list(devices.values())[0] @@ -103,14 +109,13 @@ async def main() -> None: # Connect and authenticate ac.connect() - ac.authenticate() # Getting the attributes print("-" * 20) print("First device attributes: ", ac.attributes) # Close session - session.close() + await session.close() if __name__ == "__main__":