66from .const import DOMAIN , KEYS
77from .ecu_api import APsystemsSocket , APsystemsInvalidData
88
9+
910_LOGGER = logging .getLogger (__name__ )
1011
12+ """Set defaults for the configuration."""
13+ ECU_HOST = ""
1114SCAN_INTERVAL = 300
1215PORT_RETRIES = 5
1316CACHE_REBOOT = 3
1417SHOW_GRAPHS = True
1518WIFI_SSID = "ECU-WIFI_local"
1619WIFI_PASSWORD = "default"
1720
21+
1822@config_entries .HANDLERS .register (DOMAIN )
19- class FlowHandler (config_entries .ConfigFlow , domain = DOMAIN ):
23+ class FlowHandler (config_entries .ConfigFlow ):
2024 """Handle a config flow."""
2125 VERSION = 1
2226
23- def __init__ (self ):
24- """Initialize the flow handler."""
25- self .ecu_id = None
26- self .user_input = {}
27-
2827 async def async_step_user (self , user_input = None ):
2928 errors = {}
3029 init_schema = vol .Schema ({
31- vol .Required (KEYS [0 ], default = '' ): str ,
30+ vol .Required (KEYS [0 ], default = ECU_HOST ): str ,
3231 vol .Required (KEYS [1 ], default = SCAN_INTERVAL ): int ,
33- vol .Required (KEYS [2 ], default = PORT_RETRIES ): vol .All (int , vol .Range (1 , 10 )),
32+ vol .Required (KEYS [2 ], default = PORT_RETRIES ): vol .All (int , vol .Range (min = 1 , max = 10 )),
33+ vol .Required (KEYS [3 ], default = CACHE_REBOOT ): vol .All (int , vol .Range (min = 3 , max = 5 )),
3434 vol .Optional (KEYS [4 ], default = SHOW_GRAPHS ): bool ,
35+ vol .Optional (KEYS [5 ], default = WIFI_SSID ): str ,
36+ vol .Optional (KEYS [6 ], default = WIFI_PASSWORD ): str ,
3537 })
3638
3739 if user_input :
3840 ecu_id = await test_ecu_connection (user_input )
3941 if ecu_id :
40- _LOGGER .debug ("ECU connection successful, ECU ID: %s" , ecu_id )
41- self .ecu_id = ecu_id
42- self .user_input = user_input
43- if ecu_id .startswith (("215" , "2162" )):
44- return await self .async_step_additional_options ()
42+ await self .async_set_unique_id (ecu_id )
4543 return self .async_create_entry (title = "APsystems" , data = user_input )
4644 errors ["ecu_host" ] = "no_ecu_found"
4745
46+ # Show form because user input is empty.
4847 return self .async_show_form (
4948 step_id = "user" ,
5049 data_schema = init_schema ,
5150 errors = errors
5251 )
5352
54- async def async_step_additional_options (self , user_input = None ):
55- """Handle additional options for ECU-R-Pro and ECU-C devices."""
56- errors = {}
57- additional_schema = vol .Schema ({
58- vol .Required (KEYS [3 ], default = CACHE_REBOOT ): vol .All (int , vol .Range (3 , 5 )),
59- vol .Optional (KEYS [5 ], default = WIFI_SSID ): str ,
60- vol .Optional (KEYS [6 ], default = WIFI_PASSWORD ): str ,
61- })
62-
63- if user_input :
64- _LOGGER .debug ("Received additional options: %s" , user_input )
65- user_input .update (self .user_input )
66- await self .async_set_unique_id (self .ecu_id )
67- return self .async_create_entry (title = "APsystems" , data = user_input )
68-
69- return self .async_show_form (
70- step_id = "additional_options" ,
71- data_schema = additional_schema ,
72- errors = errors ,
73- description_placeholders = {"title" : "Additional Configuration" }
74- )
75-
7653 @staticmethod
7754 @callback
7855 def async_get_options_flow (config_entry ):
@@ -84,17 +61,22 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
8461
8562 def __init__ (self , entry : config_entries .ConfigEntry ) -> None :
8663 """Initialize options flow."""
64+ super ().__init__ ()
8765 self .entry = entry
8866
8967 async def async_step_init (self , user_input = None ):
9068 """Manage the options."""
9169 errors = {}
92- _cfg = {** self .entry .data }
70+ # Get current options
71+ _config = {** self .entry .data }
9372 alter_schema = vol .Schema ({
94- vol .Required (KEYS [0 ], default = _cfg .get (KEYS [0 ])): str ,
95- vol .Required (KEYS [1 ], default = _cfg .get (KEYS [1 ], SCAN_INTERVAL )): int ,
96- vol .Required (KEYS [2 ], default = _cfg .get (KEYS [2 ], PORT_RETRIES )): vol .All (int , vol .Range (1 , 10 )),
97- vol .Optional (KEYS [4 ], default = _cfg .get (KEYS [4 ], SHOW_GRAPHS )): bool ,
73+ vol .Required (KEYS [0 ], default = _config .get (KEYS [0 ],ECU_HOST )): str ,
74+ vol .Required (KEYS [1 ], default = _config .get (KEYS [1 ], SCAN_INTERVAL )): int ,
75+ vol .Required (KEYS [2 ], default = _config .get (KEYS [2 ], PORT_RETRIES )): vol .All (int ,vol .Range (min = 1 , max = 10 )),
76+ vol .Required (KEYS [3 ], default = _config .get (KEYS [3 ], CACHE_REBOOT )): vol .All (int ,vol .Range (min = 3 , max = 5 )),
77+ vol .Optional (KEYS [4 ], default = _config .get (KEYS [4 ], SHOW_GRAPHS )): bool ,
78+ vol .Optional (KEYS [5 ], default = _config .get (KEYS [5 ], WIFI_SSID )): str ,
79+ vol .Optional (KEYS [6 ], default = _config .get (KEYS [6 ], WIFI_PASSWORD )): str ,
9880 })
9981
10082 if user_input :
@@ -104,42 +86,14 @@ async def async_step_init(self, user_input=None):
10486 self .entry ,
10587 data = {** self .entry .data , ** user_input }
10688 )
107- if ecu_id .startswith (("215" , "2162" )):
108- return await self .async_step_additional_options ()
10989 return self .async_create_entry (title = "APsystems" , data = {})
11090 errors ["ecu_host" ] = "no_ecu_found"
111-
11291 return self .async_show_form (
11392 step_id = "init" ,
11493 data_schema = alter_schema ,
11594 errors = errors
11695 )
11796
118- async def async_step_additional_options (self , user_input = None ):
119- """Handle additional options."""
120- errors = {}
121- _cfg = {** self .entry .data }
122- additional_schema = vol .Schema ({
123- vol .Required (KEYS [3 ], default = _cfg .get (KEYS [3 ], CACHE_REBOOT )): vol .All (int , vol .Range (3 , 5 )),
124- vol .Optional (KEYS [5 ], default = _cfg .get (KEYS [5 ], WIFI_SSID )): str ,
125- vol .Optional (KEYS [6 ], default = _cfg .get (KEYS [6 ], WIFI_PASSWORD )): str ,
126- })
127-
128- if user_input :
129- _LOGGER .debug ("Received additional options: %s" , user_input )
130- self .hass .config_entries .async_update_entry (
131- self .entry ,
132- data = {** self .entry .data , ** user_input }
133- )
134- return self .async_create_entry (title = "APsystems" , data = {})
135-
136- return self .async_show_form (
137- step_id = "additional_options" ,
138- data_schema = additional_schema ,
139- errors = errors ,
140- description_placeholders = {"title" : "Additional Configuration" }
141- )
142-
14397async def test_ecu_connection (input_data ):
14498 """Test the connection to the ECU and return the ECU ID if successful."""
14599 try :
0 commit comments