get translation keys

This commit is contained in:
Andre Basche 2023-03-21 01:10:41 +01:00
parent cd5a4c345e
commit 5ec1a760f6
3 changed files with 40 additions and 8 deletions

View file

@ -83,11 +83,26 @@ async with HonConnection(USER, PASSWORD) as hon:
setting.value = setting.min + setting.step 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 ## Tested devices
- Haier Washing Machine HW90 - Haier Washing Machine HW90
_Unfortunately I don't have any more Haier appliances..._
## Usage example ## Usage example
This library is used for the custom [HomeAssistant Integration "Haier hOn"](https://github.com/Andre0512/hOn). This library is used for the custom [HomeAssistant Integration "Haier hOn"](https://github.com/Andre0512/hOn).

View file

@ -1,9 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import asyncio import asyncio
import json
import logging import logging
import sys import sys
import time
from getpass import getpass from getpass import getpass
from pathlib import Path from pathlib import Path
from pprint import pprint from pprint import pprint
@ -25,6 +25,9 @@ def get_arguments():
keys = subparser.add_parser("keys", help="print as key format") 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("keys", help="print as key format", action="store_true")
keys.add_argument("--all", help="print also full keys", 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()) return vars(parser.parse_args())
@ -81,8 +84,22 @@ def create_command(commands, concat=False):
return result 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(): async def main():
args = get_arguments() args = get_arguments()
if language := args.get("translate"):
await translate(language, json_output=args.get("json"))
return
if not (user := args["user"]): if not (user := args["user"]):
user = input("User for hOn account: ") user = input("User for hOn account: ")
if not (password := args["password"]): if not (password := args["password"]):

View file

@ -3,7 +3,6 @@ import json
import logging import logging
import secrets import secrets
from datetime import datetime from datetime import datetime
from pprint import pprint
from typing import List from typing import List
import aiohttp as aiohttp import aiohttp as aiohttp
@ -16,7 +15,7 @@ _LOGGER = logging.getLogger()
class HonConnection: class HonConnection:
def __init__(self, email, password, session=None) -> None: def __init__(self, email="", password="", session=None) -> None:
super().__init__() super().__init__()
self._email = email self._email = email
self._password = password self._password = password
@ -27,6 +26,7 @@ class HonConnection:
async def __aenter__(self): async def __aenter__(self):
self._session = aiohttp.ClientSession() self._session = aiohttp.ClientSession()
if self._email and self._password:
await self.setup() await self.setup()
return self return self
@ -128,9 +128,9 @@ class HonConnection:
return data return data
return {} return {}
async def translation_keys(self): async def translation_keys(self, language="en"):
headers = {"x-api-key": const.API_KEY, "content-type": "application/json"} 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"): if url := config.get("language", {}).get("jsonPath"):
async with self._session.get(url, headers=headers) as response: async with self._session.get(url, headers=headers) as response:
if result := await response.json(): if result := await response.json():