diff --git a/custom_components/solarman/__init__.py b/custom_components/solarman/__init__.py index 989e496..3b65c0d 100644 --- a/custom_components/solarman/__init__.py +++ b/custom_components/solarman/__init__.py @@ -95,8 +95,8 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bulk_migrate(new_data, new_data, OLD_) bulk_migrate(new_options, new_options, OLD_) bulk_inherit(new_options.setdefault(CONF_ADDITIONAL_OPTIONS, {}), new_options, CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING) - bulk_delete(new_data, OLD_[CONF_SERIAL]) - bulk_delete(new_options, OLD_[CONF_SERIAL], OLD_[CONF_HOST], OLD_[CONF_PORT], CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING) + bulk_safe_delete(new_data, OLD_) + bulk_safe_delete(new_options, OLD_ | to_dict(CONF_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING)) if not new_options.get(CONF_ADDITIONAL_OPTIONS): del new_options[CONF_ADDITIONAL_OPTIONS] diff --git a/custom_components/solarman/common.py b/custom_components/solarman/common.py index 4deae35..2d4382d 100644 --- a/custom_components/solarman/common.py +++ b/custom_components/solarman/common.py @@ -36,6 +36,9 @@ def execute_async(x): loop = asyncio.get_event_loop() return loop.run_until_complete(x) +def to_dict(*keys: list): + return {k: k for k in keys} + def filter_by_keys(source: dict, keys: dict | list) -> dict: return {k: source[k] for k in source.keys() if k in keys} @@ -51,9 +54,14 @@ def bulk_migrate(target: dict, source: dict, redirect: dict): target[k] = v return target -def bulk_delete(source: dict[Any, Any], *keys: list[Any]): - for k in source.keys() & keys: - del source[k] +def bulk_delete(target: dict[Any, Any], *keys: list[Any]): + for k in target.keys() & keys: + del target[k] + +def bulk_safe_delete(target: dict[Any, Any], redirect: dict): + for k in target.keys() & redirect.keys(): + if redirect[k] in target: + del target[redirect[k]] def ensure_list(value): return value if isinstance(value, list) else [value] diff --git a/custom_components/solarman/config_flow.py b/custom_components/solarman/config_flow.py index 67ba283..90c7321 100644 --- a/custom_components/solarman/config_flow.py +++ b/custom_components/solarman/config_flow.py @@ -82,7 +82,7 @@ def remove_defaults(user_input: dict[str, Any]): return user_input class ConfigFlowHandler(ConfigFlow, domain = DOMAIN): - MINOR_VERSION = 4 + MINOR_VERSION = 5 VERSION = 1 async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult: