From 71cf7a1b8a4c3322955e13ab55fa63b02c7f622e Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Tue, 11 Jul 2023 17:13:22 +0300 Subject: [PATCH 01/15] Added water heater appliance. Added ability to send only mandatory parameters --- pyhon/appliances/wh.py | 8 ++++++++ pyhon/commands.py | 13 +++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 pyhon/appliances/wh.py diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py new file mode 100644 index 0000000..cc54f29 --- /dev/null +++ b/pyhon/appliances/wh.py @@ -0,0 +1,8 @@ +from typing import Any, Dict + +from pyhon.appliances.base import ApplianceBase + +class Appliance(ApplianceBase): + def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]: + data = super().attributes(data) + data["active"] = data.get("onOffStatus") == 1 \ No newline at end of file diff --git a/pyhon/commands.py b/pyhon/commands.py index a00b123..897d4b4 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -75,6 +75,14 @@ class HonCommand: for name, parameter in self._parameters.items(): result.setdefault(parameter.group, {})[name] = parameter.intern_value return result + + @property + def mandatory_parameter_groups(self) -> Dict[str, Dict[str, Union[str, float]]]: + result: Dict[str, Dict[str, Union[str, float]]] = {} + for name, parameter in self._parameters.items(): + if parameter.mandatory: + result.setdefault(parameter.group, {})[name] = parameter.intern_value + return result @property def parameter_value(self) -> Dict[str, Union[str, float]]: @@ -111,8 +119,9 @@ class HonCommand: name = "program" if "PROGRAM" in self._category_name else "category" self._parameters[name] = HonParameterProgram(name, self, "custom") - async def send(self) -> bool: - params = self.parameter_groups.get("parameters", {}) + async def send(self, onlyMandatory: bool = False) -> bool: + grouped_params = self.mandatory_parameter_groups if onlyMandatory else self.parameter_groups + params = grouped_params.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) ancillary_params.pop("programRules", None) self.appliance.sync_command_to_params(self.name) -- 2.39.2 From a9fd05f3f5874d1fcbb2b5e38f06bd521ca9d9b1 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Tue, 11 Jul 2023 17:40:04 +0300 Subject: [PATCH 02/15] fixed build --- pyhon/appliances/wh.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py index cc54f29..9016646 100644 --- a/pyhon/appliances/wh.py +++ b/pyhon/appliances/wh.py @@ -5,4 +5,6 @@ from pyhon.appliances.base import ApplianceBase class Appliance(ApplianceBase): def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]: data = super().attributes(data) - data["active"] = data.get("onOffStatus") == 1 \ No newline at end of file + data["active"] = data["parameters"]["onOffStatus"] == "1" + + return data \ No newline at end of file -- 2.39.2 From 7ac2908671f8552b4292696be3908132b90c74c1 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Tue, 11 Jul 2023 17:49:49 +0300 Subject: [PATCH 03/15] formatting --- pyhon/appliances/wh.py | 2 +- pyhon/commands.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py index 9016646..3af1bd0 100644 --- a/pyhon/appliances/wh.py +++ b/pyhon/appliances/wh.py @@ -7,4 +7,4 @@ class Appliance(ApplianceBase): data = super().attributes(data) data["active"] = data["parameters"]["onOffStatus"] == "1" - return data \ No newline at end of file + return data diff --git a/pyhon/commands.py b/pyhon/commands.py index 897d4b4..2132705 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -75,14 +75,14 @@ class HonCommand: for name, parameter in self._parameters.items(): result.setdefault(parameter.group, {})[name] = parameter.intern_value return result - + @property def mandatory_parameter_groups(self) -> Dict[str, Dict[str, Union[str, float]]]: result: Dict[str, Dict[str, Union[str, float]]] = {} for name, parameter in self._parameters.items(): if parameter.mandatory: result.setdefault(parameter.group, {})[name] = parameter.intern_value - return result + return result @property def parameter_value(self) -> Dict[str, Union[str, float]]: -- 2.39.2 From a17692940b4c6fbfd9359dd8bfc438e81b61c470 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Tue, 11 Jul 2023 18:38:49 +0300 Subject: [PATCH 04/15] cleanup --- pyhon/appliances/wh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py index 3af1bd0..4aa1a3c 100644 --- a/pyhon/appliances/wh.py +++ b/pyhon/appliances/wh.py @@ -2,6 +2,7 @@ from typing import Any, Dict from pyhon.appliances.base import ApplianceBase + class Appliance(ApplianceBase): def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]: data = super().attributes(data) -- 2.39.2 From ecdb976c412bb0fa9b65f90e410ab54308dad874 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Tue, 11 Jul 2023 18:47:01 +0300 Subject: [PATCH 05/15] cleanup --- pyhon/commands.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index 2132705..dcc3192 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -82,7 +82,7 @@ class HonCommand: for name, parameter in self._parameters.items(): if parameter.mandatory: result.setdefault(parameter.group, {})[name] = parameter.intern_value - return result + return result @property def parameter_value(self) -> Dict[str, Union[str, float]]: @@ -120,7 +120,8 @@ class HonCommand: self._parameters[name] = HonParameterProgram(name, self, "custom") async def send(self, onlyMandatory: bool = False) -> bool: - grouped_params = self.mandatory_parameter_groups if onlyMandatory else self.parameter_groups + grouped_params = self.mandatory_parameter_groups \ + if onlyMandatory else self.parameter_groups params = grouped_params.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) ancillary_params.pop("programRules", None) -- 2.39.2 From 68b6b7b24d494f787f05fb436252b0404b077933 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Wed, 12 Jul 2023 09:47:53 +0300 Subject: [PATCH 06/15] reformatting --- pyhon/commands.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index dcc3192..85e0988 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -82,7 +82,7 @@ class HonCommand: for name, parameter in self._parameters.items(): if parameter.mandatory: result.setdefault(parameter.group, {})[name] = parameter.intern_value - return result + return result @property def parameter_value(self) -> Dict[str, Union[str, float]]: @@ -120,8 +120,9 @@ class HonCommand: self._parameters[name] = HonParameterProgram(name, self, "custom") async def send(self, onlyMandatory: bool = False) -> bool: - grouped_params = self.mandatory_parameter_groups \ - if onlyMandatory else self.parameter_groups + grouped_params = ( + self.mandatory_parameter_groups if onlyMandatory else self.parameter_groups + ) params = grouped_params.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) ancillary_params.pop("programRules", None) -- 2.39.2 From 25fa89552deb7e9154f5c5b705668d6601005774 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Thu, 13 Jul 2023 19:34:49 +0300 Subject: [PATCH 07/15] Added ability to send specific parameters. Useful in case the command has many not mandatory parameters and you want to send only one/few --- pyhon/commands.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pyhon/commands.py b/pyhon/commands.py index 85e0988..99c3818 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -124,6 +124,18 @@ class HonCommand: self.mandatory_parameter_groups if onlyMandatory else self.parameter_groups ) params = grouped_params.get("parameters", {}) + return await self.send_parameters(params) + + async def send_specific(self, param_names: List[str]) -> bool: + params: Dict[str, str | float] = {} + + for key, parameter in self._parameters: + if key in param_names: + params[key] = parameter.value + + return await self.send_parameters(params) + + async def send_parameters(self, params: Dict[str, str | float]) -> bool: ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) ancillary_params.pop("programRules", None) self.appliance.sync_command_to_params(self.name) -- 2.39.2 From e575722dd324b3d8dbd8f7721b63328221ffab4f Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Fri, 14 Jul 2023 11:30:17 +0300 Subject: [PATCH 08/15] cleanup --- pyhon/commands.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index 123362a..fe61129 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -128,11 +128,9 @@ class HonCommand: async def send_specific(self, param_names: List[str]) -> bool: params: Dict[str, str | float] = {} - - for key, parameter in self._parameters: + for key, parameter in self._parameters.items(): if key in param_names: params[key] = parameter.value - return await self.send_parameters(params) async def send_parameters(self, params: Dict[str, str | float]) -> bool: -- 2.39.2 From e2e2fcb6e5615b5f540f40a02611ff00503a56c2 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 17 Jul 2023 10:00:50 +0300 Subject: [PATCH 09/15] Fixed code style --- pyhon/commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index fe61129..cea218b 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -119,9 +119,9 @@ class HonCommand: name = "program" if "PROGRAM" in self._category_name else "category" self._parameters[name] = HonParameterProgram(name, self, "custom") - async def send(self, onlyMandatory: bool = False) -> bool: + async def send(self, only_mandatory: bool = False) -> bool: grouped_params = ( - self.mandatory_parameter_groups if onlyMandatory else self.parameter_groups + self.mandatory_parameter_groups if only_mandatory else self.parameter_groups ) params = grouped_params.get("parameters", {}) return await self.send_parameters(params) -- 2.39.2 From 8a876d953a4520178fe1f655bcca6b479728934f Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 24 Jul 2023 16:16:07 +0300 Subject: [PATCH 10/15] sync_command - fixed typos, skip to sync(actually reset) parameters of different types. Improved WaterHeater appliance --- pyhon/appliance.py | 7 ++----- pyhon/appliances/wh.py | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 996c463..f401f1f 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -13,6 +13,7 @@ from pyhon.commands import HonCommand from pyhon.parameter.base import HonParameter from pyhon.parameter.enum import HonParameterEnum from pyhon.parameter.range import HonParameterRange +from pyhon.parameter.fixed import HonParameterFixed from pyhon.typedefs import Parameter if TYPE_CHECKING: @@ -287,7 +288,7 @@ class HonAppliance: for name, target_param in data.parameters.items(): if not (base_param := base.parameters.get(name)): - return + continue self.sync_parameter(base_param, target_param) def sync_parameter(self, main: Parameter, target: Parameter) -> None: @@ -297,10 +298,6 @@ class HonAppliance: target.max = main.max target.min = main.min target.step = main.step - elif isinstance(target, HonParameterRange): - target.max = int(main.value) - target.min = int(main.value) - target.step = 1 elif isinstance(target, HonParameterEnum): target.values = main.values target.value = main.value diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py index 4aa1a3c..a3cee95 100644 --- a/pyhon/appliances/wh.py +++ b/pyhon/appliances/wh.py @@ -1,11 +1,15 @@ from typing import Any, Dict from pyhon.appliances.base import ApplianceBase - +from pyhon.parameter.base import HonParameter class Appliance(ApplianceBase): def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]: data = super().attributes(data) - data["active"] = data["parameters"]["onOffStatus"] == "1" + parameter = data["parameters"]["onOffStatus"] + data["active"] = parameter.value == 1 if isinstance(parameter, HonParameter) else parameter == 1 return data + + def settings(self, settings: Dict[str, Any]) -> Dict[str, Any]: + return settings \ No newline at end of file -- 2.39.2 From 293dd470793b078621373d8777a92b163997f6d6 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 24 Jul 2023 16:28:32 +0300 Subject: [PATCH 11/15] cleanup --- pyhon/appliance.py | 1 - pyhon/appliances/wh.py | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index f401f1f..ca0cef6 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -13,7 +13,6 @@ from pyhon.commands import HonCommand from pyhon.parameter.base import HonParameter from pyhon.parameter.enum import HonParameterEnum from pyhon.parameter.range import HonParameterRange -from pyhon.parameter.fixed import HonParameterFixed from pyhon.typedefs import Parameter if TYPE_CHECKING: diff --git a/pyhon/appliances/wh.py b/pyhon/appliances/wh.py index a3cee95..7e92ca8 100644 --- a/pyhon/appliances/wh.py +++ b/pyhon/appliances/wh.py @@ -3,13 +3,14 @@ from typing import Any, Dict from pyhon.appliances.base import ApplianceBase from pyhon.parameter.base import HonParameter + class Appliance(ApplianceBase): def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]: data = super().attributes(data) parameter = data["parameters"]["onOffStatus"] - data["active"] = parameter.value == 1 if isinstance(parameter, HonParameter) else parameter == 1 - + is_class = isinstance(parameter, HonParameter) + data["active"] = parameter.value == 1 if is_class else parameter == 1 return data - + def settings(self, settings: Dict[str, Any]) -> Dict[str, Any]: - return settings \ No newline at end of file + return settings -- 2.39.2 From bab8bce8045b02f02df7df65ac742450a0a850ee Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 24 Jul 2023 17:36:03 +0300 Subject: [PATCH 12/15] cleanup --- pyhon/appliance.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index ca0cef6..0d0064e 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -277,7 +277,13 @@ class HonAppliance: _LOGGER.info("Can't set %s - %s", key, error) continue - def sync_command(self, main: str, target: Optional[List[str] | str] = None) -> None: + def sync_command( + self, + main: str, + target: Optional[List[str] | str] = None, + mandatory_only: bool = False, + target_parameters: Optional[List[str]] = None, + ) -> None: base: Optional[HonCommand] = self.commands.get(main) if not base: return @@ -288,6 +294,13 @@ class HonAppliance: for name, target_param in data.parameters.items(): if not (base_param := base.parameters.get(name)): continue + + if mandatory_only and not target_param.mandatory: + continue + + if target_parameters and name not in target_parameters: + continue + self.sync_parameter(base_param, target_param) def sync_parameter(self, main: Parameter, target: Parameter) -> None: -- 2.39.2 From 59943f1d4d9768a56d21657ec6a1238e62753a53 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 24 Jul 2023 18:02:00 +0300 Subject: [PATCH 13/15] clean code style --- pyhon/appliance.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 0d0064e..ef73cd0 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -281,8 +281,7 @@ class HonAppliance: self, main: str, target: Optional[List[str] | str] = None, - mandatory_only: bool = False, - target_parameters: Optional[List[str]] = None, + to_sync: Optional[List[str] | bool] = None, ) -> None: base: Optional[HonCommand] = self.commands.get(main) if not base: @@ -294,13 +293,11 @@ class HonAppliance: for name, target_param in data.parameters.items(): if not (base_param := base.parameters.get(name)): continue - - if mandatory_only and not target_param.mandatory: + if to_sync and ( + (isinstance(to_sync, list) and name not in to_sync) + or not target_param.mandatory + ): continue - - if target_parameters and name not in target_parameters: - continue - self.sync_parameter(base_param, target_param) def sync_parameter(self, main: Parameter, target: Parameter) -> None: -- 2.39.2 From 5d0225e0c3b1646fddb348bea9025edd7f5f89ed Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Mon, 24 Jul 2023 18:10:16 +0300 Subject: [PATCH 14/15] check if base parameter is mandatory --- pyhon/appliance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index ef73cd0..a75bfcd 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -295,7 +295,7 @@ class HonAppliance: continue if to_sync and ( (isinstance(to_sync, list) and name not in to_sync) - or not target_param.mandatory + or not base_param.mandatory ): continue self.sync_parameter(base_param, target_param) -- 2.39.2 From 1ebd83b70d8520f6efe3004ff893ec701465bee2 Mon Sep 17 00:00:00 2001 From: Vadym Melnychuk Date: Thu, 12 Oct 2023 16:33:28 +0300 Subject: [PATCH 15/15] Reverted back sync_command, send mandatory parameters beside with specified --- pyhon/appliance.py | 4 ++++ pyhon/commands.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index a75bfcd..4a2d07c 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -307,6 +307,10 @@ class HonAppliance: target.max = main.max target.min = main.min target.step = main.step + elif isinstance(target, HonParameterRange): + target.max = int(main.value) + target.min = int(main.value) + target.step = 1 elif isinstance(target, HonParameterEnum): target.values = main.values target.value = main.value diff --git a/pyhon/commands.py b/pyhon/commands.py index 68df63a..8c0275a 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -132,7 +132,7 @@ class HonCommand: async def send_specific(self, param_names: List[str]) -> bool: params: Dict[str, str | float] = {} for key, parameter in self._parameters.items(): - if key in param_names: + if key in param_names or parameter.mandatory: params[key] = parameter.value return await self.send_parameters(params) -- 2.39.2