Skip to content

Commit

Permalink
Ensure existing SimpliSafe websocket tasks are cancelled appropriately (
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya authored Dec 19, 2021
1 parent 667a632 commit b77fc2e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
33 changes: 20 additions & 13 deletions homeassistant/components/simplisafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,14 @@ def _async_process_new_notifications(self, system: SystemType) -> None:
self._system_notifications[system.system_id] = latest_notifications

async def _async_start_websocket_loop(self) -> None:
"""Define a callback for connecting to the websocket."""
"""Start a websocket reconnection loop."""
if TYPE_CHECKING:
assert self._api.websocket

should_reconnect = True

try:
await self._api.websocket.async_reconnect()
await self._api.websocket.async_connect()
await self._api.websocket.async_listen()
except asyncio.CancelledError:
LOGGER.debug("Request to cancel websocket loop received")
Expand All @@ -539,10 +539,25 @@ async def _async_start_websocket_loop(self) -> None:

if should_reconnect:
LOGGER.info("Disconnected from websocket; reconnecting")
await self._async_cancel_websocket_loop()
self._websocket_reconnect_task = self._hass.async_create_task(
self._async_start_websocket_loop()
)

async def _async_cancel_websocket_loop(self) -> None:
"""Stop any existing websocket reconnection loop."""
if self._websocket_reconnect_task:
self._websocket_reconnect_task.cancel()
try:
await self._websocket_reconnect_task
except asyncio.CancelledError:
LOGGER.debug("Websocket reconnection task successfully canceled")
self._websocket_reconnect_task = None

if TYPE_CHECKING:
assert self._api.websocket
await self._api.websocket.async_disconnect()

@callback
def _async_websocket_on_event(self, event: WebsocketEvent) -> None:
"""Define a callback for receiving a websocket event."""
Expand Down Expand Up @@ -591,15 +606,7 @@ async def async_websocket_disconnect_listener(_: Event) -> None:
if TYPE_CHECKING:
assert self._api.websocket

if self._websocket_reconnect_task:
self._websocket_reconnect_task.cancel()
try:
await self._websocket_reconnect_task
except asyncio.CancelledError:
LOGGER.debug("Websocket reconnection task successfully canceled")
self._websocket_reconnect_task = None

await self._api.websocket.async_disconnect()
await self._async_cancel_websocket_loop()

self.entry.async_on_unload(
self._hass.bus.async_listen_once(
Expand Down Expand Up @@ -641,15 +648,15 @@ def async_save_refresh_token(token: str) -> None:
data={**self.entry.data, CONF_TOKEN: token},
)

@callback
def async_handle_refresh_token(token: str) -> None:
async def async_handle_refresh_token(token: str) -> None:
"""Handle a new refresh token."""
async_save_refresh_token(token)

if TYPE_CHECKING:
assert self._api.websocket

# Open a new websocket connection with the fresh token:
await self._async_cancel_websocket_loop()
self._websocket_reconnect_task = self._hass.async_create_task(
self._async_start_websocket_loop()
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/simplisafe/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "SimpliSafe",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/simplisafe",
"requirements": ["simplisafe-python==2021.12.1"],
"requirements": ["simplisafe-python==2021.12.2"],
"codeowners": ["@bachya"],
"iot_class": "cloud_polling",
"dhcp": [
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ simplehound==0.3
simplepush==1.1.4

# homeassistant.components.simplisafe
simplisafe-python==2021.12.1
simplisafe-python==2021.12.2

# homeassistant.components.sisyphus
sisyphus-control==3.1.2
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ sharkiqpy==0.1.8
simplehound==0.3

# homeassistant.components.simplisafe
simplisafe-python==2021.12.1
simplisafe-python==2021.12.2

# homeassistant.components.slack
slackclient==2.5.0
Expand Down

0 comments on commit b77fc2e

Please sign in to comment.