Improve update performance

This commit is contained in:
Andre Basche 2023-06-08 19:50:56 +02:00
parent 38d09e2ef5
commit 7b51caecca
10 changed files with 63 additions and 49 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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