@@ -206,18 +206,41 @@ async def job(service: ServiceCall) -> bool:
206
206
207
207
async def async_remove_entry (hass : HomeAssistant , entry : ConfigEntry ) -> None :
208
208
"""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 )
214
222
215
223
216
224
async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
217
225
"""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 ):
220
229
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
+
221
244
return unload_ok
222
245
223
246
0 commit comments