1515from homeassistant .core import callback
1616from homeassistant .exceptions import ConfigEntryAuthFailed
1717from homeassistant .exceptions import ConfigEntryNotReady
18+ from homeassistant .helpers import selector
1819import voluptuous as vol
1920
2021from .__init__ import async_create_and_refresh_coordinators
3637from .exceptions import TimeoutException
3738
3839_LOGGER : logging .Logger = logging .getLogger (__name__ )
40+ SCAN_INTERVAL_OPTIONS = [10 , 30 , 60 , 120 ]
41+ SCAN_INTERVAL_SELECTOR_OPTIONS = [str (value ) for value in SCAN_INTERVAL_OPTIONS ]
42+
43+
44+ def _get_scan_interval (data : dict [str :Any ]) -> str :
45+ scan_interval = data .get (CONF_SCAN_INTERVAL , DEFAULT_SCAN_INTERVAL )
46+ try :
47+ scan_interval = int (scan_interval )
48+ except (TypeError , ValueError ):
49+ scan_interval = DEFAULT_SCAN_INTERVAL
50+
51+ if scan_interval not in SCAN_INTERVAL_OPTIONS :
52+ scan_interval = DEFAULT_SCAN_INTERVAL
53+
54+ return str (scan_interval )
55+
56+
57+ def _normalize_scan_interval (data : dict [str :Any ]) -> None :
58+ if CONF_SCAN_INTERVAL in data :
59+ data [CONF_SCAN_INTERVAL ] = int (data [CONF_SCAN_INTERVAL ])
3960
4061
4162def _get_auth_schema (data : dict [str :Any ]):
@@ -51,10 +72,7 @@ def _get_schema(data: dict[str:Any]):
5172 if data is None :
5273 data = {}
5374 schema = _get_auth_schema (data )
54- scan_interval = data .get (CONF_SCAN_INTERVAL , DEFAULT_SCAN_INTERVAL )
55-
56- if scan_interval not in [10 , 30 , 60 , 120 ]:
57- scan_interval = DEFAULT_SCAN_INTERVAL
75+ scan_interval = _get_scan_interval (data )
5876
5977 schema .update (
6078 {
@@ -64,9 +82,11 @@ def _get_schema(data: dict[str:Any]):
6482 vol .Required (
6583 CONF_SCAN_INTERVAL ,
6684 default = scan_interval ,
67- ): vol .All (
68- vol .Coerce (int ),
69- vol .In ({10 , 30 , 60 , 120 }),
85+ ): selector .SelectSelector (
86+ selector .SelectSelectorConfig (
87+ options = SCAN_INTERVAL_SELECTOR_OPTIONS ,
88+ mode = selector .SelectSelectorMode .DROPDOWN ,
89+ )
7090 ),
7191 vol .Required (
7292 CONF_CONSIDER_HOME ,
@@ -163,6 +183,7 @@ async def async_step_user(self, user_input=None):
163183 self ._errors = {}
164184
165185 if user_input is not None :
186+ _normalize_scan_interval (user_input )
166187 self ._errors = await _async_test_credentials (self .hass , user_input )
167188 if len (self ._errors ) == 0 :
168189 _ensure_user_input_optionals (user_input )
@@ -226,6 +247,7 @@ async def async_step_init(self, user_input: dict[str:Any] = None):
226247
227248 if user_input is not None :
228249 _ensure_user_input_optionals (user_input )
250+ _normalize_scan_interval (user_input )
229251 self .data .update (user_input )
230252
231253 self ._errors = await _async_test_credentials (self .hass , self .data )
0 commit comments