Skip to content

Commit 567f646

Browse files
Merge branch 'main' into code-quality
2 parents c4c9672 + ac179d1 commit 567f646

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

custom_components/solaredge_modbus_multi/config_flow.py

+22-25
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
class SolaredgeModbusMultiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
2949
"""Handle a config flow for SolarEdge Modbus Multi."""
3050

@@ -85,19 +105,7 @@ async def async_step_user(
85105

86106
return self.async_show_form(
87107
step_id="user",
88-
data_schema=vol.Schema(
89-
{
90-
vol.Optional(CONF_NAME, default=user_input[CONF_NAME]): cv.string,
91-
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
92-
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
93-
int
94-
),
95-
vol.Required(
96-
f"{ConfName.DEVICE_LIST}",
97-
default=user_input[ConfName.DEVICE_LIST],
98-
): cv.string,
99-
},
100-
),
108+
data_schema=generate_config_schema("user", user_input),
101109
errors=errors,
102110
)
103111

@@ -158,18 +166,7 @@ async def async_step_reconfigure(
158166

159167
return self.async_show_form(
160168
step_id="reconfigure",
161-
data_schema=vol.Schema(
162-
{
163-
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
164-
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
165-
int
166-
),
167-
vol.Required(
168-
f"{ConfName.DEVICE_LIST}",
169-
default=user_input[ConfName.DEVICE_LIST],
170-
): cv.string,
171-
},
172-
),
169+
data_schema=generate_config_schema("reconfigure", user_input),
173170
errors=errors,
174171
)
175172

custom_components/solaredge_modbus_multi/repairs.py

+2-14
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)