Properly unload SimpliSafe websocket listener (#41952)

pull/41964/head
Aaron Bach 2020-10-16 09:44:53 -06:00 committed by GitHub
parent 8c0b39eb9c
commit eff0c4a494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 7 deletions

View File

@ -173,6 +173,8 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, config_entry): async def async_setup_entry(hass, config_entry):
"""Set up SimpliSafe as config entry.""" """Set up SimpliSafe as config entry."""
hass.data[DOMAIN][DATA_LISTENER][config_entry.entry_id] = []
entry_updates = {} entry_updates = {}
if not config_entry.unique_id: if not config_entry.unique_id:
# If the config entry doesn't already have a unique ID, set one: # If the config entry doesn't already have a unique ID, set one:
@ -324,8 +326,9 @@ async def async_unload_entry(hass, entry):
) )
if unload_ok: if unload_ok:
hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id) hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id)
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(entry.entry_id) for remove_listener in hass.data[DOMAIN][DATA_LISTENER][entry.entry_id]:
remove_listener() remove_listener()
hass.data[DOMAIN][DATA_LISTENER].pop(entry.entry_id)
return unload_ok return unload_ok
@ -454,9 +457,11 @@ class SimpliSafe:
"""Define an event handler to disconnect from the websocket.""" """Define an event handler to disconnect from the websocket."""
await self.websocket.async_disconnect() await self.websocket.async_disconnect()
self._hass.data[DOMAIN][DATA_LISTENER][self.config_entry.entry_id].append(
self._hass.bus.async_listen_once( self._hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, async_websocket_disconnect EVENT_HOMEASSISTANT_STOP, async_websocket_disconnect
) )
)
self.systems = await self._api.get_systems() self.systems = await self._api.get_systems()
for system in self.systems.values(): for system in self.systems.values():
@ -483,9 +488,9 @@ class SimpliSafe:
"""Refresh data from the SimpliSafe account.""" """Refresh data from the SimpliSafe account."""
await self.async_update() await self.async_update()
self._hass.data[DOMAIN][DATA_LISTENER][ self._hass.data[DOMAIN][DATA_LISTENER][self.config_entry.entry_id].append(
self.config_entry.entry_id async_track_time_interval(self._hass, refresh, DEFAULT_SCAN_INTERVAL)
] = async_track_time_interval(self._hass, refresh, DEFAULT_SCAN_INTERVAL) )
await self.async_update() await self.async_update()