Add reauthentication flow to PoolSense (#194024)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-11-24 16:55:16 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent ac1128c55c
commit 12f24171f0
5 changed files with 140 additions and 5 deletions
@@ -4,7 +4,7 @@ from poolsense import PoolSense
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import aiohttp_client
from .const import DOMAIN
@@ -25,7 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PoolSenseConfigEntry) ->
auth_valid = await poolsense.test_poolsense_credentials()
if not auth_valid:
raise ConfigEntryError(
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="invalid_auth",
)
@@ -1,5 +1,6 @@
"""Config flow for PoolSense integration."""
from collections.abc import Mapping
import logging
from typing import Any, override
@@ -65,3 +66,39 @@ class PoolSenseConfigFlow(ConfigFlow, domain=DOMAIN):
),
errors=errors,
)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle reauthentication on an authentication error."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm reauthentication with a new password."""
errors = {}
reauth_entry = self._get_reauth_entry()
if user_input is not None:
poolsense = PoolSense(
aiohttp_client.async_get_clientsession(self.hass),
reauth_entry.data[CONF_EMAIL],
user_input[CONF_PASSWORD],
None,
)
if await poolsense.test_poolsense_credentials():
return self.async_update_reload_and_abort(
reauth_entry,
data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
)
errors["base"] = "invalid_auth"
return self.async_show_form(
step_id="reauth_confirm",
data_schema=probatio.Schema(
{probatio.Required(probatio.Secret(CONF_PASSWORD)): str}
),
description_placeholders={"email": reauth_entry.data[CONF_EMAIL]},
errors=errors,
)
@@ -7,6 +7,16 @@
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
},
"step": {
"reauth_confirm": {
"data": {
"password": "[%key:common::config_flow::data::password%]"
},
"data_description": {
"password": "The new password of your PoolSense account."
},
"description": "Enter the new password for {email}.",
"title": "[%key:common::config_flow::title::reauth%]"
},
"user": {
"data": {
"email": "[%key:common::config_flow::data::email%]",
+67 -1
View File
@@ -6,7 +6,7 @@ import pytest
from pytest_homeassistant.common import MockConfigEntry
from homeassistant.components.poolsense.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@@ -92,3 +92,69 @@ async def test_duplicate_entry(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_reauth(
hass: HomeAssistant,
mock_poolsense_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reauthentication updates the password and reloads the entry."""
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["description_placeholders"]["email"] == "test@test.com"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: "new_password"},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert mock_config_entry.data == {
CONF_EMAIL: "test@test.com",
CONF_PASSWORD: "new_password",
}
assert mock_config_entry.state is ConfigEntryState.LOADED
@pytest.mark.parametrize(
("credentials_valid", "error"),
[pytest.param(False, "invalid_auth", id="invalid_auth")],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth_errors(
hass: HomeAssistant,
mock_poolsense_client: AsyncMock,
mock_config_entry: MockConfigEntry,
credentials_valid: bool,
error: str,
) -> None:
"""Test reauthentication errors and recovery."""
mock_config_entry.add_to_hass(hass)
mock_poolsense_client.test_poolsense_credentials.return_value = credentials_valid
result = await mock_config_entry.start_reauth_flow(hass)
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_poolsense_client.test_poolsense_credentials.return_value = True
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"
+24 -2
View File
@@ -2,9 +2,10 @@
from unittest.mock import AsyncMock
from poolsense.exceptions import PoolSenseError
from pytest_homeassistant.common import MockConfigEntry
from homeassistant.config_entries import ConfigEntryState
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.core import HomeAssistant
from . import setup_integration
@@ -15,10 +16,31 @@ async def test_setup_invalid_auth(
mock_poolsense_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setup fails with invalid credentials."""
"""Test setup fails with invalid credentials and starts reauth."""
mock_poolsense_client.test_poolsense_credentials.return_value = False
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
assert mock_config_entry.reason == "Invalid authentication"
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH
assert flows[0]["context"]["entry_id"] == mock_config_entry.entry_id
async def test_setup_update_failed(
hass: HomeAssistant,
mock_poolsense_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setup is retried when the first data update fails."""
mock_poolsense_client.get_poolsense_data.side_effect = PoolSenseError(
500, "Server error."
)
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.config_entries.flow.async_progress()