Create cmd tool, full parameter name

This commit is contained in:
Andre Basche 2023-02-19 19:43:41 +01:00
parent ba054237f1
commit 83bb99a34e
6 changed files with 82 additions and 8 deletions

55
pyhon/__main__.py Executable file
View File

@ -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()

View File

@ -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 ""

View File

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

View File

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

View File

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

View File

@ -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',
]
}
)