Skip to content

Commit

Permalink
Merge pull request #664 from WillCodeForCats/config-inverter-id-list
Browse files Browse the repository at this point in the history
Add generate_config_schema
  • Loading branch information
WillCodeForCats authored Aug 29, 2024
2 parents b7f731b + 2671820 commit ac179d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
47 changes: 22 additions & 25 deletions custom_components/solaredge_modbus_multi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
from .helpers import device_list_from_string, host_valid


def generate_config_schema(step_id: str, user_input: dict[str, Any]) -> vol.Schema:
"""Generate config flow or repair schema."""
schema: dict[vol.Marker, Any] = {}

if step_id == "user":
schema |= {vol.Required(CONF_NAME, default=user_input[CONF_NAME]): cv.string}

if step_id in ["reconfigure", "confirm", "user"]:
schema |= {
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(int),
vol.Required(
f"{ConfName.DEVICE_LIST}",
default=user_input[ConfName.DEVICE_LIST],
): cv.string,
}

return vol.Schema(schema)


@callback
def solaredge_modbus_multi_entries(hass: HomeAssistant):
"""Return the hosts already configured."""
Expand Down Expand Up @@ -96,19 +116,7 @@ async def async_step_user(

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Optional(CONF_NAME, default=user_input[CONF_NAME]): cv.string,
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
int
),
vol.Required(
f"{ConfName.DEVICE_LIST}",
default=user_input[ConfName.DEVICE_LIST],
): cv.string,
},
),
data_schema=generate_config_schema("user", user_input),
errors=errors,
)

Expand Down Expand Up @@ -169,18 +177,7 @@ async def async_step_reconfigure(

return self.async_show_form(
step_id="reconfigure",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
int
),
vol.Required(
f"{ConfName.DEVICE_LIST}",
default=user_input[ConfName.DEVICE_LIST],
): cv.string,
},
),
data_schema=generate_config_schema("reconfigure", user_input),
errors=errors,
)

Expand Down
16 changes: 2 additions & 14 deletions custom_components/solaredge_modbus_multi/repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import re
from typing import cast

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant import data_entry_flow
from homeassistant.components.repairs import RepairsFlow
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError

from .config_flow import generate_config_schema
from .const import ConfDefaultStr, ConfName
from .helpers import device_list_from_string, host_valid

Expand Down Expand Up @@ -88,18 +87,7 @@ async def async_step_confirm(

return self.async_show_form(
step_id="confirm",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
int
),
vol.Required(
f"{ConfName.DEVICE_LIST}",
default=user_input[ConfName.DEVICE_LIST],
): cv.string,
}
),
data_schema=generate_config_schema("confirm", user_input),
errors=errors,
)

Expand Down

0 comments on commit ac179d1

Please sign in to comment.