Use class for attributes

This commit is contained in:
Andre Basche 2023-06-13 00:12:29 +02:00
parent 310d1bafd7
commit f6139db0b5
10 changed files with 61 additions and 19 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

37
pyhon/attributes.py Normal file
View file

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

View file

@ -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(",", "."))

View file

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