mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use async_on_unload for Nut update_listener (#61589)
This commit is contained in:
parent
65ec251309
commit
cd5fe11b44
@ -29,7 +29,6 @@ from .const import (
|
|||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
PYNUT_DATA,
|
PYNUT_DATA,
|
||||||
PYNUT_UNIQUE_ID,
|
PYNUT_UNIQUE_ID,
|
||||||
UNDO_UPDATE_LISTENER,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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)
|
_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)
|
unique_id = _unique_id_from_status(status)
|
||||||
if unique_id is None:
|
if unique_id is None:
|
||||||
unique_id = entry.entry_id
|
unique_id = entry.entry_id
|
||||||
@ -90,7 +89,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
COORDINATOR: coordinator,
|
COORDINATOR: coordinator,
|
||||||
PYNUT_DATA: data,
|
PYNUT_DATA: data,
|
||||||
PYNUT_UNIQUE_ID: unique_id,
|
PYNUT_UNIQUE_ID: unique_id,
|
||||||
UNDO_UPDATE_LISTENER: undo_listener,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
@ -108,6 +106,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
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):
|
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
@ -165,18 +170,6 @@ def _unique_id_from_status(status):
|
|||||||
return "_".join(unique_id_group)
|
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:
|
class PyNUTData:
|
||||||
"""Stores the data retrieved from NUT.
|
"""Stores the data retrieved from NUT.
|
||||||
|
|
||||||
|
@ -26,8 +26,6 @@ DOMAIN = "nut"
|
|||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
UNDO_UPDATE_LISTENER = "undo_update_listener"
|
|
||||||
|
|
||||||
DEFAULT_NAME = "NUT UPS"
|
DEFAULT_NAME = "NUT UPS"
|
||||||
DEFAULT_HOST = "localhost"
|
DEFAULT_HOST = "localhost"
|
||||||
DEFAULT_PORT = 3493
|
DEFAULT_PORT = 3493
|
||||||
|
Loading…
x
Reference in New Issue
Block a user