Skip to content

Commit

Permalink
Anpassungen für den config_flow.py
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Nov 27, 2024
1 parent 0f04ac3 commit f5ca6da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
11 changes: 11 additions & 0 deletions custom_components/solvis_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
CONF_OPTION_2,
CONF_OPTION_3,
CONF_OPTION_4,
POLL_RATE_SLOW,
POLL_RATE_DEFAULT,
)
from .coordinator import SolvisModbusCoordinator

Expand Down Expand Up @@ -69,6 +71,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data.get(CONF_OPTION_2),
entry.data.get(CONF_OPTION_3),
entry.data.get(CONF_OPTION_4),
entry.data.get(POLL_RATE_DEFAULT),
entry.data.get(POLL_RATE_SLOW),
)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator)
Expand Down Expand Up @@ -127,6 +131,13 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
if DEVICE_VERSION not in new_data:
new_data[DEVICE_VERSION] = "SC3"
config_entry.minor_version = 3
if config_entry.minor_version < 4:
_LOGGER.info(f"Migrating from version {config_entry.version}")
if POLL_RATE_DEFAULT not in new_data:
new_data[POLL_RATE_DEFAULT] = 30
if POLL_RATE_SLOW not in new_data:
new_data[POLL_RATE_SLOW] = 300
config_entry.minor_version = 4
hass.config_entries.async_update_entry(config_entry, data=new_data)
_LOGGER.info(f"Migration to version {config_entry.version} successful")
return True
74 changes: 38 additions & 36 deletions custom_components/solvis_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers import config_validation as cv

from .const import (
CONF_HOST,
Expand All @@ -21,11 +22,19 @@
CONF_OPTION_3,
CONF_OPTION_4,
DEVICE_VERSION,
POLL_RATE_DEFAULT,
POLL_RATE_SLOW,
)

_LOGGER = logging.getLogger(__name__)


def validate_poll_rates(data):
if data[POLL_RATE_SLOW] % data[POLL_RATE_DEFAULT] != 0:
raise vol.Invalid("POLL_RATE_SLOW must be a multiple of POLL_RATE_DEFAULT")
return data


def get_host_schema_config(data: ConfigType) -> Schema:
return vol.Schema(
{
Expand All @@ -51,6 +60,12 @@ def get_solvis_devices(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(DEVICE_VERSION, default="SC3"): vol.In({"SC2": 2, "SC3": 1}),
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(
vol.Coerce(int), vol.Range(min=30)
),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(
vol.Coerce(int), vol.Range(min=60)
),
}
)

Expand Down Expand Up @@ -80,7 +95,15 @@ def get_solvis_devices_options(data: ConfigType) -> Schema:
vol.Required(
DEVICE_VERSION, default=data.get(DEVICE_VERSION, "SC3")
): vol.In({"SC2": 2, "SC3": 1}),
}
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(
vol.Coerce(int), vol.Range(min=30)
),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(
vol.Coerce(int), vol.Range(min=60)
),
},
extra=vol.ALLOW_EXTRA,
validator=validate_poll_rates,
)


Expand All @@ -95,7 +118,7 @@ def get_host_schema_options(data: ConfigType) -> Schema:

class SolvisConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
MINOR_VERSION = 3
MINOR_VERSION = 4

def __init__(self) -> None:
"""Init the ConfigFlow."""
Expand All @@ -108,21 +131,6 @@ async def async_step_user(self, user_input: ConfigType | None = None) -> FlowRes
errors = {}
if user_input is not None:
self.data = user_input
# try:
# self.client = ModbusClient.AsyncModbusTcpClient(
# user_input[CONF_HOST], user_input[CONF_PORT]
# )
# await self.client.connect()
# # Perform a simple read to check the connection
# await self.client.read_holding_registers(2818, 1, 1)
# except (ConnectionException, ModbusException) as exc:
# _LOGGER.error(f"Modbus connection failed: {exc}")
# errors["base"] = "cannot_connect"
# else:
# await self.client.close()
# await self.async_set_unique_id(
# self.data[CONF_HOST], raise_on_progress=False
# )
self._abort_if_unique_id_configured()
return await self.async_step_device()

Expand All @@ -136,9 +144,12 @@ async def async_step_device(
"""Handle the device step."""
errors = {}
if user_input is not None:
self.data.update(user_input)
return await self.async_step_features()

try:
self.data.update(user_input)
return await self.async_step_features()
except vol.Invalid as exc:
errors["base"] = str(exc)
errors["device"] = exc.error_message
return self.async_show_form(
step_id="device",
data_schema=get_solvis_devices(self.data),
Expand Down Expand Up @@ -167,7 +178,7 @@ def async_get_options_flow(

class SolvisOptionsFlow(config_entries.OptionsFlow):
VERSION = 1
MINOR_VERSION = 3
MINOR_VERSION = 4

def __init__(self, config) -> None:
"""Init the ConfigFlow."""
Expand All @@ -181,18 +192,6 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
_LOGGER.debug(f"Options flow values_1: {str(self.data)}", DOMAIN)
if user_input is not None:
self.data.update(user_input)
# try:
# self.client = ModbusClient.AsyncModbusTcpClient(
# user_input[CONF_HOST], user_input[CONF_PORT]
# )
# await self.client.connect()
# # Perform a simple read to check the connection
# await self.client.read_holding_registers(2818, 1, 1)
# except (ConnectionException, ModbusException) as exc:
# _LOGGER.error(f"Modbus connection failed: {exc}")
# errors["base"] = "cannot_connect"
# else:
# await self.client.close()
return await self.async_step_device()

return self.async_show_form(
Expand All @@ -208,9 +207,12 @@ async def async_step_device(
errors = {}
_LOGGER.debug(f"Options flow values_1: {str(self.data)}", DOMAIN)
if user_input is not None:
self.data.update(user_input)
return await self.async_step_features()

try:
self.data.update(user_input)
return await self.async_step_features()
except vol.Invalid as exc:
errors["base"] = str(exc)
errors["device"] = exc
return self.async_show_form(
step_id="device",
data_schema=get_solvis_devices(self.data),
Expand Down
4 changes: 2 additions & 2 deletions custom_components/solvis_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
CONF_PORT = "port"

DEVICE_VERSION = "device_version"
POLL_RATE_DEFAULT = 30
POLL_RATE_SLOW = 300
POLL_RATE_DEFAULT = "poll_rate_default"
POLL_RATE_SLOW = "poll_rate_slow"

# Option attributes to make certain values configurable
CONF_OPTION_1 = "HKR2" # HKR 2
Expand Down

0 comments on commit f5ca6da

Please sign in to comment.