diff --git a/homeassistant/components/waze_travel_time/config_flow.py b/homeassistant/components/waze_travel_time/config_flow.py index a196c5f4f57..d0f63b97b78 100644 --- a/homeassistant/components/waze_travel_time/config_flow.py +++ b/homeassistant/components/waze_travel_time/config_flow.py @@ -2,6 +2,8 @@ from __future__ import annotations +from typing import Any + import voluptuous as vol from homeassistant.config_entries import ( @@ -119,6 +121,10 @@ class WazeConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 + def __init__(self) -> None: + """Init Config Flow.""" + self._entry: ConfigEntry | None = None + @staticmethod @callback def async_get_options_flow( @@ -127,7 +133,9 @@ class WazeConfigFlow(ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return WazeOptionsFlow(config_entry) - async def async_step_user(self, user_input=None) -> ConfigFlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} user_input = user_input or {} @@ -140,6 +148,13 @@ class WazeConfigFlow(ConfigFlow, domain=DOMAIN): user_input[CONF_DESTINATION], user_input[CONF_REGION], ): + if self._entry: + return self.async_update_reload_and_abort( + self._entry, + title=user_input[CONF_NAME], + data=user_input, + reason="reconfigure_successful", + ) return self.async_create_entry( title=user_input.get(CONF_NAME, DEFAULT_NAME), data=user_input, @@ -155,3 +170,18 @@ class WazeConfigFlow(ConfigFlow, domain=DOMAIN): data_schema=self.add_suggested_values_to_schema(CONFIG_SCHEMA, user_input), errors=errors, ) + + async def async_step_reconfigure( + self, _: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration.""" + self._entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) + assert self._entry + + data = self._entry.data.copy() + data[CONF_REGION] = data[CONF_REGION].lower() + + return self.async_show_form( + step_id="user", + data_schema=self.add_suggested_values_to_schema(CONFIG_SCHEMA, data), + ) diff --git a/homeassistant/components/waze_travel_time/strings.json b/homeassistant/components/waze_travel_time/strings.json index 2a5017a5b9f..e6dd3c3a22e 100644 --- a/homeassistant/components/waze_travel_time/strings.json +++ b/homeassistant/components/waze_travel_time/strings.json @@ -16,7 +16,8 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_location%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_location%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" } }, "options": { diff --git a/tests/components/waze_travel_time/test_config_flow.py b/tests/components/waze_travel_time/test_config_flow.py index 6d155c4e79b..5b1e3417bfc 100644 --- a/tests/components/waze_travel_time/test_config_flow.py +++ b/tests/components/waze_travel_time/test_config_flow.py @@ -53,6 +53,50 @@ async def test_minimum_fields(hass: HomeAssistant) -> None: } +@pytest.mark.usefixtures("mock_update") +async def test_reconfigure(hass: HomeAssistant) -> None: + """Test reconfigure flow.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=MOCK_CONFIG, + options=DEFAULT_OPTIONS, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + reconfigure_result = await hass.config_entries.flow.async_init( + DOMAIN, + context={ + "source": config_entries.SOURCE_RECONFIGURE, + "entry_id": entry.entry_id, + }, + ) + assert reconfigure_result["type"] is FlowResultType.FORM + assert reconfigure_result["step_id"] == "user" + + user_step_result = await hass.config_entries.flow.async_configure( + reconfigure_result["flow_id"], + { + CONF_NAME: DEFAULT_NAME, + CONF_ORIGIN: "location3", + CONF_DESTINATION: "location4", + CONF_REGION: "us", + }, + ) + assert user_step_result["type"] is FlowResultType.ABORT + assert user_step_result["reason"] == "reconfigure_successful" + await hass.async_block_till_done() + + entry = hass.config_entries.async_entries(DOMAIN)[0] + assert entry.data == { + CONF_NAME: DEFAULT_NAME, + CONF_ORIGIN: "location3", + CONF_DESTINATION: "location4", + CONF_REGION: "US", + } + + async def test_options(hass: HomeAssistant) -> None: """Test options flow.""" entry = MockConfigEntry(