Remove yeelight init dispatcher on unloading (#45359)

This commit is contained in:
Xiaonan Shen 2021-01-21 04:10:01 +08:00 committed by GitHub
parent ab62a4ce39
commit e46f1c0a10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,6 +49,7 @@ DATA_CUSTOM_EFFECTS = "custom_effects"
DATA_SCAN_INTERVAL = "scan_interval"
DATA_DEVICE = "device"
DATA_UNSUB_UPDATE_LISTENER = "unsub_update_listener"
DATA_REMOVE_INIT_DISPATCHER = "remove_init_dispatcher"
ATTR_COUNT = "count"
ATTR_ACTION = "action"
@ -182,11 +183,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Yeelight from a config entry."""
async def _initialize(host: str, capabilities: Optional[dict] = None) -> None:
async_dispatcher_connect(
remove_dispatcher = async_dispatcher_connect(
hass,
DEVICE_INITIALIZED.format(host),
_load_platforms,
)
hass.data[DOMAIN][DATA_CONFIG_ENTRIES][entry.entry_id][
DATA_REMOVE_INIT_DISPATCHER
] = remove_dispatcher
device = await _async_get_device(hass, host, entry, capabilities)
hass.data[DOMAIN][DATA_CONFIG_ENTRIES][entry.entry_id][DATA_DEVICE] = device
@ -251,6 +255,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
if unload_ok:
data = hass.data[DOMAIN][DATA_CONFIG_ENTRIES].pop(entry.entry_id)
remove_init_dispatcher = data.get(DATA_REMOVE_INIT_DISPATCHER)
if remove_init_dispatcher is not None:
remove_init_dispatcher()
data[DATA_UNSUB_UPDATE_LISTENER]()
data[DATA_DEVICE].async_unload()
if entry.data[CONF_ID]: