Use reauth helpers in glances (#128579)

This commit is contained in:
epenet 2024-10-18 10:01:20 +02:00 committed by GitHub
parent 2d90ffcbf0
commit d2eb0e1fde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,7 @@ from glances_api.exceptions import (
) )
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 ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_PASSWORD, CONF_PASSWORD,
@ -40,15 +40,11 @@ class GlancesFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a Glances config flow.""" """Handle a Glances config flow."""
VERSION = 1 VERSION = 1
_reauth_entry: ConfigEntry | None
async def async_step_reauth( async def async_step_reauth(
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error.""" """Perform reauth upon an API authentication error."""
self._reauth_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(
@ -56,9 +52,10 @@ class GlancesFlowHandler(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Confirm reauth dialog.""" """Confirm reauth dialog."""
errors = {} errors = {}
assert self._reauth_entry
reauth_entry = self._get_reauth_entry()
if user_input is not None: if user_input is not None:
user_input = {**self._reauth_entry.data, **user_input} user_input = {**reauth_entry.data, **user_input}
try: try:
await get_api(self.hass, user_input) await get_api(self.hass, user_input)
except GlancesApiAuthorizationError: except GlancesApiAuthorizationError:
@ -67,15 +64,13 @@ class GlancesFlowHandler(ConfigFlow, domain=DOMAIN):
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
else: else:
self.hass.config_entries.async_update_entry( self.hass.config_entries.async_update_entry(
self._reauth_entry, data=user_input reauth_entry, data=user_input
) )
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id) await self.hass.config_entries.async_reload(reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful") return self.async_abort(reason="reauth_successful")
return self.async_show_form( return self.async_show_form(
description_placeholders={ description_placeholders={CONF_USERNAME: reauth_entry.data[CONF_USERNAME]},
CONF_USERNAME: self._reauth_entry.data[CONF_USERNAME]
},
step_id="reauth_confirm", step_id="reauth_confirm",
data_schema=vol.Schema( data_schema=vol.Schema(
{ {