Skip to content

Commit

Permalink
Added configuration possibility for some sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Apr 1, 2024
1 parent 8841cff commit a85381a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 46 deletions.
12 changes: 7 additions & 5 deletions custom_components/solvis_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -245,7 +246,7 @@ class ModbusFieldConfig:
register=2,
multiplier=1,
edit=True,
border=(5, 75),
data=(5, 75),
),
ModbusFieldConfig( # DigIn Stoerungen
name="digin_stoerungen",
Expand All @@ -265,7 +266,7 @@ class ModbusFieldConfig:
register=2,
multiplier=1,
edit=True,
border=(10, 65),
data=(10, 65),
),
ModbusFieldConfig( # VersionSC2
name="version_sc2",
Expand All @@ -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",
Expand All @@ -301,6 +303,6 @@ class ModbusFieldConfig:
state_class="measurement",
register=2,
edit=True,
border=(0, 40),
data=(0, 40),
),
]
24 changes: 19 additions & 5 deletions custom_components/solvis_control/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -60,6 +62,8 @@ async def async_setup_entry(
register.device_class,
register.state_class,
register.enabled_by_default,
register.data,
register.address,
)
)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
21 changes: 18 additions & 3 deletions custom_components/solvis_control/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -55,6 +57,8 @@ async def async_setup_entry(
conf_host,
register.name,
register.enabled_by_default,
register.data,
register.address,
)
)

Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion custom_components/solvis_control/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 9 additions & 11 deletions custom_components/solvis_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down Expand Up @@ -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": {
Expand Down
20 changes: 9 additions & 11 deletions custom_components/solvis_control/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down Expand Up @@ -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": {
Expand Down
19 changes: 9 additions & 10 deletions custom_components/solvis_control/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit a85381a

Please sign in to comment.