Don't reinvent callback handler removal logic in several integ… (#33726)
This commit is contained in:
parent
e4ee4cf302
commit
f53dfc4308
10 changed files with 16 additions and 67 deletions
|
@ -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 = []
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue