Guard against multiple client creations (#2809)

This commit is contained in:
Joakim Sørensen 2021-04-14 14:30:49 +02:00 committed by GitHub
parent 4a53c62af8
commit dd10d3e037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -86,9 +86,14 @@ class HomeAssistantWebSocket(CoreSysAttributes):
"""Initialize Home Assistant object.""" """Initialize Home Assistant object."""
self.coresys: CoreSys = coresys self.coresys: CoreSys = coresys
self._client: Optional[WSClient] = None self._client: Optional[WSClient] = None
self._lock: asyncio.Lock = asyncio.Lock()
async def _get_ws_client(self) -> WSClient: async def _get_ws_client(self) -> WSClient:
"""Return a websocket client.""" """Return a websocket client."""
async with self._lock:
if self._client is not None:
return self._client
await self.sys_homeassistant.api.ensure_access_token() await self.sys_homeassistant.api.ensure_access_token()
client = await WSClient.connect_with_auth( client = await WSClient.connect_with_auth(
self.sys_websession_ssl, self.sys_websession_ssl,