ABS: Login via API key (#1990)
authorFabian Munkes <105975993+fmunkes@users.noreply.github.com>
Fri, 28 Feb 2025 17:44:19 +0000 (18:44 +0100)
committerGitHub <noreply@github.com>
Fri, 28 Feb 2025 17:44:19 +0000 (18:44 +0100)
music_assistant/providers/audiobookshelf/__init__.py
music_assistant/providers/audiobookshelf/constants.py
music_assistant/providers/audiobookshelf/manifest.json
requirements_all.txt

index 5b9cba5b3ce816bcbbb9ea147e0589a4545f67a0..30fc66b307f9ad324e5727e351335c1232e79015 100644 (file)
@@ -55,6 +55,7 @@ from .constants import (
     CACHE_KEY_LIBRARIES,
     CONF_HIDE_EMPTY_PODCASTS,
     CONF_PASSWORD,
+    CONF_TOKEN,
     CONF_URL,
     CONF_USERNAME,
     CONF_VERIFY_SSL,
@@ -97,6 +98,14 @@ async def get_config_entries(
     """
     # ruff: noqa: ARG001
     return (
+        ConfigEntry(
+            key="label",
+            type=ConfigEntryType.LABEL,
+            label="Please provide the address of your Audiobookshelf instance. To authenticate "
+            "you have two options: "
+            "a) Provide username AND password. Leave token empty."
+            "b) Provide ONLY the token.",
+        ),
         ConfigEntry(
             key=CONF_URL,
             type=ConfigEntryType.STRING,
@@ -108,7 +117,7 @@ async def get_config_entries(
             key=CONF_USERNAME,
             type=ConfigEntryType.STRING,
             label="Username",
-            required=True,
+            required=False,
             description="The username to authenticate to the remote server.",
         ),
         ConfigEntry(
@@ -118,6 +127,14 @@ async def get_config_entries(
             required=False,
             description="The password to authenticate to the remote server.",
         ),
+        ConfigEntry(
+            key=CONF_TOKEN,
+            type=ConfigEntryType.SECURE_STRING,
+            label="Token _instead_ of user/ password.",
+            required=False,
+            description="Instead of using username and password, you may provide the user's token."
+            "\nThe token can be seen in Audiobookshelf as an admin user in Settings -> Users.",
+        ),
         ConfigEntry(
             key=CONF_VERIFY_SSL,
             type=ConfigEntryType.BOOLEAN,
@@ -156,6 +173,7 @@ class Audiobookshelf(MusicProvider):
         base_url = str(self.config.get_value(CONF_URL))
         username = str(self.config.get_value(CONF_USERNAME))
         password = str(self.config.get_value(CONF_PASSWORD))
+        token = self.config.get_value(CONF_TOKEN)
         verify_ssl = bool(self.config.get_value(CONF_VERIFY_SSL))
         session_config = aioabs.SessionConfiguration(
             session=self.mass.http_session,
@@ -165,9 +183,16 @@ class Audiobookshelf(MusicProvider):
             pagination_items_per_page=30,  # audible provider goes with 50 for pagination
         )
         try:
-            self._client, self._client_socket = await aioabs.get_user_and_socket_client(
-                session_config=session_config, username=username, password=password
-            )
+            if token is not None:
+                session_config.token = str(token)
+                (
+                    self._client,
+                    self._client_socket,
+                ) = await aioabs.get_user_and_socket_client_by_token(session_config=session_config)
+            else:
+                self._client, self._client_socket = await aioabs.get_user_and_socket_client(
+                    session_config=session_config, username=username, password=password
+                )
             await self._client_socket.init_client()
         except AbsLoginError as exc:
             raise LoginFailed(f"Login to abs instance at {base_url} failed.") from exc
index e3663b4e10ca9ea793025d5a997dee5786028f4f..9aba4e6ffb8b20d19bfccea823dca8f163fe6592 100644 (file)
@@ -6,6 +6,7 @@ from enum import StrEnum
 CONF_URL = "url"
 CONF_USERNAME = "username"
 CONF_PASSWORD = "password"
+CONF_TOKEN = "token"
 CONF_VERIFY_SSL = "verify_ssl"
 # optionally hide podcasts with no episodes
 CONF_HIDE_EMPTY_PODCASTS = "hide_empty_podcasts"
index 444e59546d7ac90a0788eb4b66b67fca1029366d..84f410ecc2c0e9205855c618fb6b8f62aaa581d3 100644 (file)
@@ -7,7 +7,7 @@
     "@fmunkes"
   ],
   "requirements": [
-    "aioaudiobookshelf==0.1.2"
+    "aioaudiobookshelf==0.1.3"
   ],
   "documentation": "https://music-assistant.io/music-providers/audiobookshelf",
   "multi_instance": true
index db89d3d13a8300eb6de5a950c9e0e1c399daada2..1aaebb51da5a1602383ff3b0713886771296e77c 100644 (file)
@@ -1,7 +1,7 @@
 # WARNING: this file is autogenerated!
 
 Brotli>=1.0.9
-aioaudiobookshelf==0.1.2
+aioaudiobookshelf==0.1.3
 aiodns>=3.2.0
 aiofiles==24.1.0
 aiohttp==3.11.12