From f5ca6da7d24a900ebe7485bb3cad5f6b806c820a Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 27 Nov 2024 15:31:06 +0100 Subject: [PATCH] =?UTF-8?q?Anpassungen=20f=C3=BCr=20den=20config=5Fflow.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/solvis_control/__init__.py | 11 +++ .../solvis_control/config_flow.py | 74 ++++++++++--------- custom_components/solvis_control/const.py | 4 +- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/custom_components/solvis_control/__init__.py b/custom_components/solvis_control/__init__.py index 3b929a4..41ac740 100644 --- a/custom_components/solvis_control/__init__.py +++ b/custom_components/solvis_control/__init__.py @@ -23,6 +23,8 @@ CONF_OPTION_2, CONF_OPTION_3, CONF_OPTION_4, + POLL_RATE_SLOW, + POLL_RATE_DEFAULT, ) from .coordinator import SolvisModbusCoordinator @@ -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) @@ -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 diff --git a/custom_components/solvis_control/config_flow.py b/custom_components/solvis_control/config_flow.py index d2dc8b9..1920a68 100644 --- a/custom_components/solvis_control/config_flow.py +++ b/custom_components/solvis_control/config_flow.py @@ -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, @@ -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( { @@ -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) + ), } ) @@ -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, ) @@ -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.""" @@ -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() @@ -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), @@ -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.""" @@ -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( @@ -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), diff --git a/custom_components/solvis_control/const.py b/custom_components/solvis_control/const.py index 145a904..28b94d2 100644 --- a/custom_components/solvis_control/const.py +++ b/custom_components/solvis_control/const.py @@ -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