mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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:
parent
7672215095
commit
7678be8e2b
@ -20,7 +20,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
|
|||||||
{
|
{
|
||||||
vol.Required(CONF_USERNAME): str,
|
vol.Required(CONF_USERNAME): str,
|
||||||
vol.Required(CONF_PASSWORD): 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.
|
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
counter_id = data.get(CONF_COUNTER_ID)
|
||||||
client = SuezClient(
|
client = SuezClient(
|
||||||
data[CONF_USERNAME],
|
data[CONF_USERNAME],
|
||||||
data[CONF_PASSWORD],
|
data[CONF_PASSWORD],
|
||||||
data[CONF_COUNTER_ID],
|
counter_id,
|
||||||
)
|
)
|
||||||
if not await client.check_credentials():
|
if not await client.check_credentials():
|
||||||
raise InvalidAuth
|
raise InvalidAuth
|
||||||
except PySuezError as ex:
|
except PySuezError as ex:
|
||||||
raise CannotConnect from 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):
|
class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Suez Water."""
|
"""Handle a config flow for Suez Water."""
|
||||||
@ -61,6 +68,8 @@ class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except InvalidAuth:
|
except InvalidAuth:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
|
except CounterNotFound:
|
||||||
|
errors["base"] = "counter_not_found"
|
||||||
except Exception:
|
except Exception:
|
||||||
_LOGGER.exception("Unexpected exception")
|
_LOGGER.exception("Unexpected exception")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
@ -80,3 +89,7 @@ class CannotConnect(HomeAssistantError):
|
|||||||
|
|
||||||
class InvalidAuth(HomeAssistantError):
|
class InvalidAuth(HomeAssistantError):
|
||||||
"""Error to indicate there is invalid auth."""
|
"""Error to indicate there is invalid auth."""
|
||||||
|
|
||||||
|
|
||||||
|
class CounterNotFound(HomeAssistantError):
|
||||||
|
"""Error to indicate we cannot automatically found the counter id."""
|
||||||
|
@ -12,7 +12,8 @@
|
|||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
"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": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
|
@ -6,7 +6,7 @@ from pysuez.exception import PySuezError
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
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.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
@ -127,3 +127,40 @@ async def test_form_error(
|
|||||||
assert result["title"] == "test-username"
|
assert result["title"] == "test-username"
|
||||||
assert result["data"] == MOCK_DATA
|
assert result["data"] == MOCK_DATA
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user