From 531548f0003b7a93d5555b7cea91123b53f25743 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sun, 19 Feb 2023 03:30:48 +0100 Subject: [PATCH] Improve parameter settings --- pyhon/commands.py | 11 +++------- pyhon/parameter.py | 51 +++++++++++++++++++++++++++++++++++----------- setup.py | 2 +- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index 590ea49..b64fdb3 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -26,7 +26,7 @@ class HonCommand: case "fixed": result[parameter] = HonParameterFixed(parameter, attributes) if self._multi: - result["program"] = HonParameterProgram("program", {"current": self._category, "values": list(self._multi)}) + result["program"] = HonParameterProgram("program", self) return result @property @@ -42,10 +42,10 @@ class HonCommand: return await self._connector.send_command(self._device, self._name, self.parameters, self.ancillary_parameters) - async def get_programs(self): + def get_programs(self): return self._multi - async def set_program(self, program): + def set_program(self, program): self._device.commands[self._name] = self._multi[program] def _get_settings_keys(self, command=None): @@ -69,8 +69,3 @@ class HonCommand: 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/parameter.py b/pyhon/parameter.py index 615f9cb..a85843c 100644 --- a/pyhon/parameter.py +++ b/pyhon/parameter.py @@ -26,7 +26,8 @@ class HonParameterFixed(HonParameter): @value.setter def value(self, value): - raise ValueError("fixed value") + if not value == self._value: + raise ValueError("Can't change fixed value") class HonParameterRange(HonParameter): @@ -41,30 +42,43 @@ class HonParameterRange(HonParameter): def __repr__(self): return f"{self.key} [{self._min} - {self._max}]" + @property + def min(self): + return self._min + + @property + def max(self): + return self._max + + @property + def step(self): + return self._step + @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: + 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}") + else: + raise ValueError(f"Allowed: 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.get("defaultValue") + self._value = self._default or "0" self._values = attributes.get("enumValues") def __repr__(self): - return f"{self.key} {self._values}" + return f"{self.key} {self.values}" @property def values(self): - return self._values + return [str(value) for value in self._values] @property def value(self): @@ -72,13 +86,26 @@ class HonParameterEnum(HonParameter): @value.setter def value(self, value): - if value in self._values: + if value in self.values: self._value = self._value - raise ValueError(f"values {self._value}") + else: + raise ValueError(f"Allowed values {self._value}") class HonParameterProgram(HonParameterEnum): - def __init__(self, key, attributes): - super().__init__(key, attributes) - self._value = attributes["current"] - self._values = attributes["values"] + def __init__(self, key, command): + super().__init__(key, {}) + self._command = command + self._value = command._category + self._values = command._multi + + @property + def value(self): + return self._value + + @value.setter + def value(self, value): + if value in self.values: + self._command.set_program(value) + else: + raise ValueError(f"Allowed values {self._value}") diff --git a/setup.py b/setup.py index ddf0d8b..04023c3 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as f: setup( name="pyhOn", - version="0.0.6", + version="0.0.12", author="Andre Basche", description="Control hOn devices with python", long_description=long_description,