Skip to content

Commit

Permalink
Review polling logic for ThinQv1 devices (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 authored Aug 24, 2023
1 parent 4195bc5 commit 0e2985d
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions custom_components/smartthinq_sensors/wideq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ async def start(self) -> None:
"""Start monitor for ThinQ1 device."""
if self._platform_type != PlatformType.THINQ1:
return
self._work_id = None
if self._work_id:
return
self._work_id = await self._client.session.monitor_start(self._device_id)

async def stop(self) -> None:
Expand All @@ -269,32 +270,24 @@ async def _poll_v1(self) -> tuple[Any | None, bool]:
Get the current status data (a bytestring) or None if the
device is not yet ready.
"""
state = None
await self._client.refresh_devices()
if device_data := self._client.get_device(self._device_id):
state = device_data.device_state

if not state or state == "D":
_LOGGER.debug(
"Device %s not reachable, state: %s", self._device_descr, state
)
await self.stop()
return None, False

await self.start()
if not self._work_id:
await self.start()
if not self._work_id:
return None, True
return None, True

try:
return (
await self._client.session.monitor_poll(self._device_id, self._work_id),
True,
result = await self._client.session.monitor_poll(
self._device_id, self._work_id
)
except core_exc.MonitorError:
# Try to restart the task.
await self.stop()
return None, True
result = None
except Exception:
self._work_id = None
raise

if not result:
self._work_id = None

return result, True

async def _poll_v2(self, query_device=False) -> tuple[Any | None, bool]:
"""
Expand Down

0 comments on commit 0e2985d

Please sign in to comment.