Skip to content

Commit

Permalink
Add zone support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Apr 15, 2023
1 parent 9643f66 commit d52d622
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Empty file modified pyhon/__main__.py
100644 → 100755
Empty file.
41 changes: 25 additions & 16 deletions pyhon/appliance.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import importlib
from contextlib import suppress
from typing import Optional, Dict

from pyhon import helper
from pyhon.commands import HonCommand
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
Expand All @@ -17,6 +18,7 @@ def __init__(self, api, info):
self._commands = {}
self._statistics = {}
self._attributes = {}
self._zone = zone

try:
self._extra = importlib.import_module(
Expand All @@ -26,46 +28,53 @@ def __init__(self, api, info):
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:
return self[item]
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):
Expand Down
2 changes: 2 additions & 0 deletions pyhon/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __repr__(self):
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)
Expand Down
29 changes: 17 additions & 12 deletions pyhon/hon.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,19 +39,24 @@ async def create(self) -> Self:
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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="pyhOn",
version="0.7.4",
version="0.8.0b2",
author="Andre Basche",
description="Control hOn devices with python",
long_description=long_description,
Expand Down

0 comments on commit d52d622

Please sign in to comment.