Ensure existing SimpliSafe websocket tasks are cancelled appropriately (#62347)
This commit is contained in:
parent
667a632e06
commit
b77fc2e8cb
4 changed files with 23 additions and 16 deletions
|
@ -520,14 +520,14 @@ class SimpliSafe:
|
||||||
self._system_notifications[system.system_id] = latest_notifications
|
self._system_notifications[system.system_id] = latest_notifications
|
||||||
|
|
||||||
async def _async_start_websocket_loop(self) -> None:
|
async def _async_start_websocket_loop(self) -> None:
|
||||||
"""Define a callback for connecting to the websocket."""
|
"""Start a websocket reconnection loop."""
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
assert self._api.websocket
|
assert self._api.websocket
|
||||||
|
|
||||||
should_reconnect = True
|
should_reconnect = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self._api.websocket.async_reconnect()
|
await self._api.websocket.async_connect()
|
||||||
await self._api.websocket.async_listen()
|
await self._api.websocket.async_listen()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
LOGGER.debug("Request to cancel websocket loop received")
|
LOGGER.debug("Request to cancel websocket loop received")
|
||||||
|
@ -539,10 +539,25 @@ class SimpliSafe:
|
||||||
|
|
||||||
if should_reconnect:
|
if should_reconnect:
|
||||||
LOGGER.info("Disconnected from websocket; reconnecting")
|
LOGGER.info("Disconnected from websocket; reconnecting")
|
||||||
|
await self._async_cancel_websocket_loop()
|
||||||
self._websocket_reconnect_task = self._hass.async_create_task(
|
self._websocket_reconnect_task = self._hass.async_create_task(
|
||||||
self._async_start_websocket_loop()
|
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
|
@callback
|
||||||
def _async_websocket_on_event(self, event: WebsocketEvent) -> None:
|
def _async_websocket_on_event(self, event: WebsocketEvent) -> None:
|
||||||
"""Define a callback for receiving a websocket event."""
|
"""Define a callback for receiving a websocket event."""
|
||||||
|
@ -591,15 +606,7 @@ class SimpliSafe:
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
assert self._api.websocket
|
assert self._api.websocket
|
||||||
|
|
||||||
if self._websocket_reconnect_task:
|
await self._async_cancel_websocket_loop()
|
||||||
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()
|
|
||||||
|
|
||||||
self.entry.async_on_unload(
|
self.entry.async_on_unload(
|
||||||
self._hass.bus.async_listen_once(
|
self._hass.bus.async_listen_once(
|
||||||
|
@ -641,8 +648,7 @@ class SimpliSafe:
|
||||||
data={**self.entry.data, CONF_TOKEN: token},
|
data={**self.entry.data, CONF_TOKEN: token},
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
async def async_handle_refresh_token(token: str) -> None:
|
||||||
def async_handle_refresh_token(token: str) -> None:
|
|
||||||
"""Handle a new refresh token."""
|
"""Handle a new refresh token."""
|
||||||
async_save_refresh_token(token)
|
async_save_refresh_token(token)
|
||||||
|
|
||||||
|
@ -650,6 +656,7 @@ class SimpliSafe:
|
||||||
assert self._api.websocket
|
assert self._api.websocket
|
||||||
|
|
||||||
# Open a new websocket connection with the fresh token:
|
# 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._websocket_reconnect_task = self._hass.async_create_task(
|
||||||
self._async_start_websocket_loop()
|
self._async_start_websocket_loop()
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"name": "SimpliSafe",
|
"name": "SimpliSafe",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/simplisafe",
|
"documentation": "https://www.home-assistant.io/integrations/simplisafe",
|
||||||
"requirements": ["simplisafe-python==2021.12.1"],
|
"requirements": ["simplisafe-python==2021.12.2"],
|
||||||
"codeowners": ["@bachya"],
|
"codeowners": ["@bachya"],
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"dhcp": [
|
"dhcp": [
|
||||||
|
|
|
@ -2158,7 +2158,7 @@ simplehound==0.3
|
||||||
simplepush==1.1.4
|
simplepush==1.1.4
|
||||||
|
|
||||||
# homeassistant.components.simplisafe
|
# homeassistant.components.simplisafe
|
||||||
simplisafe-python==2021.12.1
|
simplisafe-python==2021.12.2
|
||||||
|
|
||||||
# homeassistant.components.sisyphus
|
# homeassistant.components.sisyphus
|
||||||
sisyphus-control==3.1.2
|
sisyphus-control==3.1.2
|
||||||
|
|
|
@ -1285,7 +1285,7 @@ sharkiqpy==0.1.8
|
||||||
simplehound==0.3
|
simplehound==0.3
|
||||||
|
|
||||||
# homeassistant.components.simplisafe
|
# homeassistant.components.simplisafe
|
||||||
simplisafe-python==2021.12.1
|
simplisafe-python==2021.12.2
|
||||||
|
|
||||||
# homeassistant.components.slack
|
# homeassistant.components.slack
|
||||||
slackclient==2.5.0
|
slackclient==2.5.0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue