Implement reauth for Sensibo (#67446)
This commit is contained in:
parent
8ee3be33e9
commit
a3a66451d7
5 changed files with 257 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
|||
"""Adds config flow for Sensibo integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pysensibo.exceptions import AuthenticationError
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -24,6 +26,55 @@ class SensiboConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
VERSION = 2
|
||||
|
||||
entry: config_entries.ConfigEntry | None
|
||||
|
||||
async def async_step_reauth(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle re-authentication with Sensibo."""
|
||||
|
||||
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Confirm re-authentication with Sensibo."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input:
|
||||
api_key = user_input[CONF_API_KEY]
|
||||
try:
|
||||
username = await async_validate_api(self.hass, api_key)
|
||||
except AuthenticationError:
|
||||
errors["base"] = "invalid_auth"
|
||||
except ConnectionError:
|
||||
errors["base"] = "cannot_connect"
|
||||
except NoDevicesError:
|
||||
errors["base"] = "no_devices"
|
||||
except NoUsernameError:
|
||||
errors["base"] = "no_username"
|
||||
else:
|
||||
assert self.entry is not None
|
||||
|
||||
if username == self.entry.unique_id:
|
||||
self.hass.config_entries.async_update_entry(
|
||||
self.entry,
|
||||
data={
|
||||
**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")
|
||||
errors["base"] = "incorrect_api_key"
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=DATA_SCHEMA,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_import(self, config: dict) -> FlowResult:
|
||||
"""Import a configuration from config.yaml."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue