Suez water: simplify config flow (#130083)

Simplify config flow for suez water. Counter_id can now be automatically be fetched by the integration.
The value is provided only in the source code of suez website and therefore not easily accessible to user not familiar with devlopment.
Still possible to explicitly set the value for user with multiple value or value defined elsewhere.
This commit is contained in:
jb101010-2 2024-11-08 14:01:36 +01:00 committed by GitHub
parent 7672215095
commit 7678be8e2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View File

@ -20,7 +20,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_COUNTER_ID): str,
vol.Optional(CONF_COUNTER_ID): str,
}
)
@ -31,16 +31,23 @@ async def validate_input(data: dict[str, Any]) -> None:
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""
try:
counter_id = data.get(CONF_COUNTER_ID)
client = SuezClient(
data[CONF_USERNAME],
data[CONF_PASSWORD],
data[CONF_COUNTER_ID],
counter_id,
)
if not await client.check_credentials():
raise InvalidAuth
except PySuezError as ex:
raise CannotConnect from ex
if counter_id is None:
try:
data[CONF_COUNTER_ID] = await client.find_counter()
except PySuezError as ex:
raise CounterNotFound from ex
class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Suez Water."""
@ -61,6 +68,8 @@ class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except CounterNotFound:
errors["base"] = "counter_not_found"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
@ -80,3 +89,7 @@ class CannotConnect(HomeAssistantError):
class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""
class CounterNotFound(HomeAssistantError):
"""Error to indicate we cannot automatically found the counter id."""

View File

@ -12,7 +12,8 @@
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
"unknown": "[%key:common::config_flow::error::unknown%]",
"counter_not_found": "Could not find counter id automatically"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"

View File

@ -6,7 +6,7 @@ from pysuez.exception import PySuezError
import pytest
from homeassistant import config_entries
from homeassistant.components.suez_water.const import DOMAIN
from homeassistant.components.suez_water.const import CONF_COUNTER_ID, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -127,3 +127,40 @@ async def test_form_error(
assert result["title"] == "test-username"
assert result["data"] == MOCK_DATA
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_auto_counter(
hass: HomeAssistant, mock_setup_entry: AsyncMock, suez_client: AsyncMock
) -> None:
"""Test form set counter if not set by user."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
partial_form = {**MOCK_DATA}
partial_form.pop(CONF_COUNTER_ID)
suez_client.find_counter.side_effect = PySuezError("test counter not found")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
partial_form,
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "counter_not_found"}
suez_client.find_counter.side_effect = None
suez_client.find_counter.return_value = MOCK_DATA[CONF_COUNTER_ID]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
partial_form,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test-username"
assert result["result"].unique_id == "test-username"
assert result["data"] == MOCK_DATA
assert len(mock_setup_entry.mock_calls) == 1