Skip to content

Commit

Permalink
fix: Config entry migration - incorrect method
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 6, 2024
1 parent 8ce2bbe commit 98be4e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
4 changes: 1 addition & 3 deletions custom_components/solarman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 14 additions & 7 deletions custom_components/solarman/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 10 additions & 10 deletions custom_components/solarman/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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!")
Expand Down Expand Up @@ -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))
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 98be4e3

Please sign in to comment.