Fix add provider logic (#527)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Mon, 13 Mar 2023 23:37:01 +0000 (00:37 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Mar 2023 23:37:01 +0000 (00:37 +0100)
* fix default provider generation

* detect beta version in regex

.github/workflows/docker-build.yml
music_assistant/server/controllers/config.py
music_assistant/server/server.py

index 8e6d25e120511ad9a105a0b4d222aca725a2174b..241688fca1516536e29e49347f9b727c8780c3ad 100644 (file)
@@ -41,7 +41,7 @@ jobs:
           # If the VERSION looks like a version number, assume that
           # this is the most recent version of the image and also
           # tag it 'latest'.
-          if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
+          if [[ $VERSION =~ ^\d+\.\d+\.\d+[[:space:]]?(b[[:space:]]?\d+)? ]]; then
             TAGS="$TAGS,${DOCKER_IMAGE}:latest"
           fi
 
index f0d34a51bb8cd4274ced1a8662b6d04f9e8f21d2..bb83a043d4cac3f2168e521ae7446e7ddecc8ab3 100644 (file)
@@ -173,16 +173,11 @@ class ConfigController:
             for prov in self.mass.get_available_providers():
                 if prov.domain != raw_conf["domain"]:
                     continue
-                return ProviderConfig.parse(
-                    prov.config_entries,
-                    raw_conf,
-                )
+                return ProviderConfig.parse(prov.config_entries, raw_conf, allow_none=True)
         raise KeyError(f"No config found for provider id {instance_id}")
 
     @api_command("config/providers/update")
-    def update_provider_config(
-        self, instance_id: str, update: ConfigUpdate, skip_reload: bool = False
-    ) -> None:
+    def update_provider_config(self, instance_id: str, update: ConfigUpdate) -> None:
         """Update ProviderConfig."""
         config = self.get_provider_config(instance_id)
         changed_keys = config.update(update)
@@ -193,10 +188,8 @@ class ConfigController:
 
         conf_key = f"{CONF_PROVIDERS}/{instance_id}"
         self.set(conf_key, config.to_raw())
-        # (re)load provider
-        if not skip_reload:
-            updated_config = self.get_provider_config(config.instance_id)
-            self.mass.create_task(self.mass.load_provider(updated_config))
+        updated_config = self.get_provider_config(config.instance_id)
+        self.mass.create_task(self.mass.load_provider(updated_config))
 
     @api_command("config/providers/create")
     def create_provider_config(self, provider_domain: str) -> ProviderConfig:
@@ -215,30 +208,39 @@ class ConfigController:
             raise KeyError(f"Unknown provider domain: {provider_domain}")
 
         # determine instance id based on previous configs
-        existing = self.get_provider_configs(provider_domain=provider_domain)
+        existing = {
+            x.instance_id for x in self.get_provider_configs(provider_domain=provider_domain)
+        }
+
         if existing and not manifest.multi_instance:
             raise ValueError(f"Provider {manifest.name} does not support multiple instances")
 
-        count = len(existing)
-        if count == 0:
+        if len(existing) == 0:
             instance_id = provider_domain
             name = manifest.name
         else:
-            instance_id = f"{provider_domain}{count+1}"
-            name = f"{manifest.name} {count+1}"
+            instance_id = f"{provider_domain}{len(existing)+1}"
+            name = f"{manifest.name} {len(existing)+1}"
 
-        return ProviderConfig.parse(
+        # all checks passed, return a default config
+        default_config = ProviderConfig.parse(
             prov.config_entries,
             {
                 "type": manifest.type.value,
                 "domain": manifest.domain,
                 "instance_id": instance_id,
                 "name": name,
-                "values": dict(),
+                "values": {},
             },
             allow_none=True,
         )
 
+        # config provided and checks passed, storeconfig
+        conf_key = f"{CONF_PROVIDERS}/{instance_id}"
+        self.set(conf_key, default_config.to_raw())
+
+        return default_config
+
     @api_command("config/providers/remove")
     async def remove_provider_config(self, instance_id: str) -> None:
         """Remove ProviderConfig."""
index 0879be98cec223c9d0926125e087ffe1de279780..1bb083a21a2bd194cf0c1fbbb64c0721b7faf32b 100644 (file)
@@ -417,9 +417,7 @@ class MusicAssistant:
             existing = any(x for x in provider_configs if x.domain == prov_manifest.domain)
             if existing:
                 continue
-            default_conf = self.config.create_provider_config(prov_manifest.domain)
-            # skip_reload to prevent race condition
-            self.config.update_provider_config(default_conf, skip_reload=True)
+            self.config.create_provider_config(prov_manifest.domain)
 
         # load all configured (and enabled) providers
         for allow_depends_on in (False, True):