|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import logging |
4 | 5 | import socket |
5 | 6 |
|
|
8 | 9 | from roborock.api import RoborockClient |
9 | 10 | from roborock.typing import RoborockCommand |
10 | 11 | from roborock.util import get_running_loop_or_create_one |
| 12 | +from roborock.exceptions import RoborockTimeout |
11 | 13 |
|
12 | 14 | secured_prefix = 199 |
13 | 15 | _LOGGER = logging.getLogger(__name__) |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class RoborockLocalClient(RoborockClient): |
17 | 19 |
|
18 | | - def __init__(self, endpoint: str, device_localkey: dict[str, str]): |
| 20 | + def __init__(self, ip: str, endpoint: str, device_localkey: dict[str, str]): |
19 | 21 | super().__init__(endpoint, device_localkey, True) |
20 | | - self.listener = RoborockSocketListener("192.168.1.232", super()._decode_msg) |
| 22 | + self.listener = RoborockSocketListener(ip, super()._decode_msg) |
21 | 23 |
|
22 | 24 | async def async_connect(self): |
23 | 25 | await self.listener.connect() |
@@ -54,9 +56,14 @@ async def connect(self): |
54 | 56 |
|
55 | 57 | async def send_message(self, data: bytes, local_key: str): |
56 | 58 | response = {} |
57 | | - async with async_timeout.timeout(self.timeout): |
58 | | - await self.loop.sock_sendall(self.socket, data) |
59 | | - while response.get('protocol') != 4: |
60 | | - message = await self.loop.sock_recv(self.socket, 4096) |
61 | | - response = self.on_message(message, local_key) |
| 59 | + try: |
| 60 | + async with async_timeout.timeout(self.timeout): |
| 61 | + await self.loop.sock_sendall(self.socket, data) |
| 62 | + while response.get('protocol') != 4: |
| 63 | + message = await self.loop.sock_recv(self.socket, 4096) |
| 64 | + response = self.on_message(message, local_key) |
| 65 | + except (asyncio.TimeoutError, asyncio.CancelledError): |
| 66 | + raise RoborockTimeout( |
| 67 | + f"Timeout after {self.timeout} seconds waiting for response" |
| 68 | + ) from None |
62 | 69 | return response |
0 commit comments