2023-03-05 10:46:51 -07:00
|
|
|
import importlib
|
|
|
|
|
2023-02-12 19:36:09 -07:00
|
|
|
from pyhon.commands import HonCommand
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
class HonDevice:
|
|
|
|
def __init__(self, connector, appliance):
|
2023-03-07 16:58:25 -07:00
|
|
|
appliance["attributes"] = {v["parName"]: v["parValue"] for v in appliance["attributes"]}
|
2023-02-12 17:41:38 -07:00
|
|
|
self._appliance = appliance
|
|
|
|
self._connector = connector
|
|
|
|
self._appliance_model = {}
|
|
|
|
|
|
|
|
self._commands = {}
|
|
|
|
self._statistics = {}
|
2023-03-04 13:27:10 -07:00
|
|
|
self._attributes = {}
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-03-07 16:58:25 -07:00
|
|
|
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("."):
|
2023-03-08 14:18:44 -07:00
|
|
|
if all([k in "0123456789" for k in key]) and type(result) is list:
|
2023-03-07 16:58:25 -07:00
|
|
|
result = result[int(key)]
|
|
|
|
else:
|
|
|
|
result = result[key]
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
if item in self.data:
|
|
|
|
return self.data[item]
|
2023-03-08 14:18:44 -07:00
|
|
|
if item in self.attributes["parameters"]:
|
|
|
|
return self.attributes["parameters"].get(item)
|
|
|
|
return self.appliance[item]
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-03-08 13:53:53 -07:00
|
|
|
def get(self, item, default=None):
|
|
|
|
try:
|
|
|
|
return self[item]
|
2023-03-08 14:18:44 -07:00
|
|
|
except (KeyError, IndexError):
|
2023-03-08 13:53:53 -07:00
|
|
|
return default
|
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
@property
|
|
|
|
def appliance_model_id(self):
|
|
|
|
return self._appliance.get("applianceModelId")
|
|
|
|
|
|
|
|
@property
|
2023-03-07 16:58:25 -07:00
|
|
|
def appliance_type(self):
|
2023-02-12 17:41:38 -07:00
|
|
|
return self._appliance.get("applianceTypeName")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mac_address(self):
|
|
|
|
return self._appliance.get("macAddress")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model_name(self):
|
|
|
|
return self._appliance.get("modelName")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def nick_name(self):
|
|
|
|
return self._appliance.get("nickName")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def commands_options(self):
|
|
|
|
return self._appliance_model.get("options")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def commands(self):
|
|
|
|
return self._commands
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attributes(self):
|
2023-03-04 13:27:10 -07:00
|
|
|
return self._attributes
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def statistics(self):
|
|
|
|
return self._statistics
|
|
|
|
|
2023-03-05 10:46:51 -07:00
|
|
|
@property
|
|
|
|
def appliance(self):
|
|
|
|
return self._appliance
|
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
async def load_commands(self):
|
|
|
|
raw = await self._connector.load_commands(self)
|
|
|
|
self._appliance_model = raw.pop("applianceModel")
|
|
|
|
for item in ["settings", "options", "dictionaryId"]:
|
|
|
|
raw.pop(item)
|
|
|
|
commands = {}
|
|
|
|
for command, attr in raw.items():
|
|
|
|
if "parameters" in attr:
|
|
|
|
commands[command] = HonCommand(command, attr, self._connector, self)
|
|
|
|
elif "parameters" in attr[list(attr)[0]]:
|
|
|
|
multi = {}
|
|
|
|
for category, attr2 in attr.items():
|
2023-02-18 14:25:51 -07:00
|
|
|
cmd = HonCommand(command, attr2, self._connector, self, multi=multi, category=category)
|
2023-02-12 17:41:38 -07:00
|
|
|
multi[category] = cmd
|
|
|
|
commands[command] = cmd
|
|
|
|
self._commands = commands
|
|
|
|
|
2023-02-18 14:25:51 -07:00
|
|
|
@property
|
|
|
|
def settings(self):
|
|
|
|
result = {}
|
2023-02-19 11:43:41 -07:00
|
|
|
for name, command in self._commands.items():
|
|
|
|
for key, setting in command.settings.items():
|
|
|
|
result[f"{name}.{key}"] = setting
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parameters(self):
|
|
|
|
result = {}
|
|
|
|
for name, command in self._commands.items():
|
|
|
|
for key, parameter in command.parameters.items():
|
2023-03-07 16:58:25 -07:00
|
|
|
result.setdefault(name, {})[key] = parameter.value
|
2023-02-18 14:25:51 -07:00
|
|
|
return result
|
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
async def load_attributes(self):
|
2023-03-07 16:58:25 -07:00
|
|
|
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"]
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
async def load_statistics(self):
|
|
|
|
self._statistics = await self._connector.load_statistics(self)
|
2023-02-12 19:36:09 -07:00
|
|
|
|
|
|
|
async def update(self):
|
|
|
|
await self.load_attributes()
|
2023-03-03 10:24:19 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
2023-03-07 16:58:25 -07:00
|
|
|
result = {"attributes": self.attributes, "appliance": self.appliance, "statistics": self.statistics,
|
2023-03-08 14:18:44 -07:00
|
|
|
**self.parameters}
|
2023-03-07 16:58:25 -07:00
|
|
|
if self._extra:
|
|
|
|
return result | self._extra.Appliance(result).get()
|
|
|
|
return result
|