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
```
## 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).

View file

@ -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"]):

View file

@ -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,6 +26,7 @@ class HonConnection:
async def __aenter__(self):
self._session = aiohttp.ClientSession()
if self._email and self._password:
await self.setup()
return self
@ -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():