Avoid blocking the event loop when unloading Monoprice (#116141)

* Avoid blocking the event loop when unloading Monoprice

* Code review suggestions
This commit is contained in:
On Freund 2024-04-27 10:26:11 +03:00 committed by GitHub
parent 244433aeca
commit 97d151d1c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -57,11 +57,25 @@ 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_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
hass.data[DOMAIN].pop(entry.entry_id)
if not unload_ok:
return False
return unload_ok
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
def _cleanup(monoprice) -> None:
"""Destroy the Monoprice object.
Destroying the Monoprice closes the serial connection, do it in an executor so the garbage
collection does not block.
"""
del monoprice
monoprice = hass.data[DOMAIN][entry.entry_id][MONOPRICE_OBJECT]
hass.data[DOMAIN].pop(entry.entry_id)
await hass.async_add_executor_job(_cleanup, monoprice)
return True
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: