Skip to content

Commit 3d75358

Browse files
committed
Added migration of config entry
1 parent e65e987 commit 3d75358

File tree

5 files changed

+40
-64
lines changed

5 files changed

+40
-64
lines changed

custom_components/weishaupt_modbus/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from homeassistant.config_entries import ConfigEntry
66
from homeassistant.core import HomeAssistant
7+
from homeassistant.const import CONF_PREFIX
78

8-
from .const import CONST
9+
from .const import CONST, CONF_DEVICE_POSTFIX, CONF_KENNFELD_FILE
910
from .modbusobject import ModbusAPI
1011

1112
PLATFORMS: list[str] = [
@@ -33,6 +34,30 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3334
return True
3435

3536

37+
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
38+
"""Migrate old entry."""
39+
warnings.warn("Check if migration of config entries is necessary.")
40+
if config_entry.version > 1:
41+
# This means the user has downgraded from a future version
42+
return False
43+
44+
if config_entry.version < 2:
45+
warnings.warn("Version 1 detected")
46+
new_data = {**config_entry.data}
47+
warnings.warn("Minor version 1 detected")
48+
new_data[CONF_PREFIX] = CONST.DEF_PREFIX
49+
new_data[CONF_DEVICE_POSTFIX] = ""
50+
new_data[CONF_KENNFELD_FILE] = CONST.DEF_KENNFELDFILE
51+
52+
warnings.warn("Update entries")
53+
54+
hass.config_entries.async_update_entry(
55+
config_entry, data=new_data, minor_version=1, version=2
56+
)
57+
58+
return True
59+
60+
3661
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3762
"""Unload entry."""
3863
# This is called when an entry/configured device is to be removed. The class

custom_components/weishaupt_modbus/config_flow.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
5656
class ConfigFlow(config_entries.ConfigFlow, domain=CONST.DOMAIN):
5757
"""Class config flow."""
5858

59-
VERSION = 1
59+
VERSION = 2
6060
# Pick one of the available connection classes in homeassistant/config_entries.py
6161
# This tells HA if it should be asking for updates, or it'll be notified of updates
6262
# automatically. This example uses PUSH, as the dummy hub will notify HA of
@@ -86,20 +86,6 @@ async def async_step_user(self, user_input=None):
8686
step_id="user", data_schema=DATA_SCHEMA, errors=errors
8787
)
8888

89-
# async def async_step_reconfigure(
90-
# self, user_input: dict[str, Any] | None = None
91-
# ) -> ConfigFlowResult:
92-
# """Add reconfigure step to allow to reconfigure a config entry."""
93-
# errors = {}
94-
# reconfigure_entry = self._get_reconfigure_entry()##
95-
96-
# return self.async_show_form(
97-
# step_id="reconfigure",
98-
# data_schema=DATA_SCHEMA,
99-
# description_placeholders={"device": reconfigure_entry.title},
100-
# errors=errors,
101-
# )
102-
10389

10490
class InvalidHost(exceptions.HomeAssistantError):
10591
"""Error to indicate there is an invalid hostname."""

custom_components/weishaupt_modbus/entities.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ async def _async_setup(self):
136136
This method will be called automatically during
137137
coordinator.async_config_entry_first_refresh.
138138
"""
139-
# await self._modbus_api.connect()
140-
# self._device = self._modbus_api.get_device()
141139
await self.fetch_data()
142140

143141
async def fetch_data(self, idx=None):
@@ -197,8 +195,6 @@ async def _async_update_data(self):
197195
# Grab active context variables to limit data required to be fetched from API
198196
# Note: using context is not required if there is no need or ability to limit
199197
# data retrieved from API.
200-
# listening_idx = set(self.async_contexts())
201-
# return await self._modbus_api.fetch_data(listening_idx)
202198
try:
203199
listening_idx = set(self.async_contexts())
204200
return await self.fetch_data(listening_idx)
@@ -242,23 +238,15 @@ def __init__(self, config_entry, modbus_item, modbus_api) -> None:
242238
self._attr_name = self._modbus_item.name
243239

244240
dev_postfix = ""
245-
try:
246-
dev_postfix = "_" + self._config_entry.data[CONF_DEVICE_POSTFIX]
247-
except KeyError:
248-
warnings.warn("Device postfix not defined, use default: ")
241+
dev_postfix = "_" + self._config_entry.data[CONF_DEVICE_POSTFIX]
249242

250243
if dev_postfix == "_":
251244
dev_postfix = ""
252245

253246
dev_prefix = CONST.DEF_PREFIX
254-
try:
255-
dev_prefix = self._config_entry.data[CONF_PREFIX]
256-
except KeyError:
257-
warnings.warn("Device prefix not defined, use default: " + CONST.DEF_PREFIX)
258-
259-
self._attr_unique_id = (
260-
dev_prefix + self._modbus_item.name + dev_postfix
261-
) # CONST.PREFIX + self._modbus_item.name
247+
dev_prefix = self._config_entry.data[CONF_PREFIX]
248+
249+
self._attr_unique_id = dev_prefix + self._modbus_item.name + dev_postfix
262250
self._dev_device = self._modbus_item.device + dev_postfix
263251
self._modbus_api = modbus_api
264252

@@ -305,20 +293,6 @@ def calc_temperature(self, val: float):
305293
# to optimize
306294
return int(val) / self._divider
307295

308-
# if val is None:
309-
# return None
310-
# if val == -32768:
311-
# # No Sensor installed
312-
# return -1
313-
# if val == -32767:
314-
# # Sensor broken
315-
# return -2
316-
# if val == 32768:
317-
# # Dont know. Whats this?
318-
# return None
319-
# if val in range(-500, 5000):
320-
# return int(val) / self._divider
321-
322296
def calc_percentage(self, val: float):
323297
"""Calculate percentage."""
324298
if val is None:
@@ -424,10 +398,6 @@ def _handle_coordinator_update(self) -> None:
424398
self._attr_native_value = self.translate_val(self._modbus_item.state)
425399
self.async_write_ha_state()
426400

427-
# async def async_update(self) -> None:
428-
# # the synching is done by the ModbusObject of the entity
429-
# self._attr_native_value = self.translate_val(0)
430-
431401
def calc_power(self, val, x, y):
432402
"""Calculate heating power from power map."""
433403
if val is None:
@@ -495,15 +465,10 @@ def _handle_coordinator_update(self) -> None:
495465

496466
async def async_set_native_value(self, value: float) -> None:
497467
await self.set_translate_val(value)
498-
# await self.coordinator.async_request_refresh()
499468
self._modbus_item.state = int(self.retranslate_val(value))
500469
self._attr_native_value = self.translate_val(self._modbus_item.state)
501470
self.async_write_ha_state()
502471

503-
# async def async_update(self) -> None:
504-
# # the synching is done by the ModbusObject of the entity
505-
# self._attr_native_value = self.translate_val(self._modbus_item.state)
506-
507472
@property
508473
def device_info(self) -> DeviceInfo:
509474
"""Return device info."""
@@ -543,11 +508,6 @@ def _handle_coordinator_update(self) -> None:
543508
self._attr_current_option = self.translate_val(self._modbus_item.state)
544509
self.async_write_ha_state()
545510

546-
# async def async_update(self) -> None:
547-
# # the synching is done by the ModbusObject of the entity
548-
# await self.coordinator.async_request_refresh()
549-
# self._attr_current_option = self.translate_val(self._modbus_item.state)
550-
551511
@property
552512
def device_info(self) -> DeviceInfo:
553513
"""Return device info."""

custom_components/weishaupt_modbus/hpconst.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ class DeviceConstants:
226226
StatusItem(2, "Pumpe"),
227227
]
228228

229+
HP_KONFIGURATION = [
230+
StatusItem(0, "Ncht konfiguriert"),
231+
StatusItem(1, "Heizen verfügbar"),
232+
StatusItem(2, "Heizen, Kühlen verfügbar"),
233+
StatusItem(3, "Heizen, Kühlen, Warmwasser verfügbar"),
234+
StatusItem(4, "Heizen, Warmwasser verfügbar"),
235+
]
236+
229237
WW_PUSH = [
230238
StatusItem(0, "AUS"),
231239
]
@@ -350,7 +358,7 @@ class DeviceConstants:
350358
ModbusItem( 33103, "Wärmeleistung", FORMATS.POWER, TYPES.SENSOR_CALC, DEVICES.WP, RANGE_CALCPOWER),
351359
ModbusItem( 33104, "Vorlauftemperatur", FORMATS.TEMPERATUR, TYPES.SENSOR, DEVICES.WP, TEMPRANGE_STD),
352360
ModbusItem( 33105, "Rücklauftemperatur", FORMATS.TEMPERATUR, TYPES.SENSOR, DEVICES.WP, TEMPRANGE_STD),
353-
ModbusItem( 43101, "Konfiguration ", FORMATS.NUMBER, TYPES.NUMBER_RO, DEVICES.WP),
361+
ModbusItem( 43101, "Konfiguration ", FORMATS.STATUS, TYPES.NUMBER_RO, DEVICES.WP, HP_KONFIGURATION),
354362
ModbusItem( 43102, "Ruhemodus", FORMATS.STATUS, TYPES.NUMBER_RO, DEVICES.WP, HP_RUHEMODUS),
355363
ModbusItem( 43103, "Pumpe Einschaltart", FORMATS.NUMBER, TYPES.NUMBER_RO, DEVICES.WP ),
356364
ModbusItem( 43104, "Pumpe Leistung Heizen", FORMATS.PERCENTAGE, TYPES.NUMBER_RO, DEVICES.WP, RANGE_PERCENTAGE),

custom_components/weishaupt_modbus/modbusobject.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def __init__(self, modbus_api: ModbusAPI, modbus_item: ModbusItem) -> None:
9797
"""
9898
self._modbus_item = modbus_item
9999
self._modbus_client = modbus_api.get_device()
100-
# await self._modbus_client.connect()
101100

102101
def check_valid(self, val):
103102
"""Checks if item is available and valid"""
@@ -122,10 +121,8 @@ def check_temperature(self, val):
122121
self._modbus_item.is_invalid = True
123122
case 32768:
124123
# Dont know. Whats this?
125-
# print("Sensor not installed: " + self._modbus_item.name)
126124
self._modbus_item.is_invalid = True
127125
case _:
128-
# print("Sensor OK " + self._modbus_item.name + " " + str(val))
129126
self._modbus_item.is_invalid = False
130127

131128
def check_percentage(self, val):

0 commit comments

Comments
 (0)