From 9787f2e9bf35f2d6688e635cd4f9a751843dd69c Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Oct 2024 07:20:59 +0200 Subject: [PATCH] Various fixes & disable connection check in setup (for now) --- .../solvis_control/config_flow.py | 77 +++++++++++-------- custom_components/solvis_control/const.py | 8 +- .../solvis_control/coordinator.py | 2 +- custom_components/solvis_control/strings.json | 11 +-- .../solvis_control/translations/de.json | 11 +-- 5 files changed, 63 insertions(+), 46 deletions(-) diff --git a/custom_components/solvis_control/config_flow.py b/custom_components/solvis_control/config_flow.py index 7a8fa9b..9fb0334 100644 --- a/custom_components/solvis_control/config_flow.py +++ b/custom_components/solvis_control/config_flow.py @@ -46,6 +46,21 @@ def get_solvis_modules(data: ConfigType) -> Schema: ) +def get_solvis_modules_options(data: ConfigType) -> Schema: + return vol.Schema( + { + vol.Required(CONF_OPTION_1, default=data.get(CONF_OPTION_1)): bool, # HKR 2 + vol.Required(CONF_OPTION_2, default=data.get(CONF_OPTION_2)): bool, # HKR 3 + vol.Required( + CONF_OPTION_3, default=data.get(CONF_OPTION_3) + ): bool, # solar collectors + vol.Required( + CONF_OPTION_4, default=data.get(CONF_OPTION_4) + ): bool, # heat pump + } + ) + + def get_host_schema_options(data: ConfigType) -> Schema: return vol.Schema( { @@ -72,23 +87,23 @@ async def async_step_user( 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_input_registers(32770, 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_features() + # 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_features() return self.async_show_form( step_id="user", data_schema=get_host_schema_config(self.data), errors=errors @@ -131,19 +146,19 @@ async def async_step_init( 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_input_registers(32770, 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_features() + # 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_features() return self.async_show_form( step_id="init", @@ -157,7 +172,7 @@ async def async_step_features( """Handle the feature step.""" if user_input is None: return self.async_show_form( - step_id="features", data_schema=get_solvis_modules(self.data) + step_id="features", data_schema=get_solvis_modules_options(self.data) ) self.data.update(user_input) return self.async_create_entry(title=self.config.get(CONF_NAME), data=self.data) diff --git a/custom_components/solvis_control/const.py b/custom_components/solvis_control/const.py index 1b97965..cb2ba74 100644 --- a/custom_components/solvis_control/const.py +++ b/custom_components/solvis_control/const.py @@ -6,10 +6,10 @@ CONF_HOST = "host" CONF_PORT = "port" # Option attributes to make certain values configurable -CONF_OPTION_1 = False # HKR 2 -CONF_OPTION_2 = False # HKR 3 -CONF_OPTION_3 = False # Solar collector -CONF_OPTION_4 = False # heat pump +CONF_OPTION_1 = "HKR2" # HKR 2 +CONF_OPTION_2 = "HKR3" # HKR 3 +CONF_OPTION_3 = "solar collector" # Solar collector +CONF_OPTION_4 = "heat pump" # heat pump DATA_COORDINATOR = "coordinator" MANUFACTURER = "Solvis" diff --git a/custom_components/solvis_control/coordinator.py b/custom_components/solvis_control/coordinator.py index 715d3a1..d73956a 100644 --- a/custom_components/solvis_control/coordinator.py +++ b/custom_components/solvis_control/coordinator.py @@ -54,7 +54,7 @@ async def _async_update_data(self): self.logger.debug("Polling data") parsed_data: dict = {} - entity_registry = self.hass.data['entity_registry'] + entity_registry = self.hass.data["entity_registry"] try: await self.modbus.connect() diff --git a/custom_components/solvis_control/strings.json b/custom_components/solvis_control/strings.json index 5de0c74..951f3be 100644 --- a/custom_components/solvis_control/strings.json +++ b/custom_components/solvis_control/strings.json @@ -12,15 +12,16 @@ "init":{ "data":{ "host":"IP-Adresse oder Hostname", - "port":"Port" + "port":"Port", + "cannot_connect": "Ein Fehler bei der Verbindung ist aufgetreten." } }, "features":{ "data":{ - "CONF_OPTION_1":"HKR 2", - "CONF_OPTION_2":"HKR 3", - "CONF_OPTION_3":"Solar Kollektoren", - "CONF_OPTION_4":"Wärmepumpe" + "hkr2":"HKR 2", + "hkr3":"HKR 3", + "solar collector":"Solar Kollektoren", + "heat pump":"Wärmepumpe" } } } diff --git a/custom_components/solvis_control/translations/de.json b/custom_components/solvis_control/translations/de.json index 5de0c74..951f3be 100644 --- a/custom_components/solvis_control/translations/de.json +++ b/custom_components/solvis_control/translations/de.json @@ -12,15 +12,16 @@ "init":{ "data":{ "host":"IP-Adresse oder Hostname", - "port":"Port" + "port":"Port", + "cannot_connect": "Ein Fehler bei der Verbindung ist aufgetreten." } }, "features":{ "data":{ - "CONF_OPTION_1":"HKR 2", - "CONF_OPTION_2":"HKR 3", - "CONF_OPTION_3":"Solar Kollektoren", - "CONF_OPTION_4":"Wärmepumpe" + "hkr2":"HKR 2", + "hkr3":"HKR 3", + "solar collector":"Solar Kollektoren", + "heat pump":"Wärmepumpe" } } }