From 125ab5eb2bd342ce387afda6849cff50b7d6c8a1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 15 Mar 2022 08:20:24 +0100 Subject: [PATCH] Handle ConnectionClosed in SamsungTV try_connect (#68125) * Handle ConnectionClosed in SamsungTV try_connect * Add tests * Add quotes around the error message Co-authored-by: epenet --- homeassistant/components/samsungtv/bridge.py | 9 +++++++ .../components/samsungtv/test_config_flow.py | 27 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py index acc6d5cd766..398b14a20b1 100644 --- a/homeassistant/components/samsungtv/bridge.py +++ b/homeassistant/components/samsungtv/bridge.py @@ -374,6 +374,15 @@ class SamsungTVWSBridge(SamsungTVBridge): self.token = remote.token LOGGER.debug("Working config: %s", config) return RESULT_SUCCESS + except ConnectionClosedError as err: + LOGGER.info( + "Working but unsupported config: %s, error: '%s'; this may " + "be an indication that access to the TV has been denied. Please " + "check the Device Connection Manager on your TV", + config, + err, + ) + result = RESULT_NOT_SUPPORTED except WebSocketException as err: LOGGER.debug( "Working but unsupported config: %s, error: %s", config, err diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index f8a787753f0..8f1a22395fb 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -6,7 +6,12 @@ import pytest from samsungctl.exceptions import AccessDenied, UnhandledResponse from samsungtvws.async_remote import SamsungTVWSAsyncRemote from samsungtvws.exceptions import ConnectionFailure, HttpApiError -from websockets.exceptions import WebSocketException, WebSocketProtocolError +from websockets import frames +from websockets.exceptions import ( + ConnectionClosedError, + WebSocketException, + WebSocketProtocolError, +) from homeassistant import config_entries from homeassistant.components import dhcp, ssdp, zeroconf @@ -283,6 +288,26 @@ async def test_user_websocket_not_supported(hass: HomeAssistant) -> None: assert result["reason"] == RESULT_NOT_SUPPORTED +async def test_user_websocket_access_denied( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test starting a flow by user for not supported device.""" + with patch( + "homeassistant.components.samsungtv.bridge.Remote", + side_effect=OSError("Boom"), + ), patch( + "homeassistant.components.samsungtv.bridge.SamsungTVWSAsyncRemote.open", + side_effect=ConnectionClosedError(rcvd=None, sent=frames.Close(1002, "")), + ): + # websocket device not supported + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER}, data=MOCK_USER_DATA + ) + assert result["type"] == "abort" + assert result["reason"] == RESULT_NOT_SUPPORTED + assert "Please check the Device Connection Manager on your TV" in caplog.text + + async def test_user_not_successful(hass: HomeAssistant) -> None: """Test starting a flow by user but no connection found.""" with patch(