Expose settings

This commit is contained in:
Andre Basche 2023-02-18 22:25:51 +01:00
parent 984d1b91b7
commit 3efa2fd3f5
5 changed files with 105 additions and 15 deletions

View file

@ -1 +1 @@
from .api import HonConnection
from .api import HonConnection

View file

@ -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

View file

@ -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():

View file

@ -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"]

View file

@ -6,8 +6,8 @@ with open("README.md", "r") as f:
long_description = f.read()
setup(
name="pyhon",
version="0.0.5",
name="pyhOn",
version="0.0.6",
author="Andre Basche",
description="Control hOn devices with python",
long_description=long_description,