mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use reauth/reconfigure helpers in trafikverket_weatherstation config flow (#128028)
This commit is contained in:
parent
acd32b500c
commit
78f4b28697
@ -13,7 +13,7 @@ from pytrafikverket.exceptions import (
|
||||
from pytrafikverket.trafikverket_weather import TrafikverketWeather
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@ -31,8 +31,6 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
entry: ConfigEntry
|
||||
|
||||
async def validate_input(self, sensor_api: str, station: str) -> None:
|
||||
"""Validate input from user input."""
|
||||
web_session = async_get_clientsession(self.hass)
|
||||
@ -84,8 +82,6 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, entry_data: Mapping[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle re-authentication with Trafikverket."""
|
||||
|
||||
self.entry = self._get_reauth_entry()
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
@ -93,12 +89,13 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
) -> ConfigFlowResult:
|
||||
"""Confirm re-authentication with Trafikverket."""
|
||||
errors: dict[str, str] = {}
|
||||
reauth_entry = self._get_reauth_entry()
|
||||
|
||||
if user_input:
|
||||
api_key = user_input[CONF_API_KEY]
|
||||
|
||||
try:
|
||||
await self.validate_input(api_key, self.entry.data[CONF_STATION])
|
||||
await self.validate_input(api_key, reauth_entry.data[CONF_STATION])
|
||||
except InvalidAuthentication:
|
||||
errors["base"] = "invalid_auth"
|
||||
except NoWeatherStationFound:
|
||||
@ -109,7 +106,7 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
return self.async_update_reload_and_abort(
|
||||
self.entry, data={**self.entry.data, CONF_API_KEY: api_key}
|
||||
reauth_entry, data_updates={CONF_API_KEY: api_key}
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
@ -122,14 +119,6 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> 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:
|
||||
@ -147,10 +136,9 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
return self.async_update_reload_and_abort(
|
||||
self.entry,
|
||||
self._get_reconfigure_entry(),
|
||||
title=user_input[CONF_STATION],
|
||||
data=user_input,
|
||||
reason="reconfigure_successful",
|
||||
)
|
||||
|
||||
schema = self.add_suggested_values_to_schema(
|
||||
@ -162,11 +150,11 @@ class TVWeatherConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
vol.Required(CONF_STATION): TextSelector(),
|
||||
}
|
||||
),
|
||||
{**self.entry.data, **(user_input or {})},
|
||||
{**self._get_reconfigure_entry().data, **(user_input or {})},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure_confirm",
|
||||
step_id="reconfigure",
|
||||
data_schema=schema,
|
||||
errors=errors,
|
||||
)
|
||||
|
@ -23,7 +23,7 @@
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
}
|
||||
},
|
||||
"reconfigure_confirm": {
|
||||
"reconfigure": {
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||
"station": "[%key:component::trafikverket_weatherstation::config::step::user::data::station%]"
|
||||
|
@ -206,7 +206,7 @@ async def test_reconfigure_flow(hass: HomeAssistant) -> None:
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
assert result["step_id"] == "reconfigure_confirm"
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
@ -265,7 +265,7 @@ async def test_reconfigure_flow_fails(
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
assert result["step_id"] == "reconfigure_confirm"
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user