diff --git a/roborock/devices/transport/local_channel.py b/roborock/devices/transport/local_channel.py index 413a90240..4f4f4356e 100644 --- a/roborock/devices/transport/local_channel.py +++ b/roborock/devices/transport/local_channel.py @@ -187,7 +187,8 @@ async def connect(self) -> None: loop = get_running_loop() protocol = _LocalProtocol(self._data_received, self._connection_lost) try: - self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT) + async with asyncio.timeout(_TIMEOUT): + self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT) self._is_connected = True except OSError as e: raise RoborockConnectionException(f"Failed to connect to {self._host}:{_PORT}") from e diff --git a/tests/devices/transport/test_local_channel.py b/tests/devices/transport/test_local_channel.py index 959a021d9..efe2fd50e 100644 --- a/tests/devices/transport/test_local_channel.py +++ b/tests/devices/transport/test_local_channel.py @@ -107,6 +107,23 @@ async def test_connection_failure(local_channel: LocalChannel, mock_loop: Mock) assert local_channel._is_connected is False +async def test_connection_timeout(local_channel: LocalChannel, mock_loop: Mock) -> None: + """Test that a connection attempt to an unreachable host times out.""" + + async def never_connect(*args: object) -> None: + await asyncio.Event().wait() + + mock_loop.create_connection.side_effect = never_connect + + with ( + patch("roborock.devices.transport.local_channel._TIMEOUT", 0.01), + pytest.raises(RoborockConnectionException, match="Failed to connect to 192.168.1.100:58867"), + ): + await local_channel.connect() + + assert not local_channel.is_connected + + async def test_close_connection(local_channel: LocalChannel, mock_loop: Mock, mock_transport: Mock) -> None: """Test closing the connection.""" await local_channel.connect()