1010from homeassistant import config_entries
1111from homeassistant .config_entries import ConfigEntry , OptionsFlow
1212from homeassistant .const import CONF_HOST , CONF_NAME , CONF_PORT , CONF_SCAN_INTERVAL
13- from homeassistant .core import HomeAssistant , callback
13+ from homeassistant .core import callback
1414from homeassistant .data_entry_flow import FlowResult
1515from homeassistant .exceptions import HomeAssistantError
1616
2525from .helpers import device_list_from_string , host_valid
2626
2727
28- @callback
29- def solaredge_modbus_multi_entries (hass : HomeAssistant ):
30- """Return the hosts already configured."""
31- return set (
32- entry .data [CONF_HOST ].lower ()
33- for entry in hass .config_entries .async_entries (DOMAIN )
34- )
28+ def generate_config_schema (step_id : str , user_input : dict [str , Any ]) -> vol .Schema :
29+ """Generate config flow or repair schema."""
30+ schema : dict [vol .Marker , Any ] = {}
31+
32+ if step_id == "user" :
33+ schema |= {vol .Required (CONF_NAME , default = user_input [CONF_NAME ]): cv .string }
34+
35+ if step_id in ["reconfigure" , "confirm" , "user" ]:
36+ schema |= {
37+ vol .Required (CONF_HOST , default = user_input [CONF_HOST ]): cv .string ,
38+ vol .Required (CONF_PORT , default = user_input [CONF_PORT ]): vol .Coerce (int ),
39+ vol .Required (
40+ f"{ ConfName .DEVICE_LIST } " ,
41+ default = user_input [ConfName .DEVICE_LIST ],
42+ ): cv .string ,
43+ }
44+
45+ return vol .Schema (schema )
3546
3647
3748class SolaredgeModbusMultiConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
@@ -68,8 +79,6 @@ async def async_step_user(
6879 else :
6980 if not host_valid (user_input [CONF_HOST ]):
7081 errors [CONF_HOST ] = "invalid_host"
71- elif user_input [CONF_HOST ] in solaredge_modbus_multi_entries (self .hass ):
72- errors [CONF_HOST ] = "already_configured"
7382 elif not 1 <= user_input [CONF_PORT ] <= 65535 :
7483 errors [CONF_PORT ] = "invalid_tcp_port"
7584 elif not 1 <= inverter_count <= 32 :
@@ -96,19 +105,7 @@ async def async_step_user(
96105
97106 return self .async_show_form (
98107 step_id = "user" ,
99- data_schema = vol .Schema (
100- {
101- vol .Optional (CONF_NAME , default = user_input [CONF_NAME ]): cv .string ,
102- vol .Required (CONF_HOST , default = user_input [CONF_HOST ]): cv .string ,
103- vol .Required (CONF_PORT , default = user_input [CONF_PORT ]): vol .Coerce (
104- int
105- ),
106- vol .Required (
107- f"{ ConfName .DEVICE_LIST } " ,
108- default = user_input [ConfName .DEVICE_LIST ],
109- ): cv .string ,
110- },
111- ),
108+ data_schema = generate_config_schema ("user" , user_input ),
112109 errors = errors ,
113110 )
114111
@@ -169,18 +166,7 @@ async def async_step_reconfigure(
169166
170167 return self .async_show_form (
171168 step_id = "reconfigure" ,
172- data_schema = vol .Schema (
173- {
174- vol .Required (CONF_HOST , default = user_input [CONF_HOST ]): cv .string ,
175- vol .Required (CONF_PORT , default = user_input [CONF_PORT ]): vol .Coerce (
176- int
177- ),
178- vol .Required (
179- f"{ ConfName .DEVICE_LIST } " ,
180- default = user_input [ConfName .DEVICE_LIST ],
181- ): cv .string ,
182- },
183- ),
169+ data_schema = generate_config_schema ("reconfigure" , user_input ),
184170 errors = errors ,
185171 )
186172
0 commit comments