Remove YAML support from August (#47615)
This commit is contained in:
parent
d49a436573
commit
f605a3c149
9 changed files with 239 additions and 299 deletions
|
@ -5,14 +5,9 @@ from august.authenticator import ValidationResult
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
|
||||
from .const import (
|
||||
CONF_LOGIN_METHOD,
|
||||
DEFAULT_TIMEOUT,
|
||||
LOGIN_METHODS,
|
||||
VERIFICATION_CODE_KEY,
|
||||
)
|
||||
from .const import CONF_LOGIN_METHOD, LOGIN_METHODS, VERIFICATION_CODE_KEY
|
||||
from .const import DOMAIN # pylint:disable=unused-import
|
||||
from .exceptions import CannotConnect, InvalidAuth, RequireValidation
|
||||
from .gateway import AugustGateway
|
||||
|
@ -68,61 +63,48 @@ class AugustConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
def __init__(self):
|
||||
"""Store an AugustGateway()."""
|
||||
self._august_gateway = None
|
||||
self.user_auth_details = {}
|
||||
self._user_auth_details = {}
|
||||
self._needs_reset = False
|
||||
self._mode = None
|
||||
super().__init__()
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle the initial step."""
|
||||
if self._august_gateway is None:
|
||||
self._august_gateway = AugustGateway(self.hass)
|
||||
self._august_gateway = AugustGateway(self.hass)
|
||||
return await self.async_step_user_validate()
|
||||
|
||||
async def async_step_user_validate(self, user_input=None):
|
||||
"""Handle authentication."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
combined_inputs = {**self.user_auth_details, **user_input}
|
||||
await self._august_gateway.async_setup(combined_inputs)
|
||||
if self._needs_reset:
|
||||
self._needs_reset = False
|
||||
await self._august_gateway.async_reset_authentication()
|
||||
|
||||
try:
|
||||
info = await async_validate_input(
|
||||
combined_inputs,
|
||||
self._august_gateway,
|
||||
)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
errors["base"] = "invalid_auth"
|
||||
except RequireValidation:
|
||||
self.user_auth_details.update(user_input)
|
||||
|
||||
return await self.async_step_validation()
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if not errors:
|
||||
self.user_auth_details.update(user_input)
|
||||
|
||||
existing_entry = await self.async_set_unique_id(
|
||||
combined_inputs[CONF_USERNAME]
|
||||
)
|
||||
if existing_entry:
|
||||
self.hass.config_entries.async_update_entry(
|
||||
existing_entry, data=info["data"]
|
||||
)
|
||||
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
||||
return self.async_abort(reason="reauth_successful")
|
||||
return self.async_create_entry(title=info["title"], data=info["data"])
|
||||
result = await self._async_auth_or_validate(user_input, errors)
|
||||
if result is not None:
|
||||
return result
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=self._async_build_schema(), errors=errors
|
||||
step_id="user_validate",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(
|
||||
CONF_LOGIN_METHOD,
|
||||
default=self._user_auth_details.get(CONF_LOGIN_METHOD, "phone"),
|
||||
): vol.In(LOGIN_METHODS),
|
||||
vol.Required(
|
||||
CONF_USERNAME,
|
||||
default=self._user_auth_details.get(CONF_USERNAME),
|
||||
): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
}
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_validation(self, user_input=None):
|
||||
"""Handle validation (2fa) step."""
|
||||
if user_input:
|
||||
return await self.async_step_user({**self.user_auth_details, **user_input})
|
||||
if self._mode == "reauth":
|
||||
return await self.async_step_reauth_validate(user_input)
|
||||
return await self.async_step_user_validate(user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="validation",
|
||||
|
@ -130,34 +112,70 @@ class AugustConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
{vol.Required(VERIFICATION_CODE_KEY): vol.All(str, vol.Strip)}
|
||||
),
|
||||
description_placeholders={
|
||||
CONF_USERNAME: self.user_auth_details.get(CONF_USERNAME),
|
||||
CONF_LOGIN_METHOD: self.user_auth_details.get(CONF_LOGIN_METHOD),
|
||||
CONF_USERNAME: self._user_auth_details[CONF_USERNAME],
|
||||
CONF_LOGIN_METHOD: self._user_auth_details[CONF_LOGIN_METHOD],
|
||||
},
|
||||
)
|
||||
|
||||
async def async_step_import(self, user_input):
|
||||
"""Handle import."""
|
||||
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
async def async_step_reauth(self, data):
|
||||
"""Handle configuration by re-auth."""
|
||||
self.user_auth_details = dict(data)
|
||||
self._user_auth_details = dict(data)
|
||||
self._mode = "reauth"
|
||||
self._needs_reset = True
|
||||
return await self.async_step_user()
|
||||
self._august_gateway = AugustGateway(self.hass)
|
||||
return await self.async_step_reauth_validate()
|
||||
|
||||
def _async_build_schema(self):
|
||||
"""Generate the config flow schema."""
|
||||
base_schema = {
|
||||
vol.Required(CONF_LOGIN_METHOD, default="phone"): vol.In(LOGIN_METHODS),
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
|
||||
}
|
||||
for key in self.user_auth_details:
|
||||
if key == CONF_PASSWORD or key not in base_schema:
|
||||
continue
|
||||
del base_schema[key]
|
||||
return vol.Schema(base_schema)
|
||||
async def async_step_reauth_validate(self, user_input=None):
|
||||
"""Handle reauth and validation."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
result = await self._async_auth_or_validate(user_input, errors)
|
||||
if result is not None:
|
||||
return result
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_validate",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
}
|
||||
),
|
||||
errors=errors,
|
||||
description_placeholders={
|
||||
CONF_USERNAME: self._user_auth_details[CONF_USERNAME],
|
||||
},
|
||||
)
|
||||
|
||||
async def _async_auth_or_validate(self, user_input, errors):
|
||||
self._user_auth_details.update(user_input)
|
||||
await self._august_gateway.async_setup(self._user_auth_details)
|
||||
if self._needs_reset:
|
||||
self._needs_reset = False
|
||||
await self._august_gateway.async_reset_authentication()
|
||||
try:
|
||||
info = await async_validate_input(
|
||||
self._user_auth_details,
|
||||
self._august_gateway,
|
||||
)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
errors["base"] = "invalid_auth"
|
||||
except RequireValidation:
|
||||
return await self.async_step_validation()
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if errors:
|
||||
return None
|
||||
|
||||
existing_entry = await self.async_set_unique_id(
|
||||
self._user_auth_details[CONF_USERNAME]
|
||||
)
|
||||
if not existing_entry:
|
||||
return self.async_create_entry(title=info["title"], data=info["data"])
|
||||
|
||||
self.hass.config_entries.async_update_entry(existing_entry, data=info["data"])
|
||||
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
||||
return self.async_abort(reason="reauth_successful")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue