Skip to content

Commit 37eeb3d

Browse files
fix: async for blocking file system operations (#213)
2 parents ce31822 + 206806f commit 37eeb3d

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

custom_components/robonect/__init__.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,41 @@ async def job(service: ServiceCall) -> bool:
206206

207207
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
208208
"""Handle removal of pubsub subscriptions created during config flow."""
209-
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
210-
storage.unlink(True)
211-
storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
212-
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
213-
storage_dir.rmdir()
209+
210+
# Define blocking file operations
211+
def remove_storage_files():
212+
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
213+
storage.unlink(missing_ok=True) # Unlink (delete) the storage file
214+
215+
storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
216+
# If the directory exists and is empty, remove it
217+
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
218+
storage_dir.rmdir()
219+
220+
# Offload the file system operations to a thread
221+
await hass.async_add_executor_job(remove_storage_files)
214222

215223

216224
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
217225
"""Unload a config entry."""
218-
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
219-
if unload_ok:
226+
227+
# Unload the platforms first
228+
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
220229
hass.data[DOMAIN].pop(entry.entry_id)
230+
231+
# Define blocking file operations
232+
def remove_storage_files():
233+
storage = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}/{entry.entry_id}")
234+
storage.unlink(missing_ok=True) # Unlink (delete) the storage file
235+
236+
storage_dir = Path(f"{hass.config.path(STORAGE_DIR)}/{DOMAIN}")
237+
# If the directory exists and is empty, remove it
238+
if storage_dir.is_dir() and not any(storage_dir.iterdir()):
239+
storage_dir.rmdir()
240+
241+
# Offload the file system operations to a thread
242+
await hass.async_add_executor_job(remove_storage_files)
243+
221244
return unload_ok
222245

223246

0 commit comments

Comments
 (0)