Clean up attribute structure

This commit is contained in:
Andre Basche 2023-03-08 00:58:25 +01:00
parent 43d61ab853
commit 5acc81acc3
6 changed files with 95 additions and 121 deletions

View file

@ -1,14 +1,12 @@
import importlib
from pprint import pprint
from pyhon.commands import HonCommand
class HonDevice:
def __init__(self, connector, appliance):
appliance["attributes"] = {v["parName"]: v["parValue"] for v in appliance["attributes"]}
self._appliance = appliance
for values in self._appliance.pop("attributes"):
self._appliance[values["parName"]] = values["parValue"]
self._connector = connector
self._appliance_model = {}
@ -16,74 +14,33 @@ class HonDevice:
self._statistics = {}
self._attributes = {}
@property
def appliance_id(self):
return self._appliance.get("applianceId")
try:
self._extra = importlib.import_module(f'pyhon.appliances.{self.appliance_type.lower()}')
except ModuleNotFoundError:
self._extra = None
def __getitem__(self, item):
if "." in item:
result = self.data
for key in item.split("."):
if all([k in "0123456789" for k in key]):
result = result[int(key)]
else:
result = result[key]
return result
else:
if item in self.data:
return self.data[item]
return self.attributes["parameters"].get(item, self.appliance[item])
@property
def appliance_model_id(self):
return self._appliance.get("applianceModelId")
@property
def appliance_status(self):
return self._appliance.get("applianceStatus")
@property
def appliance_type_id(self):
return self._appliance.get("applianceTypeId")
@property
def appliance_type_name(self):
def appliance_type(self):
return self._appliance.get("applianceTypeName")
@property
def brand(self):
return self._appliance.get("brand")
@property
def code(self):
return self._appliance.get("code")
@property
def connectivity(self):
return self._appliance.get("connectivity")
@property
def coords(self):
return self._appliance.get("coords")
@property
def eeprom_id(self):
return self._appliance.get("eepromId")
@property
def eeprom_name(self):
return self._appliance.get("eepromName")
@property
def enrollment_date(self):
return self._appliance.get("enrollmentDate")
@property
def first_enrollment(self):
return self._appliance.get("firstEnrollment")
@property
def first_enrollment_tbc(self):
return self._appliance.get("firstEnrollmentTBC")
@property
def fw_version(self):
return self._appliance.get("fwVersion")
@property
def id(self):
return self._appliance.get("id")
@property
def last_update(self):
return self._appliance.get("lastUpdate")
@property
def mac_address(self):
return self._appliance.get("macAddress")
@ -96,22 +53,6 @@ class HonDevice:
def nick_name(self):
return self._appliance.get("nickName")
@property
def purchase_date(self):
return self._appliance.get("purchaseDate")
@property
def serial_number(self):
return self._appliance.get("serialNumber")
@property
def series(self):
return self._appliance.get("series")
@property
def water_hard(self):
return self._appliance.get("waterHard")
@property
def commands_options(self):
return self._appliance_model.get("options")
@ -162,15 +103,13 @@ class HonDevice:
result = {}
for name, command in self._commands.items():
for key, parameter in command.parameters.items():
result[f"{name}.{key}"] = parameter.value
result.setdefault(name, {})[key] = parameter.value
return result
async def load_attributes(self):
data = await self._connector.load_attributes(self)
for name, values in data.get("shadow").get("parameters").items():
self._attributes[name] = values["parNewVal"]
for name, value in data.get("lastConnEvent").items():
self._attributes[f"lastConnEvent.{name}"] = value
self._attributes = await self._connector.load_attributes(self)
for name, values in self._attributes.pop("shadow").get("parameters").items():
self._attributes.setdefault("parameters", {})[name] = values["parNewVal"]
async def load_statistics(self):
self._statistics = await self._connector.load_statistics(self)
@ -180,9 +119,8 @@ class HonDevice:
@property
def data(self):
result = self.attributes | self.parameters | self.appliance | self._statistics
try:
extra = importlib.import_module(f'appliances.{self.appliance_type_name.lower()}')
return result | extra.Appliance(result).get()
except ModuleNotFoundError:
return result
result = {"attributes": self.attributes, "appliance": self.appliance, "statistics": self.statistics,
"commands": self.parameters}
if self._extra:
return result | self._extra.Appliance(result).get()
return result