From 98be4e3f72a7e248a8e681851afc850ef6475a28 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Fri, 6 Dec 2024 20:29:30 +0100 Subject: [PATCH] fix: Config entry migration - incorrect method --- custom_components/solarman/__init__.py | 4 +--- custom_components/solarman/const.py | 21 ++++++++++++++------- custom_components/solarman/parser.py | 20 ++++++++++---------- custom_components/solarman/sensor.py | 2 +- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/custom_components/solarman/__init__.py b/custom_components/solarman/__init__.py index 99cea89..0ad7c8c 100644 --- a/custom_components/solarman/__init__.py +++ b/custom_components/solarman/__init__.py @@ -80,10 +80,8 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> if (new_data := {**config_entry.data}) and (new_options := {**config_entry.options}): bulk_migrate(new_data, new_data, { CONF_SERIAL: "inverter_serial" }) - bulk_migrate(new_options, new_options, { CONF_SERIAL: "inverter_serial", CONF_HOST: "inverter_host", CONF_PORT: "inverter_port" }) - bulk_migrate(new_options.setdefault(CONF_ADDITIONAL_OPTIONS, {}), new_options, CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING) - + bulk_inherit(new_options.setdefault(CONF_ADDITIONAL_OPTIONS, {}), new_options, CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING) bulk_delete(new_data, "inverter_serial") bulk_delete(new_options, "inverter_serial", "inverter_host", "inverter_port", CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING) diff --git a/custom_components/solarman/const.py b/custom_components/solarman/const.py index 9c163bd..f264016 100644 --- a/custom_components/solarman/const.py +++ b/custom_components/solarman/const.py @@ -32,6 +32,13 @@ CONF_BATTERY_LIFE_CYCLE_RATING = "battery_life_cycle_rating" CONF_MB_SLAVE_ID = "mb_slave_id" +UPDATE_INTERVAL = "update_interval" +IS_SINGLE_CODE = "is_single_code" +REGISTERS_CODE = "registers_code" +REGISTERS_MIN_SPAN = "registers_min_span" +REGISTERS_MAX_SIZE = "registers_max_size" +DIGITS = "digits" + DEFAULT_TABLE = { "name": "Inverter", CONF_HOST: "", @@ -42,12 +49,12 @@ CONF_PHASE: 3, CONF_BATTERY_NOMINAL_VOLTAGE: 48, CONF_BATTERY_LIFE_CYCLE_RATING: 6000, - "register_update_interval": 60, - "is_single_code": False, - "registers_code": 0x03, - "registers_min_span": 25, - "registers_max_size": 125, - "digits": 6 + UPDATE_INTERVAL: 60, + IS_SINGLE_CODE: False, + REGISTERS_CODE: 0x03, + REGISTERS_MIN_SPAN: 25, + REGISTERS_MAX_SIZE: 125, + DIGITS: 6 } AUTODETECTION_REDIRECT_TABLE = ["deye_string.yaml", "deye_hybrid.yaml", "deye_micro.yaml", "deye_4mppt.yaml", "deye_2mppt.yaml", "deye_sg04lp3.yaml", "deye_sg01hp3.yaml"] @@ -93,7 +100,7 @@ ATTR_MPPT = "mppt" ATTR_PHASE = "l" -REQUEST_UPDATE_INTERVAL = "update_interval" +REQUEST_UPDATE_INTERVAL = UPDATE_INTERVAL REQUEST_MIN_SPAN = "min_span" REQUEST_MAX_SIZE = "max_size" REQUEST_CODE = "code" diff --git a/custom_components/solarman/parser.py b/custom_components/solarman/parser.py index fdefeda..1db50b7 100644 --- a/custom_components/solarman/parser.py +++ b/custom_components/solarman/parser.py @@ -13,12 +13,12 @@ class ParameterParser: def __init__(self, profile, attr): - self._update_interval = DEFAULT_TABLE["register_update_interval"] - self._is_single_code = DEFAULT_TABLE["is_single_code"] - self._code = DEFAULT_TABLE["registers_code"] - self._min_span = DEFAULT_TABLE["registers_min_span"] - self._max_size = DEFAULT_TABLE["registers_max_size"] - self._digits = DEFAULT_TABLE["digits"] + self._update_interval = DEFAULT_TABLE[UPDATE_INTERVAL] + self._is_single_code = DEFAULT_TABLE[IS_SINGLE_CODE] + self._code = DEFAULT_TABLE[REGISTERS_CODE] + self._min_span = DEFAULT_TABLE[REGISTERS_MIN_SPAN] + self._max_size = DEFAULT_TABLE[REGISTERS_MAX_SIZE] + self._digits = DEFAULT_TABLE[DIGITS] self._requests = None self._result = {} @@ -32,8 +32,8 @@ def __init__(self, profile, attr): self._min_span = default[REQUEST_MIN_SPAN] if REQUEST_MAX_SIZE in default: self._max_size = default[REQUEST_MAX_SIZE] - if "digits" in default: - self._digits = default["digits"] + if DIGITS in default: + self._digits = default[DIGITS] if "requests" in profile and "requests_fine_control" in profile: _LOGGER.debug("Fine control of request sets is enabled!") @@ -281,7 +281,7 @@ def try_parse_unsigned(self, data, definition): return value = validation["default"] - self.set_state(key, get_number(value, get_or_def(definition, "digits", self._digits))) + self.set_state(key, get_number(value, get_or_def(definition, DIGITS, self._digits))) if (a := definition.get("attributes")) is not None and "value" in a: self.set_state(key, self._result[key][0], int(value)) @@ -300,7 +300,7 @@ def try_parse_signed(self, data, definition): return value = validation["default"] - self.set_state(key, get_number(value, get_or_def(definition, "digits", self._digits))) + self.set_state(key, get_number(value, get_or_def(definition, DIGITS, self._digits))) def try_parse_ascii(self, data, definition): code = get_code(definition, "read") diff --git a/custom_components/solarman/sensor.py b/custom_components/solarman/sensor.py index 5712f1f..490ec11 100644 --- a/custom_components/solarman/sensor.py +++ b/custom_components/solarman/sensor.py @@ -121,7 +121,7 @@ def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cy super().__init__(coordinator, sensor) self._battery_nominal_voltage = battery_nominal_voltage self._battery_life_cycle_rating = battery_life_cycle_rating - self._digits = sensor["digits"] if "digits" in sensor else DEFAULT_TABLE["digits"] + self._digits = sensor[DIGITS] if DIGITS in sensor else DEFAULT_TABLE[DIGITS] def update(self): #super().update()