Proxy WebSocket close messages as well (#4942)

We can "proxy" WebSocket close messages just as well as any
other WebSocket message. This avoids an error print in the Supervisor
logs whenever any one side of the connection closes.
This commit is contained in:
Stefan Agner 2024-03-04 16:31:27 +01:00 committed by GitHub
parent 3a2c3e2f84
commit bb5e138134
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -186,6 +186,9 @@ class APIProxy(CoreSysAttributes):
return await target.send_str(msg.data)
if msg.type == WSMsgType.BINARY:
return await target.send_bytes(msg.data)
if msg.type == WSMsgType.CLOSE:
_LOGGER.debug("Received close message from WebSocket.")
return await target.close()
raise TypeError(
f"Cannot proxy websocket message of unsupported type: {msg.type}"
@ -200,6 +203,7 @@ class APIProxy(CoreSysAttributes):
# init server
server = web.WebSocketResponse(heartbeat=30)
await server.prepare(request)
addon_name = None
# handle authentication
try:
@ -223,7 +227,8 @@ class APIProxy(CoreSysAttributes):
)
return server
_LOGGER.info("WebSocket access from %s", addon.slug)
addon_name = addon.slug
_LOGGER.info("WebSocket access from %s", addon_name)
await server.send_json(
{"type": "auth_ok", "ha_version": self.sys_homeassistant.version},
@ -282,5 +287,5 @@ class APIProxy(CoreSysAttributes):
if not server.closed:
await server.close()
_LOGGER.info("Home Assistant WebSocket API connection is closed")
_LOGGER.info("Home Assistant WebSocket API for %s closed", addon_name)
return server