22from __future__ import annotations
33
44from collections .abc import Mapping
5+ from dataclasses import dataclass , field
56from typing import Any
67
7- from aionotion import async_get_client
88from aionotion .errors import InvalidCredentialsError , NotionError
99import voluptuous as vol
1010
1313from homeassistant .const import CONF_PASSWORD , CONF_USERNAME
1414from homeassistant .core import HomeAssistant
1515from homeassistant .data_entry_flow import FlowResult
16- from homeassistant .helpers import aiohttp_client
1716
18- from .const import DOMAIN , LOGGER
17+ from .const import CONF_REFRESH_TOKEN , CONF_USER_UUID , DOMAIN , LOGGER
18+ from .util import async_get_client_with_credentials
1919
2020AUTH_SCHEMA = vol .Schema (
2121 {
3030)
3131
3232
33+ @dataclass (frozen = True , kw_only = True )
34+ class CredentialsValidationResult :
35+ """Define a validation result."""
36+ 37+ user_uuid : str | None = None
38+ refresh_token : str | None = None
39+ errors : dict [str , Any ] = field (default_factory = dict )
40+ 41+ 3342async def async_validate_credentials (
3443 hass : HomeAssistant , username : str , password : str
35- ) -> dict [str , Any ]:
36- """Validate a Notion username and password (returning any errors)."""
37- session = aiohttp_client .async_get_clientsession (hass )
44+ ) -> CredentialsValidationResult :
45+ """Validate a Notion username and password."""
3846 errors = {}
3947
4048 try :
41- await async_get_client (
42- username , password , session = session , use_legacy_auth = True
43- )
49+ client = await async_get_client_with_credentials (hass , username , password )
4450 except InvalidCredentialsError :
4551 errors ["base" ] = "invalid_auth"
4652 except NotionError as err :
@@ -50,7 +56,12 @@ async def async_validate_credentials(
5056 LOGGER .exception ("Unknown error while validation credentials: %s" , err )
5157 errors ["base" ] = "unknown"
5258
53- return errors
59+ if errors :
60+ return CredentialsValidationResult (errors = errors )
61+ 62+ return CredentialsValidationResult (
63+ user_uuid = client .user_uuid , refresh_token = client .refresh_token
64+ )
5465
5566
5667class NotionFlowHandler (config_entries .ConfigFlow , domain = DOMAIN ):
@@ -84,20 +95,24 @@ async def async_step_reauth_confirm(
8495 },
8596 )
8697
87- if errors : = await async_validate_credentials (
98+ credentials_validation_result = await async_validate_credentials (
8899 self .hass , self ._reauth_entry .data [CONF_USERNAME ], user_input [CONF_PASSWORD ]
89- ):
100+ )
101+ 102+ if credentials_validation_result .errors :
90103 return self .async_show_form (
91104 step_id = "reauth_confirm" ,
92105 data_schema = REAUTH_SCHEMA ,
93- errors = errors ,
106+ errors = credentials_validation_result . errors ,
94107 description_placeholders = {
95108 CONF_USERNAME : self ._reauth_entry .data [CONF_USERNAME ]
96109 },
97110 )
98111
99112 self .hass .config_entries .async_update_entry (
100- self ._reauth_entry , data = self ._reauth_entry .data | user_input
113+ self ._reauth_entry ,
114+ data = self ._reauth_entry .data
115+ | {CONF_REFRESH_TOKEN : credentials_validation_result .refresh_token },
101116 )
102117 self .hass .async_create_task (
103118 self .hass .config_entries .async_reload (self ._reauth_entry .entry_id )
@@ -114,13 +129,22 @@ async def async_step_user(
114129 await self .async_set_unique_id (user_input [CONF_USERNAME ])
115130 self ._abort_if_unique_id_configured ()
116131
117- if errors : = await async_validate_credentials (
132+ credentials_validation_result = await async_validate_credentials (
118133 self .hass , user_input [CONF_USERNAME ], user_input [CONF_PASSWORD ]
119- ):
134+ )
135+ 136+ if credentials_validation_result .errors :
120137 return self .async_show_form (
121138 step_id = "user" ,
122139 data_schema = AUTH_SCHEMA ,
123- errors = errors ,
140+ errors = credentials_validation_result . errors ,
124141 )
125142
126- return self .async_create_entry (title = user_input [CONF_USERNAME ], data = user_input )
143+ return self .async_create_entry (
144+ title = user_input [CONF_USERNAME ],
145+ data = {
146+ CONF_USERNAME : user_input [CONF_USERNAME ],
147+ CONF_USER_UUID : credentials_validation_result .user_uuid ,
148+ CONF_REFRESH_TOKEN : credentials_validation_result .refresh_token ,
149+ },
150+ )
0 commit comments