From 7b51caecca92f89c643ac9d8c50355863c2b58d7 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Thu, 8 Jun 2023 19:50:56 +0200 Subject: [PATCH] Improve update performance --- pyhon/appliance.py | 21 ++++++++++++++++----- pyhon/appliances/base.py | 10 ++++++---- pyhon/appliances/dw.py | 10 +++++----- pyhon/appliances/ov.py | 18 +++++++++--------- pyhon/appliances/ref.py | 14 +++++++------- pyhon/appliances/td.py | 12 ++++++------ pyhon/appliances/wd.py | 12 ++++++------ pyhon/appliances/wm.py | 12 ++++++------ pyhon/commands.py | 1 + setup.py | 2 +- 10 files changed, 63 insertions(+), 49 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index e7cd8ff..d7cb836 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -242,15 +242,20 @@ class HonAppliance: 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 self._extra: + self._attributes = self._extra.attributes(self._attributes) async def load_statistics(self): self._statistics = await self.api.load_statistics(self) self._statistics |= await self.api.load_maintenance(self) - async def update(self): + async def update(self, force=False): now = datetime.now() - if not self._last_update or self._last_update < now - timedelta( - seconds=self._MINIMAL_UPDATE_INTERVAL + if ( + force + or not self._last_update + or self._last_update + < now - timedelta(seconds=self._MINIMAL_UPDATE_INTERVAL) ): self._last_update = now await self.load_attributes() @@ -286,9 +291,8 @@ class HonAppliance: "statistics": self.statistics, "additional_data": self._additional_data, **self.command_parameters, + **self.attributes, } - if self._extra: - return self._extra.data(result) return result def diagnose(self, whitespace=" ", command_only=False): @@ -320,6 +324,12 @@ class HonAppliance: ) return result.replace(self.mac_address, "xx-xx-xx-xx-xx-xx") + def sync_to_params(self, command_name): + 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) + def sync_command(self, main, target=None) -> None: base: HonCommand = self.commands.get(main) for command, data in self.commands.items(): @@ -345,6 +355,7 @@ class HonApplianceTest(HonAppliance): super().__init__(None, {}) self._name = name self.load_commands() + self.load_attributes() self._info = self._appliance_model def load_commands(self): diff --git a/pyhon/appliances/base.py b/pyhon/appliances/base.py index 10cd726..0e24b63 100644 --- a/pyhon/appliances/base.py +++ b/pyhon/appliances/base.py @@ -2,12 +2,14 @@ class ApplianceBase: def __init__(self, appliance): self.parent = appliance - def data(self, data): + def attributes(self, data): program_name = "No Program" - if program := int(data["attributes"]["parameters"].get("prCode", "0")): - if ids := self.parent.settings["startProgram.program"].ids: - program_name = ids.get(program, program_name) + if program := int(data["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) data["programName"] = program_name + return data def settings(self, settings): return settings diff --git a/pyhon/appliances/dw.py b/pyhon/appliances/dw.py index 635c73b..6e980b7 100644 --- a/pyhon/appliances/dw.py +++ b/pyhon/appliances/dw.py @@ -2,9 +2,9 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": - data["attributes"]["parameters"]["machMode"] = "0" - data["active"] = bool(data.get("attributes", {}).get("activity")) + def attributes(self, data): + data = super().attributes(data) + if data["lastConnEvent"]["category"] == "DISCONNECTED": + data["parameters"]["machMode"] = "0" + data["active"] = bool(data.get("activity")) return data diff --git a/pyhon/appliances/ov.py b/pyhon/appliances/ov.py index f09e45f..5558690 100644 --- a/pyhon/appliances/ov.py +++ b/pyhon/appliances/ov.py @@ -2,17 +2,17 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": - data["attributes"]["parameters"]["temp"] = "0" - data["attributes"]["parameters"]["onOffStatus"] = "0" - data["attributes"]["parameters"]["remoteCtrValid"] = "0" - data["attributes"]["parameters"]["remainingTimeMM"] = "0" + 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["active"] = data["attributes"]["parameters"]["onOffStatus"] == "1" + data["active"] = data["parameters"]["onOffStatus"] == "1" - if program := int(data["attributes"]["parameters"]["prCode"]): + if program := int(data["parameters"]["prCode"]): ids = self.parent.settings["startProgram.program"].ids data["programName"] = ids.get(program, "") diff --git a/pyhon/appliances/ref.py b/pyhon/appliances/ref.py index 1c9f31a..c83982f 100644 --- a/pyhon/appliances/ref.py +++ b/pyhon/appliances/ref.py @@ -2,20 +2,20 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["parameters"]["holidayMode"] == "1": + def attributes(self, data): + data = super().attributes(data) + if data["parameters"]["holidayMode"] == "1": data["modeZ1"] = "holiday" - elif data["attributes"]["parameters"]["intelligenceMode"] == "1": + elif data["parameters"]["intelligenceMode"] == "1": data["modeZ1"] = "auto_set" - elif data["attributes"]["parameters"]["quickModeZ1"] == "1": + elif data["parameters"]["quickModeZ1"] == "1": data["modeZ1"] = "super_cool" else: data["modeZ1"] = "no_mode" - if data["attributes"]["parameters"]["quickModeZ2"] == "1": + if data["parameters"]["quickModeZ2"] == "1": data["modeZ2"] = "super_freeze" - elif data["attributes"]["parameters"]["intelligenceMode"] == "1": + elif data["parameters"]["intelligenceMode"] == "1": data["modeZ2"] = "auto_set" else: data["modeZ2"] = "no_mode" diff --git a/pyhon/appliances/td.py b/pyhon/appliances/td.py index 09dc34e..ee21748 100644 --- a/pyhon/appliances/td.py +++ b/pyhon/appliances/td.py @@ -3,12 +3,12 @@ from pyhon.parameter.fixed import HonParameterFixed class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": - data["attributes"]["parameters"]["machMode"] = "0" - data["active"] = bool(data.get("attributes", {}).get("activity")) - data["pause"] = data["attributes"]["parameters"]["machMode"] == "3" + def attributes(self, data): + data = super().attributes(data) + if data["lastConnEvent"]["category"] == "DISCONNECTED": + data["parameters"]["machMode"] = "0" + data["active"] = bool(data.get("activity")) + data["pause"] = data["parameters"]["machMode"] == "3" return data def settings(self, settings): diff --git a/pyhon/appliances/wd.py b/pyhon/appliances/wd.py index 3d864f1..ee4ccac 100644 --- a/pyhon/appliances/wd.py +++ b/pyhon/appliances/wd.py @@ -2,12 +2,12 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": - data["attributes"]["parameters"]["machMode"] = "0" - data["active"] = bool(data.get("attributes", {}).get("activity")) - data["pause"] = data["attributes"]["parameters"]["machMode"] == "3" + def attributes(self, data): + data = super().attributes(data) + if data["lastConnEvent"]["category"] == "DISCONNECTED": + data["parameters"]["machMode"] = "0" + data["active"] = bool(data.get("activity")) + data["pause"] = data["parameters"]["machMode"] == "3" return data def settings(self, settings): diff --git a/pyhon/appliances/wm.py b/pyhon/appliances/wm.py index 3d864f1..ee4ccac 100644 --- a/pyhon/appliances/wm.py +++ b/pyhon/appliances/wm.py @@ -2,12 +2,12 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): - def data(self, data): - super().data(data) - if data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": - data["attributes"]["parameters"]["machMode"] = "0" - data["active"] = bool(data.get("attributes", {}).get("activity")) - data["pause"] = data["attributes"]["parameters"]["machMode"] == "3" + def attributes(self, data): + data = super().attributes(data) + if data["lastConnEvent"]["category"] == "DISCONNECTED": + data["parameters"]["machMode"] = "0" + data["active"] = bool(data.get("activity")) + data["pause"] = data["parameters"]["machMode"] == "3" return data def settings(self, settings): diff --git a/pyhon/commands.py b/pyhon/commands.py index ed13a29..a78e1b8 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -110,6 +110,7 @@ class HonCommand: async def send(self) -> bool: params = self.parameter_groups.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) + self.appliance.sync_to_params(self.name) return await self.api.send_command( self._appliance, self._name, params, ancillary_params ) diff --git a/setup.py b/setup.py index 7d2a56d..b350076 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as f: setup( name="pyhOn", - version="0.12.1", + version="0.12.2", author="Andre Basche", description="Control hOn devices with python", long_description=long_description,