Use new reauth helpers in trafikverket_ferry (#128763)

This commit is contained in:
epenet 2024-10-19 14:33:47 +02:00 committed by GitHub
parent fe7328b92e
commit dce819f57b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,7 +9,7 @@ from pytrafikverket import TrafikverketFerry
from pytrafikverket.exceptions import InvalidAuthentication, NoFerryFound from pytrafikverket.exceptions import InvalidAuthentication, NoFerryFound
import voluptuous as vol 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, CONF_NAME, CONF_WEEKDAY, WEEKDAYS from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_WEEKDAY, WEEKDAYS
from homeassistant.helpers import selector from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -49,8 +49,6 @@ class TVFerryConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
entry: ConfigEntry | None
async def validate_input( async def validate_input(
self, api_key: str, ferry_from: str, ferry_to: str self, api_key: str, ferry_from: str, ferry_to: str
) -> None: ) -> None:
@ -63,8 +61,6 @@ class TVFerryConfigFlow(ConfigFlow, domain=DOMAIN):
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle re-authentication with Trafikverket.""" """Handle re-authentication with Trafikverket."""
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
return await self.async_step_reauth_confirm() return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm( async def async_step_reauth_confirm(
@ -76,10 +72,10 @@ class TVFerryConfigFlow(ConfigFlow, domain=DOMAIN):
if user_input: if user_input:
api_key = user_input[CONF_API_KEY] api_key = user_input[CONF_API_KEY]
assert self.entry is not None reauth_entry = self._get_reauth_entry()
try: try:
await self.validate_input( await self.validate_input(
api_key, self.entry.data[CONF_FROM], self.entry.data[CONF_TO] api_key, reauth_entry.data[CONF_FROM], reauth_entry.data[CONF_TO]
) )
except InvalidAuthentication: except InvalidAuthentication:
errors["base"] = "invalid_auth" errors["base"] = "invalid_auth"
@ -88,15 +84,10 @@ class TVFerryConfigFlow(ConfigFlow, domain=DOMAIN):
except Exception: # noqa: BLE001 except Exception: # noqa: BLE001
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
else: else:
self.hass.config_entries.async_update_entry( return self.async_update_reload_and_abort(
self.entry, reauth_entry,
data={ data_updates={CONF_API_KEY: api_key},
**self.entry.data,
CONF_API_KEY: api_key,
},
) )
await self.hass.config_entries.async_reload(self.entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_show_form( return self.async_show_form(
step_id="reauth_confirm", step_id="reauth_confirm",