Fix Soundbar exclusion from SamsungTV (#51023)

* Improved check

* Fix tests

* Fix logic and tests

* Update tests
This commit is contained in:
Simone Chemelli 2021-07-12 08:58:53 +02:00 committed by GitHub
parent fd6b5ed072
commit 13c142a402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 34 deletions

View File

@ -134,6 +134,14 @@ class SamsungTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self.hass, self._bridge, self._host self.hass, self._bridge, self._host
) )
if not info: if not info:
if not _method:
LOGGER.debug(
"Samsung host %s is not supported by either %s or %s methods",
self._host,
METHOD_LEGACY,
METHOD_WEBSOCKET,
)
raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED)
return False return False
dev_info = info.get("device", {}) dev_info = info.get("device", {})
device_type = dev_info.get("type") device_type = dev_info.get("type")

View File

@ -20,6 +20,7 @@ from homeassistant.components.samsungtv.const import (
RESULT_AUTH_MISSING, RESULT_AUTH_MISSING,
RESULT_CANNOT_CONNECT, RESULT_CANNOT_CONNECT,
RESULT_NOT_SUPPORTED, RESULT_NOT_SUPPORTED,
RESULT_SUCCESS,
RESULT_UNKNOWN_HOST, RESULT_UNKNOWN_HOST,
TIMEOUT_REQUEST, TIMEOUT_REQUEST,
TIMEOUT_WEBSOCKET, TIMEOUT_WEBSOCKET,
@ -110,6 +111,14 @@ MOCK_WS_ENTRY = {
CONF_MODEL: "any", CONF_MODEL: "any",
CONF_NAME: "any", CONF_NAME: "any",
} }
MOCK_DEVICE_INFO = {
"device": {
"type": "Samsung SmartTV",
"name": "fake_name",
"modelName": "fake_model",
},
"id": "123",
}
AUTODETECT_LEGACY = { AUTODETECT_LEGACY = {
"name": "HomeAssistant", "name": "HomeAssistant",
@ -306,17 +315,22 @@ async def test_ssdp_noprefix(hass: HomeAssistant, remote: Mock):
assert result["type"] == "form" assert result["type"] == "form"
assert result["step_id"] == "confirm" assert result["step_id"] == "confirm"
# entry was added with patch(
result = await hass.config_entries.flow.async_configure( "homeassistant.components.samsungtv.bridge.SamsungTVLegacyBridge.try_connect",
result["flow_id"], user_input="whatever" return_value=RESULT_SUCCESS,
) ):
assert result["type"] == "create_entry"
assert result["title"] == "fake2_model" # entry was added
assert result["data"][CONF_HOST] == "fake2_host" result = await hass.config_entries.flow.async_configure(
assert result["data"][CONF_NAME] == "fake2_model" result["flow_id"], user_input="whatever"
assert result["data"][CONF_MANUFACTURER] == "Samsung fake2_manufacturer" )
assert result["data"][CONF_MODEL] == "fake2_model" assert result["type"] == "create_entry"
assert result["result"].unique_id == "0d1cef00-00dc-1000-9c80-4844f7b172df" assert result["title"] == "fake2_model"
assert result["data"][CONF_HOST] == "fake2_host"
assert result["data"][CONF_NAME] == "fake2_model"
assert result["data"][CONF_MANUFACTURER] == "Samsung fake2_manufacturer"
assert result["data"][CONF_MODEL] == "fake2_model"
assert result["result"].unique_id == "0d1cef00-00dc-1000-9c80-4844f7b172df"
async def test_ssdp_legacy_missing_auth(hass: HomeAssistant, remote: Mock): async def test_ssdp_legacy_missing_auth(hass: HomeAssistant, remote: Mock):
@ -334,27 +348,32 @@ async def test_ssdp_legacy_missing_auth(hass: HomeAssistant, remote: Mock):
assert result["step_id"] == "confirm" assert result["step_id"] == "confirm"
# missing authentication # missing authentication
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input="whatever" with patch(
) "homeassistant.components.samsungtv.bridge.SamsungTVLegacyBridge.try_connect",
assert result["type"] == "abort" return_value=RESULT_AUTH_MISSING,
assert result["reason"] == RESULT_AUTH_MISSING ):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input="whatever"
)
assert result["type"] == "abort"
assert result["reason"] == RESULT_AUTH_MISSING
async def test_ssdp_legacy_not_supported(hass: HomeAssistant, remote: Mock): async def test_ssdp_legacy_not_supported(hass: HomeAssistant, remote: Mock):
"""Test starting a flow from discovery for not supported device.""" """Test starting a flow from discovery for not supported device."""
# confirm to add the entry
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_SSDP}, data=MOCK_SSDP_DATA
)
assert result["type"] == "form"
assert result["step_id"] == "confirm"
with patch( with patch(
"homeassistant.components.samsungtv.bridge.Remote", "homeassistant.components.samsungtv.bridge.SamsungTVLegacyBridge.try_connect",
side_effect=UnhandledResponse("Boom"), return_value=RESULT_NOT_SUPPORTED,
): ):
# confirm to add the entry
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_SSDP}, data=MOCK_SSDP_DATA
)
assert result["type"] == "form"
assert result["step_id"] == "confirm"
# device not supported # device not supported
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input="whatever" result["flow_id"], user_input="whatever"
@ -395,17 +414,10 @@ async def test_ssdp_websocket_not_supported(hass: HomeAssistant, remote: Mock):
"homeassistant.components.samsungtv.bridge.SamsungTVWS", "homeassistant.components.samsungtv.bridge.SamsungTVWS",
side_effect=WebSocketProtocolException("Boom"), side_effect=WebSocketProtocolException("Boom"),
): ):
# confirm to add the entry # device not supported
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_SSDP}, data=MOCK_SSDP_DATA DOMAIN, context={"source": config_entries.SOURCE_SSDP}, data=MOCK_SSDP_DATA
) )
assert result["type"] == "form"
assert result["step_id"] == "confirm"
# device not supported
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input="whatever"
)
assert result["type"] == "abort" assert result["type"] == "abort"
assert result["reason"] == RESULT_NOT_SUPPORTED assert result["reason"] == RESULT_NOT_SUPPORTED
@ -431,6 +443,9 @@ async def test_ssdp_not_successful(hass: HomeAssistant, remote: Mock):
), patch( ), patch(
"homeassistant.components.samsungtv.bridge.SamsungTVWS", "homeassistant.components.samsungtv.bridge.SamsungTVWS",
side_effect=OSError("Boom"), side_effect=OSError("Boom"),
), patch(
"homeassistant.components.samsungtv.bridge.SamsungTVWSBridge.device_info",
return_value=MOCK_DEVICE_INFO,
): ):
# confirm to add the entry # confirm to add the entry
@ -456,6 +471,9 @@ async def test_ssdp_not_successful_2(hass: HomeAssistant, remote: Mock):
), patch( ), patch(
"homeassistant.components.samsungtv.bridge.SamsungTVWS", "homeassistant.components.samsungtv.bridge.SamsungTVWS",
side_effect=ConnectionFailure("Boom"), side_effect=ConnectionFailure("Boom"),
), patch(
"homeassistant.components.samsungtv.bridge.SamsungTVWSBridge.device_info",
return_value=MOCK_DEVICE_INFO,
): ):
# confirm to add the entry # confirm to add the entry