pyhOn/pyhon/__main__.py

114 lines
4.2 KiB
Python
Raw Normal View History

2023-02-19 19:43:41 +01:00
#!/usr/bin/env python
import argparse
import asyncio
2023-03-21 01:10:41 +01:00
import json
2023-02-19 19:43:41 +01:00
import logging
import sys
from getpass import getpass
from pathlib import Path
2023-06-28 19:02:11 +02:00
from typing import Tuple, Dict, Any
2023-02-19 19:43:41 +01:00
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
2023-07-16 05:53:23 +02:00
# pylint: disable=wrong-import-position
2023-06-28 19:02:11 +02:00
from pyhon import Hon, HonAPI, diagnose, printer
2023-02-19 19:43:41 +01:00
_LOGGER = logging.getLogger(__name__)
2023-06-28 19:02:11 +02:00
def get_arguments() -> Dict[str, Any]:
2023-02-19 19:43:41 +01:00
"""Get parsed arguments."""
2023-03-04 21:27:10 +01:00
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 00:58:25 +01:00
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-08 21:53:53 +01:00
keys.add_argument("--all", help="print also full keys", action="store_true")
2023-06-25 17:29:04 +02:00
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-07-12 19:36:32 +02:00
translation = subparser.add_parser(
2023-04-09 20:55:36 +02:00
"translate", help="print available translation keys"
)
2023-07-12 19:36:32 +02:00
translation.add_argument(
2023-04-09 20:55:36 +02:00
"translate", help="language (de, en, fr...)", metavar="LANGUAGE"
)
2023-07-12 19:36:32 +02:00
translation.add_argument("--json", help="print as json", action="store_true")
2024-02-09 20:33:50 +01:00
parser.add_argument("-i", "--import", help="import pyhon data", nargs="?")
2023-02-19 19:43:41 +01:00
return vars(parser.parse_args())
2023-06-28 19:02:11 +02:00
async def translate(language: str, json_output: bool = False) -> None:
2023-04-10 06:34:19 +02:00
async with HonAPI(anonymous=True) as hon:
2023-03-21 01:10:41 +01:00
keys = await hon.translation_keys(language)
if json_output:
print(json.dumps(keys, indent=4))
else:
2023-04-09 20:55:36 +02:00
clean_keys = (
json.dumps(keys)
.replace("\\n", "\\\\n")
.replace("\\\\r", "")
.replace("\\r", "")
)
2023-03-21 01:10:41 +01:00
keys = json.loads(clean_keys)
2023-06-28 19:02:11 +02:00
print(printer.pretty_print(keys))
2023-03-21 01:10:41 +01:00
2023-06-28 19:02:11 +02:00
def get_login_data(args: Dict[str, str]) -> Tuple[str, str]:
2023-06-25 17:29:04 +02:00
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 19:02:11 +02:00
async def main() -> None:
2023-02-19 19:43:41 +01:00
args = get_arguments()
2023-03-21 01:10:41 +01:00
if language := args.get("translate"):
2023-06-28 19:02:11 +02:00
await translate(language, json_output=args.get("json", ""))
2023-03-21 01:10:41 +01:00
return
2024-02-09 20:33:50 +01:00
test_data_path = Path(path) if (path := args.get("import", "")) else None
async with Hon(*get_login_data(args), test_data_path=test_data_path) as hon:
2023-04-09 20:50:28 +02:00
for device in hon.appliances:
2023-06-25 17:29:04 +02:00
if args.get("export"):
anonymous = args.get("anonymous", False)
2023-06-28 19:02:11 +02:00
path = Path(args.get("directory", "."))
2023-06-25 17:29:04 +02:00
if not args.get("zip"):
for file in await diagnose.appliance_data(device, path, anonymous):
print(f"Created {file}")
else:
2023-06-28 19:02:11 +02:00
archive = await diagnose.zip_archive(device, path, anonymous)
print(f"Created {archive}")
2023-06-25 17:29:04 +02:00
continue
2023-03-08 00:58:25 +01:00
print("=" * 10, device.appliance_type, "-", device.nick_name, "=" * 10)
if args.get("keys"):
2023-03-08 21:53:53 +01:00
data = device.data.copy()
attr = "get" if args.get("all") else "pop"
2023-04-11 22:14:36 +02:00
print(
2023-07-16 05:53:23 +02:00
printer.key_print(getattr(data["attributes"], attr)("parameters"))
2023-04-11 22:14:36 +02:00
)
2023-07-16 05:53:23 +02:00
print(printer.key_print(getattr(data, attr)("appliance")))
2023-06-28 19:02:11 +02:00
print(printer.key_print(data))
2023-04-11 22:14:36 +02:00
print(
2023-06-28 19:02:11 +02:00
printer.pretty_print(
2023-07-01 14:31:37 +02:00
printer.create_commands(device.commands, concat=True)
2023-04-11 22:14:36 +02:00
)
)
2023-03-08 00:58:25 +01:00
else:
2023-06-25 17:29:04 +02:00
print(diagnose.yaml_export(device))
2023-02-19 19:43:41 +01:00
2023-06-28 19:02:11 +02:00
def start() -> None:
2023-02-19 19:43:41 +01:00
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Aborted.")
2023-04-09 20:55:36 +02:00
if __name__ == "__main__":
2023-02-19 19:43:41 +01:00
start()