diff --git a/homeassistant/components/niko_home_control/config_flow.py b/homeassistant/components/niko_home_control/config_flow.py index a49549996b9..f755670814a 100644 --- a/homeassistant/components/niko_home_control/config_flow.py +++ b/homeassistant/components/niko_home_control/config_flow.py @@ -39,6 +39,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 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: + 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="reconfigure", data_schema=DATA_SCHEMA, errors=errors + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/homeassistant/components/niko_home_control/strings.json b/homeassistant/components/niko_home_control/strings.json index 6e2b50d4736..2abb5b71f46 100644 --- a/homeassistant/components/niko_home_control/strings.json +++ b/homeassistant/components/niko_home_control/strings.json @@ -9,13 +9,22 @@ "data_description": { "host": "The hostname or IP address of the Niko Home Control controller." } + }, + "reconfigure": { + "data": { + "host": "[%key:common::config_flow::data::host%]" + }, + "data_description": { + "host": "[%key:component::niko_home_control::config::step::user::data_description::host%]" + } } }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" } } } diff --git a/tests/components/niko_home_control/conftest.py b/tests/components/niko_home_control/conftest.py index 35260b387de..330488727bb 100644 --- a/tests/components/niko_home_control/conftest.py +++ b/tests/components/niko_home_control/conftest.py @@ -79,6 +79,7 @@ def mock_niko_home_control_connection( client = mock_client.return_value client.lights = [light, dimmable_light] client.covers = [cover] + client.connect = AsyncMock(return_value=True) yield client diff --git a/tests/components/niko_home_control/test_config_flow.py b/tests/components/niko_home_control/test_config_flow.py index 2878dc91138..41f9a3dcf9e 100644 --- a/tests/components/niko_home_control/test_config_flow.py +++ b/tests/components/niko_home_control/test_config_flow.py @@ -36,7 +36,7 @@ async def test_full_flow( assert len(mock_setup_entry.mock_calls) == 1 -async def test_cannot_connect(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: +async def test_cannot_connect(hass: HomeAssistant) -> None: """Test the cannot connect error.""" result = await hass.config_entries.flow.async_init( @@ -69,7 +69,7 @@ async def test_cannot_connect(hass: HomeAssistant, mock_setup_entry: AsyncMock) async def test_duplicate_entry( - hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry + hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: """Test uniqueness.""" @@ -88,3 +88,83 @@ async def test_duplicate_entry( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_duplicate_reconfigure_entry( + hass: HomeAssistant, + mock_niko_home_control_connection: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfigure to other existing entry.""" + mock_config_entry.add_to_hass(hass) + another_entry = MockConfigEntry( + domain=DOMAIN, + title="Niko Home Control", + data={CONF_HOST: "192.168.0.124"}, + entry_id="01JFN93M7KRA38V5AMPCJ2JYYB", + ) + another_entry.add_to_hass(hass) + + result = await mock_config_entry.start_reconfigure_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.0.124"} + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_reconfigure( + hass: HomeAssistant, + mock_niko_home_control_connection: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + + 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"}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + + +async def test_reconfigure_cannot_connect( + hass: HomeAssistant, + mock_niko_home_control_connection: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfiguration with connection error.""" + mock_config_entry.add_to_hass(hass) + + mock_niko_home_control_connection.connect.side_effect = Exception("cannot_connect") + + result = await mock_config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.0.122"}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "cannot_connect"} + + mock_niko_home_control_connection.connect.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.0.122"}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful"