more tweaks for config loading
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Fri, 16 Aug 2024 19:14:05 +0000 (21:14 +0200)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Fri, 16 Aug 2024 19:14:05 +0000 (21:14 +0200)
music_assistant/common/models/config_entries.py
music_assistant/server/controllers/config.py
music_assistant/server/providers/spotify/__init__.py

index bc31c76c9f55a4121314ecb04b26aebd3c30731a..a9428a91bf7e3e257390c18581d734f21b8db379 100644 (file)
@@ -170,10 +170,10 @@ class ConfigEntry(DataClassDictMixin):
                     self.key,
                     type(self.value),
                 )
-                self.value = self.default_value
-                return self.value
-            msg = f"{self.key} has unexpected type: {type(value)}"
-            raise ValueError(msg)
+                value = self.default_value
+            if not (value is None and allow_none):
+                msg = f"{self.key} has unexpected type: {type(value)}"
+                raise ValueError(msg)
         self.value = value
         return self.value
 
index eb55be863a0ebc8e22d2125d3efe55ab7e82a52e..798a19ec5148d94a23575154262ca6618c4bc074 100644 (file)
@@ -35,7 +35,6 @@ from music_assistant.constants import (
     CONF_SERVER_ID,
     CONFIGURABLE_CORE_CONTROLLERS,
     ENCRYPT_SUFFIX,
-    SECURE_STRING_SUBSTITUTE,
 )
 from music_assistant.server.helpers.api import api_command
 from music_assistant.server.helpers.util import load_provider_module
@@ -241,15 +240,6 @@ class ConfigController:
         if values is None:
             values = self.get(f"{CONF_PROVIDERS}/{instance_id}/values", {}) if instance_id else {}
 
-        # handle (edge) case where encrypted values are passed along
-        for key, value in values.items():
-            if not isinstance(value, str):
-                continue
-            if value == SECURE_STRING_SUBSTITUTE:
-                values[key] = value = self.get(f"{CONF_PROVIDERS}/{instance_id}/values/{key}", None)  # noqa: PLW2901
-            if value.startswith(ENCRYPT_SUFFIX):
-                values[key] = self.decrypt_string(value)
-
         return (
             await prov_mod.get_config_entries(
                 self.mass, instance_id=instance_id, action=action, values=values
@@ -315,7 +305,7 @@ class ConfigController:
     ) -> None:
         """Set single ProviderConfig value."""
         config = await self.get_provider_config(instance_id)
-        config.update({**config.to_raw(), key: value})
+        config.update({key: value})
         config.validate()
         conf_key = f"{CONF_PROVIDERS}/{config.instance_id}"
         self.set(conf_key, config.to_raw())
@@ -692,9 +682,8 @@ class ConfigController:
             return encrypted_str
         if not encrypted_str.startswith(ENCRYPT_SUFFIX):
             return encrypted_str
-        encrypted_str = encrypted_str.replace(ENCRYPT_SUFFIX, "")
         try:
-            return self._fernet.decrypt(encrypted_str.encode()).decode()
+            return self._fernet.decrypt(encrypted_str.replace(ENCRYPT_SUFFIX, "").encode()).decode()
         except InvalidToken as err:
             msg = "Password decryption failed"
             raise InvalidDataError(msg) from err
@@ -758,7 +747,8 @@ class ConfigController:
         # save the config first to prevent issues when the
         # provider wants to manipulate the config during load
         conf_key = f"{CONF_PROVIDERS}/{config.instance_id}"
-        self.set(conf_key, config.to_raw())
+        raw_conf = config.to_raw()
+        self.set(conf_key, raw_conf)
         if config.enabled:
             await self._load_provider_config(config)
         else:
index 852791a6d944a0c793947ac4f19125ce77823198..794df518aa86f75cd48793d21d304913953aacff 100644 (file)
@@ -174,6 +174,8 @@ async def get_config_entries(
     auth_required = values.get(CONF_REFRESH_TOKEN) is None
 
     if auth_required:
+        values[CONF_CLIENT_ID] = None
+        values[CONF_ACCESS_TOKEN] = None
         label_text = (
             "You need to authenticate to Spotify. Click the authenticate button below "
             "to start the authentication process which will open in a new (popup window), "