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 fr > hon_fr.yaml
$ pyhOn translate en --json > hon_en.json $ pyhOn translate en --json > hon_en.json
``` ```
## Tested devices
- Haier Washing Machine HW90
## Usage example ## Usage example
This library is used for the custom [HomeAssistant Integration "Haier hOn"](https://github.com/Andre0512/hOn). 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 import importlib
from contextlib import suppress from contextlib import suppress
from typing import Optional, Dict
from pyhon import helper from pyhon import helper
from pyhon.commands import HonCommand from pyhon.commands import HonCommand
@ -7,7 +8,7 @@ from pyhon.parameter import HonParameterFixed
class HonAppliance: class HonAppliance:
def __init__(self, api, info): def __init__(self, api, info: Dict, zone: int = 0) -> None:
if attributes := info.get("attributes"): if attributes := info.get("attributes"):
info["attributes"] = {v["parName"]: v["parValue"] for v in attributes} info["attributes"] = {v["parName"]: v["parValue"] for v in attributes}
self._info = info self._info = info
@ -17,6 +18,7 @@ class HonAppliance:
self._commands = {} self._commands = {}
self._statistics = {} self._statistics = {}
self._attributes = {} self._attributes = {}
self._zone = zone
try: try:
self._extra = importlib.import_module( self._extra = importlib.import_module(
@ -26,20 +28,21 @@ class HonAppliance:
self._extra = None self._extra = None
def __getitem__(self, item): def __getitem__(self, item):
if self._zone:
item += f"Z{self._zone}"
if "." in item: if "." in item:
result = self.data result = self.data
for key in item.split("."): 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)] result = result[int(key)]
else: else:
result = result[key] result = result[key]
return result return result
else: if item in self.data:
if item in self.data: return self.data[item]
return self.data[item] if item in self.attributes["parameters"]:
if item in self.attributes["parameters"]: return self.attributes["parameters"].get(item)
return self.attributes["parameters"].get(item) return self.info[item]
return self.info[item]
def get(self, item, default=None): def get(self, item, default=None):
try: try:
@ -47,25 +50,31 @@ class HonAppliance:
except (KeyError, IndexError): except (KeyError, IndexError):
return default 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 @property
def appliance_model_id(self): def appliance_model_id(self) -> str:
return self._info.get("applianceModelId") return self._info.get("applianceModelId")
@property @property
def appliance_type(self): def appliance_type(self) -> str:
return self._info.get("applianceTypeName") return self._info.get("applianceTypeName")
@property @property
def mac_address(self): def mac_address(self) -> str:
return self._info.get("macAddress") return self._check_name_zone("macAddress", frontend=False)
@property @property
def model_name(self): def model_name(self) -> str:
return self._info.get("modelName") return self._check_name_zone("modelName")
@property @property
def nick_name(self): def nick_name(self) -> str:
return self._info.get("nickName") return self._check_name_zone("nickName")
@property @property
def commands_options(self): def commands_options(self):

View File

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

View File

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

View File

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