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():