Skip to content

Commit

Permalink
Added possibility to enable/disable features of solvis components
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Oct 6, 2024
1 parent 8125a39 commit 9c9ae16
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 12 deletions.
21 changes: 19 additions & 2 deletions custom_components/solvis_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
from homeassistant.const import CONF_IP_ADDRESS, Platform
from homeassistant.core import HomeAssistant

from .const import CONF_HOST, CONF_PORT, DATA_COORDINATOR, DOMAIN
from .const import (
CONF_HOST,
CONF_PORT,
DATA_COORDINATOR,
DOMAIN,
CONF_OPTION_1,
CONF_OPTION_2,
CONF_OPTION_3,
CONF_OPTION_4,
)
from .coordinator import SolvisModbusCoordinator

PLATFORMS: [Platform] = [Platform.SENSOR, Platform.NUMBER, Platform.SELECT]
Expand All @@ -32,7 +41,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].setdefault(entry.entry_id, {})

# Create coordinator for polling
coordinator = SolvisModbusCoordinator(hass, conf_host, conf_port)
coordinator = SolvisModbusCoordinator(
hass,
conf_host,
conf_port,
CONF_OPTION_1,
CONF_OPTION_2,
CONF_OPTION_3,
CONF_OPTION_4,
)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator)

Expand Down
51 changes: 46 additions & 5 deletions custom_components/solvis_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import ConfigType

from .const import CONF_HOST, CONF_NAME, CONF_PORT, DOMAIN
from .const import (
CONF_HOST,
CONF_NAME,
CONF_PORT,
DOMAIN,
CONF_OPTION_1,
CONF_OPTION_2,
CONF_OPTION_3,
CONF_OPTION_4,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,6 +35,17 @@ def get_host_schema_config(data: ConfigType) -> Schema:
)


def get_solvis_modules(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(CONF_OPTION_1, default=False): bool, # HKR 2
vol.Required(CONF_OPTION_2, default=False): bool, # HKR 3
vol.Required(CONF_OPTION_3, default=False): bool, # solar collectors
vol.Required(CONF_OPTION_4, default=False): bool, # heat pump
}
)


def get_host_schema_options(data: ConfigType) -> Schema:
return vol.Schema(
{
Expand All @@ -37,7 +57,7 @@ def get_host_schema_options(data: ConfigType) -> Schema:

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

def __init__(self) -> None:
"""Init the ConfigFlow."""
Expand All @@ -57,6 +77,17 @@ async def async_step_user(
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()

async def async_step_features(
self, user_input: dict[bool, bool, bool] | None = None
) -> FlowResult:
"""Handle the feature step."""
if user_input is None:
return self.async_show_form(
step_id="features", data_schema=get_solvis_modules(self.data)
)
self.data.update(user_input)
return self.async_create_entry(title=self.data[CONF_NAME], data=self.data)

@staticmethod
Expand Down Expand Up @@ -113,6 +144,16 @@ async def async_step_init(
data_schema=get_host_schema_options(self.data),
)
self.data = user_input
return self.async_create_entry(
title=self.config_entry.get(CONF_NAME), data=self.data
)
self.data.update(user_input)
return await self.async_step_features()

async def async_step_features(
self, user_input: dict[bool, bool, bool] | None = None
) -> FlowResult:
"""Handle the feature step."""
if user_input is None:
return self.async_show_form(
step_id="features", data_schema=get_solvis_modules(self.data)
)
self.data.update(user_input)
return self.async_create_entry(title=self.config.get(CONF_NAME), data=self.data)
33 changes: 31 additions & 2 deletions custom_components/solvis_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
CONF_NAME = "name"
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

DATA_COORDINATOR = "coordinator"
MANUFACTURER = "Solvis"
Expand All @@ -21,12 +26,13 @@ class ModbusFieldConfig:
# 1 = INPUT, 2 = HOLDING

register: int = 1
negative: bool = False
entity_category: str = None
enabled_by_default: bool = True
edit: bool = False
data: tuple = None
absolute_value: bool = False
# Assign CONF_OPTION to entities
conf_option: int = 0


PORT = 502
Expand All @@ -52,6 +58,7 @@ class ModbusFieldConfig:
device_class="temperature",
state_class="measurement",
enabled_by_default=False,
conf_option=3,
),
ModbusFieldConfig( # Zirkulationsdurchfluss
name="cold_water_temp",
Expand Down Expand Up @@ -82,6 +89,7 @@ class ModbusFieldConfig:
device_class="temperature",
state_class="measurement",
enabled_by_default=False,
conf_option=3,
),
ModbusFieldConfig(
name="solar_heat_exchanger_in_water_temp",
Expand All @@ -90,6 +98,7 @@ class ModbusFieldConfig:
device_class="temperature",
state_class="measurement",
enabled_by_default=False,
conf_option=3,
),
ModbusFieldConfig(
name="solar_heat_exchanger_out_water_temp",
Expand All @@ -98,6 +107,7 @@ class ModbusFieldConfig:
device_class="temperature",
state_class="measurement",
enabled_by_default=False,
conf_option=3,
),
ModbusFieldConfig( # Speicherreferenztemperatur
name="tank_layer1_water_temp",
Expand Down Expand Up @@ -150,7 +160,6 @@ class ModbusFieldConfig:
unit="",
device_class="",
state_class="measurement",
negative=True,
multiplier=1,
entity_category="diagnostic",
absolute_value=True,
Expand Down Expand Up @@ -326,6 +335,7 @@ class ModbusFieldConfig:
register=2,
multiplier=1,
data=("2", "3", "4", "5", "6", "7"),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Solltemperatur Tag
name="hkr2_solltemperatur_tag",
Expand All @@ -337,6 +347,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 75),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Absenktemperatur Nacht
name="hkr2_absenktemperatur_nacht",
Expand All @@ -348,6 +359,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 75),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Heizkurve Tag Temp. 1
name="hkr2_heizkurve_temp_tag_1",
Expand All @@ -359,6 +371,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 50),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Heizkurve Tag Temp. 2
name="hkr2_heizkurve_temp_tag_2",
Expand All @@ -370,6 +383,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Heizkurve Tag Temp. 3
name="hkr2_heizkurve_temp_tag_3",
Expand All @@ -381,6 +395,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Heizkurve Absenkung
name="hkr2_heizkurve_temp_absenkung",
Expand All @@ -392,6 +407,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=1,
),
ModbusFieldConfig( # HKR2 Heizkurve Steilheit
name="hkr2_heizkurve_steilheit",
Expand All @@ -403,6 +419,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(20, 250),
conf_option=1,
),
ModbusFieldConfig( # Raumtemperatur_HKR2
name="raumtemperatur_hkr2",
Expand All @@ -413,6 +430,7 @@ class ModbusFieldConfig:
register=2,
edit=True,
data=(0, 40),
conf_option=1,
),
ModbusFieldConfig( # HKR3 Betriebsart
name="hkr3_betriebsart",
Expand All @@ -423,6 +441,7 @@ class ModbusFieldConfig:
register=2,
multiplier=1,
data=("2", "3", "4", "5", "6", "7"),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Solltemperatur Tag
name="hkr3_solltemperatur_tag",
Expand All @@ -434,6 +453,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 75),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Absenktemperatur Nacht
name="hkr3_absenktemperatur_nacht",
Expand All @@ -445,6 +465,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 75),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Heizkurve Tag Temp. 1
name="hkr3_heizkurve_temp_tag_1",
Expand All @@ -456,6 +477,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 50),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Heizkurve Tag Temp. 2
name="hkr3_heizkurve_temp_tag_2",
Expand All @@ -467,6 +489,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Heizkurve Tag Temp. 3
name="hkr3_heizkurve_temp_tag_3",
Expand All @@ -478,6 +501,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Heizkurve Absenkung
name="hkr3_heizkurve_temp_absenkung",
Expand All @@ -489,6 +513,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(5, 30),
conf_option=2,
),
ModbusFieldConfig( # HKR3 Heizkurve Steilheit
name="hkr3_heizkurve_steilheit",
Expand All @@ -500,6 +525,7 @@ class ModbusFieldConfig:
multiplier=1,
edit=True,
data=(20, 250),
conf_option=2,
),
ModbusFieldConfig( # Raumtemperatur_HKR3
name="raumtemperatur_hkr3",
Expand All @@ -510,6 +536,7 @@ class ModbusFieldConfig:
register=2,
edit=True,
data=(0, 40),
conf_option=2,
),
ModbusFieldConfig( # DigIn Stoerungen
name="digin_stoerungen",
Expand Down Expand Up @@ -567,6 +594,7 @@ class ModbusFieldConfig:
register=2,
edit=False,
enabled_by_default=False,
conf_option=4,
),
ModbusFieldConfig( # elektrische Wärmepumenleistung
name="elek_waermepumpe_leistung",
Expand All @@ -577,5 +605,6 @@ class ModbusFieldConfig:
register=2,
edit=False,
enabled_by_default=False,
conf_option=4,
),
]
25 changes: 22 additions & 3 deletions custom_components/solvis_control/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@
class SolvisModbusCoordinator(DataUpdateCoordinator):
"""My custom coordinator."""

def __init__(self, hass, conf_host, conf_port):
def __init__(
self,
hass,
conf_host,
conf_port,
conf_option_1: bool,
conf_option_2: bool,
conf_option_3: bool,
conf_option_4: bool,
):
"""Initialize my coordinator."""
super().__init__(
hass,
Expand All @@ -28,6 +37,10 @@ def __init__(self, hass, conf_host, conf_port):
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=30),
)
self.CONF_OPTION_4 = conf_option_4
self.CONF_OPTION_3 = conf_option_3
self.CONF_OPTION_2 = conf_option_2
self.CONF_OPTION_1 = conf_option_1
self.logger.debug("Creating client")
self.modbus = ModbusClient.AsyncModbusTcpClient(host=conf_host, port=conf_port)

Expand All @@ -46,6 +59,14 @@ async def _async_update_data(self):
self.logger.warning("Couldn't connect to device")
if self.modbus.connected:
for register in REGISTERS:
if not self.CONF_OPTION_1 and register.conf_option == 1:
continue
if not self.CONF_OPTION_2 and register.conf_option == 2:
continue
if not self.CONF_OPTION_3 and register.conf_option == 3:
continue
if not self.CONF_OPTION_4 and register.conf_option == 4:
continue
self.logger.debug("Connected to Modbus for Solvis")
try:
if register.register == 1:
Expand All @@ -65,8 +86,6 @@ async def _async_update_data(self):
parsed_data[register.name] = round(
d.decode_16bit_int() * register.multiplier, 2
)
if register.negative:
parsed_data[register.name] *= -1
if register.absolute_value:
parsed_data[register.name] = abs(parsed_data[register.name])
self.modbus.close()
Expand Down
Loading

0 comments on commit 9c9ae16

Please sign in to comment.