Skip to content

Commit 717d3e3

Browse files
committed
raise on unable to connect multiple times
1 parent b3dd59d commit 717d3e3

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

kasa/protocol.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ async def _connect(self, timeout: int) -> bool:
7777
return True
7878

7979
with contextlib.suppress(Exception):
80+
self.reader = self.writer = None
8081
task = asyncio.open_connection(
8182
self.host, TPLinkSmartHomeProtocol.DEFAULT_PORT
8283
)
@@ -123,6 +124,11 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
123124
"""Try to query a device."""
124125
for retry in range(retry_count + 1):
125126
if not await self._connect(timeout):
127+
if retry >= retry_count:
128+
_LOGGER.debug("Giving up after %s retries", retry)
129+
raise SmartDeviceException(
130+
f"Unable to connect to the device: {self.host}"
131+
)
126132
continue
127133

128134
try:
@@ -136,7 +142,7 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
136142
if retry >= retry_count:
137143
_LOGGER.debug("Giving up after %s retries", retry)
138144
raise SmartDeviceException(
139-
"Unable to query the device: %s" % ex
145+
f"Unable to query the device: {ex}"
140146
) from ex
141147

142148
_LOGGER.debug("Unable to query the device, retrying: %s", ex)

0 commit comments

Comments
 (0)