mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Add device reconfigure to Comelit config flow (#142866)
* Add device reconfigure to Comelit config flow * tweak * tweak * update quality scale * apply review comment * apply review comment * review comment * complete test
This commit is contained in:
parent
8938c109c2
commit
9c798cbb5d
@ -28,20 +28,22 @@ DEFAULT_HOST = "192.168.1.252"
|
|||||||
DEFAULT_PIN = 111111
|
DEFAULT_PIN = 111111
|
||||||
|
|
||||||
|
|
||||||
def user_form_schema(user_input: dict[str, Any] | None) -> vol.Schema:
|
USER_SCHEMA = vol.Schema(
|
||||||
"""Return user form schema."""
|
|
||||||
user_input = user_input or {}
|
|
||||||
return vol.Schema(
|
|
||||||
{
|
{
|
||||||
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||||
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
|
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
|
||||||
vol.Required(CONF_TYPE, default=BRIDGE): vol.In(DEVICE_TYPE_LIST),
|
vol.Required(CONF_TYPE, default=BRIDGE): vol.In(DEVICE_TYPE_LIST),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PIN): cv.positive_int})
|
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PIN): cv.positive_int})
|
||||||
|
STEP_RECONFIGURE = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_HOST): cv.string,
|
||||||
|
vol.Required(CONF_PORT): cv.port,
|
||||||
|
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
|
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
|
||||||
@ -87,13 +89,11 @@ class ComelitConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
if user_input is None:
|
if user_input is None:
|
||||||
return self.async_show_form(
|
return self.async_show_form(step_id="user", data_schema=USER_SCHEMA)
|
||||||
step_id="user", data_schema=user_form_schema(user_input)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
||||||
|
|
||||||
errors = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
info = await validate_input(self.hass, user_input)
|
info = await validate_input(self.hass, user_input)
|
||||||
@ -108,21 +108,21 @@ class ComelitConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_create_entry(title=info["title"], data=user_input)
|
return self.async_create_entry(title=info["title"], data=user_input)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=user_form_schema(user_input), errors=errors
|
step_id="user", data_schema=USER_SCHEMA, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_reauth(
|
async def async_step_reauth(
|
||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle reauth flow."""
|
"""Handle reauth flow."""
|
||||||
self.context["title_placeholders"] = {"host": entry_data[CONF_HOST]}
|
self.context["title_placeholders"] = {CONF_HOST: entry_data[CONF_HOST]}
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
async def async_step_reauth_confirm(
|
async def async_step_reauth_confirm(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle reauth confirm."""
|
"""Handle reauth confirm."""
|
||||||
errors = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
reauth_entry = self._get_reauth_entry()
|
reauth_entry = self._get_reauth_entry()
|
||||||
entry_data = reauth_entry.data
|
entry_data = reauth_entry.data
|
||||||
@ -163,6 +163,42 @@ class ComelitConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reconfiguration of the device."""
|
||||||
|
reconfigure_entry = self._get_reconfigure_entry()
|
||||||
|
if not user_input:
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure", data_schema=STEP_RECONFIGURE
|
||||||
|
)
|
||||||
|
|
||||||
|
updated_host = user_input[CONF_HOST]
|
||||||
|
|
||||||
|
self._async_abort_entries_match({CONF_HOST: updated_host})
|
||||||
|
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
|
try:
|
||||||
|
await validate_input(self.hass, user_input)
|
||||||
|
except CannotConnect:
|
||||||
|
errors["base"] = "cannot_connect"
|
||||||
|
except InvalidAuth:
|
||||||
|
errors["base"] = "invalid_auth"
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception("Unexpected exception")
|
||||||
|
errors["base"] = "unknown"
|
||||||
|
else:
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
reconfigure_entry, data_updates={CONF_HOST: updated_host}
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure",
|
||||||
|
data_schema=STEP_RECONFIGURE,
|
||||||
|
errors=errors,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CannotConnect(HomeAssistantError):
|
class CannotConnect(HomeAssistantError):
|
||||||
"""Error to indicate we cannot connect."""
|
"""Error to indicate we cannot connect."""
|
||||||
|
@ -70,9 +70,7 @@ rules:
|
|||||||
entity-translations: done
|
entity-translations: done
|
||||||
exception-translations: done
|
exception-translations: done
|
||||||
icon-translations: done
|
icon-translations: done
|
||||||
reconfiguration-flow:
|
reconfiguration-flow: done
|
||||||
status: todo
|
|
||||||
comment: PR in progress
|
|
||||||
repair-issues:
|
repair-issues:
|
||||||
status: exempt
|
status: exempt
|
||||||
comment: no known use cases for repair issues or flows, yet
|
comment: no known use cases for repair issues or flows, yet
|
||||||
|
@ -23,11 +23,24 @@
|
|||||||
"pin": "[%key:component::comelit::config::step::reauth_confirm::data_description::pin%]",
|
"pin": "[%key:component::comelit::config::step::reauth_confirm::data_description::pin%]",
|
||||||
"type": "The type of your Comelit device."
|
"type": "The type of your Comelit device."
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"data": {
|
||||||
|
"host": "[%key:common::config_flow::data::host%]",
|
||||||
|
"port": "[%key:common::config_flow::data::port%]",
|
||||||
|
"pin": "[%key:common::config_flow::data::pin%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"host": "[%key:component::comelit::config::step::user::data_description::host%]",
|
||||||
|
"port": "[%key:component::comelit::config::step::user::data_description::port%]",
|
||||||
|
"pin": "[%key:component::comelit::config::step::reauth_confirm::data_description::pin%]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
|
||||||
"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%]",
|
||||||
"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%]"
|
||||||
|
@ -219,3 +219,94 @@ async def test_reauth_not_successful(
|
|||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "reauth_successful"
|
assert result["reason"] == "reauth_successful"
|
||||||
assert mock_vedo_config_entry.data[CONF_PIN] == VEDO_PIN
|
assert mock_vedo_config_entry.data[CONF_PIN] == VEDO_PIN
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure_successful(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_serial_bridge: AsyncMock,
|
||||||
|
mock_serial_bridge_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the host can be reconfigured."""
|
||||||
|
mock_serial_bridge_config_entry.add_to_hass(hass)
|
||||||
|
result = await mock_serial_bridge_config_entry.start_reconfigure_flow(hass)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
# original entry
|
||||||
|
assert mock_serial_bridge_config_entry.data[CONF_HOST] == "fake_bridge_host"
|
||||||
|
|
||||||
|
new_host = "new_bridge_host"
|
||||||
|
|
||||||
|
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_HOST: new_host,
|
||||||
|
CONF_PORT: BRIDGE_PORT,
|
||||||
|
CONF_PIN: BRIDGE_PIN,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||||
|
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||||
|
|
||||||
|
# changed entry
|
||||||
|
assert mock_serial_bridge_config_entry.data[CONF_HOST] == new_host
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("side_effect", "error"),
|
||||||
|
[
|
||||||
|
(CannotConnect, "cannot_connect"),
|
||||||
|
(CannotAuthenticate, "invalid_auth"),
|
||||||
|
(ConnectionResetError, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_reconfigure_fails(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_serial_bridge: AsyncMock,
|
||||||
|
mock_serial_bridge_config_entry: MockConfigEntry,
|
||||||
|
side_effect: Exception,
|
||||||
|
error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the host can be reconfigured."""
|
||||||
|
mock_serial_bridge_config_entry.add_to_hass(hass)
|
||||||
|
result = await mock_serial_bridge_config_entry.start_reconfigure_flow(hass)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
mock_serial_bridge.login.side_effect = side_effect
|
||||||
|
|
||||||
|
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_HOST: "192.168.100.60",
|
||||||
|
CONF_PORT: BRIDGE_PORT,
|
||||||
|
CONF_PIN: BRIDGE_PIN,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reconfigure_result["type"] is FlowResultType.FORM
|
||||||
|
assert reconfigure_result["step_id"] == "reconfigure"
|
||||||
|
assert reconfigure_result["errors"] == {"base": error}
|
||||||
|
|
||||||
|
mock_serial_bridge.login.side_effect = None
|
||||||
|
|
||||||
|
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_HOST: "192.168.100.61",
|
||||||
|
CONF_PORT: BRIDGE_PORT,
|
||||||
|
CONF_PIN: BRIDGE_PIN,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||||
|
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||||
|
assert mock_serial_bridge_config_entry.data == {
|
||||||
|
CONF_HOST: "192.168.100.61",
|
||||||
|
CONF_PORT: BRIDGE_PORT,
|
||||||
|
CONF_PIN: BRIDGE_PIN,
|
||||||
|
CONF_TYPE: BRIDGE,
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user