Skip to content

Commit

Permalink
Review command for ThinQ1 refrigerators
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Jun 8, 2021
1 parent 6777da4 commit ad835c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
14 changes: 12 additions & 2 deletions custom_components/smartthinq_sensors/wideq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
44 changes: 35 additions & 9 deletions custom_components/smartthinq_sensors/wideq/refrigerator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""------------------for Refrigerator"""
import base64
import json
import logging
from typing import Optional

Expand Down Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand All @@ -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."""

Expand Down

0 comments on commit ad835c0

Please sign in to comment.