From f6139db0b5955e865bb17da3155193715ad28024 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Tue, 13 Jun 2023 00:12:29 +0200 Subject: [PATCH] Use class for attributes --- pyhon/appliance.py | 10 +++++++--- pyhon/appliances/base.py | 2 +- pyhon/appliances/dw.py | 2 +- pyhon/appliances/ov.py | 8 ++++---- pyhon/appliances/td.py | 2 +- pyhon/appliances/wd.py | 2 +- pyhon/appliances/wm.py | 2 +- pyhon/attributes.py | 37 +++++++++++++++++++++++++++++++++++++ pyhon/helper.py | 7 +++++++ pyhon/parameter/range.py | 8 +------- 10 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 pyhon/attributes.py diff --git a/pyhon/appliance.py b/pyhon/appliance.py index eb13e12..bfb16b2 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -9,6 +9,7 @@ from typing import Optional, Dict, Any from typing import TYPE_CHECKING from pyhon import helper +from pyhon.attributes import HonAttribute from pyhon.commands import HonCommand from pyhon.parameter.base import HonParameter from pyhon.parameter.fixed import HonParameterFixed @@ -61,7 +62,7 @@ class HonAppliance: if item in self.data: return self.data[item] if item in self.attributes["parameters"]: - return self.attributes["parameters"].get(item) + return self.attributes["parameters"][item].value return self.info[item] def get(self, item, default=None): @@ -241,7 +242,10 @@ class HonAppliance: async def load_attributes(self): self._attributes = await self.api.load_attributes(self) for name, values in self._attributes.pop("shadow").get("parameters").items(): - self._attributes.setdefault("parameters", {})[name] = values["parNewVal"] + if name in self._attributes.get("parameters", {}): + self._attributes["parameters"][name].update(values) + else: + self._attributes.setdefault("parameters", {})[name] = HonAttribute(values) if self._extra: self._attributes = self._extra.attributes(self._attributes) @@ -326,7 +330,7 @@ class HonAppliance: command: HonCommand = self.commands.get(command_name) for key, value in self.attributes.get("parameters", {}).items(): if isinstance(value, str) and (new := command.parameters.get(key)): - self.attributes["parameters"][key] = str(new.intern_value) + self.attributes["parameters"][key].value = str(new.intern_value) def sync_command(self, main, target=None) -> None: base: HonCommand = self.commands.get(main) diff --git a/pyhon/appliances/base.py b/pyhon/appliances/base.py index 0e24b63..dfb4c51 100644 --- a/pyhon/appliances/base.py +++ b/pyhon/appliances/base.py @@ -4,7 +4,7 @@ class ApplianceBase: def attributes(self, data): program_name = "No Program" - if program := int(data["parameters"].get("prCode", "0")): + if program := int(str(data.get("parameters", {}).get("prCode", "0"))): if start_cmd := self.parent.settings.get("startProgram.program"): if ids := start_cmd.ids: program_name = ids.get(program, program_name) diff --git a/pyhon/appliances/dw.py b/pyhon/appliances/dw.py index 6e980b7..af3da74 100644 --- a/pyhon/appliances/dw.py +++ b/pyhon/appliances/dw.py @@ -5,6 +5,6 @@ class Appliance(ApplianceBase): def attributes(self, data): data = super().attributes(data) if data["lastConnEvent"]["category"] == "DISCONNECTED": - data["parameters"]["machMode"] = "0" + data["parameters"]["machMode"].value = "0" data["active"] = bool(data.get("activity")) return data diff --git a/pyhon/appliances/ov.py b/pyhon/appliances/ov.py index 5558690..74afeac 100644 --- a/pyhon/appliances/ov.py +++ b/pyhon/appliances/ov.py @@ -5,10 +5,10 @@ class Appliance(ApplianceBase): def attributes(self, data): data = super().attributes(data) if data["lastConnEvent"]["category"] == "DISCONNECTED": - data["parameters"]["temp"] = "0" - data["parameters"]["onOffStatus"] = "0" - data["parameters"]["remoteCtrValid"] = "0" - data["parameters"]["remainingTimeMM"] = "0" + data["parameters"]["temp"].value = "0" + data["parameters"]["onOffStatus"].value = "0" + data["parameters"]["remoteCtrValid"].value = "0" + data["parameters"]["remainingTimeMM"].value = "0" data["active"] = data["parameters"]["onOffStatus"] == "1" diff --git a/pyhon/appliances/td.py b/pyhon/appliances/td.py index ee21748..6b9894e 100644 --- a/pyhon/appliances/td.py +++ b/pyhon/appliances/td.py @@ -6,7 +6,7 @@ class Appliance(ApplianceBase): def attributes(self, data): data = super().attributes(data) if data["lastConnEvent"]["category"] == "DISCONNECTED": - data["parameters"]["machMode"] = "0" + data["parameters"]["machMode"].value = "0" data["active"] = bool(data.get("activity")) data["pause"] = data["parameters"]["machMode"] == "3" return data diff --git a/pyhon/appliances/wd.py b/pyhon/appliances/wd.py index ee4ccac..3f847d0 100644 --- a/pyhon/appliances/wd.py +++ b/pyhon/appliances/wd.py @@ -5,7 +5,7 @@ class Appliance(ApplianceBase): def attributes(self, data): data = super().attributes(data) if data["lastConnEvent"]["category"] == "DISCONNECTED": - data["parameters"]["machMode"] = "0" + data["parameters"]["machMode"].value = "0" data["active"] = bool(data.get("activity")) data["pause"] = data["parameters"]["machMode"] == "3" return data diff --git a/pyhon/appliances/wm.py b/pyhon/appliances/wm.py index ee4ccac..3f847d0 100644 --- a/pyhon/appliances/wm.py +++ b/pyhon/appliances/wm.py @@ -5,7 +5,7 @@ class Appliance(ApplianceBase): def attributes(self, data): data = super().attributes(data) if data["lastConnEvent"]["category"] == "DISCONNECTED": - data["parameters"]["machMode"] = "0" + data["parameters"]["machMode"].value = "0" data["active"] = bool(data.get("activity")) data["pause"] = data["parameters"]["machMode"] == "3" return data diff --git a/pyhon/attributes.py b/pyhon/attributes.py new file mode 100644 index 0000000..3a45c29 --- /dev/null +++ b/pyhon/attributes.py @@ -0,0 +1,37 @@ +from datetime import datetime +from typing import Optional + +from pyhon.helper import str_to_float + + +class HonAttribute: + def __init__(self, data): + self._value: str = "" + self._last_update: Optional[datetime] = None + self.update(data) + + @property + def value(self) -> float | str: + try: + return str_to_float(self._value) + except ValueError: + return self._value + + @value.setter + def value(self, value) -> None: + self._value = value + + @property + def last_update(self) -> Optional[datetime]: + return self._last_update + + def update(self, data): + self._value = data.get("parNewVal", "") + if last_update := data.get("lastUpdate"): + try: + self._last_update = datetime.fromisoformat(last_update) + except ValueError: + self._last_update = None + + def __str__(self) -> str: + return self._value diff --git a/pyhon/helper.py b/pyhon/helper.py index c37ffb2..fc7555e 100644 --- a/pyhon/helper.py +++ b/pyhon/helper.py @@ -73,3 +73,10 @@ def create_rules(commands, concat=False): else: result[f"{name}.{parameter}"] = value return result + + +def str_to_float(string: str | float) -> float: + try: + return int(string) + except ValueError: + return float(str(string).replace(",", ".")) diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py index e43f22f..2cb0772 100644 --- a/pyhon/parameter/range.py +++ b/pyhon/parameter/range.py @@ -1,15 +1,9 @@ from typing import Dict, Any, List +from pyhon.helper import str_to_float from pyhon.parameter.base import HonParameter -def str_to_float(string: str | float) -> float: - try: - return int(string) - except ValueError: - return float(str(string).replace(",", ".")) - - class HonParameterRange(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group)