66from .const import DOMAIN , KEYS
77from .ecu_api import APsystemsSocket , APsystemsInvalidData
88
9-
109_LOGGER = logging .getLogger (__name__ )
1110
11+ SCAN_INTERVAL = 300
12+ PORT_RETRIES = 5
13+ CACHE_REBOOT = 3
14+ SHOW_GRAPHS = True
15+ WIFI_SSID = "ECU-WIFI_local"
16+ WIFI_PASSWORD = "default"
1217
1318@config_entries .HANDLERS .register (DOMAIN )
14- class FlowHandler (config_entries .ConfigFlow ):
19+ class FlowHandler (config_entries .ConfigFlow , domain = DOMAIN ):
1520 """Handle a config flow."""
1621 VERSION = 1
1722
23+ def __init__ (self ):
24+ """Initialize the flow handler."""
25+ self .ecu_id = None
26+ self .user_input = {}
27+
1828 async def async_step_user (self , user_input = None ):
1929 errors = {}
2030 init_schema = vol .Schema ({
21- vol .Required (KEYS [0 ], default = '' ): str , # ECU Host
22- vol .Required (KEYS [1 ], default = 300 ): int , # scan interval
23- vol .Required (KEYS [2 ], default = 5 ): vol .All (int , vol .Range (min = 1 , max = 10 )), # Port retries
24- vol .Required (KEYS [3 ], default = 3 ): vol .All (int , vol .Range (min = 3 , max = 5 )), # Cache reboot
25- vol .Optional (KEYS [4 ], default = True ): bool , # Show graphs
26- vol .Optional (KEYS [5 ], default = "ECU-WIFI_local" ): str , # SSID
27- vol .Optional (KEYS [6 ], default = "default" ): str , # Password
31+ vol .Required (KEYS [0 ], default = '' ): str ,
32+ vol .Required (KEYS [1 ], default = SCAN_INTERVAL ): int ,
33+ vol .Required (KEYS [2 ], default = PORT_RETRIES ): vol .All (int , vol .Range (1 , 10 )),
34+ vol .Optional (KEYS [4 ], default = SHOW_GRAPHS ): bool ,
2835 })
2936
3037 if user_input :
3138 ecu_id = await test_ecu_connection (user_input )
3239 if ecu_id :
33- await self .async_set_unique_id (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 ()
3445 return self .async_create_entry (title = "APsystems" , data = user_input )
3546 errors ["ecu_host" ] = "no_ecu_found"
3647
37- # Show form because user input is empty.
3848 return self .async_show_form (
3949 step_id = "user" ,
4050 data_schema = init_schema ,
4151 errors = errors
4252 )
4353
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+
4476 @staticmethod
4577 @callback
4678 def async_get_options_flow (config_entry ):
@@ -52,34 +84,17 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
5284
5385 def __init__ (self , entry : config_entries .ConfigEntry ) -> None :
5486 """Initialize options flow."""
55- super ().__init__ ()
5687 self .entry = entry
5788
5889 async def async_step_init (self , user_input = None ):
5990 """Manage the options."""
6091 errors = {}
61- # Get current options
62- current_config = {** self .entry .data }
92+ _cfg = {** self .entry .data }
6393 alter_schema = vol .Schema ({
64- vol .Required (KEYS [0 ], default = current_config .get (KEYS [0 ])): str , # ECU Host
65- vol .Required (KEYS [1 ], default = current_config .get (KEYS [1 ], 300 )): int , # Scan interval
66- vol .Required (
67- KEYS [2 ],
68- default = current_config .get (KEYS [2 ], 5 )
69- ): vol .All (
70- int ,
71- vol .Range (min = 1 , max = 10 )
72- ), # Port retries
73- vol .Required (
74- KEYS [3 ],
75- default = current_config .get (KEYS [3 ], 3 )
76- ): vol .All (
77- int ,
78- vol .Range (min = 3 , max = 5 )
79- ), # Cache reboot
80- vol .Optional (KEYS [4 ], default = current_config .get (KEYS [4 ], True )): bool , # Show graphs
81- vol .Optional (KEYS [5 ], default = current_config .get (KEYS [5 ], "ECU-local" )): str , # SSID
82- vol .Optional (KEYS [6 ], default = current_config .get (KEYS [6 ], "default" )): str , # Password
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 ,
8398 })
8499
85100 if user_input :
@@ -89,14 +104,42 @@ async def async_step_init(self, user_input=None):
89104 self .entry ,
90105 data = {** self .entry .data , ** user_input }
91106 )
107+ if ecu_id .startswith (("215" , "2162" )):
108+ return await self .async_step_additional_options ()
92109 return self .async_create_entry (title = "APsystems" , data = {})
93110 errors ["ecu_host" ] = "no_ecu_found"
111+
94112 return self .async_show_form (
95113 step_id = "init" ,
96114 data_schema = alter_schema ,
97115 errors = errors
98116 )
99117
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+
100143async def test_ecu_connection (input_data ):
101144 """Test the connection to the ECU and return the ECU ID if successful."""
102145 try :
0 commit comments