Switch to using entry.async_on_remove (#52952)

pull/52823/head
Raman Gupta 2021-07-13 04:31:49 -04:00 committed by GitHub
parent a021d7d628
commit 9cbf88d944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 34 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import asyncio
from collections import defaultdict
from typing import Callable
from async_timeout import timeout
from zwave_js_server.client import Client as ZwaveClient
@ -61,7 +60,6 @@ from .const import (
CONF_USE_ADDON,
DATA_CLIENT,
DATA_PLATFORM_SETUP,
DATA_UNSUBSCRIBE,
DOMAIN,
EVENT_DEVICE_ADDED_TO_REGISTRY,
LOGGER,
@ -126,9 +124,7 @@ async def async_setup_entry( # noqa: C901
ent_reg = entity_registry.async_get(hass)
entry_hass_data: dict = hass.data[DOMAIN].setdefault(entry.entry_id, {})
unsubscribe_callbacks: list[Callable] = []
entry_hass_data[DATA_CLIENT] = client
entry_hass_data[DATA_UNSUBSCRIBE] = unsubscribe_callbacks
entry_hass_data[DATA_PLATFORM_SETUP] = {}
registered_unique_ids: dict[str, dict[str, set[str]]] = defaultdict(dict)
@ -181,7 +177,7 @@ async def async_setup_entry( # noqa: C901
# add listener for value updated events if necessary
if value_updates_disc_info:
unsubscribe_callbacks.append(
entry.async_on_unload(
node.on(
"value updated",
lambda event: async_on_value_updated(
@ -191,14 +187,14 @@ async def async_setup_entry( # noqa: C901
)
# add listener for stateless node value notification events
unsubscribe_callbacks.append(
entry.async_on_unload(
node.on(
"value notification",
lambda event: async_on_value_notification(event["value_notification"]),
)
)
# add listener for stateless node notification events
unsubscribe_callbacks.append(
entry.async_on_unload(
node.on(
"notification",
lambda event: async_on_notification(event["notification"]),
@ -400,7 +396,7 @@ async def async_setup_entry( # noqa: C901
client_listen(hass, entry, client, driver_ready)
)
entry_hass_data[DATA_CLIENT_LISTEN_TASK] = listen_task
unsubscribe_callbacks.append(
entry.async_on_unload(
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, handle_ha_shutdown)
)
@ -442,7 +438,7 @@ async def async_setup_entry( # noqa: C901
)
# listen for new nodes being added to the mesh
unsubscribe_callbacks.append(
entry.async_on_unload(
client.driver.controller.on(
"node added",
lambda event: hass.async_create_task(
@ -452,7 +448,7 @@ async def async_setup_entry( # noqa: C901
)
# listen for nodes being removed from the mesh
# NOTE: This will not remove nodes that were removed when HA was not running
unsubscribe_callbacks.append(
entry.async_on_unload(
client.driver.controller.on(
"node removed", lambda event: async_on_node_removed(event["node"])
)
@ -515,9 +511,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
info = hass.data[DOMAIN][entry.entry_id]
for unsub in info[DATA_UNSUBSCRIBE]:
unsub()
tasks = []
for platform, task in info[DATA_PLATFORM_SETUP].items():
if task.done():

View File

@ -48,13 +48,14 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
CONF_DATA_COLLECTION_OPTED_IN,
DATA_CLIENT,
DATA_UNSUBSCRIBE,
DOMAIN,
EVENT_DEVICE_ADDED_TO_REGISTRY,
)
from .helpers import async_enable_statistics, update_data_collection_preference
from .services import BITMASK_SCHEMA
DATA_UNSUBSCRIBE = "unsubs"
# general API constants
ID = "id"
ENTRY_ID = "entry_id"

View File

@ -27,7 +27,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -249,7 +249,7 @@ async def async_setup_entry(
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{BINARY_SENSOR_DOMAIN}",

View File

@ -59,7 +59,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.temperature import convert_temperature
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
from .helpers import get_value_of_zwave_value
@ -121,7 +121,7 @@ async def async_setup_entry(
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{CLIMATE_DOMAIN}",

View File

@ -13,7 +13,6 @@ CONF_DATA_COLLECTION_OPTED_IN = "data_collection_opted_in"
DOMAIN = "zwave_js"
DATA_CLIENT = "client"
DATA_UNSUBSCRIBE = "unsubs"
DATA_PLATFORM_SETUP = "platform_setup"
EVENT_DEVICE_ADDED_TO_REGISTRY = f"{DOMAIN}_device_added_to_registry"

View File

@ -23,7 +23,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -57,7 +57,7 @@ async def async_setup_entry(
entities.append(ZWaveCover(config_entry, client, info))
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{COVER_DOMAIN}",

View File

@ -21,7 +21,7 @@ from homeassistant.util.percentage import (
ranged_value_to_percentage,
)
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -45,7 +45,7 @@ async def async_setup_entry(
entities.append(ZwaveFan(config_entry, client, info))
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{FAN_DOMAIN}",

View File

@ -27,7 +27,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -62,7 +62,7 @@ async def async_setup_entry(
light = ZwaveLight(config_entry, client, info)
async_add_entities([light])
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{LIGHT_DOMAIN}",

View File

@ -25,7 +25,7 @@ from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -62,7 +62,7 @@ async def async_setup_entry(
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass, f"{DOMAIN}_{config_entry.entry_id}_add_{LOCK_DOMAIN}", async_add_lock
)

View File

@ -9,7 +9,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -29,7 +29,7 @@ async def async_setup_entry(
entities.append(ZwaveNumberEntity(config_entry, client, info))
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{NUMBER_DOMAIN}",

View File

@ -29,7 +29,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
from .helpers import get_device_id
@ -73,7 +73,7 @@ async def async_setup_entry(
"""Add node status sensor."""
async_add_entities([ZWaveNodeStatusSensor(config_entry, client, node)])
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{SENSOR_DOMAIN}",
@ -81,7 +81,7 @@ async def async_setup_entry(
)
)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_node_status_sensor",

View File

@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
@ -44,7 +44,7 @@ async def async_setup_entry(
async_add_entities(entities)
hass.data[DOMAIN][config_entry.entry_id][DATA_UNSUBSCRIBE].append(
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{SWITCH_DOMAIN}",