Add zone support

This commit is contained in:
Andre Basche 2023-04-15 04:12:38 +02:00
parent 9643f66549
commit d52d622785
6 changed files with 45 additions and 31 deletions

View file

@ -100,8 +100,6 @@ This generates a huge output. It is recommended to pipe this into a file
$ pyhOn translate fr > hon_fr.yaml
$ pyhOn translate en --json > hon_en.json
```
## Tested devices
- Haier Washing Machine HW90
## Usage example
This library is used for the custom [HomeAssistant Integration "Haier hOn"](https://github.com/Andre0512/hOn).

0
pyhon/__main__.py Normal file → Executable file
View file

View file

@ -1,5 +1,6 @@
import importlib
from contextlib import suppress
from typing import Optional, Dict
from pyhon import helper
from pyhon.commands import HonCommand
@ -7,7 +8,7 @@ from pyhon.parameter import HonParameterFixed
class HonAppliance:
def __init__(self, api, info):
def __init__(self, api, info: Dict, zone: int = 0) -> None:
if attributes := info.get("attributes"):
info["attributes"] = {v["parName"]: v["parValue"] for v in attributes}
self._info = info
@ -17,6 +18,7 @@ class HonAppliance:
self._commands = {}
self._statistics = {}
self._attributes = {}
self._zone = zone
try:
self._extra = importlib.import_module(
@ -26,20 +28,21 @@ class HonAppliance:
self._extra = None
def __getitem__(self, item):
if self._zone:
item += f"Z{self._zone}"
if "." in item:
result = self.data
for key in item.split("."):
if all([k in "0123456789" for k in key]) and type(result) is list:
if all(k in "0123456789" for k in key) and isinstance(result, list):
result = result[int(key)]
else:
result = result[key]
return result
else:
if item in self.data:
return self.data[item]
if item in self.attributes["parameters"]:
return self.attributes["parameters"].get(item)
return self.info[item]
if item in self.data:
return self.data[item]
if item in self.attributes["parameters"]:
return self.attributes["parameters"].get(item)
return self.info[item]
def get(self, item, default=None):
try:
@ -47,25 +50,31 @@ class HonAppliance:
except (KeyError, IndexError):
return default
def _check_name_zone(self, name: str, frontend: bool = True) -> str:
middle = " Z" if frontend else "_z"
if (attribute := self._info.get(name, "")) and self._zone:
return f"{attribute}{middle}{self._zone}"
return attribute
@property
def appliance_model_id(self):
def appliance_model_id(self) -> str:
return self._info.get("applianceModelId")
@property
def appliance_type(self):
def appliance_type(self) -> str:
return self._info.get("applianceTypeName")
@property
def mac_address(self):
return self._info.get("macAddress")
def mac_address(self) -> str:
return self._check_name_zone("macAddress", frontend=False)
@property
def model_name(self):
return self._info.get("modelName")
def model_name(self) -> str:
return self._check_name_zone("modelName")
@property
def nick_name(self):
return self._info.get("nickName")
def nick_name(self) -> str:
return self._check_name_zone("nickName")
@property
def commands_options(self):

View file

@ -25,6 +25,8 @@ class HonCommand:
def _create_parameters(self, parameters):
result = {}
for parameter, attributes in parameters.items():
if parameter == "zoneMap" and self._device.zone:
attributes["default"] = self._device.zone
match attributes.get("typology"):
case "range":
result[parameter] = HonParameterRange(parameter, attributes)

View file

@ -1,5 +1,5 @@
import asyncio
from typing import List, Optional
from typing import List, Optional, Dict
from typing_extensions import Self
from aiohttp import ClientSession
@ -39,19 +39,24 @@ class Hon:
def appliances(self) -> List[HonAppliance]:
return self._appliances
async def _create_appliance(self, appliance: Dict, zone=0) -> None:
appliance = HonAppliance(self._api, appliance, zone=zone)
if appliance.mac_address is None:
return
await asyncio.gather(
*[
appliance.load_attributes(),
appliance.load_commands(),
appliance.load_statistics(),
]
)
self._appliances.append(appliance)
async def setup(self):
for appliance in (await self._api.load_appliances())["payload"]["appliances"]:
appliance = HonAppliance(self._api, appliance)
if appliance.mac_address is None:
continue
await asyncio.gather(
*[
appliance.load_attributes(),
appliance.load_commands(),
appliance.load_statistics(),
]
)
self._appliances.append(appliance)
for zone in range(int(appliance.get("zone", "0"))):
await self._create_appliance(appliance, zone=zone + 1)
await self._create_appliance(appliance)
async def close(self):
await self._api.close()

View file

@ -7,7 +7,7 @@ with open("README.md", "r") as f:
setup(
name="pyhOn",
version="0.7.4",
version="0.8.0b2",
author="Andre Basche",
description="Control hOn devices with python",
long_description=long_description,