Skip to content

Commit

Permalink
more tweaks for config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Aug 16, 2024
1 parent 51d6271 commit bf4c92f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
8 changes: 4 additions & 4 deletions music_assistant/common/models/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def parse_value(
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

Expand Down
18 changes: 4 additions & 14 deletions music_assistant/server/controllers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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
Expand Down Expand Up @@ -241,15 +240,6 @@ async def get_provider_config_entries(
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
Expand Down Expand Up @@ -315,7 +305,7 @@ async def set_provider_config_value(
) -> 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())
Expand Down Expand Up @@ -692,9 +682,8 @@ def decrypt_string(self, encrypted_str: str) -> str:
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
Expand Down Expand Up @@ -758,7 +747,8 @@ async def _update_provider_config(
# 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:
Expand Down
2 changes: 2 additions & 0 deletions music_assistant/server/providers/spotify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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), "
Expand Down

0 comments on commit bf4c92f

Please sign in to comment.