Clean up left-behind dispatcher handlers when removing RainMachine (#18488)

* Clean up left-behind dispatcher handlers when removing RainMachine

* Member comments
pull/18535/head
Aaron Bach 2018-11-17 02:42:50 -07:00 committed by Martin Hjelmare
parent 6b2f50b29e
commit e73b9b9b8f
4 changed files with 16 additions and 26 deletions

View File

@ -78,13 +78,8 @@ class RainMachineBinarySensor(RainMachineEntity, BinarySensorDevice):
"""Update the state."""
self.async_schedule_update_ha_state(True)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, SENSOR_UPDATE_TOPIC, update)
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._dispatcher_handlers.append(async_dispatcher_connect(
self.hass, SENSOR_UPDATE_TOPIC, update))
async def async_update(self):
"""Update the state."""

View File

@ -267,7 +267,7 @@ class RainMachineEntity(Entity):
def __init__(self, rainmachine):
"""Initialize."""
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
self._async_unsub_dispatcher_connect = None
self._dispatcher_handlers = []
self._name = None
self.rainmachine = rainmachine
@ -295,3 +295,8 @@ class RainMachineEntity(Entity):
def name(self) -> str:
"""Return the name of the entity."""
return self._name
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
for handler in self._dispatcher_handlers:
handler()

View File

@ -81,13 +81,8 @@ class RainMachineSensor(RainMachineEntity):
"""Update the state."""
self.async_schedule_update_ha_state(True)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, SENSOR_UPDATE_TOPIC, update)
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._dispatcher_handlers.append(async_dispatcher_connect(
self.hass, SENSOR_UPDATE_TOPIC, update))
async def async_update(self):
"""Update the sensor's state."""

View File

@ -186,13 +186,8 @@ class RainMachineProgram(RainMachineSwitch):
async def async_added_to_hass(self):
"""Register callbacks."""
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, PROGRAM_UPDATE_TOPIC, self._program_updated)
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._dispatcher_handlers.append(async_dispatcher_connect(
self.hass, PROGRAM_UPDATE_TOPIC, self._program_updated))
async def async_turn_off(self, **kwargs) -> None:
"""Turn the program off."""
@ -256,10 +251,10 @@ class RainMachineZone(RainMachineSwitch):
async def async_added_to_hass(self):
"""Register callbacks."""
async_dispatcher_connect(
self.hass, PROGRAM_UPDATE_TOPIC, self._program_updated)
async_dispatcher_connect(
self.hass, ZONE_UPDATE_TOPIC, self._program_updated)
self._dispatcher_handlers.append(async_dispatcher_connect(
self.hass, PROGRAM_UPDATE_TOPIC, self._program_updated))
self._dispatcher_handlers.append(async_dispatcher_connect(
self.hass, ZONE_UPDATE_TOPIC, self._program_updated))
async def async_turn_off(self, **kwargs) -> None:
"""Turn the zone off."""