Do not report tplink discovery failures as legacy connection failures (#124432)

* Do not report discovery failures as legacy connection failures

* Fix catching BaseException
This commit is contained in:
Steven B. 2024-08-23 16:17:48 +01:00 committed by GitHub
parent fd57931cc9
commit 61cee043e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 3 deletions

View File

@ -410,9 +410,18 @@ class TPLinkConfigFlow(ConfigFlow, domain=DOMAIN):
self._discovered_device = await Discover.discover_single(
host, credentials=credentials
)
except TimeoutError:
# Try connect() to legacy devices if discovery fails
self._discovered_device = await Device.connect(config=DeviceConfig(host))
except TimeoutError as ex:
# Try connect() to legacy devices if discovery fails. This is a
# fallback mechanism for legacy that can handle connections without
# discovery info but if it fails raise the original error which is
# applicable for newer devices.
try:
self._discovered_device = await Device.connect(
config=DeviceConfig(host)
)
except Exception: # noqa: BLE001
# Raise the original error instead of the fallback error
raise ex from ex
else:
if self._discovered_device.config.uses_http:
self._discovered_device.config.http_client = (

View File

@ -1289,6 +1289,33 @@ async def test_discovery_timeout_connect(
assert mock_connect["connect"].call_count == 1
async def test_discovery_timeout_connect_legacy_error(
hass: HomeAssistant,
mock_discovery: AsyncMock,
mock_connect: AsyncMock,
mock_init,
) -> None:
"""Test discovery tries legacy connect on timeout."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
mock_discovery["discover_single"].side_effect = TimeoutError
mock_connect["connect"].side_effect = KasaException
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert not result["errors"]
assert mock_connect["connect"].call_count == 0
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: IP_ADDRESS}
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
assert mock_connect["connect"].call_count == 1
async def test_reauth_update_other_flows(
hass: HomeAssistant,
mock_discovery: AsyncMock,