From 5ec1a760f632f36876e913f35a2751e90d3632ff Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Tue, 21 Mar 2023 01:10:41 +0100 Subject: [PATCH] get translation keys --- README.md | 19 +++++++++++++++++-- pyhon/__main__.py | 19 ++++++++++++++++++- pyhon/api.py | 10 +++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1867c8e..46cfb42 100644 --- a/README.md +++ b/README.md @@ -83,11 +83,26 @@ async with HonConnection(USER, PASSWORD) as hon: setting.value = setting.min + setting.step ``` +## Translation +To get the translation of some keys like programs, you can use the translation command to see all of hOn's available translations +```commandline +$ pyhOn translate es +AC: + APPLIANCE_RENAME: + CONTENT_CHOOSE_NAME: Antes de continuar, debes elegir un nombre... + DEFAULT_NAME: Aire acondicionado + TITLE_CHOOSE_NAME: ¡Elije un nombre para tu aire acondicionado! + TITLE_SAVE_NAME: Para cambiar el nombre de tu aparato: +... +``` +This generates a huge output. It is recommended to pipe this into a file +```commandline +$ pyhOn translate fr > hon_fr.yaml +$ pyhOn translate en --json > hon_en.json +``` ## Tested devices - Haier Washing Machine HW90 -_Unfortunately I don't have any more Haier appliances..._ - ## Usage example This library is used for the custom [HomeAssistant Integration "Haier hOn"](https://github.com/Andre0512/hOn). diff --git a/pyhon/__main__.py b/pyhon/__main__.py index 2c60c90..d68babc 100755 --- a/pyhon/__main__.py +++ b/pyhon/__main__.py @@ -1,9 +1,9 @@ #!/usr/bin/env python import argparse import asyncio +import json import logging import sys -import time from getpass import getpass from pathlib import Path from pprint import pprint @@ -25,6 +25,9 @@ def get_arguments(): keys = subparser.add_parser("keys", help="print as key format") keys.add_argument("keys", help="print as key format", action="store_true") keys.add_argument("--all", help="print also full keys", action="store_true") + 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") return vars(parser.parse_args()) @@ -81,8 +84,22 @@ def create_command(commands, concat=False): return result +async def translate(language, json_output=False): + async with HonConnection() as hon: + 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) + + async def main(): args = get_arguments() + if language := args.get("translate"): + await translate(language, json_output=args.get("json")) + return if not (user := args["user"]): user = input("User for hOn account: ") if not (password := args["password"]): diff --git a/pyhon/api.py b/pyhon/api.py index a936c95..4a72274 100644 --- a/pyhon/api.py +++ b/pyhon/api.py @@ -3,7 +3,6 @@ import json import logging import secrets from datetime import datetime -from pprint import pprint from typing import List import aiohttp as aiohttp @@ -16,7 +15,7 @@ _LOGGER = logging.getLogger() class HonConnection: - def __init__(self, email, password, session=None) -> None: + def __init__(self, email="", password="", session=None) -> None: super().__init__() self._email = email self._password = password @@ -27,7 +26,8 @@ class HonConnection: async def __aenter__(self): self._session = aiohttp.ClientSession() - await self.setup() + if self._email and self._password: + await self.setup() return self async def __aexit__(self, exc_type, exc_val, exc_tb): @@ -128,9 +128,9 @@ class HonConnection: return data return {} - async def translation_keys(self): + async def translation_keys(self, language="en"): headers = {"x-api-key": const.API_KEY, "content-type": "application/json"} - config = await self.app_config() + config = await self.app_config(language=language) if url := config.get("language", {}).get("jsonPath"): async with self._session.get(url, headers=headers) as response: if result := await response.json():