mirror of
https://github.com/Andre0512/pyhOn.git
synced 2024-12-22 18:55:32 +00:00
Expose settings
This commit is contained in:
parent
984d1b91b7
commit
3efa2fd3f5
|
@ -1,15 +1,19 @@
|
|||
from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRange
|
||||
from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRange, HonParameterProgram
|
||||
|
||||
|
||||
class HonCommand:
|
||||
def __init__(self, name, attributes, connector, device, multi=None):
|
||||
def __init__(self, name, attributes, connector, device, multi=None, category=""):
|
||||
self._connector = connector
|
||||
self._device = device
|
||||
self._name = name
|
||||
self._multi = multi or {}
|
||||
self._category = category
|
||||
self._description = attributes.get("description", "")
|
||||
self._parameters = self._create_parameters(attributes.get("parameters", {}))
|
||||
self._ancillary_parameters = self._create_parameters(attributes.get("ancillaryParameters", {}))
|
||||
self._multi = multi
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self._name} command"
|
||||
|
||||
def _create_parameters(self, parameters):
|
||||
result = {}
|
||||
|
@ -21,11 +25,14 @@ class HonCommand:
|
|||
result[parameter] = HonParameterEnum(parameter, attributes)
|
||||
case "fixed":
|
||||
result[parameter] = HonParameterFixed(parameter, attributes)
|
||||
if self._multi:
|
||||
result["program"] = HonParameterProgram("program", {"current": self._category, "values": list(self._multi)})
|
||||
return result
|
||||
|
||||
@property
|
||||
def parameters(self):
|
||||
return {key: parameter.value for key, parameter in self._parameters.items()}
|
||||
result = {key: parameter.value for key, parameter in self._parameters.items()}
|
||||
return result | {"program": self._category}
|
||||
|
||||
@property
|
||||
def ancillary_parameters(self):
|
||||
|
@ -40,3 +47,30 @@ class HonCommand:
|
|||
|
||||
async def set_program(self, program):
|
||||
self._device.commands[self._name] = self._multi[program]
|
||||
|
||||
def _get_settings_keys(self, command=None):
|
||||
command = command or self
|
||||
keys = []
|
||||
for key, parameter in command._parameters.items():
|
||||
if isinstance(parameter, HonParameterFixed):
|
||||
continue
|
||||
if key not in keys:
|
||||
keys.append(key)
|
||||
return keys
|
||||
|
||||
@property
|
||||
def setting_keys(self):
|
||||
if not self._multi:
|
||||
return self._get_settings_keys()
|
||||
result = [key for cmd in self._multi.values() for key in self._get_settings_keys(cmd)]
|
||||
return list(set(result + ["program"]))
|
||||
|
||||
@property
|
||||
def settings(self):
|
||||
return {s: self._parameters[s] for s in self.setting_keys}
|
||||
|
||||
def set_setting(self, key, value):
|
||||
if key == "program":
|
||||
self.set_program(key)
|
||||
else:
|
||||
self.parameters[key].value = value
|
||||
|
|
|
@ -135,11 +135,18 @@ class HonDevice:
|
|||
elif "parameters" in attr[list(attr)[0]]:
|
||||
multi = {}
|
||||
for category, attr2 in attr.items():
|
||||
cmd = HonCommand(command, attr2, self._connector, self, multi=multi)
|
||||
cmd = HonCommand(command, attr2, self._connector, self, multi=multi, category=category)
|
||||
multi[category] = cmd
|
||||
commands[command] = cmd
|
||||
self._commands = commands
|
||||
|
||||
@property
|
||||
def settings(self):
|
||||
result = {}
|
||||
for command in self._commands.values():
|
||||
result |= command.settings
|
||||
return result
|
||||
|
||||
async def load_attributes(self):
|
||||
data = await self._connector.load_attributes(self)
|
||||
for name, values in data.get("shadow").get("parameters").items():
|
||||
|
|
|
@ -6,6 +6,10 @@ class HonParameter:
|
|||
self._mandatory = attributes.get("mandatory")
|
||||
self._value = ""
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
return self._key
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._value if self._value is not None else "0"
|
||||
|
@ -16,20 +20,65 @@ class HonParameterFixed(HonParameter):
|
|||
super().__init__(key, attributes)
|
||||
self._value = attributes["fixedValue"]
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value):
|
||||
raise ValueError("fixed value")
|
||||
|
||||
|
||||
class HonParameterRange(HonParameter):
|
||||
def __init__(self, key, attributes):
|
||||
super().__init__(key, attributes)
|
||||
self._value = attributes.get("defaultValue")
|
||||
self._default = attributes.get("defaultValue")
|
||||
self._min = attributes["minimumValue"]
|
||||
self._max = attributes["maximumValue"]
|
||||
self._step = attributes["incrementValue"]
|
||||
self._min = int(attributes["minimumValue"])
|
||||
self._max = int(attributes["maximumValue"])
|
||||
self._step = int(attributes["incrementValue"])
|
||||
self._default = int(attributes.get("defaultValue", self._min))
|
||||
self._value = self._default
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.key} [{self._min} - {self._max}]"
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._value if self._value is not None else self._min
|
||||
|
||||
@value.setter
|
||||
def value(self, value):
|
||||
if self._min < value < self._max and not value % self._step:
|
||||
self._value = self._value
|
||||
raise ValueError(f"min {self._min} max {self._max} step {self._step}")
|
||||
|
||||
|
||||
class HonParameterEnum(HonParameter):
|
||||
def __init__(self, key, attributes):
|
||||
super().__init__(key, attributes)
|
||||
self._value = attributes.get("defaultValue", "0")
|
||||
self._default = attributes["defaultValue"]
|
||||
self._values = attributes["enumValues"]
|
||||
self._default = attributes.get("defaultValue")
|
||||
self._values = attributes.get("enumValues")
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.key} {self._values}"
|
||||
|
||||
@property
|
||||
def values(self):
|
||||
return self._values
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value):
|
||||
if value in self._values:
|
||||
self._value = self._value
|
||||
raise ValueError(f"values {self._value}")
|
||||
|
||||
|
||||
class HonParameterProgram(HonParameterEnum):
|
||||
def __init__(self, key, attributes):
|
||||
super().__init__(key, attributes)
|
||||
self._value = attributes["current"]
|
||||
self._values = attributes["values"]
|
||||
|
|
Loading…
Reference in a new issue