From 6dbd8dadd44f4fd99a3e0887b61392226d00d686 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Sat, 25 May 2024 02:54:52 +0900 Subject: [PATCH] chore: add library_test.py (#15) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: add library_test.py * Apply suggestions from code review --------- Co-authored-by: Lucas MindĂȘllo de Andrade --- .gitignore | 5 +- library_test.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 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..c5a84503 --- /dev/null +++ b/library_test.py @@ -0,0 +1,125 @@ +"""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, clouds +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, options are: " + ", ".join(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: + 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(ip_address=args.ip) + 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() + + # Getting the attributes + print("-" * 20) + print("First device attributes: ", ac.attributes) + + # Close session + await 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())