Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions pyintesishome2/intesisbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,26 @@ async def _set_value(self, device_id, uid, value):
"""Internal method to send a value to the device."""
raise NotImplementedError()

async def _send_command(self, command: str):
async def _send_command(self, command: str, *, wait_for_response: bool = True):
try:
_LOGGER.debug("Preparing to send command: %s", command)
self._received_response.clear()
if not self._writer:
if wait_for_response:
self._received_response.clear()
if not self._writer or self._writer.is_closing():
_LOGGER.error("No writer available. Cannot send command.")
self._connected = False
await self.stop()
if wait_for_response:
self._received_response.set()
return
_LOGGER.debug("Writer state: %r", self._writer)
encoded_command = command.encode("ascii")
_LOGGER.debug("Encoded command: %r (length: %d)", encoded_command, len(encoded_command))
self._writer.write(encoded_command)
await self._writer.drain()
if not wait_for_response:
_LOGGER.debug("Command sent without waiting for response.")
return
_LOGGER.debug("Command sent and drained. Waiting for response event.")
timeout = 15.0
start_time = asyncio.get_event_loop().time()
Expand All @@ -100,6 +108,8 @@ async def _send_command(self, command: str):
_LOGGER.debug("Response event set! Command succeeded.")
else:
_LOGGER.error("Response event was never set. Command failed.")
except asyncio.CancelledError:
raise
except OSError as exc:
_LOGGER.error("%s Exception. %s / %s", type(exc), exc.args, exc)
except Exception as exc:
Expand Down Expand Up @@ -161,6 +171,18 @@ async def _data_received(self):
finally:
self._connected = False
self._connecting = False
if self._keepalive_task:
await self._cancel_task_if_exists(self._keepalive_task)
self._keepalive_task = None
if self._writer:
self._writer.close()
try:
await self._writer.wait_closed()
except Exception as exc: # pylint: disable=broad-except
_LOGGER.debug("Error while waiting for writer to close: %s", exc)
self._writer = None
self._reader = None
self._receive_task = None
await self._send_update_callback()

def _update_device_state(self, device_id, uid, value):
Expand Down Expand Up @@ -196,8 +218,11 @@ async def connect(self):
async def stop(self):
"""Public method for shutting down connectivity."""
self._connected = False
self._connecting = False
await self._cancel_task_if_exists(self._receive_task)
self._receive_task = None
await self._cancel_task_if_exists(self._keepalive_task)
self._keepalive_task = None
if self._writer:
self._writer.close()
await self._writer.wait_closed()
Expand Down
8 changes: 7 additions & 1 deletion pyintesishome2/intesisbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ async def _send_keepalive(self):
try:
while True:
await asyncio.sleep(30)
await self._send_command("GET,1:AMBTEMP")
if not self._connected:
_LOGGER.debug("Stopping keepalive task because connection is inactive")
break
if not self._writer or self._writer.is_closing():
_LOGGER.warning("Keepalive aborted because writer is not available")
break
await self._send_command("GET,1:AMBTEMP", wait_for_response=False)
except asyncio.CancelledError:
_LOGGER.debug("Cancelled the keepalive task")

Expand Down
19 changes: 16 additions & 3 deletions pyintesishome2/intesishome.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,25 @@ async def _send_keepalive(self):
try:
while True:
await asyncio.sleep(120)
_LOGGER.debug("sending keepalive to {self._device_type}")
device_id = str(next(iter(self._devices)))
if not self._connected:
_LOGGER.debug("Stopping keepalive task because connection is inactive")
break
if not self._writer or self._writer.is_closing():
_LOGGER.warning("Keepalive aborted because writer is not available")
break
if not self._devices:
_LOGGER.debug("No devices registered; skipping keepalive ping")
continue
_LOGGER.debug("sending keepalive to %s", self._device_type)
try:
device_id = str(next(iter(self._devices)))
except StopIteration:
_LOGGER.debug("No device id available for keepalive")
continue
message = (
f'{{"command":"get","data":{{"deviceId":{device_id},"uid":10}}}}'
)
await self._send_command(message)
await self._send_command(message, wait_for_response=False)
except asyncio.CancelledError:
_LOGGER.debug("Cancelled the keepalive task")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="pyintesishome2",
version="1.8.7",
version="1.8.8",
description="A python3 library for running asynchronus communications with IntesisHome Smart AC Controllers",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down