pyh0n/pyhon/__main__.py

118 lines
4.2 KiB
Python
Raw Normal View History

2023-02-20 00:13:41 +05:30
#!/usr/bin/env python
import argparse
import asyncio
2023-03-21 05:40:41 +05:30
import json
2023-02-20 00:13:41 +05:30
import logging
import sys
from getpass import getpass
from pathlib import Path
2023-06-28 22:32:11 +05:30
from typing import Tuple, Dict, Any
2023-02-20 00:13:41 +05:30
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
2023-06-28 22:32:11 +05:30
from pyhon import Hon, HonAPI, diagnose, printer
2023-02-20 00:13:41 +05:30
_LOGGER = logging.getLogger(__name__)
2023-06-28 22:32:11 +05:30
def get_arguments() -> Dict[str, Any]:
2023-02-20 00:13:41 +05:30
"""Get parsed arguments."""
2023-03-05 01:57:10 +05:30
parser = argparse.ArgumentParser(description="pyhOn: Command Line Utility")
parser.add_argument("-u", "--user", help="user for haier hOn account")
parser.add_argument("-p", "--password", help="password for haier hOn account")
2023-03-08 05:28:25 +05:30
subparser = parser.add_subparsers(title="commands", metavar="COMMAND")
keys = subparser.add_parser("keys", help="print as key format")
keys.add_argument("keys", help="print as key format", action="store_true")
2023-03-09 02:23:53 +05:30
keys.add_argument("--all", help="print also full keys", action="store_true")
2023-06-25 20:59:04 +05:30
export = subparser.add_parser("export")
export.add_argument("export", help="export pyhon data", action="store_true")
export.add_argument("--zip", help="create zip archive", action="store_true")
export.add_argument("--anonymous", help="anonymize data", action="store_true")
export.add_argument("directory", nargs="?", default=Path().cwd())
2023-04-10 00:25:36 +05:30
translate = subparser.add_parser(
"translate", help="print available translation keys"
)
translate.add_argument(
"translate", help="language (de, en, fr...)", metavar="LANGUAGE"
)
2023-03-21 05:40:41 +05:30
translate.add_argument("--json", help="print as json", action="store_true")
2023-06-28 22:47:17 +05:30
parser.add_argument(
"-i", "--import", help="import pyhon data", nargs="?", default=Path().cwd()
)
2023-02-20 00:13:41 +05:30
return vars(parser.parse_args())
2023-06-28 22:32:11 +05:30
async def translate(language: str, json_output: bool = False) -> None:
2023-04-10 10:04:19 +05:30
async with HonAPI(anonymous=True) as hon:
2023-03-21 05:40:41 +05:30
keys = await hon.translation_keys(language)
if json_output:
print(json.dumps(keys, indent=4))
else:
2023-04-10 00:25:36 +05:30
clean_keys = (
json.dumps(keys)
.replace("\\n", "\\\\n")
.replace("\\\\r", "")
.replace("\\r", "")
)
2023-03-21 05:40:41 +05:30
keys = json.loads(clean_keys)
2023-06-28 22:32:11 +05:30
print(printer.pretty_print(keys))
2023-03-21 05:40:41 +05:30
2023-06-28 22:32:11 +05:30
def get_login_data(args: Dict[str, str]) -> Tuple[str, str]:
2023-06-25 20:59:04 +05:30
if not (user := args["user"]):
user = input("User for hOn account: ")
if not (password := args["password"]):
password = getpass("Password for hOn account: ")
return user, password
2023-06-28 22:32:11 +05:30
async def main() -> None:
2023-02-20 00:13:41 +05:30
args = get_arguments()
2023-03-21 05:40:41 +05:30
if language := args.get("translate"):
2023-06-28 22:32:11 +05:30
await translate(language, json_output=args.get("json", ""))
2023-03-21 05:40:41 +05:30
return
2023-06-28 22:47:17 +05:30
async with Hon(
*get_login_data(args), test_data_path=Path(args.get("import", ""))
) as hon:
2023-04-10 00:20:28 +05:30
for device in hon.appliances:
2023-06-25 20:59:04 +05:30
if args.get("export"):
anonymous = args.get("anonymous", False)
2023-06-28 22:32:11 +05:30
path = Path(args.get("directory", "."))
2023-06-25 20:59:04 +05:30
if not args.get("zip"):
for file in await diagnose.appliance_data(device, path, anonymous):
print(f"Created {file}")
else:
2023-06-28 22:32:11 +05:30
archive = await diagnose.zip_archive(device, path, anonymous)
print(f"Created {archive}")
2023-06-25 20:59:04 +05:30
continue
2023-03-08 05:28:25 +05:30
print("=" * 10, device.appliance_type, "-", device.nick_name, "=" * 10)
if args.get("keys"):
2023-03-09 02:23:53 +05:30
data = device.data.copy()
attr = "get" if args.get("all") else "pop"
2023-04-12 01:44:36 +05:30
print(
2023-06-28 22:32:11 +05:30
printer.key_print(
2023-04-12 01:44:36 +05:30
data["attributes"].__getattribute__(attr)("parameters")
)
)
2023-06-28 22:32:11 +05:30
print(printer.key_print(data.__getattribute__(attr)("appliance")))
print(printer.key_print(data))
2023-04-12 01:44:36 +05:30
print(
2023-06-28 22:32:11 +05:30
printer.pretty_print(
printer.create_command(device.commands, concat=True)
2023-04-12 01:44:36 +05:30
)
)
2023-03-08 05:28:25 +05:30
else:
2023-06-25 20:59:04 +05:30
print(diagnose.yaml_export(device))
2023-02-20 00:13:41 +05:30
2023-06-28 22:32:11 +05:30
def start() -> None:
2023-02-20 00:13:41 +05:30
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Aborted.")
2023-04-10 00:25:36 +05:30
if __name__ == "__main__":
2023-02-20 00:13:41 +05:30
start()