Skip to content

Commit

Permalink
Refactor timeout on sending commands to potentially fix #57
Browse files Browse the repository at this point in the history
  • Loading branch information
jnimmo committed Dec 17, 2024
1 parent e768298 commit e1c1ebd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 12 additions & 9 deletions pyintesishome/intesisbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ async def _send_command(self, command: str):
if self._writer:
self._writer.write(command.encode("ascii"))
await self._writer.drain()
try:
await asyncio.wait_for(
self._received_response.wait(),
timeout=5.0,
)
except asyncio.TimeoutError:
print("oops took longer than 5s!")
await self.stop()
timeout = 5.0
start_time = asyncio.get_event_loop().time()
while not self._received_response.is_set():
if asyncio.get_event_loop().time() - start_time > timeout:
_LOGGER.error("Timeout waiting for response")
await self.stop()
break
await asyncio.sleep(0.1)
except OSError as exc:
_LOGGER.error("%s Exception. %s / %s", type(exc), exc.args, exc)
except Exception as exc:
Expand All @@ -102,11 +102,14 @@ async def _data_received(self):
break
data = raw_data.decode("ascii")
_LOGGER.debug("Received: %s", data)
await self._parse_response(data)

await self._parse_response(data)

Check warning on line 107 in pyintesishome/intesisbase.py

View workflow job for this annotation

GitHub Actions / Check flake8

blank line contains whitespace
if not self._received_response.is_set():
_LOGGER.debug("Resolving set_value's await")
self._received_response.set()

Check warning on line 111 in pyintesishome/intesisbase.py

View workflow job for this annotation

GitHub Actions / Check flake8

blank line contains whitespace

except IncompleteReadError:

Check failure on line 113 in pyintesishome/intesisbase.py

View workflow job for this annotation

GitHub Actions / Check flake8

too many blank lines (2)
_LOGGER.debug(
"pyIntesisHome lost connection to the %s server", self._device_type
Expand Down
8 changes: 7 additions & 1 deletion pyintesishome/intesishome.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ async def _parse_response(self, decoded_data):
elif resp["command"] == "rssi":
# Wireless strength has changed
self._update_rssi(resp["data"]["deviceId"], resp["data"]["value"])
else:
_LOGGER.debug("Unexpected command received: %s", resp["command"])
# Ensure the _received_response event is set
if not self._received_response.is_set():
_LOGGER.debug("Setting _received_response event")
self._received_response.set()
return

async def _send_keepalive(self):
try:
while True:
await asyncio.sleep(240)
await asyncio.sleep(120)
_LOGGER.debug("sending keepalive to {self._device_type}")
device_id = str(next(iter(self._devices)))
message = (
Expand Down

0 comments on commit e1c1ebd

Please sign in to comment.