From d52d622785b56f3f0249491bacb50ebe5b771352 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sat, 15 Apr 2023 04:12:38 +0200 Subject: [PATCH] Add zone support --- README.md | 2 -- pyhon/__main__.py | 0 pyhon/appliance.py | 41 +++++++++++++++++++++++++---------------- pyhon/commands.py | 2 ++ pyhon/hon.py | 29 +++++++++++++++++------------ setup.py | 2 +- 6 files changed, 45 insertions(+), 31 deletions(-) mode change 100644 => 100755 pyhon/__main__.py diff --git a/README.md b/README.md index b28df60..cba6f50 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/pyhon/__main__.py b/pyhon/__main__.py old mode 100644 new mode 100755 diff --git a/pyhon/appliance.py b/pyhon/appliance.py index d37b294..693e9c7 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -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): diff --git a/pyhon/commands.py b/pyhon/commands.py index b12493a..b6a5427 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -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) diff --git a/pyhon/hon.py b/pyhon/hon.py index 64ac364..327f85e 100644 --- a/pyhon/hon.py +++ b/pyhon/hon.py @@ -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() diff --git a/setup.py b/setup.py index 76398a1..80cad43 100644 --- a/setup.py +++ b/setup.py @@ -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,