Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async for blocking file system operations #213

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions custom_components/robonect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The Robonect component."""

import asyncio
from datetime import timedelta
import logging
from pathlib import Path
Expand Down Expand Up @@ -206,18 +207,41 @@ async def job(service: ServiceCall) -> bool:

async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle removal of pubsub subscriptions created during config flow."""
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
storage.unlink(True)
storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
storage_dir.rmdir()

# Define blocking file operations
def remove_storage_files():
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
storage.unlink(True) # Unlink (delete) the storage file
geertmeersman marked this conversation as resolved.
Show resolved Hide resolved

storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
# If the directory exists and is empty, remove it
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
storage_dir.rmdir()

# Offload the file system operations to a thread
await asyncio.to_thread(remove_storage_files)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:

# Unload the platforms first
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)

# Define blocking file operations
def remove_storage_files():
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
storage.unlink(True) # Unlink (delete) the storage file
geertmeersman marked this conversation as resolved.
Show resolved Hide resolved

storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
# If the directory exists and is empty, remove it
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
storage_dir.rmdir()

# Offload the file system operations to a thread
await asyncio.to_thread(remove_storage_files)
geertmeersman marked this conversation as resolved.
Show resolved Hide resolved

return unload_ok


Expand Down
Loading