From 2079e41b7adac1835b60ad2754a4759c77d18b83 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Thu, 15 Oct 2020 13:05:00 -0600 Subject: [PATCH] Properly disconnect from SimpliSafe websocket on HASS shutdown (#41894) * Properly disconnect from SimpliSafe websocket on HASS shutdown * Update homeassistant/components/simplisafe/__init__.py Co-authored-by: J. Nick Koston * Update homeassistant/components/simplisafe/__init__.py Co-authored-by: J. Nick Koston Co-authored-by: J. Nick Koston --- homeassistant/components/simplisafe/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/simplisafe/__init__.py b/homeassistant/components/simplisafe/__init__.py index 3a1dfc3b23f..45f2b243750 100644 --- a/homeassistant/components/simplisafe/__init__.py +++ b/homeassistant/components/simplisafe/__init__.py @@ -23,6 +23,7 @@ from homeassistant.const import ( CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME, + EVENT_HOMEASSISTANT_STOP, ) from homeassistant.core import CoreState, callback from homeassistant.exceptions import ConfigEntryNotReady @@ -424,7 +425,7 @@ class SimpliSafeWebsocket: }, ) - async def async_websocket_connect(self): + async def async_connect(self): """Register handlers and connect to the websocket.""" self._websocket.on_connect(self._on_connect) self._websocket.on_disconnect(self._on_disconnect) @@ -432,6 +433,10 @@ class SimpliSafeWebsocket: await self._websocket.async_connect() + async def async_disconnect(self): + """Disconnect from the websocket.""" + await self._websocket.async_disconnect() + class SimpliSafe: """Define a SimpliSafe data object.""" @@ -486,7 +491,10 @@ class SimpliSafe: async def async_init(self): """Initialize the data class.""" - asyncio.create_task(self.websocket.async_websocket_connect()) + asyncio.create_task(self.websocket.async_connect()) + self._hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, self.websocket.async_disconnect + ) self.systems = await self._api.get_systems() for system in self.systems.values():