11"""Config flow for ohme integration."""
22
3+ from collections .abc import Mapping
34from typing import Any
45
56from ohme import ApiException , AuthException , OhmeApiClient
3233 }
3334)
3435
36+ REAUTH_SCHEMA = vol .Schema (
37+ {
38+ vol .Required (CONF_PASSWORD ): TextSelector (
39+ TextSelectorConfig (
40+ type = TextSelectorType .PASSWORD ,
41+ autocomplete = "current-password" ,
42+ ),
43+ ),
44+ }
45+ )
46+
3547
3648class OhmeConfigFlow (ConfigFlow , domain = DOMAIN ):
3749 """Config flow."""
@@ -46,14 +58,9 @@ async def async_step_user(
4658 if user_input is not None :
4759 self ._async_abort_entries_match ({CONF_EMAIL : user_input [CONF_EMAIL ]})
4860
49- instance = OhmeApiClient (user_input [CONF_EMAIL ], user_input [CONF_PASSWORD ])
50- try :
51- await instance .async_login ()
52- except AuthException :
53- errors ["base" ] = "invalid_auth"
54- except ApiException :
55- errors ["base" ] = "unknown"
56-
61+ errors = await self ._validate_account (
62+ user_input [CONF_EMAIL ], user_input [CONF_PASSWORD ]
63+ )
5764 if not errors :
5865 return self .async_create_entry (
5966 title = user_input [CONF_EMAIL ], data = user_input
@@ -62,3 +69,48 @@ async def async_step_user(
6269 return self .async_show_form (
6370 step_id = "user" , data_schema = USER_SCHEMA , errors = errors
6471 )
72+
73+ async def async_step_reauth (
74+ self , entry_data : Mapping [str , Any ]
75+ ) -> ConfigFlowResult :
76+ """Handle re-authentication."""
77+ return await self .async_step_reauth_confirm ()
78+
79+ async def async_step_reauth_confirm (
80+ self , user_input : dict [str , Any ] | None = None
81+ ) -> ConfigFlowResult :
82+ """Handle re-authentication confirmation."""
83+ errors : dict [str , str ] = {}
84+ reauth_entry = self ._get_reauth_entry ()
85+ if user_input is not None :
86+ errors = await self ._validate_account (
87+ reauth_entry .data [CONF_EMAIL ],
88+ user_input [CONF_PASSWORD ],
89+ )
90+ if not errors :
91+ return self .async_update_reload_and_abort (
92+ reauth_entry ,
93+ data_updates = user_input ,
94+ )
95+ return self .async_show_form (
96+ step_id = "reauth_confirm" ,
97+ data_schema = REAUTH_SCHEMA ,
98+ description_placeholders = {"email" : reauth_entry .data [CONF_EMAIL ]},
99+ errors = errors ,
100+ )
101+
102+ async def _validate_account (self , email : str , password : str ) -> dict [str , str ]:
103+ """Validate Ohme account and return dict of errors."""
104+ errors : dict [str , str ] = {}
105+ client = OhmeApiClient (
106+ email ,
107+ password ,
108+ )
109+ try :
110+ await client .async_login ()
111+ except AuthException :
112+ errors ["base" ] = "invalid_auth"
113+ except ApiException :
114+ errors ["base" ] = "unknown"
115+
116+ return errors
0 commit comments