From 4816c22d07335b3d05d29284ff251e9aeab8e415 Mon Sep 17 00:00:00 2001 From: Warren Date: Sun, 21 Jan 2024 19:37:11 +1100 Subject: [PATCH] Fix proper merging of config entries --- .../ha_behringer_mixer/__init__.py | 29 ++++++++++++++++++- .../ha_behringer_mixer/config_flow.py | 2 +- .../ha_behringer_mixer/coordinator.py | 7 +++-- .../ha_behringer_mixer/manifest.json | 2 +- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/custom_components/ha_behringer_mixer/__init__.py b/custom_components/ha_behringer_mixer/__init__.py index 8150242..d49ef43 100644 --- a/custom_components/ha_behringer_mixer/__init__.py +++ b/custom_components/ha_behringer_mixer/__init__.py @@ -6,7 +6,7 @@ from homeassistant.core import HomeAssistant from .api import BehringerMixerApiClient -from .const import DOMAIN +from .const import DOMAIN, LOGGER from .coordinator import MixerDataUpdateCoordinator PLATFORMS: list[Platform] = [ @@ -53,3 +53,30 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: """Reload config entry.""" await async_unload_entry(hass, entry) await async_setup_entry(hass, entry) + + +async def async_migrate_entry(hass, config_entry: ConfigEntry): + """Migrate old entry.""" + LOGGER.debug("Migrating from version %s", config_entry.version) + + if config_entry.version < 2: + """Update Config data to include valid channels/bussses etc.""" + client = BehringerMixerApiClient(mixer_ip=config_entry.data.get("MIXER_IP"), mixer_type=config_entry.data.get("MIXER_TYPE")) + await client.setup(test_connection_only=True) + await client.async_get_data() + await client.stop() + mixer_info = client.mixer_info() + new = {**config_entry.data} + new["CHANNEL_CONFIG"] = list(range(1, mixer_info.get('channel', {}).get("number") + 1)) + new["BUS_CONFIG"] = list(range(1, mixer_info.get('bus', {}).get("number") + 1)) + new["DCA_CONFIG"] = list(range(1, mixer_info.get('dca', {}).get("number") + 1)) + new["MATRIX_CONFIG"] = list(range(1, mixer_info.get('matrix', {}).get("number") + 1)) + new["AUXIN_CONFIG"] = list(range(1, mixer_info.get('auxin', {}).get("number") + 1)) + new["MAIN_CONFIG"] = True + new["CHANNELSENDS_CONFIG"] = False + new["BUSSENDS_CONFIG"] = False + config_entry.version = 2 + hass.config_entries.async_update_entry(config_entry, data=new) + LOGGER.debug("Migration to version %s successful", config_entry.version) + + return True diff --git a/custom_components/ha_behringer_mixer/config_flow.py b/custom_components/ha_behringer_mixer/config_flow.py index 7fe938f..450241d 100644 --- a/custom_components/ha_behringer_mixer/config_flow.py +++ b/custom_components/ha_behringer_mixer/config_flow.py @@ -18,7 +18,7 @@ class BehringerMixerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for BehringerMixer.""" - VERSION = 1 + VERSION = 2 async def async_step_user( self, diff --git a/custom_components/ha_behringer_mixer/coordinator.py b/custom_components/ha_behringer_mixer/coordinator.py index e7699cd..1c0f67f 100644 --- a/custom_components/ha_behringer_mixer/coordinator.py +++ b/custom_components/ha_behringer_mixer/coordinator.py @@ -62,13 +62,14 @@ def build_entity_catalog(self, mixer_info): for entity_type in types: # num_type = mixer_info.get(entity_type, {}).get("number") base_key = mixer_info.get(entity_type, {}).get("base_address") - for index_number in self.config_entry.data[entity_type.upper() + "_CONFIG"]: + config_key = entity_type.upper() + "_CONFIG" + for index_number in self.config_entry.data.get(config_key, []): self.fader_group(entities, entity_type, index_number, base_key) # Channel to bus sends if self.config_entry.data.get("CHANNELSENDS_CONFIG"): base_key = mixer_info.get("channel_sends", {}).get("base_address") - for channel_number in self.config_entry.data["CHANNEL_CONFIG"]: - for bus_number in self.config_entry.data["BUS_CONFIG"]: + for channel_number in self.config_entry.data.get("CHANNEL_CONFIG", []): + for bus_number in self.config_entry.data.get("BUS_CONFIG", []): self.fader_group( entities, "chsend", diff --git a/custom_components/ha_behringer_mixer/manifest.json b/custom_components/ha_behringer_mixer/manifest.json index 7b3f2fd..51876c7 100644 --- a/custom_components/ha_behringer_mixer/manifest.json +++ b/custom_components/ha_behringer_mixer/manifest.json @@ -10,5 +10,5 @@ "requirements": [ "behringer-mixer==0.4.2" ], - "version": "0.1.3" + "version": "0.1.4" }