Skip to content

Commit

Permalink
В config flow добавлено отображение ошибки авторизации
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Apr 23, 2024
1 parent 14cecee commit f4fd95c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions custom_components/yandex_station_intents/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,30 @@ async def async_step_cookies(self, user_input: ConfigType) -> FlowResult:
host = next(p["domain"] for p in raw if p["domain"].startswith(".yandex."))
cookies = {p["name"]: p["value"] for p in raw}
except (TypeError, KeyError, json.decoder.JSONDecodeError):
return await self._show_form(AuthMethod.COOKIES, errors={"base": "cookies.invalid_format"})
return await self._show_form(AuthMethod.COOKIES, error_code="cookies.invalid_format")

try:
response = await self._session.login_cookies(host, cookies)
except AuthException as e:
_LOGGER.error(f"Ошибка авторизации: {e}")
return await self._show_form(AuthMethod.COOKIES, errors={"base": "auth.error"})
return await self._show_form(AuthMethod.COOKIES, error_code="auth.error", error_description=str(e))

return await self._check_yandex_response(response, AuthMethod.COOKIES)

async def async_step_token(self, user_input: ConfigType) -> FlowResult:
response = await self._session.validate_token(user_input[AuthMethod.TOKEN])
return await self._check_yandex_response(response, AuthMethod.TOKEN)

async def _show_form(self, method: AuthMethod, errors: dict[str, str] | None = None) -> FlowResult:
async def _show_form(
self, method: AuthMethod, error_code: str | None = None, error_description: str | None = None
) -> FlowResult:
errors = {}
if error_code:
errors["base"] = error_code

return self.async_show_form(
step_id=str(method),
errors=errors,
description_placeholders={"error_description": error_description},
data_schema=vol.Schema({vol.Required(str(method)): str}),
)

Expand All @@ -103,7 +109,6 @@ async def _check_yandex_response(self, response: LoginResponse, method: AuthMeth
return self.async_create_entry(title=response.display_login, data={CONF_X_TOKEN: response.x_token})

elif response.error:
_LOGGER.error(f"Ошибка авторизации: {response.error}")
return await self._show_form(method, errors={"base": "auth.error"})
return await self._show_form(method, error_code="auth.error", error_description=response.error)

raise NotImplementedError
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"error": {
"cookies.invalid_format": "Это не похоже на JSON из расширения Copy Cookies",
"auth.error": "Ошибка авторизации, проверьте журнал Home Assistant"
"auth.error": "Ошибка авторизации: {error_description}"
},
"step": {
"user": {
Expand Down

0 comments on commit f4fd95c

Please sign in to comment.