diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index fee4da743c8..2bb25000504 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -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(): diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index 1c303eea053..1e1dc2382fc 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -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" diff --git a/homeassistant/components/zwave_js/binary_sensor.py b/homeassistant/components/zwave_js/binary_sensor.py index 537f4f8e49e..71de7270d9a 100644 --- a/homeassistant/components/zwave_js/binary_sensor.py +++ b/homeassistant/components/zwave_js/binary_sensor.py @@ -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}", diff --git a/homeassistant/components/zwave_js/climate.py b/homeassistant/components/zwave_js/climate.py index 43363538500..1621e87cfab 100644 --- a/homeassistant/components/zwave_js/climate.py +++ b/homeassistant/components/zwave_js/climate.py @@ -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}", diff --git a/homeassistant/components/zwave_js/const.py b/homeassistant/components/zwave_js/const.py index ae390ce4581..2bbe35de664 100644 --- a/homeassistant/components/zwave_js/const.py +++ b/homeassistant/components/zwave_js/const.py @@ -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" diff --git a/homeassistant/components/zwave_js/cover.py b/homeassistant/components/zwave_js/cover.py index e01f2871604..71033277388 100644 --- a/homeassistant/components/zwave_js/cover.py +++ b/homeassistant/components/zwave_js/cover.py @@ -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}", diff --git a/homeassistant/components/zwave_js/fan.py b/homeassistant/components/zwave_js/fan.py index 89b99e90110..71f483c548f 100644 --- a/homeassistant/components/zwave_js/fan.py +++ b/homeassistant/components/zwave_js/fan.py @@ -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}", diff --git a/homeassistant/components/zwave_js/light.py b/homeassistant/components/zwave_js/light.py index 748d199e93f..f3cabe8b6a7 100644 --- a/homeassistant/components/zwave_js/light.py +++ b/homeassistant/components/zwave_js/light.py @@ -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}", diff --git a/homeassistant/components/zwave_js/lock.py b/homeassistant/components/zwave_js/lock.py index 42230a9c267..ad4a736d63e 100644 --- a/homeassistant/components/zwave_js/lock.py +++ b/homeassistant/components/zwave_js/lock.py @@ -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 ) diff --git a/homeassistant/components/zwave_js/number.py b/homeassistant/components/zwave_js/number.py index 2a3c9820a69..e53e5942999 100644 --- a/homeassistant/components/zwave_js/number.py +++ b/homeassistant/components/zwave_js/number.py @@ -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}", diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index 064275e5729..f20a12a519a 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -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", diff --git a/homeassistant/components/zwave_js/switch.py b/homeassistant/components/zwave_js/switch.py index 1fb5480f2a1..ae98ddc563a 100644 --- a/homeassistant/components/zwave_js/switch.py +++ b/homeassistant/components/zwave_js/switch.py @@ -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}",