Add reconfigure step for waze_travel_time (#114885)

* Add reconfigure step for waze_travel_time

* Add reconfigure_successful string

* Update tests/components/waze_travel_time/test_config_flow.py

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Kevin Stillhammer 2024-04-07 00:06:30 +02:00 committed by GitHub
parent ed451cab3b
commit cce8e4839a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 77 additions and 2 deletions

View file

@ -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),
)

View file

@ -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": {

View file

@ -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(