From 17d73cdeb813e0b6278429f7abbbb1fbd6dd9428 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sat, 1 Jul 2023 16:04:34 +0200 Subject: [PATCH] Sync parameter to settings --- pyhon/appliance.py | 25 ++++++++++++++++++++++--- pyhon/commands.py | 2 +- pyhon/parameter/range.py | 4 +++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index c2bcddf..3e79192 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -103,7 +103,8 @@ class HonAppliance: @property def brand(self) -> str: - return self._check_name_zone("brand") + brand = self._check_name_zone("brand") + return brand[0].upper() + brand[1:] @property def nick_name(self) -> str: @@ -165,6 +166,7 @@ class HonAppliance: self._commands = command_loader.commands self._additional_data = command_loader.additional_data self._appliance_model = command_loader.appliance_data + self.sync_params_to_command("settings") async def load_attributes(self) -> None: self._attributes = await self.api.load_attributes(self) @@ -194,6 +196,7 @@ class HonAppliance: ): self._last_update = now await self.load_attributes() + self.sync_params_to_command("settings") @property def command_parameters(self) -> Dict[str, Dict[str, str | float]]: @@ -237,15 +240,31 @@ class HonAppliance: async def data_archive(self, path: Path) -> str: return await diagnose.zip_archive(self, path, anonymous=True) - def sync_to_params(self, command_name: str) -> None: + def sync_command_to_params(self, command_name: str) -> None: if not (command := self.commands.get(command_name)): return - for key, value in self.attributes.get("parameters", {}).items(): + for key in self.attributes.get("parameters", {}): if new := command.parameters.get(key): self.attributes["parameters"][key].update( str(new.intern_value), shield=True ) + def sync_params_to_command(self, command_name: str) -> None: + if not (command := self.commands.get(command_name)): + return + for key in command.setting_keys: + if (new := self.attributes.get("parameters", {}).get(key)) is None: + continue + setting = command.settings[key] + try: + if not isinstance(setting, HonParameterRange): + command.settings[key].value = str(new.value) + else: + command.settings[key].value = float(new.value) + except ValueError as error: + _LOGGER.info("Can't set %s - %s", key, error) + continue + def sync_command(self, main: str, target: Optional[List[str] | str] = None) -> None: base: Optional[HonCommand] = self.commands.get(main) if not base: diff --git a/pyhon/commands.py b/pyhon/commands.py index 7af1f7f..a00b123 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -115,7 +115,7 @@ class HonCommand: params = self.parameter_groups.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) ancillary_params.pop("programRules", None) - self.appliance.sync_to_params(self.name) + self.appliance.sync_command_to_params(self.name) try: result = await self.api.send_command( self._appliance, self._name, params, ancillary_params diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py index e4077d7..e27314e 100644 --- a/pyhon/parameter/range.py +++ b/pyhon/parameter/range.py @@ -49,7 +49,9 @@ class HonParameterRange(HonParameter): @value.setter def value(self, value: str | float) -> None: value = str_to_float(value) - if self.min <= value <= self.max and not (value - self.min) % self.step: + if self.min <= value <= self.max and not ((value - self.min) * 100) % ( + self.step * 100 + ): self._value = value self.check_trigger(value) else: