Skip to content

Commit ac179d1

Browse files
Merge pull request #664 from WillCodeForCats/config-inverter-id-list
Add generate_config_schema
2 parents b7f731b + 2671820 commit ac179d1

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

custom_components/solaredge_modbus_multi/config_flow.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@
2525
from .helpers import device_list_from_string, host_valid
2626

2727

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)
46+
47+
2848
@callback
2949
def solaredge_modbus_multi_entries(hass: HomeAssistant):
3050
"""Return the hosts already configured."""
@@ -96,19 +116,7 @@ async def async_step_user(
96116

97117
return self.async_show_form(
98118
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-
),
119+
data_schema=generate_config_schema("user", user_input),
112120
errors=errors,
113121
)
114122

@@ -169,18 +177,7 @@ async def async_step_reconfigure(
169177

170178
return self.async_show_form(
171179
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-
),
180+
data_schema=generate_config_schema("reconfigure", user_input),
184181
errors=errors,
185182
)
186183

custom_components/solaredge_modbus_multi/repairs.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import re
66
from typing import cast
77

8-
import homeassistant.helpers.config_validation as cv
9-
import voluptuous as vol
108
from homeassistant import data_entry_flow
119
from homeassistant.components.repairs import RepairsFlow
1210
from homeassistant.config_entries import ConfigEntry
1311
from homeassistant.const import CONF_HOST, CONF_PORT
1412
from homeassistant.core import HomeAssistant
1513
from homeassistant.exceptions import HomeAssistantError
1614

15+
from .config_flow import generate_config_schema
1716
from .const import ConfDefaultStr, ConfName
1817
from .helpers import device_list_from_string, host_valid
1918

@@ -88,18 +87,7 @@ async def async_step_confirm(
8887

8988
return self.async_show_form(
9089
step_id="confirm",
91-
data_schema=vol.Schema(
92-
{
93-
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
94-
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
95-
int
96-
),
97-
vol.Required(
98-
f"{ConfName.DEVICE_LIST}",
99-
default=user_input[ConfName.DEVICE_LIST],
100-
): cv.string,
101-
}
102-
),
90+
data_schema=generate_config_schema("confirm", user_input),
10391
errors=errors,
10492
)
10593

0 commit comments

Comments
 (0)