From 12a9695798e8bb975ef361877d0a7775feb7fc7d Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 20 Apr 2021 20:53:05 +0200 Subject: [PATCH] Use config_entry.on_unload rather than local listener implementation in Axis (#49495) --- homeassistant/components/axis/__init__.py | 2 +- homeassistant/components/axis/binary_sensor.py | 2 +- homeassistant/components/axis/device.py | 7 +------ homeassistant/components/axis/light.py | 2 +- homeassistant/components/axis/switch.py | 2 +- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/axis/__init__.py b/homeassistant/components/axis/__init__.py index acbdc2ca782..e3c4d20fc04 100644 --- a/homeassistant/components/axis/__init__.py +++ b/homeassistant/components/axis/__init__.py @@ -26,7 +26,7 @@ async def async_setup_entry(hass, config_entry): await device.async_update_device_registry() - device.listeners.append( + config_entry.async_on_unload( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.shutdown) ) diff --git a/homeassistant/components/axis/binary_sensor.py b/homeassistant/components/axis/binary_sensor.py index 32d4afa328d..222a356d4f9 100644 --- a/homeassistant/components/axis/binary_sensor.py +++ b/homeassistant/components/axis/binary_sensor.py @@ -53,7 +53,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ): async_add_entities([AxisBinarySensor(event, device)]) - device.listeners.append( + config_entry.async_on_unload( async_dispatcher_connect(hass, device.signal_new_event, async_add_sensor) ) diff --git a/homeassistant/components/axis/device.py b/homeassistant/components/axis/device.py index b2af9e0efc6..cc9922b290c 100644 --- a/homeassistant/components/axis/device.py +++ b/homeassistant/components/axis/device.py @@ -58,8 +58,6 @@ class AxisNetworkDevice: self.fw_version = None self.product_type = None - self.listeners = [] - @property def host(self): """Return the host address of this device.""" @@ -190,7 +188,7 @@ class AxisNetworkDevice: status = {} if status.get("data", {}).get("status", {}).get("state") == "active": - self.listeners.append( + self.config_entry.async_on_unload( await mqtt.async_subscribe( hass, f"{self.api.vapix.serial_number}/#", self.mqtt_message ) @@ -279,9 +277,6 @@ class AxisNetworkDevice: if not unload_ok: return False - for unsubscribe_listener in self.listeners: - unsubscribe_listener() - return True diff --git a/homeassistant/components/axis/light.py b/homeassistant/components/axis/light.py index 75a42b13cbf..e627d6ccdbd 100644 --- a/homeassistant/components/axis/light.py +++ b/homeassistant/components/axis/light.py @@ -32,7 +32,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if event.CLASS == CLASS_LIGHT and event.TYPE == "Light": async_add_entities([AxisLight(event, device)]) - device.listeners.append( + config_entry.async_on_unload( async_dispatcher_connect(hass, device.signal_new_event, async_add_sensor) ) diff --git a/homeassistant/components/axis/switch.py b/homeassistant/components/axis/switch.py index f3436b3eb83..e509716fc1f 100644 --- a/homeassistant/components/axis/switch.py +++ b/homeassistant/components/axis/switch.py @@ -22,7 +22,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if event.CLASS == CLASS_OUTPUT: async_add_entities([AxisSwitch(event, device)]) - device.listeners.append( + config_entry.async_on_unload( async_dispatcher_connect(hass, device.signal_new_event, async_add_switch) )