2023-02-19 18:43:41 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse
|
|
|
|
import asyncio
|
2023-03-21 00:10:41 +00:00
|
|
|
import json
|
2023-02-19 18:43:41 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from getpass import getpass
|
|
|
|
from pathlib import Path
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
2023-04-09 16:13:50 +00:00
|
|
|
from pyhon import HonAPI
|
2023-02-19 18:43:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_arguments():
|
|
|
|
"""Get parsed arguments."""
|
2023-03-04 20:27:10 +00: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-07 23:58:25 +00: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 20:53:53 +00:00
|
|
|
keys.add_argument("--all", help="print also full keys", action="store_true")
|
2023-03-21 00:10:41 +00:00
|
|
|
translate = subparser.add_parser("translate", help="print available translation keys")
|
|
|
|
translate.add_argument("translate", help="language (de, en, fr...)", metavar="LANGUAGE")
|
|
|
|
translate.add_argument("--json", help="print as json", action="store_true")
|
2023-02-19 18:43:41 +00:00
|
|
|
return vars(parser.parse_args())
|
|
|
|
|
|
|
|
|
2023-03-06 18:45:46 +00:00
|
|
|
# yaml.dump() would be done the same, but needs an additional dependency...
|
2023-03-04 20:27:10 +00:00
|
|
|
def pretty_print(data, key="", intend=0, is_list=False):
|
|
|
|
if type(data) is list:
|
|
|
|
if key:
|
|
|
|
print(f"{' ' * intend}{'- ' if is_list else ''}{key}:")
|
|
|
|
intend += 1
|
|
|
|
for i, value in enumerate(data):
|
|
|
|
pretty_print(value, intend=intend, is_list=True)
|
|
|
|
elif type(data) is dict:
|
|
|
|
if key:
|
|
|
|
print(f"{' ' * intend}{'- ' if is_list else ''}{key}:")
|
|
|
|
intend += 1
|
|
|
|
for i, (key, value) in enumerate(sorted(data.items())):
|
|
|
|
if is_list and not i:
|
|
|
|
pretty_print(value, key=key, intend=intend, is_list=True)
|
|
|
|
elif is_list:
|
|
|
|
pretty_print(value, key=key, intend=intend + 1)
|
|
|
|
else:
|
|
|
|
pretty_print(value, key=key, intend=intend)
|
|
|
|
else:
|
|
|
|
print(f"{' ' * intend}{'- ' if is_list else ''}{key}{': ' if key else ''}{data}")
|
|
|
|
|
|
|
|
|
2023-03-07 23:58:25 +00:00
|
|
|
def key_print(data, key="", start=True):
|
|
|
|
if type(data) is list:
|
|
|
|
for i, value in enumerate(data):
|
|
|
|
key_print(value, key=f"{key}.{i}", start=False)
|
|
|
|
elif type(data) is dict:
|
|
|
|
for k, value in sorted(data.items()):
|
|
|
|
key_print(value, key=k if start else f"{key}.{k}", start=False)
|
|
|
|
else:
|
|
|
|
print(f"{key}: {data}")
|
|
|
|
|
|
|
|
|
|
|
|
def create_command(commands, concat=False):
|
2023-03-06 18:45:46 +00:00
|
|
|
result = {}
|
|
|
|
for name, command in commands.items():
|
2023-03-07 23:58:25 +00:00
|
|
|
if not concat:
|
|
|
|
result[name] = {}
|
2023-03-06 18:45:46 +00:00
|
|
|
for parameter, data in command.parameters.items():
|
|
|
|
if data.typology == "enum":
|
2023-03-07 23:58:25 +00:00
|
|
|
value = data.values
|
|
|
|
elif data.typology == "range":
|
|
|
|
value = {"min": data.min, "max": data.max, "step": data.step}
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
if not concat:
|
|
|
|
result[name][parameter] = value
|
|
|
|
else:
|
|
|
|
result[f"{name}.{parameter}"] = value
|
2023-03-06 18:45:46 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-03-21 00:10:41 +00:00
|
|
|
async def translate(language, json_output=False):
|
2023-04-09 16:13:50 +00:00
|
|
|
async with HonAPI() as hon:
|
2023-03-21 00:10:41 +00:00
|
|
|
keys = await hon.translation_keys(language)
|
|
|
|
if json_output:
|
|
|
|
print(json.dumps(keys, indent=4))
|
|
|
|
else:
|
|
|
|
clean_keys = json.dumps(keys).replace("\\n", "\\\\n").replace("\\\\r", "").replace("\\r", "")
|
|
|
|
keys = json.loads(clean_keys)
|
|
|
|
pretty_print(keys)
|
|
|
|
|
|
|
|
|
2023-02-19 18:43:41 +00:00
|
|
|
async def main():
|
|
|
|
args = get_arguments()
|
2023-03-21 00:10:41 +00:00
|
|
|
if language := args.get("translate"):
|
|
|
|
await translate(language, json_output=args.get("json"))
|
|
|
|
return
|
2023-02-19 18:43:41 +00:00
|
|
|
if not (user := args["user"]):
|
2023-03-04 20:27:10 +00:00
|
|
|
user = input("User for hOn account: ")
|
2023-02-19 18:43:41 +00:00
|
|
|
if not (password := args["password"]):
|
2023-03-04 20:27:10 +00:00
|
|
|
password = getpass("Password for hOn account: ")
|
2023-04-09 16:13:50 +00:00
|
|
|
async with HonAPI(user, password) as hon:
|
2023-02-19 18:43:41 +00:00
|
|
|
for device in hon.devices:
|
2023-03-07 23:58:25 +00:00
|
|
|
print("=" * 10, device.appliance_type, "-", device.nick_name, "=" * 10)
|
|
|
|
if args.get("keys"):
|
2023-03-08 20:53:53 +00:00
|
|
|
data = device.data.copy()
|
|
|
|
attr = "get" if args.get("all") else "pop"
|
|
|
|
key_print(data["attributes"].__getattribute__(attr)("parameters"))
|
|
|
|
key_print(data.__getattribute__(attr)("appliance"))
|
|
|
|
key_print(data)
|
2023-03-07 23:58:25 +00:00
|
|
|
pretty_print(create_command(device.commands, concat=True))
|
|
|
|
else:
|
|
|
|
pretty_print({"data": device.data})
|
|
|
|
pretty_print({"settings": create_command(device.commands)})
|
2023-02-19 18:43:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
try:
|
|
|
|
asyncio.run(main())
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Aborted.")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
start()
|