Skip to content

Commit

Permalink
Merge pull request #29 from geertmeersman/dev-current
Browse files Browse the repository at this point in the history
fix: async for blocking file system operations
  • Loading branch information
geertmeersman authored Oct 8, 2024
2 parents b6efd73 + 23a5ece commit f496bd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
34 changes: 0 additions & 34 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

30 changes: 18 additions & 12 deletions custom_components/yoin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import random

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.storage import STORAGE_DIR, Store
Expand All @@ -21,11 +17,7 @@

from .client import YoinClient
from .const import COORDINATOR_MIN_UPDATE_INTERVAL, DOMAIN, PLATFORMS
from .exceptions import (
BadCredentialsException,
YoinException,
YoinServiceException,
)
from .exceptions import BadCredentialsException, YoinException, YoinServiceException
from .models import YoinItem

_LOGGER = logging.getLogger(__name__)
Expand All @@ -47,8 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id].setdefault(platform, set())

client = YoinClient(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD]
username=entry.data[CONF_USERNAME], password=entry.data[CONF_PASSWORD]
)

storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
Expand Down Expand Up @@ -78,9 +69,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

# 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

0 comments on commit f496bd9

Please sign in to comment.