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:
Simone Chemelli 2025-05-19 17:12:27 +03:00 committed by GitHub
parent 8938c109c2
commit 9c798cbb5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 161 additions and 23 deletions

View File

@ -28,20 +28,22 @@ DEFAULT_HOST = "192.168.1.252"
DEFAULT_PIN = 111111
def user_form_schema(user_input: dict[str, Any] | None) -> 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_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
vol.Required(CONF_TYPE, default=BRIDGE): vol.In(DEVICE_TYPE_LIST),
}
)
USER_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
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_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]:
@ -87,13 +89,11 @@ class ComelitConfigFlow(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=user_form_schema(user_input)
)
return self.async_show_form(step_id="user", data_schema=USER_SCHEMA)
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
errors = {}
errors: dict[str, str] = {}
try:
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_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(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""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()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reauth confirm."""
errors = {}
errors: dict[str, str] = {}
reauth_entry = self._get_reauth_entry()
entry_data = reauth_entry.data
@ -163,6 +163,42 @@ class ComelitConfigFlow(ConfigFlow, domain=DOMAIN):
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):
"""Error to indicate we cannot connect."""

View File

@ -70,9 +70,7 @@ rules:
entity-translations: done
exception-translations: done
icon-translations: done
reconfiguration-flow:
status: todo
comment: PR in progress
reconfiguration-flow: done
repair-issues:
status: exempt
comment: no known use cases for repair issues or flows, yet

View File

@ -23,11 +23,24 @@
"pin": "[%key:component::comelit::config::step::reauth_confirm::data_description::pin%]",
"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": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
"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%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"

View File

@ -219,3 +219,94 @@ async def test_reauth_not_successful(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
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,
}