11"""Config flow for Sagemcom integration."""
2- import logging
32
43from aiohttp import ClientError
54from homeassistant import config_entries
1312from homeassistant .core import callback
1413from homeassistant .helpers .aiohttp_client import async_get_clientsession
1514from sagemcom_api .client import SagemcomClient
16- from sagemcom_api .enums import EncryptionMethod
1715from sagemcom_api .exceptions import (
1816 AccessRestrictionException ,
1917 AuthenticationException ,
18+ LoginRetryErrorException ,
2019 LoginTimeoutException ,
2120 MaximumSessionCountException ,
21+ UnsupportedHostException ,
2222)
2323import voluptuous as vol
2424
25- from .const import CONF_ENCRYPTION_METHOD , DOMAIN
25+ from .const import CONF_ENCRYPTION_METHOD , DOMAIN , LOGGER
2626from .options_flow import OptionsFlow
2727
28- _LOGGER = logging .getLogger (__name__ )
29-
30- ENCRYPTION_METHODS = [item .value for item in EncryptionMethod ]
31-
32- DATA_SCHEMA = vol .Schema (
33- {
34- vol .Required (CONF_HOST ): str ,
35- vol .Optional (CONF_USERNAME ): str ,
36- vol .Optional (CONF_PASSWORD ): str ,
37- vol .Required (CONF_ENCRYPTION_METHOD ): vol .In (ENCRYPTION_METHODS ),
38- vol .Required (CONF_SSL , default = False ): bool ,
39- vol .Required (CONF_VERIFY_SSL , default = False ): bool ,
40- }
41- )
42-
4328
4429class ConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
4530 """Handle a config flow for Sagemcom."""
4631
4732 VERSION = 1
4833 CONNECTION_CLASS = config_entries .CONN_CLASS_LOCAL_POLL
4934
35+ _host : str | None = None
36+ _username : str | None = None
37+
5038 async def async_validate_input (self , user_input ):
5139 """Validate user credentials."""
52- username = user_input .get (CONF_USERNAME ) or ""
40+ self . _username = user_input .get (CONF_USERNAME ) or ""
5341 password = user_input .get (CONF_PASSWORD ) or ""
54- host = user_input [CONF_HOST ]
55- encryption_method = user_input [CONF_ENCRYPTION_METHOD ]
42+ self ._host = user_input [CONF_HOST ]
5643 ssl = user_input [CONF_SSL ]
5744
5845 session = async_get_clientsession (self .hass , user_input [CONF_VERIFY_SSL ])
5946
6047 client = SagemcomClient (
61- host ,
62- username ,
63- password ,
64- EncryptionMethod (encryption_method ),
65- session ,
48+ host = self ._host ,
49+ username = self ._username ,
50+ password = password ,
51+ session = session ,
6652 ssl = ssl ,
6753 )
6854
55+ user_input [CONF_ENCRYPTION_METHOD ] = await client .get_encryption_method ()
56+ LOGGER .debug (
57+ "Detected encryption method: %s" , user_input [CONF_ENCRYPTION_METHOD ]
58+ )
59+
6960 await client .login ()
7061 await client .logout ()
7162
7263 return self .async_create_entry (
73- title = host ,
64+ title = self . _host ,
7465 data = user_input ,
7566 )
7667
@@ -89,18 +80,32 @@ async def async_step_user(self, user_input=None):
8980 errors ["base" ] = "access_restricted"
9081 except AuthenticationException :
9182 errors ["base" ] = "invalid_auth"
92- except (TimeoutError , ClientError ):
83+ except (TimeoutError , ClientError , ConnectionError ):
9384 errors ["base" ] = "cannot_connect"
9485 except LoginTimeoutException :
9586 errors ["base" ] = "login_timeout"
9687 except MaximumSessionCountException :
9788 errors ["base" ] = "maximum_session_count"
89+ except LoginRetryErrorException :
90+ errors ["base" ] = "login_retry_error"
91+ except UnsupportedHostException :
92+ errors ["base" ] = "unsupported_host"
9893 except Exception as exception : # pylint: disable=broad-except
9994 errors ["base" ] = "unknown"
100- _LOGGER .exception (exception )
95+ LOGGER .exception (exception )
10196
10297 return self .async_show_form (
103- step_id = "user" , data_schema = DATA_SCHEMA , errors = errors
98+ step_id = "user" ,
99+ data_schema = vol .Schema (
100+ {
101+ vol .Required (CONF_HOST , default = self ._host ): str ,
102+ vol .Optional (CONF_USERNAME , default = self ._username ): str ,
103+ vol .Optional (CONF_PASSWORD ): str ,
104+ vol .Required (CONF_SSL , default = False ): bool ,
105+ vol .Required (CONF_VERIFY_SSL , default = False ): bool ,
106+ }
107+ ),
108+ errors = errors ,
104109 )
105110
106111 @staticmethod
0 commit comments