From f53dfc4308e43d2f948fba45e23801b8d773abef Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sun, 5 Apr 2020 23:36:23 -0600 Subject: [PATCH] =?UTF-8?q?Don't=20reinvent=20callback=20handler=20removal?= =?UTF-8?q?=20logic=20in=20several=20integ=E2=80=A6=20(#33726)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homeassistant/components/airvisual/sensor.py | 11 +---------- .../components/ambient_station/__init__.py | 13 ++++--------- homeassistant/components/flunearyou/sensor.py | 11 +---------- homeassistant/components/iqvia/__init__.py | 9 ++------- homeassistant/components/notion/__init__.py | 11 ++--------- homeassistant/components/openuv/__init__.py | 11 +---------- homeassistant/components/rainmachine/__init__.py | 7 ------- .../components/rainmachine/binary_sensor.py | 2 +- homeassistant/components/rainmachine/sensor.py | 2 +- homeassistant/components/rainmachine/switch.py | 6 +++--- 10 files changed, 16 insertions(+), 67 deletions(-) diff --git a/homeassistant/components/airvisual/sensor.py b/homeassistant/components/airvisual/sensor.py index 49a5f53361f..20e76bf86b6 100644 --- a/homeassistant/components/airvisual/sensor.py +++ b/homeassistant/components/airvisual/sensor.py @@ -95,7 +95,6 @@ class AirVisualSensor(Entity): def __init__(self, airvisual, kind, name, icon, unit, locale, geography_id): """Initialize.""" self._airvisual = airvisual - self._async_unsub_dispatcher_connects = [] self._geography_id = geography_id self._icon = icon self._kind = kind @@ -159,9 +158,7 @@ class AirVisualSensor(Entity): """Update the state.""" self.async_schedule_update_ha_state(True) - self._async_unsub_dispatcher_connects.append( - async_dispatcher_connect(self.hass, TOPIC_UPDATE, update) - ) + self.async_on_remove(async_dispatcher_connect(self.hass, TOPIC_UPDATE, update)) async def async_update(self): """Update the sensor.""" @@ -206,9 +203,3 @@ class AirVisualSensor(Entity): self._attrs["long"] = self._airvisual.geography_data[CONF_LONGITUDE] self._attrs.pop(ATTR_LATITUDE, None) self._attrs.pop(ATTR_LONGITUDE, None) - - async def async_will_remove_from_hass(self) -> None: - """Disconnect dispatcher listener when removed.""" - for cancel in self._async_unsub_dispatcher_connects: - cancel() - self._async_unsub_dispatcher_connects = [] diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index f3f2397d214..66a22d44366 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -437,7 +437,6 @@ class AmbientWeatherEntity(Entity): """Initialize the sensor.""" self._ambient = ambient self._device_class = device_class - self._async_unsub_dispatcher_connect = None self._mac_address = mac_address self._sensor_name = sensor_name self._sensor_type = sensor_type @@ -503,18 +502,14 @@ class AmbientWeatherEntity(Entity): self.update_from_latest_data() self.async_write_ha_state() - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, f"ambient_station_data_update_{self._mac_address}", update + self.async_on_remove( + async_dispatcher_connect( + self.hass, f"ambient_station_data_update_{self._mac_address}", update + ) ) self.update_from_latest_data() - async def async_will_remove_from_hass(self): - """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - self._async_unsub_dispatcher_connect = None - @callback def update_from_latest_data(self): """Update the entity from the latest data.""" diff --git a/homeassistant/components/flunearyou/sensor.py b/homeassistant/components/flunearyou/sensor.py index 6868d21ce1f..22c56c10038 100644 --- a/homeassistant/components/flunearyou/sensor.py +++ b/homeassistant/components/flunearyou/sensor.py @@ -56,7 +56,6 @@ class FluNearYouSensor(Entity): def __init__(self, fny, sensor_type, name, category, icon, unit): """Initialize the sensor.""" - self._async_unsub_dispatcher_connect = None self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._category = category self._fny = fny @@ -110,20 +109,12 @@ class FluNearYouSensor(Entity): self.update_from_latest_data() self.async_write_ha_state() - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, TOPIC_UPDATE, update - ) - + self.async_on_remove(async_dispatcher_connect(self.hass, TOPIC_UPDATE, update)) await self._fny.async_register_api_interest(self._sensor_type) - self.update_from_latest_data() async def async_will_remove_from_hass(self) -> None: """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - self._async_unsub_dispatcher_connect = None - self._fny.async_deregister_api_interest(self._sensor_type) @callback diff --git a/homeassistant/components/iqvia/__init__.py b/homeassistant/components/iqvia/__init__.py index 1f487dd345c..cd75e88bb44 100644 --- a/homeassistant/components/iqvia/__init__.py +++ b/homeassistant/components/iqvia/__init__.py @@ -239,7 +239,6 @@ class IQVIAEntity(Entity): def __init__(self, iqvia, sensor_type, name, icon, zip_code): """Initialize the sensor.""" - self._async_unsub_dispatcher_connect = None self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._icon = icon self._iqvia = iqvia @@ -301,8 +300,8 @@ class IQVIAEntity(Entity): self.update_from_latest_data() self.async_write_ha_state() - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, TOPIC_DATA_UPDATE, update + self.async_on_remove( + async_dispatcher_connect(self.hass, TOPIC_DATA_UPDATE, update) ) await self._iqvia.async_register_api_interest(self._type) @@ -315,10 +314,6 @@ class IQVIAEntity(Entity): async def async_will_remove_from_hass(self): """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - self._async_unsub_dispatcher_connect = None - self._iqvia.async_deregister_api_interest(self._type) if self._type == TYPE_ALLERGY_FORECAST: # Entities that lose interest in allergy forecast data should also lose diff --git a/homeassistant/components/notion/__init__.py b/homeassistant/components/notion/__init__.py index d608199f6fc..b06ba768765 100644 --- a/homeassistant/components/notion/__init__.py +++ b/homeassistant/components/notion/__init__.py @@ -211,7 +211,6 @@ class NotionEntity(Entity): self, notion, task_id, sensor_id, bridge_id, system_id, name, device_class ): """Initialize the entity.""" - self._async_unsub_dispatcher_connect = None self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._bridge_id = bridge_id self._device_class = device_class @@ -309,18 +308,12 @@ class NotionEntity(Entity): self.update_from_latest_data() self.async_write_ha_state() - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, TOPIC_DATA_UPDATE, update + self.async_on_remove( + async_dispatcher_connect(self.hass, TOPIC_DATA_UPDATE, update) ) self.update_from_latest_data() - async def async_will_remove_from_hass(self): - """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - self._async_unsub_dispatcher_connect = None - @callback def update_from_latest_data(self): """Update the entity from the latest data.""" diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index df4ff9735c3..ee8e5f0b8ee 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -233,7 +233,6 @@ class OpenUvEntity(Entity): def __init__(self, openuv): """Initialize.""" - self._async_unsub_dispatcher_connect = None self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._available = True self._name = None @@ -263,18 +262,10 @@ class OpenUvEntity(Entity): self.update_from_latest_data() self.async_write_ha_state() - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, TOPIC_UPDATE, update - ) + self.async_on_remove(async_dispatcher_connect(self.hass, TOPIC_UPDATE, update)) self.update_from_latest_data() - async def async_will_remove_from_hass(self): - """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - self._async_unsub_dispatcher_connect = None - def update_from_latest_data(self): """Update the sensor using the latest data.""" raise NotImplementedError diff --git a/homeassistant/components/rainmachine/__init__.py b/homeassistant/components/rainmachine/__init__.py index 03dc49d7475..e1054f296d0 100644 --- a/homeassistant/components/rainmachine/__init__.py +++ b/homeassistant/components/rainmachine/__init__.py @@ -410,7 +410,6 @@ class RainMachineEntity(Entity): """Initialize.""" self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._device_class = None - self._dispatcher_handlers = [] self._name = None self.rainmachine = rainmachine @@ -454,12 +453,6 @@ class RainMachineEntity(Entity): self.update_from_latest_data() self.async_write_ha_state() - async def async_will_remove_from_hass(self): - """Disconnect dispatcher listener when removed.""" - for handler in self._dispatcher_handlers: - handler() - self._dispatcher_handlers = [] - @callback def update_from_latest_data(self): """Update the entity.""" diff --git a/homeassistant/components/rainmachine/binary_sensor.py b/homeassistant/components/rainmachine/binary_sensor.py index 40802af2bfa..e5fdc8d6b46 100644 --- a/homeassistant/components/rainmachine/binary_sensor.py +++ b/homeassistant/components/rainmachine/binary_sensor.py @@ -126,7 +126,7 @@ class RainMachineBinarySensor(RainMachineEntity, BinarySensorDevice): async def async_added_to_hass(self): """Register callbacks.""" - self._dispatcher_handlers.append( + self.async_on_remove( async_dispatcher_connect(self.hass, SENSOR_UPDATE_TOPIC, self._update_state) ) await self.rainmachine.async_register_sensor_api_interest(self._api_category) diff --git a/homeassistant/components/rainmachine/sensor.py b/homeassistant/components/rainmachine/sensor.py index 287ce29a3cc..4d48b0cd049 100644 --- a/homeassistant/components/rainmachine/sensor.py +++ b/homeassistant/components/rainmachine/sensor.py @@ -143,7 +143,7 @@ class RainMachineSensor(RainMachineEntity): async def async_added_to_hass(self): """Register callbacks.""" - self._dispatcher_handlers.append( + self.async_on_remove( async_dispatcher_connect(self.hass, SENSOR_UPDATE_TOPIC, self._update_state) ) await self.rainmachine.async_register_sensor_api_interest(self._api_category) diff --git a/homeassistant/components/rainmachine/switch.py b/homeassistant/components/rainmachine/switch.py index fd36e23cb0e..b93f607f853 100644 --- a/homeassistant/components/rainmachine/switch.py +++ b/homeassistant/components/rainmachine/switch.py @@ -188,7 +188,7 @@ class RainMachineProgram(RainMachineSwitch): async def async_added_to_hass(self): """Register callbacks.""" - self._dispatcher_handlers.append( + self.async_on_remove( async_dispatcher_connect( self.hass, PROGRAM_UPDATE_TOPIC, self._update_state ) @@ -248,12 +248,12 @@ class RainMachineZone(RainMachineSwitch): async def async_added_to_hass(self): """Register callbacks.""" - self._dispatcher_handlers.append( + self.async_on_remove( async_dispatcher_connect( self.hass, PROGRAM_UPDATE_TOPIC, self._update_state ) ) - self._dispatcher_handlers.append( + self.async_on_remove( async_dispatcher_connect(self.hass, ZONE_UPDATE_TOPIC, self._update_state) )