From 3efa2fd3f5a3e3d7564df14edc5da6143a9a3f76 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sat, 18 Feb 2023 22:25:51 +0100 Subject: [PATCH] Expose settings --- pyhon/__init__.py | 2 +- pyhon/commands.py | 42 ++++++++++++++++++++++++++++--- pyhon/device.py | 9 ++++++- pyhon/parameter.py | 63 ++++++++++++++++++++++++++++++++++++++++------ setup.py | 4 +-- 5 files changed, 105 insertions(+), 15 deletions(-) diff --git a/pyhon/__init__.py b/pyhon/__init__.py index 17e63aa..b13705c 100644 --- a/pyhon/__init__.py +++ b/pyhon/__init__.py @@ -1 +1 @@ -from .api import HonConnection \ No newline at end of file +from .api import HonConnection diff --git a/pyhon/commands.py b/pyhon/commands.py index 84333af..590ea49 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -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 diff --git a/pyhon/device.py b/pyhon/device.py index bdeb974..32dfd4f 100644 --- a/pyhon/device.py +++ b/pyhon/device.py @@ -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(): diff --git a/pyhon/parameter.py b/pyhon/parameter.py index 589aca9..615f9cb 100644 --- a/pyhon/parameter.py +++ b/pyhon/parameter.py @@ -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"] diff --git a/setup.py b/setup.py index 7710e44..ddf0d8b 100644 --- a/setup.py +++ b/setup.py @@ -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,