Handle auth errors in velux integration and add reauth flow (#159596)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
wollew
2025-12-23 14:11:40 +01:00
committed by GitHub
co-authored by Joost Lekkerkerker
parent 2160827a50
commit c115b418ac
6 changed files with 192 additions and 2 deletions
@@ -60,6 +60,10 @@ async def test_user_flow(
@pytest.mark.parametrize(
("exception", "error"),
[
(
PyVLXException("Login to KLF 200 failed, check credentials"),
"invalid_auth",
),
(PyVLXException("DUMMY"), "cannot_connect"),
(Exception("DUMMY"), "unknown"),
],
@@ -138,6 +142,94 @@ async def test_user_flow_duplicate_entry(
assert result["reason"] == "already_configured"
async def test_reauth_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pyvlx: AsyncMock,
) -> None:
"""Test that reauth flow works with valid credentials."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_PASSWORD: "New Password",
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert mock_config_entry.data[CONF_PASSWORD] == "New Password"
mock_pyvlx.connect.assert_called_once()
mock_pyvlx.disconnect.assert_called_once()
@pytest.mark.parametrize(
("exception", "error"),
[
(
PyVLXException("Login to KLF 200 failed, check credentials"),
"invalid_auth",
),
(PyVLXException("DUMMY"), "cannot_connect"),
(Exception("DUMMY"), "unknown"),
],
)
async def test_reauth_errors(
hass: HomeAssistant,
mock_pyvlx: AsyncMock,
mock_config_entry: MockConfigEntry,
exception: Exception,
error: str,
) -> None:
"""Test error handling in reauth flow."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
mock_pyvlx.connect.side_effect = exception
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_PASSWORD: "New Password",
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": error}
mock_pyvlx.connect.assert_called_once()
mock_pyvlx.disconnect.assert_not_called()
mock_pyvlx.connect.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: "New Password"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert mock_config_entry.data[CONF_PASSWORD] == "New Password"
mock_pyvlx.disconnect.assert_called_once()
async def test_dhcp_discovery(
hass: HomeAssistant,
mock_pyvlx: AsyncMock,
@@ -175,6 +267,10 @@ async def test_dhcp_discovery(
@pytest.mark.parametrize(
("exception", "error"),
[
(
PyVLXException("Login to KLF 200 failed, check credentials"),
"invalid_auth",
),
(PyVLXException("DUMMY"), "cannot_connect"),
(Exception("DUMMY"), "unknown"),
],
+24
View File
@@ -64,6 +64,30 @@ async def test_setup_retry_on_oserror_during_scenes(
mock_pyvlx.load_nodes.assert_not_called()
async def test_setup_auth_error(
mock_config_entry: ConfigEntry, hass: HomeAssistant, mock_pyvlx: AsyncMock
) -> None:
"""Test that PyVLXException with auth message raises ConfigEntryAuthFailed and starts reauth flow."""
mock_pyvlx.load_scenes.side_effect = PyVLXException(
"Login to KLF 200 failed, check credentials"
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
# ConfigEntryAuthFailed results in SETUP_ERROR state
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["step_id"] == "reauth_confirm"
mock_pyvlx.load_scenes.assert_awaited_once()
mock_pyvlx.load_nodes.assert_not_called()
@pytest.fixture
def platform() -> Platform:
"""Fixture to specify platform to test."""