mirror of
https://github.com/Andre0512/pyhOn.git
synced 2024-01-19 02:50:44 +00:00
Expose settings
This commit is contained in:
parent
984d1b91b7
commit
3efa2fd3f5
|
@ -1 +1 @@
|
||||||
from .api import HonConnection
|
from .api import HonConnection
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRange
|
from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRange, HonParameterProgram
|
||||||
|
|
||||||
|
|
||||||
class HonCommand:
|
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._connector = connector
|
||||||
self._device = device
|
self._device = device
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self._multi = multi or {}
|
||||||
|
self._category = category
|
||||||
self._description = attributes.get("description", "")
|
self._description = attributes.get("description", "")
|
||||||
self._parameters = self._create_parameters(attributes.get("parameters", {}))
|
self._parameters = self._create_parameters(attributes.get("parameters", {}))
|
||||||
self._ancillary_parameters = self._create_parameters(attributes.get("ancillaryParameters", {}))
|
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):
|
def _create_parameters(self, parameters):
|
||||||
result = {}
|
result = {}
|
||||||
|
@ -21,11 +25,14 @@ class HonCommand:
|
||||||
result[parameter] = HonParameterEnum(parameter, attributes)
|
result[parameter] = HonParameterEnum(parameter, attributes)
|
||||||
case "fixed":
|
case "fixed":
|
||||||
result[parameter] = HonParameterFixed(parameter, attributes)
|
result[parameter] = HonParameterFixed(parameter, attributes)
|
||||||
|
if self._multi:
|
||||||
|
result["program"] = HonParameterProgram("program", {"current": self._category, "values": list(self._multi)})
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parameters(self):
|
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
|
@property
|
||||||
def ancillary_parameters(self):
|
def ancillary_parameters(self):
|
||||||
|
@ -40,3 +47,30 @@ class HonCommand:
|
||||||
|
|
||||||
async def set_program(self, program):
|
async def set_program(self, program):
|
||||||
self._device.commands[self._name] = self._multi[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]]:
|
elif "parameters" in attr[list(attr)[0]]:
|
||||||
multi = {}
|
multi = {}
|
||||||
for category, attr2 in attr.items():
|
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
|
multi[category] = cmd
|
||||||
commands[command] = cmd
|
commands[command] = cmd
|
||||||
self._commands = commands
|
self._commands = commands
|
||||||
|
|
||||||
|
@property
|
||||||
|
def settings(self):
|
||||||
|
result = {}
|
||||||
|
for command in self._commands.values():
|
||||||
|
result |= command.settings
|
||||||
|
return result
|
||||||
|
|
||||||
async def load_attributes(self):
|
async def load_attributes(self):
|
||||||
data = await self._connector.load_attributes(self)
|
data = await self._connector.load_attributes(self)
|
||||||
for name, values in data.get("shadow").get("parameters").items():
|
for name, values in data.get("shadow").get("parameters").items():
|
||||||
|
|
|
@ -6,6 +6,10 @@ class HonParameter:
|
||||||
self._mandatory = attributes.get("mandatory")
|
self._mandatory = attributes.get("mandatory")
|
||||||
self._value = ""
|
self._value = ""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def key(self):
|
||||||
|
return self._key
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
return self._value if self._value is not None else "0"
|
return self._value if self._value is not None else "0"
|
||||||
|
@ -16,20 +20,65 @@ class HonParameterFixed(HonParameter):
|
||||||
super().__init__(key, attributes)
|
super().__init__(key, attributes)
|
||||||
self._value = attributes["fixedValue"]
|
self._value = attributes["fixedValue"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self):
|
||||||
|
return self._value
|
||||||
|
|
||||||
|
@value.setter
|
||||||
|
def value(self, value):
|
||||||
|
raise ValueError("fixed value")
|
||||||
|
|
||||||
|
|
||||||
class HonParameterRange(HonParameter):
|
class HonParameterRange(HonParameter):
|
||||||
def __init__(self, key, attributes):
|
def __init__(self, key, attributes):
|
||||||
super().__init__(key, attributes)
|
super().__init__(key, attributes)
|
||||||
self._value = attributes.get("defaultValue")
|
self._min = int(attributes["minimumValue"])
|
||||||
self._default = attributes.get("defaultValue")
|
self._max = int(attributes["maximumValue"])
|
||||||
self._min = attributes["minimumValue"]
|
self._step = int(attributes["incrementValue"])
|
||||||
self._max = attributes["maximumValue"]
|
self._default = int(attributes.get("defaultValue", self._min))
|
||||||
self._step = attributes["incrementValue"]
|
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):
|
class HonParameterEnum(HonParameter):
|
||||||
def __init__(self, key, attributes):
|
def __init__(self, key, attributes):
|
||||||
super().__init__(key, attributes)
|
super().__init__(key, attributes)
|
||||||
self._value = attributes.get("defaultValue", "0")
|
self._value = attributes.get("defaultValue", "0")
|
||||||
self._default = attributes["defaultValue"]
|
self._default = attributes.get("defaultValue")
|
||||||
self._values = attributes["enumValues"]
|
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"]
|
||||||
|
|
4
setup.py
4
setup.py
|
@ -6,8 +6,8 @@ with open("README.md", "r") as f:
|
||||||
long_description = f.read()
|
long_description = f.read()
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="pyhon",
|
name="pyhOn",
|
||||||
version="0.0.5",
|
version="0.0.6",
|
||||||
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,
|
||||||
|
|
Loading…
Reference in a new issue