diff --git a/homeassistant/components/satel_integra/config_flow.py b/homeassistant/components/satel_integra/config_flow.py index af9a82e40482..58498fb81f19 100644 --- a/homeassistant/components/satel_integra/config_flow.py +++ b/homeassistant/components/satel_integra/config_flow.py @@ -107,6 +107,13 @@ ZONE_SCHEMA = ZONE_AND_OUTPUT_SCHEMA.extend( SWITCHABLE_OUTPUT_SCHEMA = probatio.Schema({probatio.Required(CONF_NAME): cv.string}) +def _validate_user_code(code: str | None) -> dict[str, str]: + """Validate the user code used by control commands.""" + if code is not None and not 4 <= len(code) <= 16: + return {CONF_CODE: "invalid_code_length"} + return {} + + async def _async_validate_zone_temperature_sensor( entry: SatelConfigEntry, zone_number: int ) -> dict[str, str]: @@ -199,16 +206,22 @@ class SatelConfigFlow(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle code configuration.""" + errors: dict[str, str] = {} + if user_input is not None: - return self.async_create_entry( - title=self.connection_data[CONF_HOST], - data=self.connection_data, - options={CONF_CODE: user_input.get(CONF_CODE)}, - ) + code = user_input.get(CONF_CODE) + errors = _validate_user_code(code) + if not errors: + return self.async_create_entry( + title=self.connection_data[CONF_HOST], + data=self.connection_data, + options={CONF_CODE: code}, + ) return self.async_show_form( step_id="code", data_schema=CODE_SCHEMA, + errors=errors, ) async def async_step_reconfigure( @@ -293,14 +306,20 @@ class SatelOptionsFlow(OptionsFlow): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Init step.""" + errors: dict[str, str] = {} + if user_input is not None: - return self.async_create_entry(data={CONF_CODE: user_input.get(CONF_CODE)}) + code = user_input.get(CONF_CODE) + errors = _validate_user_code(code) + if not errors: + return self.async_create_entry(data={CONF_CODE: code}) return self.async_show_form( step_id="init", data_schema=self.add_suggested_values_to_schema( CODE_SCHEMA, self.config_entry.options ), + errors=errors, ) diff --git a/homeassistant/components/satel_integra/strings.json b/homeassistant/components/satel_integra/strings.json index fc8e456a4909..71f53505b029 100644 --- a/homeassistant/components/satel_integra/strings.json +++ b/homeassistant/components/satel_integra/strings.json @@ -12,6 +12,7 @@ "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "connection_initialization_failed": "Successfully connected, but failed to read data from the panel. Please check the integration encryption key is correct if your panel requires encryption.", + "invalid_code_length": "User code must be between 4 and 16 characters long.", "panel_busy": "Successfully connected, but alarm panel reports it's busy. Please check no other connections are active.", "unknown": "[%key:common::config_flow::error::unknown%]" }, @@ -216,6 +217,9 @@ } }, "options": { + "error": { + "invalid_code_length": "[%key:component::satel_integra::config::error::invalid_code_length%]" + }, "step": { "init": { "data": { diff --git a/tests/components/satel_integra/test_config_flow.py b/tests/components/satel_integra/test_config_flow.py index 80711a059612..c9c58639da6a 100644 --- a/tests/components/satel_integra/test_config_flow.py +++ b/tests/components/satel_integra/test_config_flow.py @@ -104,6 +104,39 @@ async def test_setup_flow( assert len(mock_setup_entry.mock_calls) == 1 +@pytest.mark.parametrize("code", ["123", "12345678901234567"]) +async def test_setup_flow_rejects_invalid_code( + hass: HomeAssistant, + mock_satel: AsyncMock, + mock_setup_entry: AsyncMock, + code: str, +) -> None: + """Test setup rejects a user code with an unsupported length.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], MOCK_CONFIG_DATA + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_CODE: code} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "code" + assert result["errors"] == {CONF_CODE: "invalid_code_length"} + assert len(mock_setup_entry.mock_calls) == 0 + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_CODE: "4321"} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["options"] == {CONF_CODE: "4321"} + assert len(mock_setup_entry.mock_calls) == 1 + + @pytest.mark.parametrize( ("exception", "error"), [ @@ -190,6 +223,37 @@ async def test_options_flow( assert mock_reload_after_entry_update.call_count == 1 +@pytest.mark.parametrize("code", ["123", "12345678901234567"]) +async def test_options_flow_rejects_invalid_code( + hass: HomeAssistant, + mock_satel: AsyncMock, + mock_reload_after_entry_update: MagicMock, + mock_config_entry: MockConfigEntry, + code: str, +) -> None: + """Test options reject a user code with an unsupported length.""" + await setup_integration(hass, mock_config_entry) + + result = await hass.config_entries.options.async_init(mock_config_entry.entry_id) + result = await hass.config_entries.options.async_configure( + result["flow_id"], {CONF_CODE: code} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + assert result["errors"] == {CONF_CODE: "invalid_code_length"} + assert mock_config_entry.options == MOCK_CONFIG_OPTIONS + assert mock_reload_after_entry_update.call_count == 0 + + result = await hass.config_entries.options.async_configure( + result["flow_id"], {CONF_CODE: "4321"} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert mock_config_entry.options == {CONF_CODE: "4321"} + assert mock_reload_after_entry_update.call_count == 1 + + @pytest.mark.parametrize( ("user_input", "subentry"), [