diff --git a/homeassistant/components/e3dc/config_flow.py b/homeassistant/components/e3dc/config_flow.py index 0f978fc6b52e..954ed8c0efd9 100644 --- a/homeassistant/components/e3dc/config_flow.py +++ b/homeassistant/components/e3dc/config_flow.py @@ -162,6 +162,27 @@ class E3dcConfigFlow(ConfigFlow, domain=DOMAIN): }, ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle updating an existing entry's connection details.""" + entry = self._get_reconfigure_entry() + errors: dict[str, str] = {} + if user_input is not None: + device, errors = await self._async_validate(user_input) + if device is not None: + await self.async_set_unique_id(device.serial_number) + self._abort_if_unique_id_mismatch() + return self.async_update_reload_and_abort(entry, data=user_input) + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + STEP_USER_DATA_SCHEMA, user_input or entry.data + ), + errors=errors, + ) + async def _async_validate( self, data: dict[str, Any] ) -> tuple[E3dcPowerStation | None, dict[str, str]]: diff --git a/homeassistant/components/e3dc/quality_scale.yaml b/homeassistant/components/e3dc/quality_scale.yaml index 7e5e41647d34..d9ad7efa2c98 100644 --- a/homeassistant/components/e3dc/quality_scale.yaml +++ b/homeassistant/components/e3dc/quality_scale.yaml @@ -68,7 +68,7 @@ rules: entity-translations: done exception-translations: done icon-translations: todo - reconfiguration-flow: todo + reconfiguration-flow: done repair-issues: todo stale-devices: todo diff --git a/homeassistant/components/e3dc/strings.json b/homeassistant/components/e3dc/strings.json index 7c6a68d3a348..3069ebf3eba6 100644 --- a/homeassistant/components/e3dc/strings.json +++ b/homeassistant/components/e3dc/strings.json @@ -6,6 +6,7 @@ "no_local_address": "The power station announced itself, but on no network Home Assistant is connected to.", "no_serial_number": "[%key:component::e3dc::config::error::no_serial_number%]", "not_e3dc": "[%key:component::e3dc::config::error::not_e3dc%]", + "unique_id_mismatch": "Reconfigure the same power station you originally set up.", "unknown": "[%key:common::config_flow::error::unknown%]" }, "error": { @@ -16,6 +17,17 @@ }, "flow_title": "{name}", "step": { + "reconfigure": { + "data": { + "host": "[%key:common::config_flow::data::host%]", + "port": "[%key:common::config_flow::data::port%]" + }, + "data_description": { + "host": "[%key:component::e3dc::config::step::user::data_description::host%]", + "port": "[%key:component::e3dc::config::step::user::data_description::port%]" + }, + "description": "Update how to reach this power station." + }, "user": { "data": { "host": "[%key:common::config_flow::data::host%]", @@ -81,7 +93,7 @@ "message": "The device is not an E3/DC power station in E3/DC protocol mode." }, "serial_mismatch": { - "message": "The device at this address has serial number {found}, not {expected}." + "message": "The device at this address has serial number {found}, not {expected}. If the power station's IP address changed, reconfigure the entry." } } } diff --git a/tests/components/e3dc/test_config_flow.py b/tests/components/e3dc/test_config_flow.py index 8b5fb9c1a250..c331ff3543d5 100644 --- a/tests/components/e3dc/test_config_flow.py +++ b/tests/components/e3dc/test_config_flow.py @@ -238,6 +238,107 @@ async def test_user_step_already_configured( assert result["reason"] == "already_configured" +@pytest.mark.usefixtures("mock_setup_entry") +async def test_reconfigure( + hass: HomeAssistant, + identified: MockModbusConnection, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfiguring probes the new address and moves the entry there.""" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reconfigure_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + # The form starts out with the entry's current address + assert { + str(key): key.description["suggested_value"] + for key in result["data_schema"].schema + } == MOCK_ENTRY_DATA + + with _patch_temporary_unit(identified) as mock_temporary_unit: + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.1.200", CONF_PORT: 1502} + ) + mock_temporary_unit.assert_called_once_with( + hass, ModbusTcpParams(host="192.168.1.200", port=1502), 1 + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data == {CONF_HOST: "192.168.1.200", CONF_PORT: 1502} + + +@pytest.mark.usefixtures("mock_setup_entry") +async def test_reconfigure_other_device( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> None: + """Test reconfiguring onto a station with another serial number keeps the old address.""" + connection = MockModbusConnection() + seed_identity(connection.for_unit(1), serial="S10-99999999999") + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reconfigure_flow(hass) + with _patch_temporary_unit(connection): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.1.200", CONF_PORT: 502} + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "unique_id_mismatch" + assert mock_config_entry.data == MOCK_ENTRY_DATA + + +@pytest.mark.usefixtures("mock_setup_entry") +async def test_reconfigure_cannot_connect( + hass: HomeAssistant, + identified: MockModbusConnection, + mock_config_entry: MockConfigEntry, +) -> None: + """Test a station that does not answer during reconfigure shows an error and the user can retry.""" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reconfigure_flow(hass) + with patch( + "homeassistant.components.e3dc.config_flow.async_get_temporary_unit", + side_effect=ModbusTimeoutError(), + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.1.200", CONF_PORT: 502} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "cannot_connect"} + assert mock_config_entry.data == MOCK_ENTRY_DATA + + with _patch_temporary_unit(identified): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.1.200", CONF_PORT: 502} + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data == {CONF_HOST: "192.168.1.200", CONF_PORT: 502} + + +async def test_reconfigure_reloads_a_loaded_entry( + hass: HomeAssistant, + identified: MockModbusConnection, + init_integration: MockConfigEntry, +) -> None: + """Test reconfiguring a loaded entry sets it up again on the new port.""" + result = await init_integration.start_reconfigure_flow(hass) + with ( + _patch_temporary_unit(identified), + patch( + "homeassistant.components.e3dc.async_setup_entry", + new_callable=AsyncMock, + return_value=None, + ) as mock_setup, + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: "192.168.1.100", CONF_PORT: 1502} + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert init_integration.data[CONF_PORT] == 1502 + mock_setup.assert_called_once() + + @pytest.mark.usefixtures("mock_setup_entry") async def test_zeroconf(hass: HomeAssistant, identified: MockModbusConnection) -> None: """Test discovery picks the address on Home Assistant's network and probes it."""