From bb5e138134e8f0c2062f9ce51a06ec2d296f6987 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 4 Mar 2024 16:31:27 +0100 Subject: [PATCH] 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. --- supervisor/api/proxy.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/supervisor/api/proxy.py b/supervisor/api/proxy.py index d8a68aae2..9550b8deb 100644 --- a/supervisor/api/proxy.py +++ b/supervisor/api/proxy.py @@ -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