1
mirror of https://github.com/home-assistant/core synced 2024-08-06 09:34:49 +02:00

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 <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-03-15 08:20:24 +01:00 committed by GitHub
parent 7876ffe9e3
commit 125ab5eb2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -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(