Clean-up AsusWRT setup entry (#65860)

This commit is contained in:
ollo69 2022-02-06 09:35:49 +01:00 committed by GitHub
parent 72601ccf32
commit 1c8a34c349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -123,33 +123,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
router.async_on_close(entry.add_update_listener(update_listener))
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
async def async_close_connection(event):
"""Close AsusWrt connection on HA Stop."""
await router.close()
stop_listener = hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, async_close_connection
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
DATA_ASUSWRT: router,
"stop_listener": stop_listener,
}
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_ASUSWRT: router}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
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]["stop_listener"]()
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
router = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]
await router.close()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok