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 all commits
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
37 changes: 30 additions & 7 deletions custom_components/robonect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,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(missing_ok=True) # Unlink (delete) the storage file

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 hass.async_add_executor_job(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(missing_ok=True) # Unlink (delete) the storage file

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 hass.async_add_executor_job(remove_storage_files)

return unload_ok


Expand Down
Loading