Skip to content

Commit

Permalink
Avoid creating TimerHandle when already connected (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 21, 2023
1 parent bd2ebea commit f1ff8df
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions aiohomekit/controller/ip/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ async def connection_made(self, secure):
await self.subscribe(self.subscriptions)

async def _ensure_connected(self):
if self._shutdown:
return
"""Ensure we are connected to the device."""
connection = self.connection
if self._shutdown or connection.is_connected:
return

try:
async with asyncio_timeout(10):
await connection.ensure_connection()
Expand All @@ -142,22 +144,21 @@ async def _ensure_connected(self):
last_connector_error, asyncio.TimeoutError
):
raise AccessoryDisconnectedError(
f"Timeout while waiting for connection to device {self.connection.host}:{self.connection.port}"
f"Timeout while waiting for connection to device {connection.host}:{connection.port}"
)
# The exception name is included since otherwise the error message
# is not very helpful as it could be something like `step 3`
raise AccessoryDisconnectedError(
f"Error while connecting to device {self.connection.host}:{self.connection.port}: "
f"Error while connecting to device {connection.host}:{connection.port}: "
f"{last_connector_error} ({type(last_connector_error).__name__})"
)

if not self.connection.is_connected:
if not connection.is_connected:
raise AccessoryDisconnectedError(
f"Ensure connection returned but still not connected: {self.connection.host}:{self.connection.port}"
f"Ensure connection returned but still not connected: {connection.host}:{connection.port}"
)

else:
self._callback_availability_changed(True)
self._callback_availability_changed(True)

async def close(self) -> None:
"""
Expand Down

0 comments on commit f1ff8df

Please sign in to comment.