mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Add reconfigure step to trafikverket weather (#127140)
* Add reconfigure step to trafikverket weather * Use helper * Fix * Fix review comments
This commit is contained in:
parent
f8b192bd94
commit
a3b1a30d06
@ -17,6 +17,11 @@ from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResu
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.selector import (
|
||||
TextSelector,
|
||||
TextSelectorConfig,
|
||||
TextSelectorType,
|
||||
)
|
||||
|
||||
from .const import CONF_STATION, DOMAIN
|
||||
|
||||
@ -118,3 +123,56 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
data_schema=vol.Schema({vol.Required(CONF_API_KEY): cv.string}),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, entry_data: Mapping[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle re-configuration with Trafikverket."""
|
||||
|
||||
self.entry = self._get_reconfigure_entry()
|
||||
return await self.async_step_reconfigure_confirm()
|
||||
|
||||
async def async_step_reconfigure_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Confirm re-configuration with Trafikverket."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input:
|
||||
try:
|
||||
await self.validate_input(
|
||||
user_input[CONF_API_KEY], user_input[CONF_STATION]
|
||||
)
|
||||
except InvalidAuthentication:
|
||||
errors["base"] = "invalid_auth"
|
||||
except NoWeatherStationFound:
|
||||
errors["base"] = "invalid_station"
|
||||
except MultipleWeatherStationsFound:
|
||||
errors["base"] = "more_stations"
|
||||
except Exception: # noqa: BLE001
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
return self.async_update_reload_and_abort(
|
||||
self.entry,
|
||||
title=user_input[CONF_STATION],
|
||||
data=user_input,
|
||||
reason="reconfigure_successful",
|
||||
)
|
||||
|
||||
schema = self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): TextSelector(
|
||||
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||
),
|
||||
vol.Required(CONF_STATION): TextSelector(),
|
||||
}
|
||||
),
|
||||
{**self.entry.data, **(user_input or {})},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure_confirm",
|
||||
data_schema=schema,
|
||||
errors=errors,
|
||||
)
|
||||
|
@ -2,7 +2,8 @@
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
@ -21,6 +22,12 @@
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
}
|
||||
},
|
||||
"reconfigure_confirm": {
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||
"station": "[%key:component::trafikverket_weatherstation::config::step::user::data::station%]"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -192,3 +192,111 @@ async def test_reauth_flow_fails(
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": base_error}
|
||||
|
||||
|
||||
async def test_reconfigure_flow(hass: HomeAssistant) -> None:
|
||||
"""Test a reconfigure flow."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_STATION: "Vallby",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
assert result["step_id"] == "reconfigure_confirm"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.trafikverket_weatherstation.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert entry.data == {"api_key": "1234567891", "station": "Vallby_new"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "base_error"),
|
||||
[
|
||||
(
|
||||
InvalidAuthentication,
|
||||
"invalid_auth",
|
||||
),
|
||||
(
|
||||
NoWeatherStationFound,
|
||||
"invalid_station",
|
||||
),
|
||||
(
|
||||
MultipleWeatherStationsFound,
|
||||
"more_stations",
|
||||
),
|
||||
(
|
||||
Exception,
|
||||
"cannot_connect",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_flow_fails(
|
||||
hass: HomeAssistant, side_effect: Exception, base_error: str
|
||||
) -> None:
|
||||
"""Test a reauthentication flow."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_STATION: "Vallby",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
assert result["step_id"] == "reconfigure_confirm"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
|
||||
side_effect=side_effect(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": base_error}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.trafikverket_weatherstation.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "1234567891", CONF_STATION: "Vallby_new"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert entry.data == {"api_key": "1234567891", "station": "Vallby_new"}
|
||||
|
Loading…
x
Reference in New Issue
Block a user