mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Add reconfigure flow to yale_smart_alarm (#129536)
This commit is contained in:
parent
e9e20229a3
commit
af58b0c3b7
@ -25,7 +25,6 @@ from .const import (
|
||||
DEFAULT_AREA_ID,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
LOGGER,
|
||||
YALE_BASE_ERRORS,
|
||||
)
|
||||
|
||||
@ -52,6 +51,18 @@ OPTIONS_SCHEMA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
def validate_credentials(username: str, password: str) -> dict[str, Any]:
|
||||
"""Validate credentials."""
|
||||
errors: dict[str, str] = {}
|
||||
try:
|
||||
YaleSmartAlarmClient(username, password)
|
||||
except AuthenticationError:
|
||||
errors = {"base": "invalid_auth"}
|
||||
except YALE_BASE_ERRORS:
|
||||
errors = {"base": "cannot_connect"}
|
||||
return errors
|
||||
|
||||
|
||||
class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Yale integration."""
|
||||
|
||||
@ -73,24 +84,16 @@ class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Dialog that informs the user that reauth is required."""
|
||||
errors = {}
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
reauth_entry = self._get_reauth_entry()
|
||||
username = reauth_entry.data[CONF_USERNAME]
|
||||
password = user_input[CONF_PASSWORD]
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
YaleSmartAlarmClient, username, password
|
||||
)
|
||||
except AuthenticationError as error:
|
||||
LOGGER.error("Authentication failed. Check credentials %s", error)
|
||||
errors = {"base": "invalid_auth"}
|
||||
except YALE_BASE_ERRORS as error:
|
||||
LOGGER.error("Connection to API failed %s", error)
|
||||
errors = {"base": "cannot_connect"}
|
||||
|
||||
errors = await self.hass.async_add_executor_job(
|
||||
validate_credentials, username, password
|
||||
)
|
||||
if not errors:
|
||||
return self.async_update_reload_and_abort(
|
||||
reauth_entry,
|
||||
@ -103,11 +106,42 @@ class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reconfiguration of existing entry."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
reconfigure_entry = self._get_reconfigure_entry()
|
||||
username = user_input[CONF_USERNAME]
|
||||
|
||||
errors = await self.hass.async_add_executor_job(
|
||||
validate_credentials, username, user_input[CONF_PASSWORD]
|
||||
)
|
||||
if (
|
||||
username != reconfigure_entry.unique_id
|
||||
and await self.async_set_unique_id(username)
|
||||
):
|
||||
errors["base"] = "unique_id_exists"
|
||||
if not errors:
|
||||
return self.async_update_reload_and_abort(
|
||||
reconfigure_entry,
|
||||
unique_id=username,
|
||||
data_updates=user_input,
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure",
|
||||
data_schema=DATA_SCHEMA,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
errors = {}
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
username = user_input[CONF_USERNAME]
|
||||
@ -115,17 +149,9 @@ class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
name = DEFAULT_NAME
|
||||
area = user_input.get(CONF_AREA_ID, DEFAULT_AREA_ID)
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
YaleSmartAlarmClient, username, password
|
||||
)
|
||||
except AuthenticationError as error:
|
||||
LOGGER.error("Authentication failed. Check credentials %s", error)
|
||||
errors = {"base": "invalid_auth"}
|
||||
except YALE_BASE_ERRORS as error:
|
||||
LOGGER.error("Connection to API failed %s", error)
|
||||
errors = {"base": "cannot_connect"}
|
||||
|
||||
errors = await self.hass.async_add_executor_job(
|
||||
validate_credentials, username, password
|
||||
)
|
||||
if not errors:
|
||||
await self.async_set_unique_id(username)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
@ -2,11 +2,13 @@
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||
},
|
||||
"error": {
|
||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"unique_id_exists": "Another config entry with this username already exist"
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
@ -21,6 +23,13 @@
|
||||
"data": {
|
||||
"password": "[%key:common::config_flow::data::password%]"
|
||||
}
|
||||
},
|
||||
"reconfigure": {
|
||||
"data": {
|
||||
"username": "[%key:common::config_flow::data::username%]",
|
||||
"password": "[%key:common::config_flow::data::password%]",
|
||||
"area_id": "[%key:component::yale_smart_alarm::config::step::user::data::area_id%]"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -239,6 +239,211 @@ async def test_reauth_flow_error(
|
||||
}
|
||||
|
||||
|
||||
async def test_reconfigure(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure config flow."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="test-username",
|
||||
data={
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
"name": "Yale Smart Alarm",
|
||||
"area_id": "1",
|
||||
},
|
||||
version=2,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient",
|
||||
return_value="",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "test-username",
|
||||
"password": "new-test-password",
|
||||
"area_id": "2",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "reconfigure_successful"
|
||||
assert entry.data == {
|
||||
"username": "test-username",
|
||||
"password": "new-test-password",
|
||||
"name": "Yale Smart Alarm",
|
||||
"area_id": "2",
|
||||
}
|
||||
|
||||
|
||||
async def test_reconfigure_username_exist(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure config flow abort other username already exist."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="test-username",
|
||||
data={
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
"name": "Yale Smart Alarm",
|
||||
"area_id": "1",
|
||||
},
|
||||
version=2,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
entry2 = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="other-username",
|
||||
data={
|
||||
"username": "other-username",
|
||||
"password": "test-password",
|
||||
"name": "Yale Smart Alarm 2",
|
||||
"area_id": "1",
|
||||
},
|
||||
version=2,
|
||||
)
|
||||
entry2.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient",
|
||||
return_value="",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "other-username",
|
||||
"password": "test-password",
|
||||
"area_id": "1",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "unique_id_exists"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient",
|
||||
return_value="",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "other-new-username",
|
||||
"password": "test-password",
|
||||
"area_id": "1",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert entry.data == {
|
||||
"username": "other-new-username",
|
||||
"name": "Yale Smart Alarm",
|
||||
"password": "test-password",
|
||||
"area_id": "1",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("sideeffect", "p_error"),
|
||||
[
|
||||
(AuthenticationError, "invalid_auth"),
|
||||
(ConnectionError, "cannot_connect"),
|
||||
(TimeoutError, "cannot_connect"),
|
||||
(UnknownError, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_flow_error(
|
||||
hass: HomeAssistant, sideeffect: Exception, p_error: str
|
||||
) -> None:
|
||||
"""Test a reauthentication flow."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="test-username",
|
||||
data={
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
"name": "Yale Smart Alarm",
|
||||
"area_id": "1",
|
||||
},
|
||||
version=2,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient",
|
||||
side_effect=sideeffect,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "test-username",
|
||||
"password": "update-password",
|
||||
"area_id": "1",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": p_error}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient",
|
||||
return_value="",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.yale_smart_alarm.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "test-username",
|
||||
"password": "new-test-password",
|
||||
"area_id": "1",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert entry.data == {
|
||||
"username": "test-username",
|
||||
"name": "Yale Smart Alarm",
|
||||
"password": "new-test-password",
|
||||
"area_id": "1",
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow(hass: HomeAssistant) -> None:
|
||||
"""Test options config flow."""
|
||||
entry = MockConfigEntry(
|
||||
|
Loading…
x
Reference in New Issue
Block a user