33
44from typing import Any
55from homeassistant import config_entries
6+ from homeassistant .core import callback
67from homeassistant .helpers .selector import selector , EntitySelector , EntityFilterSelectorConfig
78from .const import *
89
@@ -96,4 +97,104 @@ async def async_step_pricing(self, user_input: dict[str, Any] | None = None) ->
9697 self .data [CONF_PRICING_STEP ] = user_input
9798 return self .async_create_entry (title = DOMAIN , data = self .data )
9899
99- return self .async_show_form (step_id = "pricing" , data_schema = PRICING_SCHEMA )
100+ return self .async_show_form (step_id = "pricing" , data_schema = PRICING_SCHEMA )
101+
102+
103+ @staticmethod
104+ @callback
105+ def async_get_options_flow (config_entry : config_entries .ConfigEntry ):
106+ """Get the options flow for this handler."""
107+ return OptionsFlowHandler ()
108+
109+
110+ class OptionsFlowHandler (config_entries .OptionsFlowWithReload ):
111+ """Handles options flow for the component"""
112+
113+ async def async_step_init (self , user_input : dict [str , Any ] | None = None ) -> config_entries .ConfigFlowResult :
114+ """Invoked when a user initiates a flow via the user interface."""
115+ old_config = self .hass .data [DOMAIN ][CONFIG ]
116+ old_brand = Brand (str (old_config [CONF_USER_STEP ][CONF_INVERTER_BRAND ]))
117+ OPTIONS_INIT_SCHEMA = vol .Schema (
118+ {
119+ vol .Required (CONF_INVERTER_BRAND , default = old_brand ): selector ({
120+ "select" : {
121+ "options" : [brand .value for brand in Brand ]
122+ }
123+ })
124+ }
125+ )
126+ errors = {}
127+ self .data = {}
128+ if user_input is not None :
129+ self .brand = Brand (user_input [CONF_INVERTER_BRAND ])
130+ self .data [CONF_USER_STEP ] = user_input
131+ return await self .async_step_connect ()
132+
133+ return self .async_show_form (step_id = "init" , data_schema = OPTIONS_INIT_SCHEMA , errors = errors )
134+
135+ async def async_step_connect (self , user_input : dict [str , Any ] | None = None ) -> config_entries .ConfigFlowResult :
136+ """Configure IP, port and modbus slave ID"""
137+ errors = {}
138+ old_config = self .hass .data [DOMAIN ][CONFIG ]
139+ old_ip = old_config [CONF_CONNECT_STEP ][CONF_IP ]
140+ old_port = old_config [CONF_CONNECT_STEP ][CONF_PORT ]
141+
142+ OPTIONS_CONNECT_SCHEMA = vol .Schema (
143+ {
144+ vol .Required (CONF_IP , default = old_ip ): str ,
145+ vol .Required (CONF_PORT , default = old_port ): vol .Coerce (int ),
146+ vol .Required (CONF_SLAVE_ID , default = map_default_ID (brand = self .brand )): vol .Coerce (int ),
147+ }
148+ )
149+
150+ if user_input is not None :
151+ try :
152+ validated_schema = OPTIONS_CONNECT_SCHEMA (user_input )
153+ except Exception as e :
154+ errors ["base" ] = "invalid_input"
155+
156+ if not errors :
157+ self .data [CONF_CONNECT_STEP ] = user_input
158+ return await self .async_step_energy_meter ()
159+
160+ return self .async_show_form (step_id = "connect" , data_schema = OPTIONS_CONNECT_SCHEMA , errors = errors )
161+
162+ async def async_step_energy_meter (self , user_input : dict [str , Any ] | None = None ) -> config_entries .ConfigFlowResult :
163+ """Configure energy meter entity IDs"""
164+ errors = {}
165+ old_config = self .hass .data [DOMAIN ][CONFIG ]
166+ old_pwr_imp_ent_id = old_config [CONF_ENERGY_METER_STEP ][CONF_PWR_IMP_ENT_ID ]
167+ old_pwr_exp_ent_id = old_config [CONF_ENERGY_METER_STEP ][CONF_PWR_EXP_ENT_ID ]
168+
169+ OPTIONS_ENERGY_METER_SCHEMA = vol .Schema (
170+ {
171+ vol .Required (CONF_PWR_IMP_ENT_ID , default = old_pwr_imp_ent_id ): EntitySelector (EntityFilterSelectorConfig (domain = ["sensor" , "input_number" , "number" ])),
172+ vol .Required (CONF_PWR_EXP_ENT_ID , default = old_pwr_exp_ent_id ): EntitySelector (EntityFilterSelectorConfig (domain = ["sensor" , "input_number" , "number" ])),
173+ }
174+ )
175+
176+ if user_input is not None :
177+ self .data [CONF_ENERGY_METER_STEP ] = user_input
178+ return await self .async_step_pricing ()
179+
180+ return self .async_show_form (step_id = "energy_meter" , data_schema = OPTIONS_ENERGY_METER_SCHEMA , errors = errors )
181+
182+ async def async_step_pricing (self , user_input : dict [str , Any ] | None = None ) -> config_entries .ConfigFlowResult :
183+ """Configure SDAC injection tariff entity ID"""
184+ errors = {}
185+ old_config = self .hass .data [DOMAIN ][CONFIG ]
186+ old_inj_tariff_ent_id = old_config [CONF_PRICING_STEP ][CONF_INJ_TARIFF_ENT_ID ]
187+ old_price_ent_id = old_config [CONF_PRICING_STEP ][CONF_PRICE_ENT_ID ]
188+
189+ OPTIONS_PRICING_SCHEMA = vol .Schema (
190+ {
191+ vol .Required (CONF_INJ_TARIFF_ENT_ID , default = old_inj_tariff_ent_id ): EntitySelector (EntityFilterSelectorConfig (domain = ["sensor" , "input_number" , "number" ])),
192+ vol .Required (CONF_PRICE_ENT_ID , default = old_price_ent_id ): EntitySelector (EntityFilterSelectorConfig (domain = ["sensor" , "input_number" , "number" ])),
193+ }
194+ )
195+
196+ if user_input is not None :
197+ self .data [CONF_PRICING_STEP ] = user_input
198+ return self .async_create_entry (data = self .data )
199+
200+ return self .async_show_form (step_id = "pricing" , data_schema = OPTIONS_PRICING_SCHEMA , errors = errors )
0 commit comments