From a85381a74764b530e6a1156b9920af06dc364550 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 1 Apr 2024 22:31:00 +0200 Subject: [PATCH] Added configuration possibility for some sensors --- custom_components/solvis_control/const.py | 12 ++++++---- custom_components/solvis_control/number.py | 24 +++++++++++++++---- custom_components/solvis_control/select.py | 21 +++++++++++++--- custom_components/solvis_control/sensor.py | 2 +- custom_components/solvis_control/strings.json | 20 +++++++--------- .../solvis_control/translations/de.json | 20 +++++++--------- .../solvis_control/translations/en.json | 19 +++++++-------- 7 files changed, 72 insertions(+), 46 deletions(-) diff --git a/custom_components/solvis_control/const.py b/custom_components/solvis_control/const.py index 566f556..8e2eee3 100644 --- a/custom_components/solvis_control/const.py +++ b/custom_components/solvis_control/const.py @@ -24,7 +24,7 @@ class ModbusFieldConfig: entity_category: str = None enabled_by_default: bool = True edit: bool = False - border: tuple = (0, 100) + data: tuple = None PORT = 502 @@ -224,6 +224,7 @@ class ModbusFieldConfig: state_class=None, register=2, multiplier=1, + data=("2", "3", "4", "5", "6", "7"), ), ModbusFieldConfig( # HKR1 Absenktemperatur Nacht name="hkr1_absenktemperatur_nacht", @@ -234,7 +235,7 @@ class ModbusFieldConfig: register=2, multiplier=1, edit=True, - border=(5, 75), + data=(5, 75), ), ModbusFieldConfig( # HKR1 Solltemperatur Tag name="hkr1_solltemperatur_tag", @@ -245,7 +246,7 @@ class ModbusFieldConfig: register=2, multiplier=1, edit=True, - border=(5, 75), + data=(5, 75), ), ModbusFieldConfig( # DigIn Stoerungen name="digin_stoerungen", @@ -265,7 +266,7 @@ class ModbusFieldConfig: register=2, multiplier=1, edit=True, - border=(10, 65), + data=(10, 65), ), ModbusFieldConfig( # VersionSC2 name="version_sc2", @@ -292,6 +293,7 @@ class ModbusFieldConfig: device_class=None, state_class=None, multiplier=1, + data=("0", "1", "2", "3"), ), ModbusFieldConfig( # Raumtemperatur_HKR1 name="raumtemperatur_hkr1", @@ -301,6 +303,6 @@ class ModbusFieldConfig: state_class="measurement", register=2, edit=True, - border=(0, 40), + data=(0, 40), ), ] diff --git a/custom_components/solvis_control/number.py b/custom_components/solvis_control/number.py index 11b242d..47f7d90 100644 --- a/custom_components/solvis_control/number.py +++ b/custom_components/solvis_control/number.py @@ -4,6 +4,8 @@ import logging import re +from pymodbus.exceptions import ConnectionException + from homeassistant.components.number import NumberEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME @@ -48,7 +50,7 @@ async def async_setup_entry( for register in REGISTERS: if not register.edit: continue - if register.address == 2818: + if register.address in (2818, 2049): continue sensors_to_add.append( SolvisSensor( @@ -60,6 +62,8 @@ async def async_setup_entry( register.device_class, register.state_class, register.enabled_by_default, + register.data, + register.address, ) ) @@ -77,10 +81,12 @@ def __init__( device_class: str | None = None, state_class: str | None = None, enabled_by_default: bool = True, + data: tuple = None, + modbus_address: int = None, ): """Init entity.""" super().__init__(coordinator) - + self.modbus_address = modbus_address self._address = address self._response_key = name self.entity_registry_enabled_default = enabled_by_default @@ -92,8 +98,8 @@ def __init__( self._attr_has_entity_name = True self.unique_id = f"{re.sub('^[A-Za-z0-9_-]*$', '', name)}_{name}" self.translation_key = name - self.native_min_value = 10 - self.native_max_value = 65 + self.native_min_value = data[0] + self.native_max_value = data[1] self.native_step = 1.0 @callback @@ -135,4 +141,12 @@ def _handle_coordinator_update(self) -> None: async def async_set_native_value(self, value: float) -> None: """Update the current value.""" - print(value) + try: + await self.coordinator.modbus.connect() + except ConnectionException: + self.logger.warning("Couldn't connect to device") + if self.coordinator.modbus.connected: + await self.coordinator.modbus.write_register( + self.modbus_address, int(value), slave=1 + ) + self.coordinator.modbus.close() diff --git a/custom_components/solvis_control/select.py b/custom_components/solvis_control/select.py index 8f62d3d..8341eb5 100644 --- a/custom_components/solvis_control/select.py +++ b/custom_components/solvis_control/select.py @@ -4,6 +4,8 @@ import logging import re +from pymodbus.exceptions import ConnectionException + from homeassistant.components.select import SelectEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME @@ -46,7 +48,7 @@ async def async_setup_entry( sensors_to_add = [] for register in REGISTERS: - if register.address != 2818: + if register.address not in (2818, 2049): continue sensors_to_add.append( SolvisSensor( @@ -55,6 +57,8 @@ async def async_setup_entry( conf_host, register.name, register.enabled_by_default, + register.data, + register.address, ) ) @@ -69,10 +73,13 @@ def __init__( address, name: str, enabled_by_default: bool = True, + data: tuple = None, + modbus_address: int = None, ): """Init entity.""" super().__init__(coordinator) + self.modbus_address = modbus_address self._address = address self._response_key = name self.entity_registry_enabled_default = enabled_by_default @@ -82,7 +89,7 @@ def __init__( self.unique_id = f"{re.sub('^[A-Za-z0-9_-]*$', '', name)}_{name}" self.translation_key = name self._attr_current_option = None - self._attr_options = ["2", "3", "4", "5", "6", "7"] + self._attr_options = data @callback def _handle_coordinator_update(self) -> None: @@ -123,4 +130,12 @@ def _handle_coordinator_update(self) -> None: async def async_select_option(self, option: str) -> None: """Change the selected option.""" - print(option) + try: + await self.coordinator.modbus.connect() + except ConnectionException: + self.logger.warning("Couldn't connect to device") + if self.coordinator.modbus.connected: + await self.coordinator.modbus.write_register( + self.modbus_address, int(option), slave=1 + ) + self.coordinator.modbus.close() diff --git a/custom_components/solvis_control/sensor.py b/custom_components/solvis_control/sensor.py index b9f8937..79ef715 100644 --- a/custom_components/solvis_control/sensor.py +++ b/custom_components/solvis_control/sensor.py @@ -48,7 +48,7 @@ async def async_setup_entry( for register in REGISTERS: if register.edit: continue - if register.address == 2818: + if register.address in (2818, 2049): continue sensors_to_add.append( SolvisSensor( diff --git a/custom_components/solvis_control/strings.json b/custom_components/solvis_control/strings.json index 4d72e6d..e71447c 100644 --- a/custom_components/solvis_control/strings.json +++ b/custom_components/solvis_control/strings.json @@ -69,7 +69,15 @@ "5": "Standby", "6": "Eco", "7": "Urlaub" - + } + }, + "zirkulation_betriebsart": { + "name": "Zirkulation Betriebsart", + "state": { + "0": "Aus", + "1": "Puls", + "2": "Zeit", + "3": "Puls/Zeit" } } }, @@ -155,16 +163,6 @@ "version_nbg": { "name": "Version NBG" }, - - "zirkulation_betriebsart": { - "name": "Zirkulation Betriebsart", - "state": { - "1": "Aus", - "2": "Puls", - "3": "Zeit", - "4": "Puls/Zeit" - } - }, "digin_stoerungen": { "name": "Störungen", "state": { diff --git a/custom_components/solvis_control/translations/de.json b/custom_components/solvis_control/translations/de.json index 4d72e6d..e71447c 100644 --- a/custom_components/solvis_control/translations/de.json +++ b/custom_components/solvis_control/translations/de.json @@ -69,7 +69,15 @@ "5": "Standby", "6": "Eco", "7": "Urlaub" - + } + }, + "zirkulation_betriebsart": { + "name": "Zirkulation Betriebsart", + "state": { + "0": "Aus", + "1": "Puls", + "2": "Zeit", + "3": "Puls/Zeit" } } }, @@ -155,16 +163,6 @@ "version_nbg": { "name": "Version NBG" }, - - "zirkulation_betriebsart": { - "name": "Zirkulation Betriebsart", - "state": { - "1": "Aus", - "2": "Puls", - "3": "Zeit", - "4": "Puls/Zeit" - } - }, "digin_stoerungen": { "name": "Störungen", "state": { diff --git a/custom_components/solvis_control/translations/en.json b/custom_components/solvis_control/translations/en.json index 2461f5d..e71447c 100644 --- a/custom_components/solvis_control/translations/en.json +++ b/custom_components/solvis_control/translations/en.json @@ -69,7 +69,15 @@ "5": "Standby", "6": "Eco", "7": "Urlaub" - + } + }, + "zirkulation_betriebsart": { + "name": "Zirkulation Betriebsart", + "state": { + "0": "Aus", + "1": "Puls", + "2": "Zeit", + "3": "Puls/Zeit" } } }, @@ -155,15 +163,6 @@ "version_nbg": { "name": "Version NBG" }, - "zirkulation_betriebsart": { - "name": "Zirkulation Betriebsart", - "state": { - "1": "Aus", - "2": "Puls", - "3": "Zeit", - "4": "Puls/Zeit" - } - }, "digin_stoerungen": { "name": "Störungen", "state": {