99from .const import (
1010 DOMAIN ,
1111 CONF_DEVICE_PATH ,
12+ CONF_SCAN_INTERVAL ,
1213 DEFAULT_DEVICE_PATH ,
14+ DEFAULT_SCAN_INTERVAL ,
1315)
1416from .axpert import AxpertInverter
1517
@@ -26,6 +28,7 @@ async def async_step_user(self, user_input=None) -> FlowResult:
2628
2729 if user_input is not None :
2830 device_path = user_input .get (CONF_DEVICE_PATH )
31+ scan_interval = user_input .get (CONF_SCAN_INTERVAL , DEFAULT_SCAN_INTERVAL )
2932
3033 # Validate connection
3134 try :
@@ -44,10 +47,16 @@ async def async_step_user(self, user_input=None) -> FlowResult:
4447 step_id = "user" ,
4548 data_schema = vol .Schema ({
4649 vol .Required (CONF_DEVICE_PATH , default = DEFAULT_DEVICE_PATH ): str ,
50+ vol .Optional (CONF_SCAN_INTERVAL , default = DEFAULT_SCAN_INTERVAL ): vol .All (vol .Coerce (int ), vol .Range (min = 1 )),
4751 }),
4852 errors = errors ,
4953 )
5054
55+ @staticmethod
56+ def async_get_options_flow (config_entry ):
57+ """Get the options flow for this handler."""
58+ return AxpertOptionsFlowHandler ()
59+
5160 def _validate_connection (self , path : str ):
5261 """Try to connect to the inverter."""
5362 inverter = AxpertInverter (path )
@@ -56,11 +65,58 @@ def _validate_connection(self, path: str):
5665 # Actually in axpert.py we implemented send_command.
5766 # Let's try get_device_id. If it fails, open might fail or read timeout.
5867 try :
59- # Just instantiate and try one command
60- # Note: get_device_id might not work on all firmware, QPIGS is safer as main data source.
61- # But let's try QPIGS to be sure we can read data.
62- res = inverter .get_general_status ()
63- if not res :
64- raise Exception ("Empty response from QPIGS during validation" )
68+ # Just instantiate and try one command
69+ # Note: get_device_id might not work on all firmware, QPIGS is safer as main data source.
70+ # But let's try QPIGS to be sure we can read data.
71+ res = inverter .get_general_status ()
72+ if not res :
73+ raise Exception ("Empty response from QPIGS during validation" )
74+ except Exception as e :
75+ raise Exception (f"Validation failed: { e } " )
76+
77+ class AxpertOptionsFlowHandler (config_entries .OptionsFlow ):
78+ """Handle options flow for Axpert Inverter."""
79+
80+ async def async_step_init (self , user_input = None ) -> FlowResult :
81+ """Manage the options."""
82+ errors = {}
83+
84+ if user_input is not None :
85+ device_path = user_input .get (CONF_DEVICE_PATH )
86+
87+ # We can optionally validate the new path here
88+ # But normally we just accept it and let the reload handle connection
89+ try :
90+ # Basic validation using the executor
91+ inverter = AxpertInverter (device_path )
92+ await self .hass .async_add_executor_job (self ._validate_connection_options , inverter )
93+
94+ return self .async_create_entry (title = "" , data = user_input )
95+ except Exception as e :
96+ _LOGGER .error (f"Failed to connect to new path: { e } " )
97+ errors ["base" ] = "cannot_connect"
98+
99+ current_path = self .config_entry .options .get (
100+ CONF_DEVICE_PATH , self .config_entry .data .get (CONF_DEVICE_PATH , DEFAULT_DEVICE_PATH )
101+ )
102+ current_scan_interval = self .config_entry .options .get (
103+ CONF_SCAN_INTERVAL , self .config_entry .data .get (CONF_SCAN_INTERVAL , DEFAULT_SCAN_INTERVAL )
104+ )
105+
106+ return self .async_show_form (
107+ step_id = "init" ,
108+ data_schema = vol .Schema ({
109+ vol .Required (CONF_DEVICE_PATH , default = current_path ): str ,
110+ vol .Optional (CONF_SCAN_INTERVAL , default = current_scan_interval ): vol .All (vol .Coerce (int ), vol .Range (min = 1 )),
111+ }),
112+ errors = errors ,
113+ )
114+
115+ def _validate_connection_options (self , inverter : AxpertInverter ):
116+ """Validate connection during options update."""
117+ try :
118+ res = inverter .get_general_status ()
119+ if not res :
120+ raise Exception ("Empty response from QPIGS during validation" )
65121 except Exception as e :
66122 raise Exception (f"Validation failed: { e } " )
0 commit comments