From 83bb99a34e61864b0bbbd903fe82f051f6d0dd6b Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sun, 19 Feb 2023 19:43:41 +0100 Subject: [PATCH] Create cmd tool, full parameter name --- pyhon/__main__.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ pyhon/auth.py | 2 +- pyhon/commands.py | 4 +++- pyhon/device.py | 13 +++++++++-- pyhon/parameter.py | 7 ++++-- setup.py | 9 ++++++-- 6 files changed, 82 insertions(+), 8 deletions(-) create mode 100755 pyhon/__main__.py diff --git a/pyhon/__main__.py b/pyhon/__main__.py new file mode 100755 index 0000000..f0e5733 --- /dev/null +++ b/pyhon/__main__.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +import argparse +import asyncio +import logging +import sys +import time +from getpass import getpass +from pathlib import Path +from pprint import pprint + +if __name__ == "__main__": + sys.path.insert(0, str(Path(__file__).parent.parent)) + +from pyhon import HonConnection + +_LOGGER = logging.getLogger(__name__) + + +def get_arguments(): + """Get parsed arguments.""" + parser = argparse.ArgumentParser(description="hOn: Command Line Utility") + parser.add_argument("-u", "--user", help="user of haier hOn account") + parser.add_argument("-p", "--password", help="password of haier hOn account") + return vars(parser.parse_args()) + + +async def main(): + args = get_arguments() + if not (user := args["user"]): + user = input("User of hOn account: ") + if not (password := args["password"]): + password = getpass("Password of hOn account: ") + async with HonConnection(user, password) as hon: + await hon.setup() + for device in hon.devices: + print(10 * "=", device.nick_name, 10 * "=") + print(10 * "-", "attributes", 10 * "-") + pprint(device.attributes) + print(10 * "-", "statistics", 10 * "-") + pprint(device.statistics) + print(10 * "-", "commands", 10 * "-") + pprint(device.parameters) + print(10 * "-", "settings", 10 * "-") + pprint(device.settings) + + +def start(): + try: + asyncio.run(main()) + except KeyboardInterrupt: + print("Aborted.") + + +if __name__ == '__main__': + start() diff --git a/pyhon/auth.py b/pyhon/auth.py index 333df2a..33c89bd 100644 --- a/pyhon/auth.py +++ b/pyhon/auth.py @@ -69,7 +69,7 @@ class HonAuth: except json.JSONDecodeError: if framework := re.findall('clientOutOfSync.*?Expected: ([\\w-]+?) Actual: (.*?)"', text): self._framework, actual = framework[0] - _LOGGER.warning('Framework update from "%s" to "%s"', self._framework, actual) + _LOGGER.debug('Framework update from "%s" to "%s"', self._framework, actual) return await self._get_frontdoor_url(session, email, password) _LOGGER.error("Unable to retrieve the frontdoor URL. Message: " + text) return "" diff --git a/pyhon/commands.py b/pyhon/commands.py index b64fdb3..ab76f28 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -32,7 +32,9 @@ class HonCommand: @property def parameters(self): result = {key: parameter.value for key, parameter in self._parameters.items()} - return result | {"program": self._category} + if self._multi: + result |= {"program": self._category} + return result @property def ancillary_parameters(self): diff --git a/pyhon/device.py b/pyhon/device.py index 32dfd4f..d76d760 100644 --- a/pyhon/device.py +++ b/pyhon/device.py @@ -143,8 +143,17 @@ class HonDevice: @property def settings(self): result = {} - for command in self._commands.values(): - result |= command.settings + for name, command in self._commands.items(): + for key, setting in command.settings.items(): + result[f"{name}.{key}"] = setting + return result + + @property + def parameters(self): + result = {} + for name, command in self._commands.items(): + for key, parameter in command.parameters.items(): + result[f"{name}.{key}"] = parameter return result async def load_attributes(self): diff --git a/pyhon/parameter.py b/pyhon/parameter.py index a85843c..0de79b2 100644 --- a/pyhon/parameter.py +++ b/pyhon/parameter.py @@ -20,6 +20,9 @@ class HonParameterFixed(HonParameter): super().__init__(key, attributes) self._value = attributes["fixedValue"] + def __repr__(self): + return f"{self.__class__} (<{self.key}> fixed)" + @property def value(self): return self._value @@ -40,7 +43,7 @@ class HonParameterRange(HonParameter): self._value = self._default def __repr__(self): - return f"{self.key} [{self._min} - {self._max}]" + return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])" @property def min(self): @@ -74,7 +77,7 @@ class HonParameterEnum(HonParameter): self._values = attributes.get("enumValues") def __repr__(self): - return f"{self.key} {self.values}" + return f"{self.__class__} (<{self.key}> {self.values})" @property def values(self): diff --git a/setup.py b/setup.py index 04023c3..5b77e13 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as f: setup( name="pyhOn", - version="0.0.12", + version="0.0.13", author="Andre Basche", description="Control hOn devices with python", long_description=long_description, @@ -18,5 +18,10 @@ setup( packages=find_packages(), include_package_data=True, python_requires=">=3.10", - install_requires=["aiohttp"] + install_requires=["aiohttp"], + entry_points={ + 'console_scripts': [ + 'pyhOn = pyhon.__main__:start', + ] + } )