Use async_on_unload for Nut update_listener (#61589)

This commit is contained in:
ollo69 2021-12-13 21:42:25 +01:00 committed by GitHub
parent 65ec251309
commit cd5fe11b44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 17 deletions

View File

@ -29,7 +29,6 @@ from .const import (
PLATFORMS,
PYNUT_DATA,
PYNUT_UNIQUE_ID,
UNDO_UPDATE_LISTENER,
)
_LOGGER = logging.getLogger(__name__)
@ -80,7 +79,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug("NUT Sensors Available: %s", status)
undo_listener = entry.add_update_listener(_async_update_listener)
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
unique_id = _unique_id_from_status(status)
if unique_id is None:
unique_id = entry.entry_id
@ -90,7 +89,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
COORDINATOR: coordinator,
PYNUT_DATA: data,
PYNUT_UNIQUE_ID: unique_id,
UNDO_UPDATE_LISTENER: undo_listener,
}
device_registry = dr.async_get(hass)
@ -108,6 +106,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
@ -165,18 +170,6 @@ def _unique_id_from_status(status):
return "_".join(unique_id_group)
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)
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class PyNUTData:
"""Stores the data retrieved from NUT.

View File

@ -26,8 +26,6 @@ DOMAIN = "nut"
PLATFORMS = [Platform.SENSOR]
UNDO_UPDATE_LISTENER = "undo_update_listener"
DEFAULT_NAME = "NUT UPS"
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 3493