From 52333bb7203e87ebbe7fefb11d766973a9d16d67 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 5 May 2022 07:15:24 +0200 Subject: [PATCH] Only test for EncryptedBridge in Samsung J/H models (#71291) --- .../components/samsungtv/__init__.py | 14 +++++----- homeassistant/components/samsungtv/bridge.py | 27 ++++++++++++------- tests/components/samsungtv/conftest.py | 4 +-- .../components/samsungtv/test_config_flow.py | 24 ++++++++--------- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/samsungtv/__init__.py b/homeassistant/components/samsungtv/__init__.py index dae4033ad4c..a7b8f7d1aec 100644 --- a/homeassistant/components/samsungtv/__init__.py +++ b/homeassistant/components/samsungtv/__init__.py @@ -30,7 +30,12 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.typing import ConfigType -from .bridge import SamsungTVBridge, async_get_device_info, mac_from_device_info +from .bridge import ( + SamsungTVBridge, + async_get_device_info, + mac_from_device_info, + model_requires_encryption, +) from .const import ( CONF_ON_ACTION, CONF_SESSION_ID, @@ -214,11 +219,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -def _model_requires_encryption(model: str | None) -> bool: - """H and J models need pairing with PIN.""" - return model is not None and len(model) > 4 and model[4] in ("H", "J") - - async def _async_create_bridge_with_updated_data( hass: HomeAssistant, entry: ConfigEntry ) -> SamsungTVBridge: @@ -279,7 +279,7 @@ async def _async_create_bridge_with_updated_data( LOGGER.info("Updated model to %s for %s", model, host) updated_data[CONF_MODEL] = model - if _model_requires_encryption(model) and method != METHOD_ENCRYPTED_WEBSOCKET: + if model_requires_encryption(model) and method != METHOD_ENCRYPTED_WEBSOCKET: LOGGER.info( "Detected model %s for %s. Some televisions from H and J series use " "an encrypted protocol but you are using %s which may not be supported", diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py index 52ab86337dd..c3201a493eb 100644 --- a/homeassistant/components/samsungtv/bridge.py +++ b/homeassistant/components/samsungtv/bridge.py @@ -85,6 +85,11 @@ def mac_from_device_info(info: dict[str, Any]) -> str | None: return None +def model_requires_encryption(model: str | None) -> bool: + """H and J models need pairing with PIN.""" + return model is not None and len(model) > 4 and model[4] in ("H", "J") + + async def async_get_device_info( hass: HomeAssistant, host: str, @@ -99,17 +104,19 @@ async def async_get_device_info( port, info, ) - encrypted_bridge = SamsungTVEncryptedBridge( - hass, METHOD_ENCRYPTED_WEBSOCKET, host, ENCRYPTED_WEBSOCKET_PORT - ) - result = await encrypted_bridge.async_try_connect() - if result != RESULT_CANNOT_CONNECT: - return ( - result, - ENCRYPTED_WEBSOCKET_PORT, - METHOD_ENCRYPTED_WEBSOCKET, - info, + # Check the encrypted port if the model requires encryption + if model_requires_encryption(info.get("device", {}).get("modelName")): + encrypted_bridge = SamsungTVEncryptedBridge( + hass, METHOD_ENCRYPTED_WEBSOCKET, host, ENCRYPTED_WEBSOCKET_PORT ) + result = await encrypted_bridge.async_try_connect() + if result != RESULT_CANNOT_CONNECT: + return ( + result, + ENCRYPTED_WEBSOCKET_PORT, + METHOD_ENCRYPTED_WEBSOCKET, + info, + ) return RESULT_SUCCESS, port, METHOD_WEBSOCKET, info # Try legacy port diff --git a/tests/components/samsungtv/conftest.py b/tests/components/samsungtv/conftest.py index d7f8ed0d1a1..764022f3501 100644 --- a/tests/components/samsungtv/conftest.py +++ b/tests/components/samsungtv/conftest.py @@ -22,7 +22,7 @@ from samsungtvws.remote import ChannelEmitCommand from homeassistant.components.samsungtv.const import WEBSOCKET_SSL_PORT import homeassistant.util.dt as dt_util -from .const import SAMPLE_DEVICE_INFO_WIFI +from .const import SAMPLE_DEVICE_INFO_UE48JU6400, SAMPLE_DEVICE_INFO_WIFI @pytest.fixture(autouse=True) @@ -177,7 +177,7 @@ def rest_api_fixture_non_ssl_only() -> Mock: """Mock rest_device_info to fail for ssl and work for non-ssl.""" if self.port == WEBSOCKET_SSL_PORT: raise ResponseError - return SAMPLE_DEVICE_INFO_WIFI + return SAMPLE_DEVICE_INFO_UE48JU6400 with patch( "homeassistant.components.samsungtv.bridge.SamsungTVAsyncRest", diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index d2a9d10caf2..40397a68d7d 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -340,16 +340,16 @@ async def test_user_encrypted_websocket( ) assert result4["type"] == "create_entry" - assert result4["title"] == "Living Room (82GXARRS)" + assert result4["title"] == "TV-UE48JU6470 (UE48JU6400)" assert result4["data"][CONF_HOST] == "fake_host" - assert result4["data"][CONF_NAME] == "Living Room" + assert result4["data"][CONF_NAME] == "TV-UE48JU6470" assert result4["data"][CONF_MAC] == "aa:bb:ww:ii:ff:ii" assert result4["data"][CONF_MANUFACTURER] == "Samsung" - assert result4["data"][CONF_MODEL] == "82GXARRS" + assert result4["data"][CONF_MODEL] == "UE48JU6400" assert result4["data"][CONF_SSDP_RENDERING_CONTROL_LOCATION] is None assert result4["data"][CONF_TOKEN] == "037739871315caef138547b03e348b72" assert result4["data"][CONF_SESSION_ID] == "1" - assert result4["result"].unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" + assert result4["result"].unique_id == "223da676-497a-4e06-9507-5e27ec4f0fb3" @pytest.mark.usefixtures("rest_api_failing") @@ -714,19 +714,19 @@ async def test_ssdp_encrypted_websocket_success_populates_mac_address_and_ssdp_l ) assert result4["type"] == "create_entry" - assert result4["title"] == "Living Room (82GXARRS)" + assert result4["title"] == "TV-UE48JU6470 (UE48JU6400)" assert result4["data"][CONF_HOST] == "fake_host" - assert result4["data"][CONF_NAME] == "Living Room" + assert result4["data"][CONF_NAME] == "TV-UE48JU6470" assert result4["data"][CONF_MAC] == "aa:bb:ww:ii:ff:ii" assert result4["data"][CONF_MANUFACTURER] == "Samsung fake_manufacturer" - assert result4["data"][CONF_MODEL] == "82GXARRS" + assert result4["data"][CONF_MODEL] == "UE48JU6400" assert ( result4["data"][CONF_SSDP_RENDERING_CONTROL_LOCATION] == "https://fake_host:12345/test" ) assert result4["data"][CONF_TOKEN] == "037739871315caef138547b03e348b72" assert result4["data"][CONF_SESSION_ID] == "1" - assert result4["result"].unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" + assert result4["result"].unique_id == "223da676-497a-4e06-9507-5e27ec4f0fb3" @pytest.mark.usefixtures("rest_api_non_ssl_only") @@ -1036,13 +1036,13 @@ async def test_dhcp_wireless(hass: HomeAssistant) -> None: result["flow_id"], user_input="whatever" ) assert result["type"] == "create_entry" - assert result["title"] == "Living Room (82GXARRS)" + assert result["title"] == "TV-UE48JU6470 (UE48JU6400)" assert result["data"][CONF_HOST] == "fake_host" - assert result["data"][CONF_NAME] == "Living Room" + assert result["data"][CONF_NAME] == "TV-UE48JU6470" assert result["data"][CONF_MAC] == "aa:bb:ww:ii:ff:ii" assert result["data"][CONF_MANUFACTURER] == "Samsung" - assert result["data"][CONF_MODEL] == "82GXARRS" - assert result["result"].unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" + assert result["data"][CONF_MODEL] == "UE48JU6400" + assert result["result"].unique_id == "223da676-497a-4e06-9507-5e27ec4f0fb3" @pytest.mark.usefixtures("remotews", "rest_api", "remoteencws_failing")