1+ """Config flow."""
2+
3+ import asyncio
14import logging
5+
26import voluptuous as vol
3- import asyncio
4- from homeassistant import config_entries , exceptions
7+
8+ from homeassistant import config_entries
59from homeassistant .core import callback
610
711from .const import DOMAIN , KEYS
812
913_LOGGER = logging .getLogger (__name__ )
1014
1115
16+ async def validate_ip (ip_address : str ) -> bool :
17+ """Validate EMA server ip."""
18+ try :
19+ reader , writer = await asyncio .wait_for (
20+ asyncio .open_connection (ip_address , 8995 ), timeout = 3.0
21+ )
22+ # Close the connection neatly
23+ writer .close ()
24+ await writer .wait_closed ()
25+ except (OSError , TimeoutError ):
26+ return False
27+ else :
28+ return True
29+
30+
1231class ConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
1332 """Integration configuration."""
33+
1434 VERSION = 1
1535 CONNECTION_CLASS = config_entries .CONN_CLASS_LOCAL_PUSH
1636
@@ -24,71 +44,62 @@ def async_get_options_flow(config_entry):
2444 async def async_step_user (self , user_input = None ):
2545 """First step: Initial set-up of integration options."""
2646 _LOGGER .debug ("async_step_user" )
27- schema = vol .Schema ({
28- vol .Required (KEYS [0 ], default = "3.67.1.32" ): str ,
29- vol .Required (KEYS [1 ], default = "1800" ): str ,
30- vol .Required (KEYS [2 ], default = "300" ): str ,
31- vol .Required (KEYS [3 ], default = "660" ): str ,
32- vol .Required (KEYS [4 ], default = True ): bool ,
33- })
47+ schema = vol .Schema (
48+ {
49+ vol .Required (KEYS [0 ], default = "3.67.1.32" ): str ,
50+ vol .Required (KEYS [1 ], default = "1800" ): str ,
51+ vol .Required (KEYS [2 ], default = "300" ): str ,
52+ vol .Required (KEYS [3 ], default = "660" ): str ,
53+ vol .Required (KEYS [4 ], default = True ): bool ,
54+ }
55+ )
3456
3557 if user_input is not None :
36- if await OptionsFlowHandler .validate_ip (user_input ["ema_host" ]):
37- return self .async_create_entry (title = "APsystems ECU proxy" , data = user_input )
38- else :
39- return self .async_show_form (
40- step_id = "user" ,
41- data_schema = schema ,
42- errors = {"base" : "Could not connect to the specified EMA host" },
58+ if await validate_ip (user_input ["ema_host" ]):
59+ return self .async_create_entry (
60+ title = "APsystems ECU proxy" , data = user_input
4361 )
62+
63+ return self .async_show_form (
64+ step_id = "user" ,
65+ data_schema = schema ,
66+ errors = {"base" : "Could not connect to the specified EMA host" },
67+ )
4468 return self .async_show_form (step_id = "user" , data_schema = schema )
4569
4670
4771class OptionsFlowHandler (config_entries .OptionsFlow ):
4872 """Regular change of integration options."""
73+
4974 def __init__ (self , config_entry : config_entries .ConfigEntry ) -> None :
75+ """Init options handler."""
5076 self .config_entry = config_entry
5177
5278 async def async_step_init (self , user_input = None ):
5379 """Second step: Altering the integration options."""
80+ errors = {}
5481 current_options = (
55- self .config_entry .data
56- if not self .config_entry .options
82+ self .config_entry .data
83+ if not self .config_entry .options
5784 else self .config_entry .options
5885 )
5986 _LOGGER .debug ("async_step_init with configuration: %s" , current_options )
60-
61- schema = vol .Schema ({
62- vol .Required (key , default = current_options .get (key )): (
63- str if key != "send_to_ema" else bool
64- )
65- for key in KEYS
66- })
6787
88+ schema = vol .Schema (
89+ {
90+ vol .Required (key , default = current_options .get (key )): (
91+ str if key != "send_to_ema" else bool
92+ )
93+ for key in KEYS
94+ }
95+ )
6896
6997 if user_input is not None :
70- if await self .validate_ip (user_input ["ema_host" ]):
71- updated_options = current_options .copy ()
72- updated_options .update (user_input )
73- return self .async_create_entry (title = "" , data = updated_options )
74- else :
75- return self .async_show_form (
76- step_id = "init" ,
77- data_schema = schema ,
78- errors = {"base" : "Could not connect to the specified EMA host" },
98+ if await validate_ip (user_input ["ema_host" ]):
99+ self .hass .config_entries .async_update_entry (
100+ self .config_entry , data = user_input
79101 )
80- return self .async_show_form ( step_id = "init " , data_schema = schema )
102+ return self .async_create_entry ( title = " " , data = {} )
81103
82- @staticmethod
83- async def validate_ip (ip_address : str ) -> bool :
84- try :
85- reader , writer = await asyncio .wait_for (
86- asyncio .open_connection (ip_address , 8995 ),
87- timeout = 3.0
88- )
89- # Close the connection neatly
90- writer .close ()
91- await writer .wait_closed ()
92- return True
93- except (OSError , asyncio .TimeoutError ):
94- return False
104+ errors ["base" ] = "Could not connect to the specified EMA host"
105+ return self .async_show_form (step_id = "init" , data_schema = schema , errors = errors )
0 commit comments