diff --git a/custom_components/smartthinq_sensors/wideq/device.py b/custom_components/smartthinq_sensors/wideq/device.py index 5c6f530a..3f581362 100644 --- a/custom_components/smartthinq_sensors/wideq/device.py +++ b/custom_components/smartthinq_sensors/wideq/device.py @@ -488,6 +488,12 @@ def reference_name(self, key, value, ref_key="_comment"): return reference[value].get("label") return None + @property + def binary_control_data(self): + """Check that type of control is BINARY(BYTE). + """ + return self._data["ControlWifi"]["type"] == "BINARY(BYTE)" + def get_control_cmd(self, cmd_key, ctrl_key=None): """Get the payload used to send the command.""" control = None @@ -503,7 +509,6 @@ def get_control_cmd(self, cmd_key, ctrl_key=None): def binary_monitor_data(self): """Check that type of monitoring is BINARY(BYTE). """ - return self._data["Monitoring"]["type"] == "BINARY(BYTE)" def decode_monitor_binary(self, data): @@ -718,6 +723,12 @@ def target_key(self, key, value, target): return data.get("targetKey", {}).get(target, {}).get(value) + @property + def binary_control_data(self): + """Check that type of control is BINARY(BYTE). + """ + return False + def get_control_cmd(self, cmd_key, ctrl_key=None): """Get the payload used to send the command.""" control = None @@ -733,7 +744,6 @@ def get_control_cmd(self, cmd_key, ctrl_key=None): def binary_monitor_data(self): """Check that type of monitoring is BINARY(BYTE). """ - return False def decode_monitor_binary(self, data): diff --git a/custom_components/smartthinq_sensors/wideq/refrigerator.py b/custom_components/smartthinq_sensors/wideq/refrigerator.py index 711cee88..146bbda9 100644 --- a/custom_components/smartthinq_sensors/wideq/refrigerator.py +++ b/custom_components/smartthinq_sensors/wideq/refrigerator.py @@ -1,4 +1,6 @@ """------------------for Refrigerator""" +import base64 +import json import logging from typing import Optional @@ -46,7 +48,7 @@ REFR_CTRL_BASIC = ["Control", "basicCtrl"] REFR_STATE_ECO_FRIENDLY = ["EcoFriendly", "ecoFriendly"] -CMD_STATE_ECO_FRIENDLY = [REFR_CTRL_BASIC, ["Set", "basicCtrl"], ["REEF", "ecoFriendly"]] +CMD_STATE_ECO_FRIENDLY = [REFR_CTRL_BASIC, ["SetControl", "basicCtrl"], REFR_STATE_ECO_FRIENDLY] _LOGGER = logging.getLogger(__name__) @@ -83,15 +85,29 @@ def _get_feature_title(self, item_key, def_value): return def_value return FEATURE_DESCR.get(title_value, def_value) - def _prepare_command(self, ctrl_key, command, key, value): - """Prepare command for specific device.""" - if not self.model_info.is_info_v2: - return None - - cmd = self.model_info.get_control_cmd(command, ctrl_key) - if not cmd: - return None + def _prepare_command_v1(self, cmd, key, value): + """Prepare command for specific ThinQ1 device.""" + data_key = "value" + if cmd.get(data_key, "") == "ControlData": + data_key = "data" + str_data = cmd.get(data_key) + + if str_data: + status_data = self._status.data + for dt_key, dt_value in status_data.items(): + if dt_key == key: + dt_value = value + str_data = str_data.replace(f"{{{{{dt_key}}}}}", dt_value) + _LOGGER.debug("Command data content: %s", str_data) + if self.model_info.binary_control_data: + cmd["format"] = "B64" + str_list = json.loads(str_data) + str_data = base64.b64encode(bytes(str_list)).decode("ascii") + cmd[data_key] = str_data + return cmd + def _prepare_command_v2(self, cmd, key, value): + """Prepare command for specific ThinQ2 device.""" data_set = cmd.pop("data", None) if not data_set: return None @@ -106,6 +122,16 @@ def _prepare_command(self, ctrl_key, command, key, value): return cmd + def _prepare_command(self, ctrl_key, command, key, value): + """Prepare command for specific device.""" + cmd = self.model_info.get_control_cmd(command, ctrl_key) + if not cmd: + return None + + if self.model_info.is_info_v2: + return self._prepare_command_v2(cmd, key, value) + return self._prepare_command_v1(cmd, key, value) + def set_eco_friendly(self, turn_on=False): """Switch the echo friendly status."""