Use resumable uploads in Google Drive (#138010)

* Use resumable uploads in Google Drive

* tests
This commit is contained in:
tronikos 2025-02-09 09:30:52 -08:00 committed by Franck Nijhof
parent ff22bbd0e4
commit 201bf95ab8
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
6 changed files with 16 additions and 11 deletions

View File

@ -146,9 +146,10 @@ class DriveClient:
backup.backup_id,
backup_metadata,
)
await self._api.upload_file(
await self._api.resumable_upload_file(
backup_metadata,
open_stream,
backup.size,
timeout=ClientTimeout(total=_UPLOAD_AND_DOWNLOAD_TIMEOUT),
)
_LOGGER.debug(

View File

@ -10,5 +10,5 @@
"iot_class": "cloud_polling",
"loggers": ["google_drive_api"],
"quality_scale": "platinum",
"requirements": ["python-google-drive-api==0.0.2"]
"requirements": ["python-google-drive-api==0.1.0"]
}

2
requirements_all.txt generated
View File

@ -2385,7 +2385,7 @@ python-gc100==1.0.3a0
python-gitlab==1.6.0
# homeassistant.components.google_drive
python-google-drive-api==0.0.2
python-google-drive-api==0.1.0
# homeassistant.components.analytics_insights
python-homeassistant-analytics==0.8.1

View File

@ -1930,7 +1930,7 @@ python-fullykiosk==0.0.14
# python-gammu==3.2.4
# homeassistant.components.google_drive
python-google-drive-api==0.0.2
python-google-drive-api==0.1.0
# homeassistant.components.analytics_insights
python-homeassistant-analytics==0.8.1

View File

@ -136,7 +136,7 @@
}),
),
tuple(
'upload_file',
'resumable_upload_file',
tuple(
dict({
'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}',
@ -151,6 +151,7 @@
}),
}),
"CoreBackupReaderWriter.async_receive_backup.<locals>.open_backup() -> 'AsyncIterator[bytes]'",
987,
),
dict({
'timeout': dict({
@ -207,7 +208,7 @@
}),
),
tuple(
'upload_file',
'resumable_upload_file',
tuple(
dict({
'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}',
@ -222,6 +223,7 @@
}),
}),
"CoreBackupReaderWriter.async_receive_backup.<locals>.open_backup() -> 'AsyncIterator[bytes]'",
987,
),
dict({
'timeout': dict({

View File

@ -281,7 +281,7 @@ async def test_agents_upload(
snapshot: SnapshotAssertion,
) -> None:
"""Test agent upload backup."""
mock_api.upload_file = AsyncMock(return_value=None)
mock_api.resumable_upload_file = AsyncMock(return_value=None)
client = await hass_client()
@ -306,7 +306,7 @@ async def test_agents_upload(
assert f"Uploading backup: {TEST_AGENT_BACKUP.backup_id}" in caplog.text
assert f"Uploaded backup: {TEST_AGENT_BACKUP.backup_id}" in caplog.text
mock_api.upload_file.assert_called_once()
mock_api.resumable_upload_file.assert_called_once()
assert [tuple(mock_call) for mock_call in mock_api.mock_calls] == snapshot
@ -322,7 +322,7 @@ async def test_agents_upload_create_folder_if_missing(
mock_api.create_file = AsyncMock(
return_value={"id": "new folder id", "name": "Home Assistant"}
)
mock_api.upload_file = AsyncMock(return_value=None)
mock_api.resumable_upload_file = AsyncMock(return_value=None)
client = await hass_client()
@ -348,7 +348,7 @@ async def test_agents_upload_create_folder_if_missing(
assert f"Uploaded backup: {TEST_AGENT_BACKUP.backup_id}" in caplog.text
mock_api.create_file.assert_called_once()
mock_api.upload_file.assert_called_once()
mock_api.resumable_upload_file.assert_called_once()
assert [tuple(mock_call) for mock_call in mock_api.mock_calls] == snapshot
@ -359,7 +359,9 @@ async def test_agents_upload_fail(
mock_api: MagicMock,
) -> None:
"""Test agent upload backup fails."""
mock_api.upload_file = AsyncMock(side_effect=GoogleDriveApiError("some error"))
mock_api.resumable_upload_file = AsyncMock(
side_effect=GoogleDriveApiError("some error")
)
client = await hass_client()