Skip to content

Commit

Permalink
Review wash devices commands (issue #489) (#502)
Browse files Browse the repository at this point in the history
* Allow wake-up for Thinq2 device that support stand-by
* Retry command execution to fix timeout
  • Loading branch information
ollo69 authored Feb 19, 2023
1 parent e571d05 commit 8bdb2d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion custom_components/smartthinq_sensors/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ThinQSwitchEntityDescription(SwitchEntityDescription):
value_fn=lambda x: x.is_power_on and not x.device.stand_by,
turn_off_fn=lambda x: x.device.power_off(),
turn_on_fn=lambda x: x.device.wake_up(),
available_fn=lambda x: x.is_power_on,
available_fn=lambda x: x.is_power_on or x.device.stand_by,
),
)
REFRIGERATOR_SWITCH: Tuple[ThinQSwitchEntityDescription, ...] = (
Expand Down
66 changes: 49 additions & 17 deletions custom_components/smartthinq_sensors/wideq/devices/washerDryer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""------------------for Washer and Dryer"""
from __future__ import annotations

import asyncio
import base64
import json
import logging
Expand Down Expand Up @@ -237,7 +238,7 @@ def remote_start_enabled(self) -> bool:
async def power_off(self):
"""Power off the device."""
keys = self._get_cmd_keys(CMD_POWER_OFF)
await self.set(keys[0], keys[1], value=keys[2])
await self.set_with_retry(keys[0], keys[1], value=keys[2], num_retry=2)
self._remote_start_status = None
self._update_status(POWER_STATUS_KEY, STATE_WM_POWER_OFF)

Expand All @@ -247,17 +248,7 @@ async def wake_up(self):
raise InvalidDeviceStatus()

keys = self._get_cmd_keys(CMD_WAKE_UP)
# we retry more times to wakeup
retry = 2
for i in range(retry):
try:
await self.set(keys[0], keys[1], value=keys[2])
except Exception as exc: # pylint: disable=broad-except
_LOGGER.warning(
"Error in wake-up %s, tentative %s: %s", self.name, i, exc
)
if i == retry - 1:
raise
await self.set_with_retry(keys[0], keys[1], value=keys[2], num_retry=2)
self._stand_by = False

async def remote_start(self):
Expand All @@ -266,7 +257,7 @@ async def remote_start(self):
raise InvalidDeviceStatus()

keys = self._get_cmd_keys(CMD_REMOTE_START)
await self.set(keys[0], keys[1], key=keys[2])
await self.set_with_retry(keys[0], keys[1], key=keys[2], num_retry=2)

async def set(
self, ctrl_key, command, *, key=None, value=None, data=None, ctrl_path=None
Expand All @@ -281,6 +272,43 @@ async def set(
ctrl_path=ctrl_path,
)

async def set_with_retry(
self,
ctrl_key,
command,
*,
key=None,
value=None,
data=None,
ctrl_path=None,
num_retry=1,
):
"""Set a device's control for `key` to `value` with retry."""
if num_retry <= 0:
num_retry = 1
for i in range(num_retry):
try:
await self.set(
ctrl_key,
command,
key=key,
value=value,
data=data,
ctrl_path=ctrl_path,
)
return
except Exception as exc: # pylint: disable=broad-except
if i == num_retry - 1:
raise
_LOGGER.debug(
"Device %s, error executing command %s, tentative %s: %s",
self.name,
command,
i,
exc,
)
await asyncio.sleep(1)

def reset_status(self):
tcl_count = None
if self._status:
Expand All @@ -290,10 +318,14 @@ def reset_status(self):

def _set_remote_start_opt(self, res):
"""Save the status to use for remote start."""
self._stand_by = (
self._status.device_features.get(WashDeviceFeatures.STANDBY)
== StateOptions.ON
)
standby_enable = self.model_info.config_value("standbyEnable")
if standby_enable and not self._should_poll:
self._stand_by = not self._status.is_on
else:
self._stand_by = (
self._status.device_features.get(WashDeviceFeatures.STANDBY)
== StateOptions.ON
)
remote_start = self._status.device_features.get(WashDeviceFeatures.REMOTESTART)
if remote_start == StateOptions.ON:
if self._remote_start_status is None:
Expand Down

0 comments on commit 8bdb2d3

Please sign in to comment.