From 1feda62877d6d3d2885afc280e79ec78fccf3dd0 Mon Sep 17 00:00:00 2001 From: "Glenn Vandeuren (aka Iondependent)" Date: Wed, 25 Dec 2024 12:49:35 +0100 Subject: [PATCH 1/3] Add reconfigure flow --- .../niko_home_control/config_flow.py | 19 ++++++++ .../niko_home_control/test_config_flow.py | 46 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/homeassistant/components/niko_home_control/config_flow.py b/homeassistant/components/niko_home_control/config_flow.py index f37e5e9248a3a5..42fce0ab9bdefc 100644 --- a/homeassistant/components/niko_home_control/config_flow.py +++ b/homeassistant/components/niko_home_control/config_flow.py @@ -35,6 +35,25 @@ class NikoHomeControlConfigFlow(ConfigFlow, domain=DOMAIN): MINOR_VERSION = 2 + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration of the integration.""" + errors: dict[str, str] = {} + if user_input: + self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) + error = await test_connection(user_input[CONF_HOST]) + if not error: + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data_updates=user_input, + ) + errors["base"] = error + + return self.async_show_form( + step_id="user", data_schema=DATA_SCHEMA, errors=errors + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/tests/components/niko_home_control/test_config_flow.py b/tests/components/niko_home_control/test_config_flow.py index f911f4ebb1a1b3..a22503e0d8b4b7 100644 --- a/tests/components/niko_home_control/test_config_flow.py +++ b/tests/components/niko_home_control/test_config_flow.py @@ -138,3 +138,49 @@ async def test_duplicate_import_entry( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_reconfigure_duplicate_config( + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry +) -> None: + """Test the reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert set(result["data_schema"].schema) == {CONF_HOST} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.0.123"} + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_reconfigure( + hass: HomeAssistant, + mock_niko_home_control_connection: AsyncMock, + mock_setup_entry: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert set(result["data_schema"].schema) == {CONF_HOST} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.0.122"}, + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.CREATE_ENTRY From 7210c3588ed8fad54c9cd2610eef543521413f90 Mon Sep 17 00:00:00 2001 From: "Glenn Vandeuren (aka Iondependent)" Date: Wed, 25 Dec 2024 16:06:08 +0100 Subject: [PATCH 2/3] Update test_config_flow.py --- .../niko_home_control/test_config_flow.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/components/niko_home_control/test_config_flow.py b/tests/components/niko_home_control/test_config_flow.py index a22503e0d8b4b7..a79c164467a4da 100644 --- a/tests/components/niko_home_control/test_config_flow.py +++ b/tests/components/niko_home_control/test_config_flow.py @@ -184,3 +184,29 @@ async def test_reconfigure( ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY + + +async def test_cannot_connect_after_reconfigure( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the cannot connect error.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + with patch( + "homeassistant.components.niko_home_control.config_flow.NHCController.connect", + side_effect=Exception, + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.0.122"}, + ) + assert result["errors"] == {"base": "cannot_connect"} From fc9bf92746109637105447948e1d54e73beba326 Mon Sep 17 00:00:00 2001 From: "Glenn Vandeuren (aka Iondependent)" Date: Thu, 26 Dec 2024 11:51:35 +0100 Subject: [PATCH 3/3] Cover some more --- .../niko_home_control/config_flow.py | 2 +- .../niko_home_control/test_config_flow.py | 28 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/niko_home_control/config_flow.py b/homeassistant/components/niko_home_control/config_flow.py index 42fce0ab9bdefc..0cfcaf9ad82192 100644 --- a/homeassistant/components/niko_home_control/config_flow.py +++ b/homeassistant/components/niko_home_control/config_flow.py @@ -40,7 +40,7 @@ async def async_step_reconfigure( ) -> ConfigFlowResult: """Handle reconfiguration of the integration.""" errors: dict[str, str] = {} - if user_input: + if user_input is not None: self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) error = await test_connection(user_input[CONF_HOST]) if not error: diff --git a/tests/components/niko_home_control/test_config_flow.py b/tests/components/niko_home_control/test_config_flow.py index a79c164467a4da..b1e40d1f5d40f8 100644 --- a/tests/components/niko_home_control/test_config_flow.py +++ b/tests/components/niko_home_control/test_config_flow.py @@ -140,28 +140,36 @@ async def test_duplicate_import_entry( assert result["reason"] == "already_configured" -async def test_reconfigure_duplicate_config( +async def test_duplicate_reconfigure_entry( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: - """Test the reconfigure flow.""" + """Test uniqueness.""" + mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await mock_config_entry.start_reconfigure_flow(hass) - - assert result["type"] is FlowResultType.FORM - assert set(result["data_schema"].schema) == {CONF_HOST} - result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_HOST: "192.168.0.123"} ) - await hass.async_block_till_done() - assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" +async def test_reconfigure_setup( + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry +) -> None: + """Test the reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert set(result["data_schema"].schema) == {CONF_HOST} + + async def test_reconfigure( hass: HomeAssistant, mock_niko_home_control_connection: AsyncMock, @@ -186,7 +194,7 @@ async def test_reconfigure( assert result["type"] is FlowResultType.CREATE_ENTRY -async def test_cannot_connect_after_reconfigure( +async def test_reconfigure_cannot_connect( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry,