From 1fe66df962c0344df5649a046ed6a65965b204a5 Mon Sep 17 00:00:00 2001 From: Rob Bierbooms Date: Wed, 4 Nov 2020 11:30:46 +0100 Subject: [PATCH] Cleanup dispatchers when unloading rfxtrx (#42803) * Cleanup dispatchers * Fix name * Refactor code * Fix typo in function description * Fix pylint error * Change function definition * Move cleanup_callbacks data to domain --- homeassistant/components/rfxtrx/__init__.py | 20 +++++++++++++++++-- .../components/rfxtrx/binary_sensor.py | 8 ++------ homeassistant/components/rfxtrx/const.py | 4 ++++ homeassistant/components/rfxtrx/cover.py | 6 ++---- homeassistant/components/rfxtrx/light.py | 6 ++---- homeassistant/components/rfxtrx/sensor.py | 6 ++---- homeassistant/components/rfxtrx/switch.py | 6 ++---- 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 9c0615fdb5e..5f8d14710e3 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -49,6 +49,9 @@ from .const import ( CONF_OFF_DELAY, CONF_REMOVE_DEVICE, CONF_SIGNAL_REPETITIONS, + DATA_CLEANUP_CALLBACKS, + DATA_LISTENER, + DATA_RFXOBJECT, DEVICE_PACKET_TYPE_LIGHTING4, EVENT_RFXTRX_EVENT, SERVICE_SEND, @@ -93,8 +96,6 @@ DATA_TYPES = OrderedDict( ) _LOGGER = logging.getLogger(__name__) -DATA_RFXOBJECT = "rfxobject" -DATA_LISTENER = "ha_stop" def _bytearray_string(data): @@ -188,6 +189,8 @@ async def async_setup_entry(hass, entry: config_entries.ConfigEntry): """Set up the RFXtrx component.""" hass.data.setdefault(DOMAIN, {}) + hass.data[DOMAIN][DATA_CLEANUP_CALLBACKS] = [] + await async_setup_internal(hass, entry) for domain in DOMAINS: @@ -212,12 +215,17 @@ async def async_unload_entry(hass, entry: config_entries.ConfigEntry): hass.services.async_remove(DOMAIN, SERVICE_SEND) + for cleanup_callback in hass.data[DOMAIN][DATA_CLEANUP_CALLBACKS]: + cleanup_callback() + listener = hass.data[DOMAIN][DATA_LISTENER] listener() rfx_object = hass.data[DOMAIN][DATA_RFXOBJECT] await hass.async_add_executor_job(rfx_object.close_connection) + hass.data.pop(DOMAIN) + return True @@ -428,6 +436,14 @@ def get_device_id(device, data_bits=None): return (f"{device.packettype:x}", f"{device.subtype:x}", id_string) +def connect_auto_add(hass, entry_data, callback_fun): + """Connect to dispatcher for automatic add.""" + if entry_data[CONF_AUTOMATIC_ADD]: + hass.data[DOMAIN][DATA_CLEANUP_CALLBACKS].append( + hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, callback_fun) + ) + + class RfxtrxEntity(RestoreEntity): """Represents a Rfxtrx device. diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index 7fe89e747bc..78eb49740d5 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -19,11 +19,10 @@ from homeassistant.core import callback from homeassistant.helpers import event as evt from . import ( - CONF_AUTOMATIC_ADD, CONF_DATA_BITS, CONF_OFF_DELAY, - SIGNAL_EVENT, RfxtrxEntity, + connect_auto_add, find_possible_pt2262_device, get_device_id, get_pt2262_cmd, @@ -147,10 +146,7 @@ async def async_setup_entry( async_add_entities([sensor]) # Subscribe to main RFXtrx events - if discovery_info[CONF_AUTOMATIC_ADD]: - hass.helpers.dispatcher.async_dispatcher_connect( - SIGNAL_EVENT, binary_sensor_update - ) + connect_auto_add(hass, discovery_info, binary_sensor_update) class RfxtrxBinarySensor(RfxtrxEntity, BinarySensorEntity): diff --git a/homeassistant/components/rfxtrx/const.py b/homeassistant/components/rfxtrx/const.py index 404d344cc71..28aec125644 100644 --- a/homeassistant/components/rfxtrx/const.py +++ b/homeassistant/components/rfxtrx/const.py @@ -33,3 +33,7 @@ SERVICE_SEND = "send" DEVICE_PACKET_TYPE_LIGHTING4 = 0x13 EVENT_RFXTRX_EVENT = "rfxtrx_event" + +DATA_RFXOBJECT = "rfxobject" +DATA_LISTENER = "ha_stop" +DATA_CLEANUP_CALLBACKS = "cleanup_callbacks" diff --git a/homeassistant/components/rfxtrx/cover.py b/homeassistant/components/rfxtrx/cover.py index 86950308f55..dfbaa60f589 100644 --- a/homeassistant/components/rfxtrx/cover.py +++ b/homeassistant/components/rfxtrx/cover.py @@ -6,12 +6,11 @@ from homeassistant.const import CONF_DEVICES, STATE_OPEN from homeassistant.core import callback from . import ( - CONF_AUTOMATIC_ADD, CONF_DATA_BITS, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, - SIGNAL_EVENT, RfxtrxCommandEntity, + connect_auto_add, get_device_id, get_rfx_object, ) @@ -81,8 +80,7 @@ async def async_setup_entry( async_add_entities([entity]) # Subscribe to main RFXtrx events - if discovery_info[CONF_AUTOMATIC_ADD]: - hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, cover_update) + connect_auto_add(hass, discovery_info, cover_update) class RfxtrxCover(RfxtrxCommandEntity, CoverEntity): diff --git a/homeassistant/components/rfxtrx/light.py b/homeassistant/components/rfxtrx/light.py index 33ee5ea4748..fd790581eda 100644 --- a/homeassistant/components/rfxtrx/light.py +++ b/homeassistant/components/rfxtrx/light.py @@ -12,12 +12,11 @@ from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.core import callback from . import ( - CONF_AUTOMATIC_ADD, CONF_DATA_BITS, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, - SIGNAL_EVENT, RfxtrxCommandEntity, + connect_auto_add, get_device_id, get_rfx_object, ) @@ -95,8 +94,7 @@ async def async_setup_entry( async_add_entities([entity]) # Subscribe to main RFXtrx events - if discovery_info[CONF_AUTOMATIC_ADD]: - hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, light_update) + connect_auto_add(hass, discovery_info, light_update) class RfxtrxLight(RfxtrxCommandEntity, LightEntity): diff --git a/homeassistant/components/rfxtrx/sensor.py b/homeassistant/components/rfxtrx/sensor.py index 81e0c60e055..c897e164119 100644 --- a/homeassistant/components/rfxtrx/sensor.py +++ b/homeassistant/components/rfxtrx/sensor.py @@ -20,11 +20,10 @@ from homeassistant.const import ( from homeassistant.core import callback from . import ( - CONF_AUTOMATIC_ADD, CONF_DATA_BITS, DATA_TYPES, - SIGNAL_EVENT, RfxtrxEntity, + connect_auto_add, get_device_id, get_rfx_object, ) @@ -127,8 +126,7 @@ async def async_setup_entry( async_add_entities([entity]) # Subscribe to main RFXtrx events - if discovery_info[CONF_AUTOMATIC_ADD]: - hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, sensor_update) + connect_auto_add(hass, discovery_info, sensor_update) class RfxtrxSensor(RfxtrxEntity): diff --git a/homeassistant/components/rfxtrx/switch.py b/homeassistant/components/rfxtrx/switch.py index 53069210794..96c066d5f3e 100644 --- a/homeassistant/components/rfxtrx/switch.py +++ b/homeassistant/components/rfxtrx/switch.py @@ -8,13 +8,12 @@ from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.core import callback from . import ( - CONF_AUTOMATIC_ADD, CONF_DATA_BITS, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, DOMAIN, - SIGNAL_EVENT, RfxtrxCommandEntity, + connect_auto_add, get_device_id, get_rfx_object, ) @@ -92,8 +91,7 @@ async def async_setup_entry( async_add_entities([entity]) # Subscribe to main RFXtrx events - if discovery_info[CONF_AUTOMATIC_ADD]: - hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, switch_update) + connect_auto_add(hass, discovery_info, switch_update) class RfxtrxSwitch(RfxtrxCommandEntity, SwitchEntity):