DSMR: Use entry unload to unsub update listener (#52220)

pull/51824/head^2
Franck Nijhof 2021-06-27 16:58:08 +02:00 committed by GitHub
parent 75d29b3d9e
commit 3f49cdf9bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 8 deletions

View File

@ -5,7 +5,7 @@ from contextlib import suppress
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import DATA_LISTENER, DATA_TASK, DOMAIN, PLATFORMS
from .const import DATA_TASK, DOMAIN, PLATFORMS
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@ -14,9 +14,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = {}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
listener = entry.add_update_listener(async_update_options)
hass.data[DOMAIN][entry.entry_id][DATA_LISTENER] = listener
entry.async_on_unload(entry.add_update_listener(async_update_options))
return True
@ -24,7 +22,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
task = hass.data[DOMAIN][entry.entry_id][DATA_TASK]
listener = hass.data[DOMAIN][entry.entry_id][DATA_LISTENER]
# Cancel the reconnect task
task.cancel()
@ -33,8 +30,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
listener()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -36,7 +36,6 @@ DEFAULT_PRECISION = 3
DEFAULT_RECONNECT_INTERVAL = 30
DEFAULT_TIME_BETWEEN_UPDATE = 30
DATA_LISTENER = "listener"
DATA_TASK = "task"
DEVICE_NAME_ENERGY = "Energy Meter"