Skip to content

Commit be30743

Browse files
fix: warning for blocking event loop when downloading backups (#169)
* Fix: Address blocking file operation in log (#145) - Resolved Home Assistant warning about blocking file operations in the `auto_backup` integration (#145). - Updated `handlers.py` to replace synchronous `open` calls with `aiofiles.open` for non-blocking file writes. - Improved compatibility with Home Assistant's asyncio architecture, preventing event loop warnings. - Verified changes to ensure integration functionality remains intact. * chore: remove .DS_Store files * refactor: format with black * chore: add aiofiles dependency --------- Co-authored-by: Josh Willox <joshwillox@gmail.com>
1 parent f7c7f41 commit be30743

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

custom_components/auto_backup/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import aiofiles
12
import asyncio
23
import logging
34
import shutil
@@ -142,12 +143,12 @@ async def download_backup(
142143
_LOGGER.error("%s return code %d.", command, request.status)
143144
raise HassioAPIError()
144145

145-
with open(destination, "wb") as file:
146+
async with aiofiles.open(destination, "wb") as file:
146147
while True:
147148
chunk = await request.content.read(CHUNK_SIZE)
148149
if not chunk:
149150
break
150-
file.write(chunk)
151+
await file.write(chunk)
151152

152153
_LOGGER.info("Downloaded backup '%s' to '%s'", slug, destination)
153154
return

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description = ""
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
8+
"aiofiles>=24.1.0",
89
"colorlog>=6.9.0",
910
"homeassistant>=2024.11.2",
1011
]

uv.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)