Skip to content

Commit

Permalink
Suez water: simplify config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jb101010-2 committed Nov 7, 2024
1 parent dac6271 commit b231230
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
17 changes: 15 additions & 2 deletions homeassistant/components/suez_water/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_COUNTER_ID): str,
vol.Optional(CONF_COUNTER_ID): str,
}
)

Expand All @@ -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."""
Expand All @@ -61,6 +68,8 @@ async def async_step_user(
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"
Expand All @@ -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."""
3 changes: 2 additions & 1 deletion homeassistant/components/suez_water/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%]"
Expand Down
39 changes: 38 additions & 1 deletion tests/components/suez_water/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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

Expand Down Expand Up @@ -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 we get the form."""
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

0 comments on commit b231230

Please sign in to comment.